/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ddmlib;
import com.android.annotations.NonNull;
import com.android.ddmlib.Log.LogLevel;
import com.google.common.base.Joiner;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Thread.State;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A connection to the host-side android debug bridge (adb)
* <p/>This is the central point to communicate with any devices, emulators, or the applications
* running on them.
* <p/><b>{@link #init(boolean)} must be called before anything is done.</b>
*/
public final class AndroidDebugBridge {
/*
* Minimum and maximum version of adb supported. This correspond to
* ADB_SERVER_VERSION found in //device/tools/adb/adb.h
*/
private static final int ADB_VERSION_MICRO_MIN = 20;
private static final int ADB_VERSION_MICRO_MAX = -1;
private static final Pattern sAdbVersion = Pattern.compile(
"^.*(\\d+)\\.(\\d+)\\.(\\d+)$"); //$NON-NLS-1$
private static final String ADB = "adb"; //$NON-NLS-1$
private static final String DDMS = "ddms"; //$NON-NLS-1$
private static final String SERVER_PORT_ENV_VAR = "ANDROID_ADB_SERVER_PORT"; //$NON-NLS-1$
// Where to find the ADB bridge.
static final String DEFAULT_ADB_HOST = "127.0.0.1"; //$NON-NLS-1$
static final int DEFAULT_ADB_PORT = 5039;
/** Port where adb server will be started **/
private static int sAdbServerPort = 0;
private static InetAddress sHostAddr;
private static InetSocketAddress sSocketAddr;
private static AndroidDebugBridge sThis;
private static boolean sInitialized = false;
private static boolean sClientSupport;
/** Full path to adb. */
private String mAdbOsLocation = null;
private boolean mVersionCheck;
private boolean mStarted = false;
private DeviceMonitor mDeviceMonitor;
private static final ArrayList<IDebugBridgeChangeListener> sBridgeListeners =
new ArrayList<IDebugBridgeChangeListener>();
private static final ArrayList<IDeviceChangeListener> sDeviceListeners =
new ArrayList<IDeviceChangeListener>();
private static final ArrayList<IClientChangeListener> sClientListeners =
new ArrayList<IClientChangeListener>();
// lock object for synchronization
private static final Object sLock = sBridgeListeners;
/**
* Classes which implement this interface provide a method that deals
* with {@link AndroidDebugBridge} changes.
*/
public interface IDebugBridgeChangeListener {
/**
* Sent when a new {@link AndroidDebugBridge} is connected.
* <p/>
* This is sent from a non UI thread.
* @param bridge the new {@link AndroidDebugBridge} object.
*/
public void bridgeChanged(AndroidDebugBridge bridge);
}
/**
* Classes which implement this interface provide methods that deal
* with {@link IDevice} addition, deletion, and changes.
*/
public interface IDeviceChangeListener {
/**
* Sent when the a device is connected to the {@link AndroidDebugBridge}.
* <p/>
* This is sent from a non UI thread.
* @param device the new device.
*/
public void deviceConnected(IDevice device);
/**
* Sent when the a device is connected to the {@link AndroidDebugBridge}.
* <p/>
* This is sent from a non UI thread.
* @param device the new device.
*/
public void deviceDisconnected(IDevice device);
/**
* Sent when a device data changed, or when clients are started/terminated on the device.
* <p/>
* This is sent from a non UI thread.
* @param device the device that was updated.
* @param changeMask the mask describing what changed. It can contain any of the following
* values: {@link IDevice#CHANGE_BUILD_INFO}, {@link IDevice#CHANGE_STATE},
* {@link IDevice#CHANGE_CLIENT_LIST}
*/
public void deviceChanged(IDevice device, int changeMask);
}
/**
* Classes which implement this interface provide methods that deal
* with {@link Client} changes.
*/
public interface IClientChangeListener {
/**
* Sent when an existing client information changed.
* <p/>
* This is sent from a non UI thread.
* @param client the updated client.
* @param changeMask the bit mask describing the changed properties. It can contain
* any of the following values: {@link Client#CHANGE_INFO},
* {@link Client#CHANGE_DEBUGGER_STATUS}, {@link Client#CHANGE_THREAD_MODE},
* {@link Client#CHANGE_THREAD_DATA}, {@link Client#CHANGE_HEAP_MODE},
* {@link Client#CHANGE_HEAP_DATA}, {@link Client#CHANGE_NATIVE_HEAP_DATA}
*/
public void clientChanged(Client client, int changeMask);
}
/**
* Initialized the library only if needed.
*
* @param clientSupport Indicates whether the library should enable the monitoring and
* interaction with applications running on the devices.
*
* @see #init(boolean)
*/
public static synchronized void initIfNeeded(boolean clientSupport) {
if (sInitialized) {
return;
}
init(clientSupport);
}
/**
* Initializes the <code>ddm</code> library.
* <p/>This must be called once <b>before</b> any call to
* {@link #createBridge(String, boolean)}.
* <p>The library can be initialized in 2 ways:
* <ul>
* <li>Mode 1: <var>clientSupport</var> == <code>true</code>.<br>The library monitors the
* devices and the applications running on them. It will connect to each application, as a
* debugger of sort, to be able to interact with them through JDWP packets.</li>
* <li>Mode 2: <var>clientSupport</var> == <code>false</code>.<br>The library only monitors
* devices. The applications are left untouched, letting other tools built on
* <code>ddmlib</code> to connect a debugger to them.</li>
* </ul>
* <p/><b>Only one tool can run in mode 1 at the same time.</b>
* <p/>Note that mode 1 does not prevent debugging of applications running on devices. Mode 1
* lets debuggers connect to <code>ddmlib</code> which acts as a proxy between the debuggers and
* the applications to debug. See {@link Client#getDebuggerListenPort()}.
* <p/>The preferences of <code>ddmlib</code> should also be initialized with whatever default
* values were changed from the default values.
* <p/>When the application quits, {@link #terminate()} should be called.
* @param clientSupport Indicates whether the library should enable the monitoring and
* interaction with applications running on the devices.
* @see AndroidDebugBridge#createBridge(String, boolean)
* @see DdmPreferences
*/
public static synchronized void init(boolean clientSupport) {
if (sInitialized) {
th
没有合适的资源?快使用搜索试试~ 我知道了~
ddmlib-22.9.1源码

共209个文件
class:132个
java:71个
jar:3个


Android studio、eclipse的ddms连接设备的原理,和截屏代码分析
资源推荐
资源详情
资源评论







收起资源包目录





































































































共 209 条
- 1
- 2
- 3
资源评论

- _望2022-11-18还可以吧,可以自己二次开发,可以试试
- yilonglucky2017-07-25很好,学习了
- weixin_382574642017-04-10很好,正在学习
longtian635241
- 粉丝: 34
- 资源: 48

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
