• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
59         private String mState;
60 
DeviceState(String state)61         DeviceState(String state) {
62             mState = state;
63         }
64 
65         /**
66          * Returns a {@link DeviceState} from the string returned by <code>adb devices</code>.
67          *
68          * @param state the device state.
69          * @return a {@link DeviceState} object or <code>null</code> if the state is unknown.
70          */
getState(String state)71         public static DeviceState getState(String state) {
72             for (DeviceState deviceState : values()) {
73                 if (deviceState.mState.equals(state)) {
74                     return deviceState;
75                 }
76             }
77             return null;
78         }
79     }
80 
81     /**
82      * Returns the serial number of the device.
83      */
getSerialNumber()84     public String getSerialNumber();
85 
86     /**
87      * Returns the name of the AVD the emulator is running.
88      * <p/>This is only valid if {@link #isEmulator()} returns true.
89      * <p/>If the emulator is not running any AVD (for instance it's running from an Android source
90      * tree build), this method will return "<code>&lt;build&gt;</code>".
91      *
92      * @return the name of the AVD or <code>null</code> if there isn't any.
93      */
getAvdName()94     public String getAvdName();
95 
96     /**
97      * Returns the state of the device.
98      */
getState()99     public DeviceState getState();
100 
101     /**
102      * Returns the device properties. It contains the whole output of 'getprop'
103      */
getProperties()104     public Map<String, String> getProperties();
105 
106     /**
107      * Returns the number of property for this device.
108      */
getPropertyCount()109     public int getPropertyCount();
110 
111     /**
112      * Returns a property value.
113      *
114      * @param name the name of the value to return.
115      * @return the value or <code>null</code> if the property does not exist.
116      */
getProperty(String name)117     public String getProperty(String name);
118 
119     /**
120      * Returns a mount point.
121      *
122      * @param name the name of the mount point to return
123      *
124      * @see #MNT_EXTERNAL_STORAGE
125      * @see #MNT_ROOT
126      * @see #MNT_DATA
127      */
getMountPoint(String name)128     public String getMountPoint(String name);
129 
130     /**
131      * Returns if the device is ready.
132      *
133      * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#ONLINE}.
134      */
isOnline()135     public boolean isOnline();
136 
137     /**
138      * Returns <code>true</code> if the device is an emulator.
139      */
isEmulator()140     public boolean isEmulator();
141 
142     /**
143      * Returns if the device is offline.
144      *
145      * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#OFFLINE}.
146      */
isOffline()147     public boolean isOffline();
148 
149     /**
150      * Returns if the device is in bootloader mode.
151      *
152      * @return <code>true</code> if {@link #getState()} returns {@link DeviceState#BOOTLOADER}.
153      */
isBootLoader()154     public boolean isBootLoader();
155 
156     /**
157      * Returns whether the {@link Device} has {@link Client}s.
158      */
hasClients()159     public boolean hasClients();
160 
161     /**
162      * Returns the array of clients.
163      */
getClients()164     public Client[] getClients();
165 
166     /**
167      * Returns a {@link Client} by its application name.
168      *
169      * @param applicationName the name of the application
170      * @return the <code>Client</code> object or <code>null</code> if no match was found.
171      */
getClient(String applicationName)172     public Client getClient(String applicationName);
173 
174     /**
175      * Returns a {@link SyncService} object to push / pull files to and from the device.
176      *
177      * @return <code>null</code> if the SyncService couldn't be created. This can happen if adb
178      *            refuse to open the connection because the {@link IDevice} is invalid
179      *            (or got disconnected).
180      * @throws TimeoutException in case of timeout on the connection.
181      * @throws AdbCommandRejectedException if adb rejects the command
182      * @throws IOException if the connection with adb failed.
183      */
getSyncService()184     public SyncService getSyncService()
185             throws TimeoutException, AdbCommandRejectedException, IOException;
186 
187     /**
188      * Returns a {@link FileListingService} for this device.
189      */
getFileListingService()190     public FileListingService getFileListingService();
191 
192     /**
193      * Takes a screen shot of the device and returns it as a {@link RawImage}.
194      *
195      * @return the screenshot as a <code>RawImage</code> or <code>null</code> if something
196      *            went wrong.
197      * @throws TimeoutException in case of timeout on the connection.
198      * @throws AdbCommandRejectedException if adb rejects the command
199      * @throws IOException in case of I/O error on the connection.
200      */
getScreenshot()201     public RawImage getScreenshot() throws TimeoutException, AdbCommandRejectedException,
202             IOException;
203 
204     /**
205      * Executes a shell command on the device, and sends the result to a <var>receiver</var>
206      * <p/>This is similar to calling
207      * <code>executeShellCommand(command, receiver, DdmPreferences.getTimeOut())</code>.
208      *
209      * @param command the shell command to execute
210      * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell
211      *            command
212      * @throws TimeoutException in case of timeout on the connection.
213      * @throws AdbCommandRejectedException if adb rejects the command
214      * @throws ShellCommandUnresponsiveException in case the shell command doesn't send output
215      *            for a given time.
216      * @throws IOException in case of I/O error on the connection.
217      *
218      * @see #executeShellCommand(String, IShellOutputReceiver, int)
219      * @see DdmPreferences#getTimeOut()
220      */
executeShellCommand(String command, IShellOutputReceiver receiver)221     public void executeShellCommand(String command, IShellOutputReceiver receiver)
222             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
223             IOException;
224 
225     /**
226      * Executes a shell command on the device, and sends the result to a <var>receiver</var>.
227      * <p/><var>maxTimeToOutputResponse</var> is used as a maximum waiting time when expecting the
228      * command output from the device.<br>
229      * At any time, if the shell command does not output anything for a period longer than
230      * <var>maxTimeToOutputResponse</var>, then the method will throw
231      * {@link ShellCommandUnresponsiveException}.
232      * <p/>For commands like log output, a <var>maxTimeToOutputResponse</var> value of 0, meaning
233      * that the method will never throw and will block until the receiver's
234      * {@link IShellOutputReceiver#isCancelled()} returns <code>true</code>, should be
235      * used.
236      *
237      * @param command the shell command to execute
238      * @param receiver the {@link IShellOutputReceiver} that will receives the output of the shell
239      *            command
240      * @param maxTimeToOutputResponse the maximum amount of time during which the command is allowed
241      *            to not output any response. A value of 0 means the method will wait forever
242      *            (until the <var>receiver</var> cancels the execution) for command output and
243      *            never throw.
244      * @throws TimeoutException in case of timeout on the connection when sending the command.
245      * @throws AdbCommandRejectedException if adb rejects the command.
246      * @throws ShellCommandUnresponsiveException in case the shell command doesn't send any output
247      *            for a period longer than <var>maxTimeToOutputResponse</var>.
248      * @throws IOException in case of I/O error on the connection.
249      *
250      * @see DdmPreferences#getTimeOut()
251      */
executeShellCommand(String command, IShellOutputReceiver receiver, int maxTimeToOutputResponse)252     public void executeShellCommand(String command, IShellOutputReceiver receiver,
253             int maxTimeToOutputResponse)
254             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
255             IOException;
256 
257     /**
258      * Runs the event log service and outputs the event log to the {@link LogReceiver}.
259      * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true.
260      * @param receiver the receiver to receive the event log entries.
261      * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the
262      * timeout happens during setup. Once logs start being received, no timeout will occur as it's
263      * not possible to detect a difference between no log and timeout.
264      * @throws AdbCommandRejectedException if adb rejects the command
265      * @throws IOException in case of I/O error on the connection.
266      */
runEventLogService(LogReceiver receiver)267     public void runEventLogService(LogReceiver receiver)
268             throws TimeoutException, AdbCommandRejectedException, IOException;
269 
270     /**
271      * Runs the log service for the given log and outputs the log to the {@link LogReceiver}.
272      * <p/>This call is blocking until {@link LogReceiver#isCancelled()} returns true.
273      *
274      * @param logname the logname of the log to read from.
275      * @param receiver the receiver to receive the event log entries.
276      * @throws TimeoutException in case of timeout on the connection. This can only be thrown if the
277      *            timeout happens during setup. Once logs start being received, no timeout will
278      *            occur as it's not possible to detect a difference between no log and timeout.
279      * @throws AdbCommandRejectedException if adb rejects the command
280      * @throws IOException in case of I/O error on the connection.
281      */
runLogService(String logname, LogReceiver receiver)282     public void runLogService(String logname, LogReceiver receiver)
283             throws TimeoutException, AdbCommandRejectedException, IOException;
284 
285     /**
286      * Creates a port forwarding between a local and a remote port.
287      *
288      * @param localPort the local port to forward
289      * @param remotePort the remote port.
290      * @return <code>true</code> if success.
291      * @throws TimeoutException in case of timeout on the connection.
292      * @throws AdbCommandRejectedException if adb rejects the command
293      * @throws IOException in case of I/O error on the connection.
294      */
createForward(int localPort, int remotePort)295     public void createForward(int localPort, int remotePort)
296             throws TimeoutException, AdbCommandRejectedException, IOException;
297 
298     /**
299      * Removes a port forwarding between a local and a remote port.
300      *
301      * @param localPort the local port to forward
302      * @param remotePort the remote port.
303      * @return <code>true</code> if success.
304      * @throws TimeoutException in case of timeout on the connection.
305      * @throws AdbCommandRejectedException if adb rejects the command
306      * @throws IOException in case of I/O error on the connection.
307      */
removeForward(int localPort, int remotePort)308     public void removeForward(int localPort, int remotePort)
309             throws TimeoutException, AdbCommandRejectedException, IOException;
310 
311     /**
312      * Returns the name of the client by pid or <code>null</code> if pid is unknown
313      * @param pid the pid of the client.
314      */
getClientName(int pid)315     public String getClientName(int pid);
316 
317     /**
318      * Installs an Android application on device.
319      * This is a helper method that combines the syncPackageToDevice, installRemotePackage,
320      * and removePackage steps
321      *
322      * @param packageFilePath the absolute file system path to file on local host to install
323      * @param reinstall set to <code>true</code> if re-install of app should be performed
324      * @return a {@link String} with an error code, or <code>null</code> if success.
325      * @throws TimeoutException in case of timeout on the connection.
326      * @throws AdbCommandRejectedException if adb rejects the command
327      * @throws ShellCommandUnresponsiveException if the device didn't respond for long time when
328      *            performing the action.
329      * @throws IOException in case of I/O error on the connection.
330      */
installPackage(String packageFilePath, boolean reinstall)331     public String installPackage(String packageFilePath, boolean reinstall)
332             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
333             IOException;
334 
335     /**
336      * Pushes a file to device
337      *
338      * @param localFilePath the absolute path to file on local host
339      * @return {@link String} destination path on device for file
340      * @throws TimeoutException in case of timeout on the connection.
341      * @throws AdbCommandRejectedException if adb rejects the command
342      * @throws IOException in case of I/O error on the connection.
343      */
syncPackageToDevice(String localFilePath)344     public String syncPackageToDevice(String localFilePath)
345             throws TimeoutException, AdbCommandRejectedException, IOException;
346 
347     /**
348      * Installs the application package that was pushed to a temporary location on the device.
349      *
350      * @param remoteFilePath absolute file path to package file on device
351      * @param reinstall set to <code>true</code> if re-install of app should be performed
352      * @throws TimeoutException in case of timeout on the connection.
353      * @throws AdbCommandRejectedException if adb rejects the command
354      * @throws ShellCommandUnresponsiveException if the device didn't respond for long time when
355      *            performing the action.
356      * @throws IOException if installation failed
357      */
installRemotePackage(String remoteFilePath, boolean reinstall)358     public String installRemotePackage(String remoteFilePath, boolean reinstall)
359             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
360             IOException;
361 
362     /**
363      * Removes a file from device.
364      *
365      * @param remoteFilePath path on device of file to remove
366      * @throws TimeoutException in case of timeout on the connection.
367      * @throws AdbCommandRejectedException if adb rejects the command
368      * @throws ShellCommandUnresponsiveException if the device didn't respond for long time when
369      *            performing the action.
370      * @throws IOException if file removal failed
371      */
removeRemotePackage(String remoteFilePath)372     public void removeRemotePackage(String remoteFilePath)
373             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
374             IOException;
375 
376     /**
377      * Uninstalls an package from the device.
378      *
379      * @param packageName the Android application package name to uninstall
380      * @return a {@link String} with an error code, or <code>null</code> if success.
381      * @throws TimeoutException in case of timeout on the connection.
382      * @throws AdbCommandRejectedException if adb rejects the command
383      * @throws ShellCommandUnresponsiveException if the device didn't respond for long time when
384      *            performing the action.
385      * @throws IOException
386      */
uninstallPackage(String packageName)387     public String uninstallPackage(String packageName)
388             throws TimeoutException, AdbCommandRejectedException, ShellCommandUnresponsiveException,
389             IOException;
390 
391     /**
392      * Reboot the device.
393      *
394      * @param into the bootloader name to reboot into, or null to just reboot the device.
395      * @throws TimeoutException in case of timeout on the connection.
396      * @throws AdbCommandRejectedException if adb rejects the command
397      * @throws IOException
398      */
reboot(String into)399     public void reboot(String into)
400             throws TimeoutException, AdbCommandRejectedException, IOException;
401 }
402