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 @Deprecated 46 public final static String PROP_BUILD_VERSION_NUMBER = PROP_BUILD_API_LEVEL; 47 48 public final static String MNT_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; //$NON-NLS-1$ 49 public final static String MNT_ROOT = "ANDROID_ROOT"; //$NON-NLS-1$ 50 public final static String MNT_DATA = "ANDROID_DATA"; //$NON-NLS-1$ 51 52 /** 53 * The state of a device. 54 */ 55 public static enum DeviceState { 56 BOOTLOADER("bootloader"), //$NON-NLS-1$ 57 OFFLINE("offline"), //$NON-NLS-1$ 58 ONLINE("device"), //$NON-NLS-1$ 59 RECOVERY("recovery"); //$NON-NLS-1$ 60 61 private String mState; 62 DeviceState(String state)63 DeviceState(String state) { 64 mState = state; 65 } 66 67 /** 68 * Returns a {@link DeviceState} from the string returned by <code>adb devices</code>. 69 * 70 * @param state the device state. 71 * @return a {@link DeviceState} object or <code>null</code> if the state is unknown. 72 */ getState(String state)73 public static DeviceState getState(String state) { 74 for (DeviceState deviceState : values()) { 75 if (deviceState.mState.equals(state)) { 76 return deviceState; 77 } 78 } 79 return null; 80 } 81 } 82 83 /** 84 * Namespace of a Unix Domain Socket created on the device. 85 */ 86 public static enum DeviceUnixSocketNamespace { 87 ABSTRACT("localabstract"), //$NON-NLS-1$ 88 FILESYSTEM("localfilesystem"), //$NON-NLS-1$ 89 RESERVED("localreserved"); //$NON-NLS-1$ 90 91 private String mType; 92 DeviceUnixSocketNamespace(String type)93 private DeviceUnixSocketNamespace(String type) { 94 mType = type; 95 } 96 getType()97 String getType() { 98 return mType; 99 } 100 }; 101 102 /** 103 * Returns the serial number of the device. 104 */ getSerialNumber()105 public String getSerialNumber(); 106 107 /** 108 * Returns the name of the AVD the emulator is running. 109 * <p/>This is only valid if {@link #isEmulator()} returns true. 110 * <p/>If the emulator is not running any AVD (for instance it's running from an Android source 111 * tree build), this method will return "<code><build></code>". 112 * 113 * @return the name of the AVD or <code>null</code> if there isn't any. 114 */ getAvdName()115 public String getAvdName(); 116 117 /** 118 * Returns the state of the device. 119 */ getState()120 public DeviceState getState(); 121 122 /** 123 * Returns the device properties. It contains the whole output of 'getprop' 124 */ getProperties()125 public Map<String, String> getProperties(); 126 127 /** 128 * Returns the number of property for this device. 129 */ getPropertyCount()130 public int getPropertyCount(); 131 132 /** 133 * Returns the cached property value. 134 * 135 * @param name the name of the value to return. 136 * @return the value or <code>null</code> if the property does not exist or has not yet been 137 * cached. 138 */ getProperty(String name)139 public String getProperty(String name); 140 141 /** 142 * Returns <code>true></code> if properties have been cached 143 */ arePropertiesSet()144 public boolean arePropertiesSet(); 145 146 /** 147 * A variant of {@link #getProperty(String)} that will attempt to retrieve the given 148 * property from device directly, without using cache. 149 * 150 * @param name the name of the value to return. 151 * @return the value or <code>null</code> if the property does not exist 152 * @throws TimeoutException in case of timeout on the connection. 153 * @throws AdbCommandRejectedException if adb rejects the command 154 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output for a 155 * given time. 156 * @throws IOException in case of I/O error on the connection. 157 */ getPropertySync(String name)158 public String getPropertySync(String name) throws TimeoutException, 159 AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException; 160 161 /** 162 * A combination of {@link #getProperty(String)} and {@link #getPropertySync(String)} that 163 * will attempt to retrieve the property from cache if available, and if not, will query the 164 * device directly. 165 * 166 * @param name the name of the value to return. 167 * @return the value or <code>null</code> if the property does not exist 168 * @throws TimeoutException in case of timeout on the connection. 169 * @throws AdbCommandRejectedException if adb rejects the command 170 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output for a 171 * given time. 172 * @throws IOException in case of I/O error on the connection. 173 */ getPropertyCacheOrSync(String name)174 public String getPropertyCacheOrSync(String name) throws TimeoutException, 175 AdbCommandRejectedException, ShellCommandUnresponsiveException, IOException; 176 177 /** 178 * Returns a mount point. 179 * 180 * @param name the name of the mount point to return 181 * 182 * @see #MNT_EXTERNAL_STORAGE 183 * @see #MNT_ROOT 184 * @see #MNT_DATA 185 */ getMountPoint(String name)186 public String getMountPoint(String name); 187 188 /** 189 * Returns if the device is ready. 190 * 191 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#ONLINE}. 192 */ isOnline()193 public boolean isOnline(); 194 195 /** 196 * Returns <code>true</code> if the device is an emulator. 197 */ isEmulator()198 public boolean isEmulator(); 199 200 /** 201 * Returns if the device is offline. 202 * 203 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#OFFLINE}. 204 */ isOffline()205 public boolean isOffline(); 206 207 /** 208 * Returns if the device is in bootloader mode. 209 * 210 * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#BOOTLOADER}. 211 */ isBootLoader()212 public boolean isBootLoader(); 213 214 /** 215 * Returns whether the {@link Device} has {@link Client}s. 216 */ hasClients()217 public boolean hasClients(); 218 219 /** 220 * Returns the array of clients. 221 */ getClients()222 public Client[] getClients(); 223 224 /** 225 * Returns a {@link Client} by its application name. 226 * 227 * @param applicationName the name of the application 228 * @return the <code>Client</code> object or <code>null</code> if no match was found. 229 */ getClient(String applicationName)230 public Client getClient(String applicationName); 231 232 /** 233 * Returns a {@link SyncService} object to push / pull files to and from the device. 234 * 235 * @return <code>null</code> if the SyncService couldn't be created. This can happen if adb 236 * refuse to open the connection because the {@link IDevice} is invalid 237 * (or got disconnected). 238 * @throws TimeoutException in case of timeout on the connection. 239 * @throws AdbCommandRejectedException if adb rejects the command 240 * @throws IOException if the connection with adb failed. 241 */ getSyncService()242 public SyncService getSyncService() 243 throws TimeoutException, AdbCommandRejectedException, IOException; 244 245 /** 246 * Returns a {@link FileListingService} for this device. 247 */ getFileListingService()248 public FileListingService getFileListingService(); 249 250 /** 251 * Takes a screen shot of the device and returns it as a {@link RawImage}. 252 * 253 * @return the screenshot as a <code>RawImage</code> or <code>null</code> if something 254 * went wrong. 255 * @throws TimeoutException in case of timeout on the connection. 256 * @throws AdbCommandRejectedException if adb rejects the command 257 * @throws IOException in case of I/O error on the connection. 258 */ getScreenshot()259 public RawImage getScreenshot() throws TimeoutException, AdbCommandRejectedException, 260 IOException; 261 262 /** 263 * Executes a shell command on the device, and sends the result to a <var>receiver</var> 264 * <p/>This is similar to calling 265 * <code>executeShellCommand(command, receiver, DdmPreferences.getTimeOut())</code>. 266 * 267 * @param command the shell command to execute 268 * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell 269 * command 270 * @throws TimeoutException in case of timeout on the connection. 271 * @throws AdbCommandRejectedException if adb rejects the command 272 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output 273 * for a given time. 274 * @throws IOException in case of I/O error on the connection. 275 * 276 * @see #executeShellCommand(String, IShellOutputReceiver, int) 277 * @see DdmPreferences#getTimeOut() 278 */ executeShellCommand(String command, IShellOutputReceiver receiver)279 public void executeShellCommand(String command, IShellOutputReceiver receiver) 280 throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, 281 IOException; 282 283 /** 284 * Executes a shell command on the device, and sends the result to a <var>receiver</var>. 285 * <p/><var>maxTimeToOutputResponse</var> is used as a maximum waiting time when expecting the 286 * command output from the device.<br> 287 * At any time, if the shell command does not output anything for a period longer than 288 * <var>maxTimeToOutputResponse</var>, then the method will throw 289 * {@link ShellCommandUnresponsiveException}. 290 * <p/>For commands like log output, a <var>maxTimeToOutputResponse</var> value of 0, meaning 291 * that the method will never throw and will block until the receiver's 292 * {@link IShellOutputReceiver#isCancelled()} returns <code>true</code>, should be 293 * used. 294 * 295 * @param command the shell command to execute 296 * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell 297 * command 298 * @param maxTimeToOutputResponse the maximum amount of time during which the command is allowed 299 * to not output any response. A value of 0 means the method will wait forever 300 * (until the <var>receiver</var> cancels the execution) for command output and 301 * never throw. 302 * @throws TimeoutException in case of timeout on the connection when sending the command. 303 * @throws AdbCommandRejectedException if adb rejects the command. 304 * @throws ShellCommandUnresponsiveException in case the shell command doesn't send any output 305 * for a period longer than <var>maxTimeToOutputResponse</var>. 306 * @throws IOException in case of I/O error on the connection. 307 * 308 * @see DdmPreferences#getTimeOut() 309 */ executeShellCommand(String command, IShellOutputReceiver receiver, int maxTimeToOutputResponse)310 public void executeShellCommand(String command, IShellOutputReceiver receiver, 311 int maxTimeToOutputResponse) 312 throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException, 313 IOException; 314 315 /** 316 * Runs the event log service and outputs the event log to the {@link LogReceiver}. 317 * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true. 318 * @param receiver the receiver to receive the event log entries. 319 * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the 320 * timeout happens during setup. Once logs start being received, no timeout will occur as it's 321 * not possible to detect a difference between no log and timeout. 322 * @throws AdbCommandRejectedException if adb rejects the command 323 * @throws IOException in case of I/O error on the connection. 324 */ runEventLogService(LogReceiver receiver)325 public void runEventLogService(LogReceiver receiver) 326 throws TimeoutException, AdbCommandRejectedException, IOException; 327 328 /** 329 * Runs the log service for the given log and outputs the log to the {@link LogReceiver}. 330 * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true. 331 * 332 * @param logname the logname of the log to read from. 333 * @param receiver the receiver to receive the event log entries. 334 * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the 335 * timeout happens during setup. Once logs start being received, no timeout will 336 * occur as it's not possible to detect a difference between no log and timeout. 337 * @throws AdbCommandRejectedException if adb rejects the command 338 * @throws IOException in case of I/O error on the connection. 339 */ runLogService(String logname, LogReceiver receiver)340 public void runLogService(String logname, LogReceiver receiver) 341 throws TimeoutException, AdbCommandRejectedException, IOException; 342 343 /** 344 * Creates a port forwarding between a local and a remote port. 345 * 346 * @param localPort the local port to forward 347 * @param remotePort the remote port. 348 * @return <code>true</code> if success. 349 * @throws TimeoutException in case of timeout on the connection. 350 * @throws AdbCommandRejectedException if adb rejects the command 351 * @throws IOException in case of I/O error on the connection. 352 */ createForward(int localPort, int remotePort)353 public void createForward(int localPort, int remotePort) 354 throws TimeoutException, AdbCommandRejectedException, IOException; 355 356 /** 357 * Creates a port forwarding between a local TCP port and a remote Unix Domain Socket. 358 * 359 * @param localPort the local port to forward 360 * @param remoteSocketName name of the unix domain socket created on the device 361 * @param namespace namespace in which the unix domain socket was created 362 * @return <code>true</code> if success. 363 * @throws TimeoutException in case of timeout on the connection. 364 * @throws AdbCommandRejectedException if adb rejects the command 365 * @throws IOException in case of I/O error on the connection. 366 */ createForward(int localPort, String remoteSocketName, DeviceUnixSocketNamespace namespace)367 public void createForward(int localPort, String remoteSocketName, 368 DeviceUnixSocketNamespace namespace) 369 throws TimeoutException, AdbCommandRejectedException, IOException; 370 371 /** 372 * Removes a port forwarding between a local and a remote port. 373 * 374 * @param localPort the local port to forward 375 * @param remotePort the remote port. 376 * @return <code>true</code> if success. 377 * @throws TimeoutException in case of timeout on the connection. 378 * @throws AdbCommandRejectedException if adb rejects the command 379 * @throws IOException in case of I/O error on the connection. 380 */ removeForward(int localPort, int remotePort)381 public void removeForward(int localPort, int remotePort) 382 throws TimeoutException, AdbCommandRejectedException, IOException; 383 384 /** 385 * Removes an existing port forwarding between a local and a remote port. 386 * 387 * @param localPort the local port to forward 388 * @param remoteSocketName the remote unix domain socket name. 389 * @param namespace namespace in which the unix domain socket was created 390 * @return <code>true</code> if success. 391 * @throws TimeoutException in case of timeout on the connection. 392 * @throws AdbCommandRejectedException if adb rejects the command 393 * @throws IOException in case of I/O error on the connection. 394 */ removeForward(int localPort, String remoteSocketName, DeviceUnixSocketNamespace namespace)395 public void removeForward(int localPort, String remoteSocketName, 396 DeviceUnixSocketNamespace namespace) 397 throws TimeoutException, AdbCommandRejectedException, IOException; 398 399 /** 400 * Returns the name of the client by pid or <code>null</code> if pid is unknown 401 * @param pid the pid of the client. 402 */ getClientName(int pid)403 public String getClientName(int pid); 404 405 /** 406 * Push a single file. 407 * @param local the local filepath. 408 * @param remote The remote filepath. 409 * 410 * @throws IOException in case of I/O error on the connection. 411 * @throws AdbCommandRejectedException if adb rejects the command 412 * @throws TimeoutException in case of a timeout reading responses from the device. 413 * @throws SyncException if file could not be pushed 414 */ pushFile(String local, String remote)415 public void pushFile(String local, String remote) 416 throws IOException, AdbCommandRejectedException, TimeoutException, SyncException; 417 418 /** 419 * Pulls a single file. 420 * 421 * @param remote the full path to the remote file 422 * @param local The local destination. 423 * 424 * @throws IOException in case of an IO exception. 425 * @throws AdbCommandRejectedException if adb rejects the command 426 * @throws TimeoutException in case of a timeout reading responses from the device. 427 * @throws SyncException in case of a sync exception. 428 */ pullFile(String remote, String local)429 public void pullFile(String remote, String local) 430 throws IOException, AdbCommandRejectedException, TimeoutException, SyncException; 431 432 /** 433 * Installs an Android application on device. This is a helper method that combines the 434 * syncPackageToDevice, installRemotePackage, and removePackage steps 435 * 436 * @param packageFilePath the absolute file system path to file on local host to install 437 * @param reinstall set to <code>true</code> if re-install of app should be performed 438 * @param extraArgs optional extra arguments to pass. See 'adb shell pm install --help' for 439 * available options. 440 * @return a {@link String} with an error code, or <code>null</code> if success. 441 * @throws InstallException if the installation fails. 442 */ installPackage(String packageFilePath, boolean reinstall, String... extraArgs)443 public String installPackage(String packageFilePath, boolean reinstall, String... extraArgs) 444 throws InstallException; 445 446 /** 447 * Pushes a file to device 448 * 449 * @param localFilePath the absolute path to file on local host 450 * @return {@link String} destination path on device for file 451 * @throws TimeoutException in case of timeout on the connection. 452 * @throws AdbCommandRejectedException if adb rejects the command 453 * @throws IOException in case of I/O error on the connection. 454 * @throws SyncException if an error happens during the push of the package on the device. 455 */ syncPackageToDevice(String localFilePath)456 public String syncPackageToDevice(String localFilePath) 457 throws TimeoutException, AdbCommandRejectedException, IOException, SyncException; 458 459 /** 460 * Installs the application package that was pushed to a temporary location on the device. 461 * 462 * @param remoteFilePath absolute file path to package file on device 463 * @param reinstall set to <code>true</code> if re-install of app should be performed 464 * @param extraArgs optional extra arguments to pass. See 'adb shell pm install --help' for 465 * available options. 466 * @throws InstallException if the installation fails. 467 */ installRemotePackage(String remoteFilePath, boolean reinstall, String... extraArgs)468 public String installRemotePackage(String remoteFilePath, boolean reinstall, 469 String... extraArgs) throws InstallException; 470 471 /** 472 * Removes a file from device. 473 * 474 * @param remoteFilePath path on device of file to remove 475 * @throws InstallException if the installation fails. 476 */ removeRemotePackage(String remoteFilePath)477 public void removeRemotePackage(String remoteFilePath) throws InstallException; 478 479 /** 480 * Uninstalls an package from the device. 481 * 482 * @param packageName the Android application package name to uninstall 483 * @return a {@link String} with an error code, or <code>null</code> if success. 484 * @throws InstallException if the uninstallation fails. 485 */ uninstallPackage(String packageName)486 public String uninstallPackage(String packageName) throws InstallException; 487 488 /** 489 * Reboot the device. 490 * 491 * @param into the bootloader name to reboot into, or null to just reboot the device. 492 * @throws TimeoutException in case of timeout on the connection. 493 * @throws AdbCommandRejectedException if adb rejects the command 494 * @throws IOException 495 */ reboot(String into)496 public void reboot(String into) 497 throws TimeoutException, AdbCommandRejectedException, IOException; 498 499 /** 500 * Return the device's battery level, from 0 to 100 percent. 501 * <p/> 502 * The battery level may be cached. Only queries the device for its 503 * battery level if 5 minutes have expired since the last successful query. 504 * 505 * @return the battery level or <code>null</code> if it could not be retrieved 506 */ getBatteryLevel()507 public Integer getBatteryLevel() throws TimeoutException, 508 AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException; 509 510 /** 511 * Return the device's battery level, from 0 to 100 percent. 512 * <p/> 513 * The battery level may be cached. Only queries the device for its 514 * battery level if <code>freshnessMs</code> ms have expired since the last successful query. 515 * 516 * @param freshnessMs 517 * @return the battery level or <code>null</code> if it could not be retrieved 518 * @throws ShellCommandUnresponsiveException 519 */ getBatteryLevel(long freshnessMs)520 public Integer getBatteryLevel(long freshnessMs) throws TimeoutException, 521 AdbCommandRejectedException, IOException, ShellCommandUnresponsiveException; 522 523 } 524