1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmlib; 18 19 import com.android.ddmlib.log.LogReceiver; 20 21 import java.io.IOException; 22 import java.util.Map; 23 24 /** 25 * A Device. It can be a physical device or an emulator. 26 */ 27 public interface IDevice { 28 29 public final static String PROP_BUILD_VERSION = "ro.build.version.release"; 30 public final static String PROP_BUILD_API_LEVEL = "ro.build.version.sdk"; 31 public final static String PROP_BUILD_CODENAME = "ro.build.version.codename"; 32 33 public final static String PROP_DEBUGGABLE = "ro.debuggable"; 34 35 /** Serial number of the first connected emulator. */ 36 public final static String FIRST_EMULATOR_SN = "emulator-5554"; //$NON-NLS-1$ 37 /** Device change bit mask: {@link DeviceState} change. */ 38 public static final int CHANGE_STATE = 0x0001; 39 /** Device change bit mask: {@link Client} list change. */ 40 public static final int CHANGE_CLIENT_LIST = 0x0002; 41 /** Device change bit mask: build info change. */ 42 public static final int CHANGE_BUILD_INFO = 0x0004; 43 44 /** @deprecated Use {@link #PROP_BUILD_API_LEVEL}. */ 45 public final static String PROP_BUILD_VERSION_NUMBER = PROP_BUILD_API_LEVEL; 46 47 public final static String MNT_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; //$NON-NLS-1$ 48 public final static String MNT_ROOT = "ANDROID_ROOT"; //$NON-NLS-1$ 49 public final static String MNT_DATA = "ANDROID_DATA"; //$NON-NLS-1$ 50 51 /** 52 * The state of a device. 53 */ 54 public static enum DeviceState { 55 BOOTLOADER("bootloader"), //$NON-NLS-1$ 56 OFFLINE("offline"), //$NON-NLS-1$ 57 ONLINE("device"), //$NON-NLS-1$ 58 RECOVERY("recovery"); //$NON-NLS-1$ 59 60 private String mState; 61 DeviceState(String state)62 DeviceState(String state) { 63 mState = state; 64 } 65 66 /** 67 * Returns a {@link DeviceState} from the string returned by <code>adb devices</code>. 68 * 69 * @param state the device state. 70 * @return a {@link DeviceState} object or <code>null</code> if the state is unknown. 71 */ getState(String state)72 public static DeviceState getState(String state) { 73 for (DeviceState deviceState : values()) { 74 if (deviceState.mState.equals(state)) { 75 return deviceState; 76 } 77 } 78 return null; 79 } 80 } 81 82 /** 83 * Returns the serial number of the device. 84 */ getSerialNumber()85 public String getSerialNumber(); 86 87 /** 88 * Returns the name of the AVD the emulator is running. 89 * <p/>This is only valid if {@link #isEmulator()} returns true. 90 * <p/>If the emulator is not running any AVD (for instance it's running from an Android source 91 * tree build), this method will return "<code><build></code>". 92 * 93 * @return the name of the AVD or <code>null</code> if there isn't any. 94 */ getAvdName()95 public String getAvdName(); 96 97 /** 98 * Returns the state of the device. 99 */ getState()100 public DeviceState getState(); 101 102 /** 103 * Returns the device properties. It contains the whole output of 'getprop' 104 */ getProperties()105 public Map<String, String> getProperties(); 106 107 /** 108 * Returns the number of property for this device. 109 */ getPropertyCount()110 public int getPropertyCount(); 111 112 /** 113 * Returns the cached property value. 114 * 115 * @param name the name of the value to return. 116 * @return the value or <code>null</code> if the property does not exist or has not yet been 117 * cached. 118 */ getProperty(String name)119 public String getProperty(String name); 120 121 /** 122 * Returns <code>true></code> if properties have been cached 123 */ arePropertiesSet()124 public boolean arePropertiesSet(); 125 126 /** 127 * A variant of {@link #getProperty(String)} that will attempt to retrieve the given 128 * property from device directly, without using cache. 129 * 130 * @param name the name of the value to return. 131 * @return the value or <code>null</code> if the property does not exist 132 * @throws TimeoutException in case of timeout on the connection. 133 * @throws AdbCommandRejectedException if adb rejects the command 134 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output for a 135 * given time. 136 * @throws IOException in case of I/O error on the connection. 137 */ getPropertySync(String name)138 public String getPropertySync(String name) throws TimeoutException, 139 AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException; 140 141 /** 142 * A combination of {@link #getProperty(String)} and {@link #getPropertySync(String)} that 143 * will attempt to retrieve the property from cache if available, and if not, will query the 144 * device directly. 145 * 146 * @param name the name of the value to return. 147 * @return the value or <code>null</code> if the property does not exist 148 * @throws TimeoutException in case of timeout on the connection. 149 * @throws AdbCommandRejectedException if adb rejects the command 150 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output for a 151 * given time. 152 * @throws IOException in case of I/O error on the connection. 153 */ getPropertyCacheOrSync(String name)154 public String getPropertyCacheOrSync(String name) throws TimeoutException, 155 AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException; 156 157 /** 158 * Returns a mount point. 159 * 160 * @param name the name of the mount point to return 161 * 162 * @see #MNT_EXTERNAL_STORAGE 163 * @see #MNT_ROOT 164 * @see #MNT_DATA 165 */ getMountPoint(String name)166 public String getMountPoint(String name); 167 168 /** 169 * Returns if the device is ready. 170 * 171 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#ONLINE}. 172 */ isOnline()173 public boolean isOnline(); 174 175 /** 176 * Returns <code>true</code> if the device is an emulator. 177 */ isEmulator()178 public boolean isEmulator(); 179 180 /** 181 * Returns if the device is offline. 182 * 183 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#OFFLINE}. 184 */ isOffline()185 public boolean isOffline(); 186 187 /** 188 * Returns if the device is in bootloader mode. 189 * 190 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#BOOTLOADER}. 191 */ isBootLoader()192 public boolean isBootLoader(); 193 194 /** 195 * Returns whether the {@link Device} has {@link Client}s. 196 */ hasClients()197 public boolean hasClients(); 198 199 /** 200 * Returns the array of clients. 201 */ getClients()202 public Client[] getClients(); 203 204 /** 205 * Returns a {@link Client} by its application name. 206 * 207 * @param applicationName the name of the application 208 * @return the <code>Client</code> object or <code>null</code> if no match was found. 209 */ getClient(String applicationName)210 public Client getClient(String applicationName); 211 212 /** 213 * Returns a {@link SyncService} object to push / pull files to and from the device. 214 * 215 * @return <code>null</code> if the SyncService couldn't be created. This can happen if adb 216 * refuse to open the connection because the {@link IDevice} is invalid 217 * (or got disconnected). 218 * @throws TimeoutException in case of timeout on the connection. 219 * @throws AdbCommandRejectedException if adb rejects the command 220 * @throws IOException if the connection with adb failed. 221 */ getSyncService()222 public SyncService getSyncService() 223 throws TimeoutException, AdbCommandRejectedException, IOException; 224 225 /** 226 * Returns a {@link FileListingService} for this device. 227 */ getFileListingService()228 public FileListingService getFileListingService(); 229 230 /** 231 * Takes a screen shot of the device and returns it as a {@link RawImage}. 232 * 233 * @return the screenshot as a <code>RawImage</code> or <code>null</code> if something 234 * went wrong. 235 * @throws TimeoutException in case of timeout on the connection. 236 * @throws AdbCommandRejectedException if adb rejects the command 237 * @throws IOException in case of I/O error on the connection. 238 */ getScreenshot()239 public RawImage getScreenshot() throws TimeoutException, AdbCommandRejectedException, 240 IOException; 241 242 /** 243 * Executes a shell command on the device, and sends the result to a <var>receiver</var> 244 * <p/>This is similar to calling 245 * <code>executeShellCommand(command, receiver, DdmPreferences.getTimeOut())</code>. 246 * 247 * @param command the shell command to execute 248 * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell 249 * command 250 * @throws TimeoutException in case of timeout on the connection. 251 * @throws AdbCommandRejectedException if adb rejects the command 252 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output 253 * for a given time. 254 * @throws IOException in case of I/O error on the connection. 255 * 256 * @see #executeShellCommand(String, IShellOutputReceiver, int) 257 * @see DdmPreferences#getTimeOut() 258 */ executeShellCommand(String command, IShellOutputReceiver receiver)259 public void executeShellCommand(String command, IShellOutputReceiver receiver) 260 throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, 261 IOException; 262 263 /** 264 * Executes a shell command on the device, and sends the result to a <var>receiver</var>. 265 * <p/><var>maxTimeToOutputResponse</var> is used as a maximum waiting time when expecting the 266 * command output from the device.<br> 267 * At any time, if the shell command does not output anything for a period longer than 268 * <var>maxTimeToOutputResponse</var>, then the method will throw 269 * {@link ShellCommandUnresponsiveException}. 270 * <p/>For commands like log output, a <var>maxTimeToOutputResponse</var> value of 0, meaning 271 * that the method will never throw and will block until the receiver's 272 * {@link IShellOutputReceiver#isCancelled()} returns <code>true</code>, should be 273 * used. 274 * 275 * @param command the shell command to execute 276 * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell 277 * command 278 * @param maxTimeToOutputResponse the maximum amount of time during which the command is allowed 279 * to not output any response. A value of 0 means the method will wait forever 280 * (until the <var>receiver</var> cancels the execution) for command output and 281 * never throw. 282 * @throws TimeoutException in case of timeout on the connection when sending the command. 283 * @throws AdbCommandRejectedException if adb rejects the command. 284 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send any output 285 * for a period longer than <var>maxTimeToOutputResponse</var>. 286 * @throws IOException in case of I/O error on the connection. 287 * 288 * @see DdmPreferences#getTimeOut() 289 */ executeShellCommand(String command, IShellOutputReceiver receiver, int maxTimeToOutputResponse)290 public void executeShellCommand(String command, IShellOutputReceiver receiver, 291 int maxTimeToOutputResponse) 292 throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, 293 IOException; 294 295 /** 296 * Runs the event log service and outputs the event log to the {@link LogReceiver}. 297 * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true. 298 * @param receiver the receiver to receive the event log entries. 299 * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the 300 * timeout happens during setup. Once logs start being received, no timeout will occur as it's 301 * not possible to detect a difference between no log and timeout. 302 * @throws AdbCommandRejectedException if adb rejects the command 303 * @throws IOException in case of I/O error on the connection. 304 */ runEventLogService(LogReceiver receiver)305 public void runEventLogService(LogReceiver receiver) 306 throws TimeoutException, AdbCommandRejectedException, IOException; 307 308 /** 309 * Runs the log service for the given log and outputs the log to the {@link LogReceiver}. 310 * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true. 311 * 312 * @param logname the logname of the log to read from. 313 * @param receiver the receiver to receive the event log entries. 314 * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the 315 * timeout happens during setup. Once logs start being received, no timeout will 316 * occur as it's not possible to detect a difference between no log and timeout. 317 * @throws AdbCommandRejectedException if adb rejects the command 318 * @throws IOException in case of I/O error on the connection. 319 */ runLogService(String logname, LogReceiver receiver)320 public void runLogService(String logname, LogReceiver receiver) 321 throws TimeoutException, AdbCommandRejectedException, IOException; 322 323 /** 324 * Creates a port forwarding between a local and a remote port. 325 * 326 * @param localPort the local port to forward 327 * @param remotePort the remote port. 328 * @return <code>true</code> if success. 329 * @throws TimeoutException in case of timeout on the connection. 330 * @throws AdbCommandRejectedException if adb rejects the command 331 * @throws IOException in case of I/O error on the connection. 332 */ createForward(int localPort, int remotePort)333 public void createForward(int localPort, int remotePort) 334 throws TimeoutException, AdbCommandRejectedException, IOException; 335 336 /** 337 * Removes a port forwarding between a local and a remote port. 338 * 339 * @param localPort the local port to forward 340 * @param remotePort the remote port. 341 * @return <code>true</code> if success. 342 * @throws TimeoutException in case of timeout on the connection. 343 * @throws AdbCommandRejectedException if adb rejects the command 344 * @throws IOException in case of I/O error on the connection. 345 */ removeForward(int localPort, int remotePort)346 public void removeForward(int localPort, int remotePort) 347 throws TimeoutException, AdbCommandRejectedException, IOException; 348 349 /** 350 * Returns the name of the client by pid or <code>null</code> if pid is unknown 351 * @param pid the pid of the client. 352 */ getClientName(int pid)353 public String getClientName(int pid); 354 355 /** 356 * Push a single file. 357 * @param local the local filepath. 358 * @param remote The remote filepath. 359 * 360 * @throws IOException in case of I/O error on the connection. 361 * @throws AdbCommandRejectedException if adb rejects the command 362 * @throws TimeoutException in case of a timeout reading responses from the device. 363 * @throws SyncException if file could not be pushed 364 */ pushFile(String local, String remote)365 public void pushFile(String local, String remote) 366 throws IOException, AdbCommandRejectedException, TimeoutException, SyncException; 367 368 /** 369 * Pulls a single file. 370 * 371 * @param remote the full path to the remote file 372 * @param local The local destination. 373 * 374 * @throws IOException in case of an IO exception. 375 * @throws AdbCommandRejectedException if adb rejects the command 376 * @throws TimeoutException in case of a timeout reading responses from the device. 377 * @throws SyncException in case of a sync exception. 378 */ pullFile(String remote, String local)379 public void pullFile(String remote, String local) 380 throws IOException, AdbCommandRejectedException, TimeoutException, SyncException; 381 382 /** 383 * Installs an Android application on device. This is a helper method that combines the 384 * syncPackageToDevice, installRemotePackage, and removePackage steps 385 * 386 * @param packageFilePath the absolute file system path to file on local host to install 387 * @param reinstall set to <code>true</code> if re-install of app should be performed 388 * @param extraArgs optional extra arguments to pass. See 'adb shell pm install --help' for 389 * available options. 390 * @return a {@link String} with an error code, or <code>null</code> if success. 391 * @throws InstallException if the installation fails. 392 */ installPackage(String packageFilePath, boolean reinstall, String... extraArgs)393 public String installPackage(String packageFilePath, boolean reinstall, String... extraArgs) 394 throws InstallException; 395 396 /** 397 * Pushes a file to device 398 * 399 * @param localFilePath the absolute path to file on local host 400 * @return {@link String} destination path on device for file 401 * @throws TimeoutException in case of timeout on the connection. 402 * @throws AdbCommandRejectedException if adb rejects the command 403 * @throws IOException in case of I/O error on the connection. 404 * @throws SyncException if an error happens during the push of the package on the device. 405 */ syncPackageToDevice(String localFilePath)406 public String syncPackageToDevice(String localFilePath) 407 throws TimeoutException, AdbCommandRejectedException, IOException, SyncException; 408 409 /** 410 * Installs the application package that was pushed to a temporary location on the device. 411 * 412 * @param remoteFilePath absolute file path to package file on device 413 * @param reinstall set to <code>true</code> if re-install of app should be performed 414 * @param extraArgs optional extra arguments to pass. See 'adb shell pm install --help' for 415 * available options. 416 * @throws InstallException if the installation fails. 417 */ installRemotePackage(String remoteFilePath, boolean reinstall, String... extraArgs)418 public String installRemotePackage(String remoteFilePath, boolean reinstall, 419 String... extraArgs) throws InstallException; 420 421 /** 422 * Removes a file from device. 423 * 424 * @param remoteFilePath path on device of file to remove 425 * @throws InstallException if the installation fails. 426 */ removeRemotePackage(String remoteFilePath)427 public void removeRemotePackage(String remoteFilePath) throws InstallException; 428 429 /** 430 * Uninstalls an package from the device. 431 * 432 * @param packageName the Android application package name to uninstall 433 * @return a {@link String} with an error code, or <code>null</code> if success. 434 * @throws InstallException if the uninstallation fails. 435 */ uninstallPackage(String packageName)436 public String uninstallPackage(String packageName) throws InstallException; 437 438 /** 439 * Reboot the device. 440 * 441 * @param into the bootloader name to reboot into, or null to just reboot the device. 442 * @throws TimeoutException in case of timeout on the connection. 443 * @throws AdbCommandRejectedException if adb rejects the command 444 * @throws IOException 445 */ reboot(String into)446 public void reboot(String into) 447 throws TimeoutException, AdbCommandRejectedException, IOException; 448 } 449