3 ** Copyright 2007, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
19 package com.android.commands.am;
21 import static android.app.ActivityManager.RESIZE_MODE_SYSTEM;
22 import static android.app.ActivityManager.RESIZE_MODE_USER;
23 import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
24 import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
26 import android.app.ActivityManager;
27 import android.app.ActivityManager.StackInfo;
28 import android.app.ActivityManagerNative;
29 import android.app.ActivityOptions;
30 import android.app.IActivityContainer;
31 import android.app.IActivityController;
32 import android.app.IActivityManager;
33 import android.app.IInstrumentationWatcher;
34 import android.app.Instrumentation;
35 import android.app.IStopUserCallback;
36 import android.app.ProfilerInfo;
37 import android.app.UiAutomationConnection;
38 import android.app.usage.ConfigurationStats;
39 import android.app.usage.IUsageStatsManager;
40 import android.app.usage.UsageStatsManager;
41 import android.content.ComponentCallbacks2;
42 import android.content.ComponentName;
43 import android.content.Context;
44 import android.content.IIntentReceiver;
45 import android.content.Intent;
46 import android.content.pm.IPackageManager;
47 import android.content.pm.InstrumentationInfo;
48 import android.content.pm.ParceledListSlice;
49 import android.content.pm.ResolveInfo;
50 import android.content.pm.UserInfo;
51 import android.content.res.AssetManager;
52 import android.content.res.Configuration;
53 import android.content.res.Resources;
54 import android.graphics.Rect;
55 import android.os.Binder;
56 import android.os.Build;
57 import android.os.Bundle;
58 import android.os.ParcelFileDescriptor;
59 import android.os.RemoteException;
60 import android.os.SELinux;
61 import android.os.ServiceManager;
62 import android.os.ShellCommand;
63 import android.os.SystemClock;
64 import android.os.SystemProperties;
65 import android.os.UserHandle;
66 import android.text.TextUtils;
67 import android.util.AndroidException;
68 import android.util.ArrayMap;
69 import android.util.DisplayMetrics;
70 import android.view.IWindowManager;
72 import com.android.internal.os.BaseCommand;
73 import com.android.internal.util.HexDump;
74 import com.android.internal.util.Preconditions;
76 import java.io.BufferedReader;
78 import java.io.FileNotFoundException;
79 import java.io.IOException;
80 import java.io.InputStreamReader;
81 import java.io.PrintStream;
82 import java.io.PrintWriter;
83 import java.net.URISyntaxException;
84 import java.util.ArrayList;
85 import java.util.Collections;
86 import java.util.Comparator;
87 import java.util.List;
89 public class Am extends BaseCommand {
91 private static final String SHELL_PACKAGE_NAME = "com.android.shell";
93 // Is the object moving in a positive direction?
94 private static final boolean MOVING_FORWARD = true;
95 // Is the object moving in the horizontal plan?
96 private static final boolean MOVING_HORIZONTALLY = true;
97 // Is the object current point great then its target point?
98 private static final boolean GREATER_THAN_TARGET = true;
99 // Amount we reduce the stack size by when testing a task re-size.
100 private static final int STACK_BOUNDS_INSET = 10;
102 private IActivityManager mAm;
103 private IPackageManager mPm;
105 private int mStartFlags = 0;
106 private boolean mWaitOption = false;
107 private boolean mStopOption = false;
109 private int mRepeat = 0;
111 private String mReceiverPermission;
113 private String mProfileFile;
114 private int mSamplingInterval;
115 private boolean mAutoStop;
116 private int mStackId;
119 * Command-line entry point.
121 * @param args The command-line arguments
123 public static void main(String[] args) {
124 (new Am()).run(args);
128 public void onShowUsage(PrintStream out) {
129 PrintWriter pw = new PrintWriter(out);
131 "usage: am [subcommand] [options]\n" +
132 "usage: am start [-D] [-N] [-W] [-P <FILE>] [--start-profiler <FILE>]\n" +
133 " [--sampling INTERVAL] [-R COUNT] [-S]\n" +
134 " [--track-allocation] [--user <USER_ID> | current] <INTENT>\n" +
135 " am startservice [--user <USER_ID> | current] <INTENT>\n" +
136 " am stopservice [--user <USER_ID> | current] <INTENT>\n" +
137 " am force-stop [--user <USER_ID> | all | current] <PACKAGE>\n" +
138 " am kill [--user <USER_ID> | all | current] <PACKAGE>\n" +
140 " am broadcast [--user <USER_ID> | all | current] <INTENT>\n" +
141 " am instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w]\n" +
142 " [--user <USER_ID> | current]\n" +
143 " [--no-window-animation] [--abi <ABI>] <COMPONENT>\n" +
144 " am profile start [--user <USER_ID> current] [--sampling INTERVAL] <PROCESS> <FILE>\n" +
145 " am profile stop [--user <USER_ID> current] [<PROCESS>]\n" +
146 " am dumpheap [--user <USER_ID> current] [-n] <PROCESS> <FILE>\n" +
147 " am set-debug-app [-w] [--persistent] <PACKAGE>\n" +
148 " am clear-debug-app\n" +
149 " am set-watch-heap <PROCESS> <MEM-LIMIT>\n" +
150 " am clear-watch-heap\n" +
151 " am bug-report [--progress]\n" +
152 " am monitor [--gdb <port>]\n" +
153 " am hang [--allow-restart]\n" +
155 " am idle-maintenance\n" +
156 " am screen-compat [on|off] <PACKAGE>\n" +
157 " am package-importance <PACKAGE>\n" +
158 " am to-uri [INTENT]\n" +
159 " am to-intent-uri [INTENT]\n" +
160 " am to-app-uri [INTENT]\n" +
161 " am switch-user <USER_ID>\n" +
162 " am start-user <USER_ID>\n" +
163 " am unlock-user <USER_ID> [TOKEN_HEX]\n" +
164 " am stop-user [-w] [-f] <USER_ID>\n" +
165 " am stack start <DISPLAY_ID> <INTENT>\n" +
166 " am stack movetask <TASK_ID> <STACK_ID> [true|false]\n" +
167 " am stack resize <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
168 " am stack resize-animated <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
169 " am stack resize-docked-stack <LEFT,TOP,RIGHT,BOTTOM> [<TASK_LEFT,TASK_TOP,TASK_RIGHT,TASK_BOTTOM>]\n" +
170 " am stack size-docked-stack-test: <STEP_SIZE> <l|t|r|b> [DELAY_MS]\n" +
171 " am stack move-top-activity-to-pinned-stack: <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
172 " am stack positiontask <TASK_ID> <STACK_ID> <POSITION>\n" +
174 " am stack info <STACK_ID>\n" +
175 " am stack remove <STACK_ID>\n" +
176 " am task lock <TASK_ID>\n" +
177 " am task lock stop\n" +
178 " am task resizeable <TASK_ID> [0 (unresizeable) | 1 (crop_windows) | 2 (resizeable) | 3 (resizeable_and_pipable)]\n" +
179 " am task resize <TASK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n" +
180 " am task drag-task-test <TASK_ID> <STEP_SIZE> [DELAY_MS] \n" +
181 " am task size-task-test <TASK_ID> <STEP_SIZE> [DELAY_MS] \n" +
183 " am suppress-resize-config-changes <true|false>\n" +
184 " am set-inactive [--user <USER_ID>] <PACKAGE> true|false\n" +
185 " am get-inactive [--user <USER_ID>] <PACKAGE>\n" +
186 " am send-trim-memory [--user <USER_ID>] <PROCESS>\n" +
187 " [HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE]\n" +
188 " am get-current-user\n" +
190 "am start: start an Activity. Options are:\n" +
191 " -D: enable debugging\n" +
192 " -N: enable native debugging\n" +
193 " -W: wait for launch to complete\n" +
194 " --start-profiler <FILE>: start profiler and send results to <FILE>\n" +
195 " --sampling INTERVAL: use sample profiling with INTERVAL microseconds\n" +
196 " between samples (use with --start-profiler)\n" +
197 " -P <FILE>: like above, but profiling stops when app goes idle\n" +
198 " -R: repeat the activity launch <COUNT> times. Prior to each repeat,\n" +
199 " the top activity will be finished.\n" +
200 " -S: force stop the target app before starting the activity\n" +
201 " --track-allocation: enable tracking of object allocations\n" +
202 " --user <USER_ID> | current: Specify which user to run as; if not\n" +
203 " specified then run as the current user.\n" +
204 " --stack <STACK_ID>: Specify into which stack should the activity be put." +
206 "am startservice: start a Service. Options are:\n" +
207 " --user <USER_ID> | current: Specify which user to run as; if not\n" +
208 " specified then run as the current user.\n" +
210 "am stopservice: stop a Service. Options are:\n" +
211 " --user <USER_ID> | current: Specify which user to run as; if not\n" +
212 " specified then run as the current user.\n" +
214 "am force-stop: force stop everything associated with <PACKAGE>.\n" +
215 " --user <USER_ID> | all | current: Specify user to force stop;\n" +
216 " all users if not specified.\n" +
218 "am kill: Kill all processes associated with <PACKAGE>. Only kills.\n" +
219 " processes that are safe to kill -- that is, will not impact the user\n" +
221 " --user <USER_ID> | all | current: Specify user whose processes to kill;\n" +
222 " all users if not specified.\n" +
224 "am kill-all: Kill all background processes.\n" +
226 "am broadcast: send a broadcast Intent. Options are:\n" +
227 " --user <USER_ID> | all | current: Specify which user to send to; if not\n" +
228 " specified then send to all users.\n" +
229 " --receiver-permission <PERMISSION>: Require receiver to hold permission.\n" +
231 "am instrument: start an Instrumentation. Typically this target <COMPONENT>\n" +
232 " is the form <TEST_PACKAGE>/<RUNNER_CLASS> or only <TEST_PACKAGE> if there \n" +
233 " is only one instrumentation. Options are:\n" +
234 " -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT). Use with\n" +
235 " [-e perf true] to generate raw output for performance measurements.\n" +
236 " -e <NAME> <VALUE>: set argument <NAME> to <VALUE>. For test runners a\n" +
237 " common form is [-e <testrunner_flag> <value>[,<value>...]].\n" +
238 " -p <FILE>: write profiling data to <FILE>\n" +
239 " -w: wait for instrumentation to finish before returning. Required for\n" +
241 " --user <USER_ID> | current: Specify user instrumentation runs in;\n" +
242 " current user if not specified.\n" +
243 " --no-window-animation: turn off window animations while running.\n" +
244 " --abi <ABI>: Launch the instrumented process with the selected ABI.\n" +
245 " This assumes that the process supports the selected ABI.\n" +
247 "am trace-ipc: Trace IPC transactions.\n" +
248 " start: start tracing IPC transactions.\n" +
249 " stop: stop tracing IPC transactions and dump the results to file.\n" +
250 " --dump-file <FILE>: Specify the file the trace should be dumped to.\n" +
252 "am profile: start and stop profiler on a process. The given <PROCESS> argument\n" +
253 " may be either a process name or pid. Options are:\n" +
254 " --user <USER_ID> | current: When supplying a process name,\n" +
255 " specify user of process to profile; uses current user if not specified.\n" +
257 "am dumpheap: dump the heap of a process. The given <PROCESS> argument may\n" +
258 " be either a process name or pid. Options are:\n" +
259 " -n: dump native heap instead of managed heap\n" +
260 " --user <USER_ID> | current: When supplying a process name,\n" +
261 " specify user of process to dump; uses current user if not specified.\n" +
263 "am set-debug-app: set application <PACKAGE> to debug. Options are:\n" +
264 " -w: wait for debugger when application starts\n" +
265 " --persistent: retain this value\n" +
267 "am clear-debug-app: clear the previously set-debug-app.\n" +
269 "am set-watch-heap: start monitoring pss size of <PROCESS>, if it is at or\n" +
270 " above <HEAP-LIMIT> then a heap dump is collected for the user to report\n" +
272 "am clear-watch-heap: clear the previously set-watch-heap.\n" +
274 "am bug-report: request bug report generation; will launch a notification\n" +
275 " when done to select where it should be delivered. Options are: \n" +
276 " --progress: will launch a notification right away to show its progress.\n" +
278 "am monitor: start monitoring for crashes or ANRs.\n" +
279 " --gdb: start gdbserv on the given port at crash/ANR\n" +
281 "am hang: hang the system.\n" +
282 " --allow-restart: allow watchdog to perform normal system restart\n" +
284 "am restart: restart the user-space system.\n" +
286 "am idle-maintenance: perform idle maintenance now.\n" +
288 "am screen-compat: control screen compatibility mode of <PACKAGE>.\n" +
290 "am package-importance: print current importance of <PACKAGE>.\n" +
292 "am to-uri: print the given Intent specification as a URI.\n" +
294 "am to-intent-uri: print the given Intent specification as an intent: URI.\n" +
296 "am to-app-uri: print the given Intent specification as an android-app: URI.\n" +
298 "am switch-user: switch to put USER_ID in the foreground, starting\n" +
299 " execution of that user if it is currently stopped.\n" +
301 "am start-user: start USER_ID in background if it is currently stopped,\n" +
302 " use switch-user if you want to start the user in foreground.\n" +
304 "am stop-user: stop execution of USER_ID, not allowing it to run any\n" +
305 " code until a later explicit start or switch to it.\n" +
306 " -w: wait for stop-user to complete.\n" +
307 " -f: force stop even if there are related users that cannot be stopped.\n" +
309 "am stack start: start a new activity on <DISPLAY_ID> using <INTENT>.\n" +
311 "am stack movetask: move <TASK_ID> from its current stack to the top (true) or" +
312 " bottom (false) of <STACK_ID>.\n" +
314 "am stack resize: change <STACK_ID> size and position to <LEFT,TOP,RIGHT,BOTTOM>.\n" +
316 "am stack resize-docked-stack: change docked stack to <LEFT,TOP,RIGHT,BOTTOM>\n" +
317 " and supplying temporary different task bounds indicated by\n" +
318 " <TASK_LEFT,TOP,RIGHT,BOTTOM>\n" +
320 "am stack size-docked-stack-test: test command for sizing docked stack by\n" +
321 " <STEP_SIZE> increments from the side <l>eft, <t>op, <r>ight, or <b>ottom\n" +
322 " applying the optional [DELAY_MS] between each step.\n" +
324 "am stack move-top-activity-to-pinned-stack: moves the top activity from\n" +
325 " <STACK_ID> to the pinned stack using <LEFT,TOP,RIGHT,BOTTOM> for the\n" +
326 " bounds of the pinned stack.\n" +
328 "am stack positiontask: place <TASK_ID> in <STACK_ID> at <POSITION>" +
330 "am stack list: list all of the activity stacks and their sizes.\n" +
332 "am stack info: display the information about activity stack <STACK_ID>.\n" +
334 "am stack remove: remove stack <STACK_ID>.\n" +
336 "am task lock: bring <TASK_ID> to the front and don't allow other tasks to run.\n" +
338 "am task lock stop: end the current task lock.\n" +
340 "am task resizeable: change resizeable mode of <TASK_ID>.\n" +
341 " 0 (unresizeable) | 1 (crop_windows) | 2 (resizeable) | 3 (resizeable_and_pipable)\n" +
343 "am task resize: makes sure <TASK_ID> is in a stack with the specified bounds.\n" +
344 " Forces the task to be resizeable and creates a stack if no existing stack\n" +
345 " has the specified bounds.\n" +
347 "am task drag-task-test: test command for dragging/moving <TASK_ID> by\n" +
348 " <STEP_SIZE> increments around the screen applying the optional [DELAY_MS]\n" +
349 " between each step.\n" +
351 "am task size-task-test: test command for sizing <TASK_ID> by <STEP_SIZE>" +
352 " increments within the screen applying the optional [DELAY_MS] between\n" +
355 "am get-config: retrieve the configuration and any recent configurations\n" +
356 " of the device.\n" +
357 "am suppress-resize-config-changes: suppresses configuration changes due to\n" +
358 " user resizing an activity/task.\n" +
360 "am set-inactive: sets the inactive state of an app.\n" +
362 "am get-inactive: returns the inactive state of an app.\n" +
364 "am send-trim-memory: send a memory trim event to a <PROCESS>.\n" +
366 "am get-current-user: returns id of the current foreground user.\n" +
368 "am supports-multiwindow: returns true if the device supports multiwindow.\n" +
371 Intent.printIntentArgsHelp(pw, "");
376 public void onRun() throws Exception {
378 mAm = ActivityManagerNative.getDefault();
380 System.err.println(NO_SYSTEM_ERROR_CODE);
381 throw new AndroidException("Can't connect to activity manager; is the system running?");
384 mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
386 System.err.println(NO_SYSTEM_ERROR_CODE);
387 throw new AndroidException("Can't connect to package manager; is the system running?");
390 String op = nextArgRequired();
392 if (op.equals("start")) {
394 } else if (op.equals("startservice")) {
396 } else if (op.equals("stopservice")) {
398 } else if (op.equals("force-stop")) {
400 } else if (op.equals("kill")) {
402 } else if (op.equals("kill-all")) {
404 } else if (op.equals("instrument")) {
406 } else if (op.equals("trace-ipc")) {
408 } else if (op.equals("broadcast")) {
410 } else if (op.equals("profile")) {
412 } else if (op.equals("dumpheap")) {
414 } else if (op.equals("set-debug-app")) {
416 } else if (op.equals("clear-debug-app")) {
418 } else if (op.equals("set-watch-heap")) {
420 } else if (op.equals("clear-watch-heap")) {
422 } else if (op.equals("bug-report")) {
424 } else if (op.equals("monitor")) {
426 } else if (op.equals("hang")) {
428 } else if (op.equals("restart")) {
430 } else if (op.equals("idle-maintenance")) {
431 runIdleMaintenance();
432 } else if (op.equals("screen-compat")) {
434 } else if (op.equals("package-importance")) {
435 runPackageImportance();
436 } else if (op.equals("to-uri")) {
438 } else if (op.equals("to-intent-uri")) {
439 runToUri(Intent.URI_INTENT_SCHEME);
440 } else if (op.equals("to-app-uri")) {
441 runToUri(Intent.URI_ANDROID_APP_SCHEME);
442 } else if (op.equals("switch-user")) {
444 } else if (op.equals("start-user")) {
445 runStartUserInBackground();
446 } else if (op.equals("unlock-user")) {
448 } else if (op.equals("stop-user")) {
450 } else if (op.equals("stack")) {
452 } else if (op.equals("task")) {
454 } else if (op.equals("get-config")) {
456 } else if (op.equals("suppress-resize-config-changes")) {
457 runSuppressResizeConfigChanges();
458 } else if (op.equals("set-inactive")) {
460 } else if (op.equals("get-inactive")) {
462 } else if (op.equals("send-trim-memory")) {
464 } else if (op.equals("get-current-user")) {
466 } else if (op.equals("supports-multiwindow")) {
467 runSupportsMultiwindow();
469 showError("Error: unknown command '" + op + "'");
473 int parseUserArg(String arg) {
475 if ("all".equals(arg)) {
476 userId = UserHandle.USER_ALL;
477 } else if ("current".equals(arg) || "cur".equals(arg)) {
478 userId = UserHandle.USER_CURRENT;
480 userId = Integer.parseInt(arg);
485 private Intent makeIntent(int defUser) throws URISyntaxException {
491 mSamplingInterval = 0;
494 mStackId = INVALID_STACK_ID;
496 return Intent.parseCommandArgs(mArgs, new Intent.CommandOptionHandler() {
498 public boolean handleOption(String opt, ShellCommand cmd) {
499 if (opt.equals("-D")) {
500 mStartFlags |= ActivityManager.START_FLAG_DEBUG;
501 } else if (opt.equals("-N")) {
502 mStartFlags |= ActivityManager.START_FLAG_NATIVE_DEBUGGING;
503 } else if (opt.equals("-W")) {
505 } else if (opt.equals("-P")) {
506 mProfileFile = nextArgRequired();
508 } else if (opt.equals("--start-profiler")) {
509 mProfileFile = nextArgRequired();
511 } else if (opt.equals("--sampling")) {
512 mSamplingInterval = Integer.parseInt(nextArgRequired());
513 } else if (opt.equals("-R")) {
514 mRepeat = Integer.parseInt(nextArgRequired());
515 } else if (opt.equals("-S")) {
517 } else if (opt.equals("--track-allocation")) {
518 mStartFlags |= ActivityManager.START_FLAG_TRACK_ALLOCATION;
519 } else if (opt.equals("--user")) {
520 mUserId = parseUserArg(nextArgRequired());
521 } else if (opt.equals("--receiver-permission")) {
522 mReceiverPermission = nextArgRequired();
523 } else if (opt.equals("--stack")) {
524 mStackId = Integer.parseInt(nextArgRequired());
533 private void runStartService() throws Exception {
534 Intent intent = makeIntent(UserHandle.USER_CURRENT);
535 if (mUserId == UserHandle.USER_ALL) {
536 System.err.println("Error: Can't start activity with user 'all'");
539 System.out.println("Starting service: " + intent);
540 ComponentName cn = mAm.startService(null, intent, intent.getType(),
541 SHELL_PACKAGE_NAME, mUserId);
543 System.err.println("Error: Not found; no service started.");
544 } else if (cn.getPackageName().equals("!")) {
545 System.err.println("Error: Requires permission " + cn.getClassName());
546 } else if (cn.getPackageName().equals("!!")) {
547 System.err.println("Error: " + cn.getClassName());
551 private void runStopService() throws Exception {
552 Intent intent = makeIntent(UserHandle.USER_CURRENT);
553 if (mUserId == UserHandle.USER_ALL) {
554 System.err.println("Error: Can't stop activity with user 'all'");
557 System.out.println("Stopping service: " + intent);
558 int result = mAm.stopService(null, intent, intent.getType(), mUserId);
560 System.err.println("Service not stopped: was not running.");
561 } else if (result == 1) {
562 System.err.println("Service stopped");
563 } else if (result == -1) {
564 System.err.println("Error stopping service");
568 private void runStart() throws Exception {
569 Intent intent = makeIntent(UserHandle.USER_CURRENT);
571 if (mUserId == UserHandle.USER_ALL) {
572 System.err.println("Error: Can't start service with user 'all'");
576 String mimeType = intent.getType();
577 if (mimeType == null && intent.getData() != null
578 && "content".equals(intent.getData().getScheme())) {
579 mimeType = mAm.getProviderMimeType(intent.getData(), mUserId);
586 if (intent.getComponent() != null) {
587 packageName = intent.getComponent().getPackageName();
589 List<ResolveInfo> activities = mPm.queryIntentActivities(intent, mimeType, 0,
591 if (activities == null || activities.size() <= 0) {
592 System.err.println("Error: Intent does not match any activities: "
595 } else if (activities.size() > 1) {
596 System.err.println("Error: Intent matches multiple activities; can't stop: "
600 packageName = activities.get(0).activityInfo.packageName;
602 System.out.println("Stopping: " + packageName);
603 mAm.forceStopPackage(packageName, mUserId);
607 System.out.println("Starting: " + intent);
608 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
610 ParcelFileDescriptor fd = null;
611 ProfilerInfo profilerInfo = null;
613 if (mProfileFile != null) {
615 fd = openForSystemServer(
616 new File(mProfileFile),
617 ParcelFileDescriptor.MODE_CREATE |
618 ParcelFileDescriptor.MODE_TRUNCATE |
619 ParcelFileDescriptor.MODE_WRITE_ONLY);
620 } catch (FileNotFoundException e) {
621 System.err.println("Error: Unable to open file: " + mProfileFile);
622 System.err.println("Consider using a file under /data/local/tmp/");
625 profilerInfo = new ProfilerInfo(mProfileFile, fd, mSamplingInterval, mAutoStop);
628 IActivityManager.WaitResult result = null;
630 final long startTime = SystemClock.uptimeMillis();
631 ActivityOptions options = null;
632 if (mStackId != INVALID_STACK_ID) {
633 options = ActivityOptions.makeBasic();
634 options.setLaunchStackId(mStackId);
637 result = mAm.startActivityAndWait(null, null, intent, mimeType,
638 null, null, 0, mStartFlags, profilerInfo,
639 options != null ? options.toBundle() : null, mUserId);
642 res = mAm.startActivityAsUser(null, null, intent, mimeType,
643 null, null, 0, mStartFlags, profilerInfo,
644 options != null ? options.toBundle() : null, mUserId);
646 final long endTime = SystemClock.uptimeMillis();
647 PrintStream out = mWaitOption ? System.out : System.err;
648 boolean launched = false;
650 case ActivityManager.START_SUCCESS:
653 case ActivityManager.START_SWITCHES_CANCELED:
656 "Warning: Activity not started because the "
657 + " current activity is being kept for the user.");
659 case ActivityManager.START_DELIVERED_TO_TOP:
662 "Warning: Activity not started, intent has "
663 + "been delivered to currently running "
664 + "top-most instance.");
666 case ActivityManager.START_RETURN_INTENT_TO_CALLER:
669 "Warning: Activity not started because intent "
670 + "should be handled by the caller");
672 case ActivityManager.START_TASK_TO_FRONT:
675 "Warning: Activity not started, its current "
676 + "task has been brought to the front");
678 case ActivityManager.START_INTENT_NOT_RESOLVED:
680 "Error: Activity not started, unable to "
681 + "resolve " + intent.toString());
683 case ActivityManager.START_CLASS_NOT_FOUND:
684 out.println(NO_CLASS_ERROR_CODE);
685 out.println("Error: Activity class " +
686 intent.getComponent().toShortString()
687 + " does not exist.");
689 case ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT:
691 "Error: Activity not started, you requested to "
692 + "both forward and receive its result");
694 case ActivityManager.START_PERMISSION_DENIED:
696 "Error: Activity not started, you do not "
697 + "have permission to access it.");
699 case ActivityManager.START_NOT_VOICE_COMPATIBLE:
701 "Error: Activity not started, voice control not allowed for: "
704 case ActivityManager.START_NOT_CURRENT_USER_ACTIVITY:
706 "Error: Not allowed to start background user activity"
707 + " that shouldn't be displayed for all users.");
711 "Error: Activity not started, unknown error code " + res);
714 if (mWaitOption && launched) {
715 if (result == null) {
716 result = new IActivityManager.WaitResult();
717 result.who = intent.getComponent();
719 System.out.println("Status: " + (result.timeout ? "timeout" : "ok"));
720 if (result.who != null) {
721 System.out.println("Activity: " + result.who.flattenToShortString());
723 if (result.thisTime >= 0) {
724 System.out.println("ThisTime: " + result.thisTime);
726 if (result.totalTime >= 0) {
727 System.out.println("TotalTime: " + result.totalTime);
729 System.out.println("WaitTime: " + (endTime-startTime));
730 System.out.println("Complete");
736 } while (mRepeat > 0);
739 private void runForceStop() throws Exception {
740 int userId = UserHandle.USER_ALL;
743 while ((opt=nextOption()) != null) {
744 if (opt.equals("--user")) {
745 userId = parseUserArg(nextArgRequired());
747 System.err.println("Error: Unknown option: " + opt);
751 mAm.forceStopPackage(nextArgRequired(), userId);
754 private void runKill() throws Exception {
755 int userId = UserHandle.USER_ALL;
758 while ((opt=nextOption()) != null) {
759 if (opt.equals("--user")) {
760 userId = parseUserArg(nextArgRequired());
762 System.err.println("Error: Unknown option: " + opt);
766 mAm.killBackgroundProcesses(nextArgRequired(), userId);
769 private void runKillAll() throws Exception {
770 mAm.killAllBackgroundProcesses();
773 private void sendBroadcast() throws Exception {
774 Intent intent = makeIntent(UserHandle.USER_CURRENT);
775 IntentReceiver receiver = new IntentReceiver();
776 String[] requiredPermissions = mReceiverPermission == null ? null
777 : new String[] {mReceiverPermission};
778 System.out.println("Broadcasting: " + intent);
779 mAm.broadcastIntent(null, intent, null, receiver, 0, null, null, requiredPermissions,
780 android.app.AppOpsManager.OP_NONE, null, true, false, mUserId);
781 receiver.waitForFinish();
784 private void runInstrument() throws Exception {
785 String profileFile = null;
786 boolean wait = false;
787 boolean rawMode = false;
788 boolean no_window_animation = false;
789 int userId = UserHandle.USER_CURRENT;
790 Bundle args = new Bundle();
791 String argKey = null, argValue = null;
792 IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
796 while ((opt=nextOption()) != null) {
797 if (opt.equals("-p")) {
798 profileFile = nextArgRequired();
799 } else if (opt.equals("-w")) {
801 } else if (opt.equals("-r")) {
803 } else if (opt.equals("-e")) {
804 argKey = nextArgRequired();
805 argValue = nextArgRequired();
806 args.putString(argKey, argValue);
807 } else if (opt.equals("--no_window_animation")
808 || opt.equals("--no-window-animation")) {
809 no_window_animation = true;
810 } else if (opt.equals("--user")) {
811 userId = parseUserArg(nextArgRequired());
812 } else if (opt.equals("--abi")) {
813 abi = nextArgRequired();
815 System.err.println("Error: Unknown option: " + opt);
820 if (userId == UserHandle.USER_ALL) {
821 System.err.println("Error: Can't start instrumentation with user 'all'");
825 String cnArg = nextArgRequired();
828 if (cnArg.contains("/")) {
829 cn = ComponentName.unflattenFromString(cnArg);
830 if (cn == null) throw new IllegalArgumentException("Bad component name: " + cnArg);
832 List<InstrumentationInfo> infos = mPm.queryInstrumentation(null, 0).getList();
834 final int numInfos = infos == null ? 0: infos.size();
835 List<ComponentName> cns = new ArrayList<>();
836 for (int i = 0; i < numInfos; i++) {
837 InstrumentationInfo info = infos.get(i);
839 ComponentName c = new ComponentName(info.packageName, info.name);
840 if (cnArg.equals(info.packageName)) {
845 if (cns.size() == 0) {
846 throw new IllegalArgumentException("No instrumentation found for: " + cnArg);
847 } else if (cns.size() == 1) {
850 StringBuilder cnsStr = new StringBuilder();
851 final int numCns = cns.size();
852 for (int i = 0; i < numCns; i++) {
853 cnsStr.append(cns.get(i).flattenToString());
858 cnsStr.setLength(cnsStr.length() - 2);
860 throw new IllegalArgumentException("Found multiple instrumentations: "
861 + cnsStr.toString());
865 InstrumentationWatcher watcher = null;
866 UiAutomationConnection connection = null;
868 watcher = new InstrumentationWatcher();
869 watcher.setRawOutput(rawMode);
870 connection = new UiAutomationConnection();
873 float[] oldAnims = null;
874 if (no_window_animation) {
875 oldAnims = wm.getAnimationScales();
876 wm.setAnimationScale(0, 0.0f);
877 wm.setAnimationScale(1, 0.0f);
881 final String[] supportedAbis = Build.SUPPORTED_ABIS;
882 boolean matched = false;
883 for (String supportedAbi : supportedAbis) {
884 if (supportedAbi.equals(abi)) {
891 throw new AndroidException(
892 "INSTRUMENTATION_FAILED: Unsupported instruction set " + abi);
896 if (!mAm.startInstrumentation(cn, profileFile, 0, args, watcher, connection, userId, abi)) {
897 throw new AndroidException("INSTRUMENTATION_FAILED: " + cn.flattenToString());
900 if (watcher != null) {
901 if (!watcher.waitForFinish()) {
902 System.out.println("INSTRUMENTATION_ABORTED: System has crashed.");
906 if (oldAnims != null) {
907 wm.setAnimationScales(oldAnims);
911 private void runTraceIpc() throws Exception {
912 String op = nextArgRequired();
913 if (op.equals("start")) {
915 } else if (op.equals("stop")) {
918 showError("Error: unknown command '" + op + "'");
923 private void runTraceIpcStart() throws Exception {
924 System.out.println("Starting IPC tracing.");
925 mAm.startBinderTracking();
928 private void runTraceIpcStop() throws Exception {
930 String filename = null;
931 while ((opt=nextOption()) != null) {
932 if (opt.equals("--dump-file")) {
933 filename = nextArgRequired();
935 System.err.println("Error: Unknown option: " + opt);
939 if (filename == null) {
940 System.err.println("Error: Specify filename to dump logs to.");
944 ParcelFileDescriptor fd = null;
947 File file = new File(filename);
949 fd = openForSystemServer(file,
950 ParcelFileDescriptor.MODE_CREATE |
951 ParcelFileDescriptor.MODE_TRUNCATE |
952 ParcelFileDescriptor.MODE_WRITE_ONLY);
953 } catch (FileNotFoundException e) {
954 System.err.println("Error: Unable to open file: " + filename);
955 System.err.println("Consider using a file under /data/local/tmp/");
960 if (!mAm.stopBinderTrackingAndDump(fd)) {
961 throw new AndroidException("STOP TRACE FAILED.");
964 System.out.println("Stopped IPC tracing. Dumping logs to: " + filename);
967 static void removeWallOption() {
968 String props = SystemProperties.get("dalvik.vm.extra-opts");
969 if (props != null && props.contains("-Xprofile:wallclock")) {
970 props = props.replace("-Xprofile:wallclock", "");
971 props = props.trim();
972 SystemProperties.set("dalvik.vm.extra-opts", props);
976 private void runProfile() throws Exception {
977 String profileFile = null;
978 boolean start = false;
979 boolean wall = false;
980 int userId = UserHandle.USER_CURRENT;
982 mSamplingInterval = 0;
984 String process = null;
986 String cmd = nextArgRequired();
988 if ("start".equals(cmd)) {
991 while ((opt=nextOption()) != null) {
992 if (opt.equals("--user")) {
993 userId = parseUserArg(nextArgRequired());
994 } else if (opt.equals("--wall")) {
996 } else if (opt.equals("--sampling")) {
997 mSamplingInterval = Integer.parseInt(nextArgRequired());
999 System.err.println("Error: Unknown option: " + opt);
1003 process = nextArgRequired();
1004 } else if ("stop".equals(cmd)) {
1006 while ((opt=nextOption()) != null) {
1007 if (opt.equals("--user")) {
1008 userId = parseUserArg(nextArgRequired());
1010 System.err.println("Error: Unknown option: " + opt);
1014 process = nextArg();
1016 // Compatibility with old syntax: process is specified first.
1018 cmd = nextArgRequired();
1019 if ("start".equals(cmd)) {
1021 } else if (!"stop".equals(cmd)) {
1022 throw new IllegalArgumentException("Profile command " + process + " not valid");
1026 if (userId == UserHandle.USER_ALL) {
1027 System.err.println("Error: Can't profile with user 'all'");
1031 ParcelFileDescriptor fd = null;
1032 ProfilerInfo profilerInfo = null;
1035 profileFile = nextArgRequired();
1037 fd = openForSystemServer(
1038 new File(profileFile),
1039 ParcelFileDescriptor.MODE_CREATE |
1040 ParcelFileDescriptor.MODE_TRUNCATE |
1041 ParcelFileDescriptor.MODE_WRITE_ONLY);
1042 } catch (FileNotFoundException e) {
1043 System.err.println("Error: Unable to open file: " + profileFile);
1044 System.err.println("Consider using a file under /data/local/tmp/");
1047 profilerInfo = new ProfilerInfo(profileFile, fd, mSamplingInterval, false);
1052 // XXX doesn't work -- this needs to be set before booting.
1053 String props = SystemProperties.get("dalvik.vm.extra-opts");
1054 if (props == null || !props.contains("-Xprofile:wallclock")) {
1055 props = props + " -Xprofile:wallclock";
1056 //SystemProperties.set("dalvik.vm.extra-opts", props);
1059 //removeWallOption();
1061 if (!mAm.profileControl(process, userId, start, profilerInfo, profileType)) {
1063 throw new AndroidException("PROFILE FAILED on process " + process);
1067 //removeWallOption();
1072 private void runDumpHeap() throws Exception {
1073 boolean managed = true;
1074 int userId = UserHandle.USER_CURRENT;
1077 while ((opt=nextOption()) != null) {
1078 if (opt.equals("--user")) {
1079 userId = parseUserArg(nextArgRequired());
1080 if (userId == UserHandle.USER_ALL) {
1081 System.err.println("Error: Can't dump heap with user 'all'");
1084 } else if (opt.equals("-n")) {
1087 System.err.println("Error: Unknown option: " + opt);
1091 String process = nextArgRequired();
1092 String heapFile = nextArgRequired();
1093 ParcelFileDescriptor fd = null;
1096 File file = new File(heapFile);
1098 fd = openForSystemServer(file,
1099 ParcelFileDescriptor.MODE_CREATE |
1100 ParcelFileDescriptor.MODE_TRUNCATE |
1101 ParcelFileDescriptor.MODE_WRITE_ONLY);
1102 } catch (FileNotFoundException e) {
1103 System.err.println("Error: Unable to open file: " + heapFile);
1104 System.err.println("Consider using a file under /data/local/tmp/");
1108 if (!mAm.dumpHeap(process, userId, managed, heapFile, fd)) {
1109 throw new AndroidException("HEAP DUMP FAILED on process " + process);
1113 private void runSetDebugApp() throws Exception {
1114 boolean wait = false;
1115 boolean persistent = false;
1118 while ((opt=nextOption()) != null) {
1119 if (opt.equals("-w")) {
1121 } else if (opt.equals("--persistent")) {
1124 System.err.println("Error: Unknown option: " + opt);
1129 String pkg = nextArgRequired();
1130 mAm.setDebugApp(pkg, wait, persistent);
1133 private void runClearDebugApp() throws Exception {
1134 mAm.setDebugApp(null, false, true);
1137 private void runSetWatchHeap() throws Exception {
1138 String proc = nextArgRequired();
1139 String limit = nextArgRequired();
1140 mAm.setDumpHeapDebugLimit(proc, 0, Long.parseLong(limit), null);
1143 private void runClearWatchHeap() throws Exception {
1144 String proc = nextArgRequired();
1145 mAm.setDumpHeapDebugLimit(proc, 0, -1, null);
1148 private void runBugReport() throws Exception {
1150 int bugreportType = ActivityManager.BUGREPORT_OPTION_FULL;
1151 while ((opt=nextOption()) != null) {
1152 if (opt.equals("--progress")) {
1153 bugreportType = ActivityManager.BUGREPORT_OPTION_INTERACTIVE;
1155 System.err.println("Error: Unknown option: " + opt);
1159 mAm.requestBugReport(bugreportType);
1160 System.out.println("Your lovely bug report is being created; please be patient.");
1163 private void runSwitchUser() throws Exception {
1164 String user = nextArgRequired();
1165 mAm.switchUser(Integer.parseInt(user));
1168 private void runStartUserInBackground() throws Exception {
1169 String user = nextArgRequired();
1170 boolean success = mAm.startUserInBackground(Integer.parseInt(user));
1172 System.out.println("Success: user started");
1174 System.err.println("Error: could not start user");
1178 private byte[] argToBytes(String arg) {
1179 if (arg.equals("!")) {
1182 return HexDump.hexStringToByteArray(arg);
1186 private void runUnlockUser() throws Exception {
1187 int userId = Integer.parseInt(nextArgRequired());
1188 byte[] token = argToBytes(nextArgRequired());
1189 byte[] secret = argToBytes(nextArgRequired());
1190 boolean success = mAm.unlockUser(userId, token, secret, null);
1192 System.out.println("Success: user unlocked");
1194 System.err.println("Error: could not unlock user");
1198 private static class StopUserCallback extends IStopUserCallback.Stub {
1199 private boolean mFinished = false;
1201 public synchronized void waitForFinish() {
1203 while (!mFinished) wait();
1204 } catch (InterruptedException e) {
1205 throw new IllegalStateException(e);
1210 public synchronized void userStopped(int userId) {
1216 public synchronized void userStopAborted(int userId) {
1222 private void runStopUser() throws Exception {
1223 boolean wait = false;
1224 boolean force = false;
1226 while ((opt = nextOption()) != null) {
1227 if ("-w".equals(opt)) {
1229 } else if ("-f".equals(opt)) {
1232 System.err.println("Error: unknown option: " + opt);
1236 int user = Integer.parseInt(nextArgRequired());
1237 StopUserCallback callback = wait ? new StopUserCallback() : null;
1239 int res = mAm.stopUser(user, force, callback);
1240 if (res != ActivityManager.USER_OP_SUCCESS) {
1243 case ActivityManager.USER_OP_IS_CURRENT:
1244 txt = " (Can't stop current user)";
1246 case ActivityManager.USER_OP_UNKNOWN_USER:
1247 txt = " (Unknown user " + user + ")";
1249 case ActivityManager.USER_OP_ERROR_IS_SYSTEM:
1250 txt = " (System user cannot be stopped)";
1252 case ActivityManager.USER_OP_ERROR_RELATED_USERS_CANNOT_STOP:
1253 txt = " (Can't stop user " + user
1254 + " - one of its related users can't be stopped)";
1257 System.err.println("Switch failed: " + res + txt);
1258 } else if (callback != null) {
1259 callback.waitForFinish();
1263 class MyActivityController extends IActivityController.Stub {
1264 final String mGdbPort;
1265 final boolean mMonkey;
1267 static final int STATE_NORMAL = 0;
1268 static final int STATE_CRASHED = 1;
1269 static final int STATE_EARLY_ANR = 2;
1270 static final int STATE_ANR = 3;
1274 static final int RESULT_DEFAULT = 0;
1276 static final int RESULT_CRASH_DIALOG = 0;
1277 static final int RESULT_CRASH_KILL = 1;
1279 static final int RESULT_EARLY_ANR_CONTINUE = 0;
1280 static final int RESULT_EARLY_ANR_KILL = 1;
1282 static final int RESULT_ANR_DIALOG = 0;
1283 static final int RESULT_ANR_KILL = 1;
1284 static final int RESULT_ANR_WAIT = 1;
1288 Process mGdbProcess;
1290 boolean mGotGdbPrint;
1292 MyActivityController(String gdbPort, boolean monkey) {
1298 public boolean activityResuming(String pkg) {
1299 synchronized (this) {
1300 System.out.println("** Activity resuming: " + pkg);
1306 public boolean activityStarting(Intent intent, String pkg) {
1307 synchronized (this) {
1308 System.out.println("** Activity starting: " + pkg);
1314 public boolean appCrashed(String processName, int pid, String shortMsg, String longMsg,
1315 long timeMillis, String stackTrace) {
1316 synchronized (this) {
1317 System.out.println("** ERROR: PROCESS CRASHED");
1318 System.out.println("processName: " + processName);
1319 System.out.println("processPid: " + pid);
1320 System.out.println("shortMsg: " + shortMsg);
1321 System.out.println("longMsg: " + longMsg);
1322 System.out.println("timeMillis: " + timeMillis);
1323 System.out.println("stack:");
1324 System.out.print(stackTrace);
1325 System.out.println("#");
1326 int result = waitControllerLocked(pid, STATE_CRASHED);
1327 return result == RESULT_CRASH_KILL ? false : true;
1332 public int appEarlyNotResponding(String processName, int pid, String annotation) {
1333 synchronized (this) {
1334 System.out.println("** ERROR: EARLY PROCESS NOT RESPONDING");
1335 System.out.println("processName: " + processName);
1336 System.out.println("processPid: " + pid);
1337 System.out.println("annotation: " + annotation);
1338 int result = waitControllerLocked(pid, STATE_EARLY_ANR);
1339 if (result == RESULT_EARLY_ANR_KILL) return -1;
1345 public int appNotResponding(String processName, int pid, String processStats) {
1346 synchronized (this) {
1347 System.out.println("** ERROR: PROCESS NOT RESPONDING");
1348 System.out.println("processName: " + processName);
1349 System.out.println("processPid: " + pid);
1350 System.out.println("processStats:");
1351 System.out.print(processStats);
1352 System.out.println("#");
1353 int result = waitControllerLocked(pid, STATE_ANR);
1354 if (result == RESULT_ANR_KILL) return -1;
1355 if (result == RESULT_ANR_WAIT) return 1;
1361 public int systemNotResponding(String message) {
1362 synchronized (this) {
1363 System.out.println("** ERROR: PROCESS NOT RESPONDING");
1364 System.out.println("message: " + message);
1365 System.out.println("#");
1366 System.out.println("Allowing system to die.");
1371 void killGdbLocked() {
1372 mGotGdbPrint = false;
1373 if (mGdbProcess != null) {
1374 System.out.println("Stopping gdbserver");
1375 mGdbProcess.destroy();
1378 if (mGdbThread != null) {
1379 mGdbThread.interrupt();
1384 int waitControllerLocked(int pid, int state) {
1385 if (mGdbPort != null) {
1389 System.out.println("Starting gdbserver on port " + mGdbPort);
1390 System.out.println("Do the following:");
1391 System.out.println(" adb forward tcp:" + mGdbPort + " tcp:" + mGdbPort);
1392 System.out.println(" gdbclient app_process :" + mGdbPort);
1394 mGdbProcess = Runtime.getRuntime().exec(new String[] {
1395 "gdbserver", ":" + mGdbPort, "--attach", Integer.toString(pid)
1397 final InputStreamReader converter = new InputStreamReader(
1398 mGdbProcess.getInputStream());
1399 mGdbThread = new Thread() {
1402 BufferedReader in = new BufferedReader(converter);
1406 synchronized (MyActivityController.this) {
1407 if (mGdbThread == null) {
1411 mGotGdbPrint = true;
1412 MyActivityController.this.notifyAll();
1416 line = in.readLine();
1420 System.out.println("GDB: " + line);
1422 } catch (IOException e) {
1430 // Stupid waiting for .5s. Doesn't matter if we end early.
1433 } catch (InterruptedException e) {
1436 } catch (IOException e) {
1437 System.err.println("Failure starting gdbserver: " + e);
1442 System.out.println("");
1443 printMessageForState();
1445 while (mState != STATE_NORMAL) {
1448 } catch (InterruptedException e) {
1457 void resumeController(int result) {
1458 synchronized (this) {
1459 mState = STATE_NORMAL;
1465 void printMessageForState() {
1468 System.out.println("Monitoring activity manager... available commands:");
1471 System.out.println("Waiting after crash... available commands:");
1472 System.out.println("(c)ontinue: show crash dialog");
1473 System.out.println("(k)ill: immediately kill app");
1475 case STATE_EARLY_ANR:
1476 System.out.println("Waiting after early ANR... available commands:");
1477 System.out.println("(c)ontinue: standard ANR processing");
1478 System.out.println("(k)ill: immediately kill app");
1481 System.out.println("Waiting after ANR... available commands:");
1482 System.out.println("(c)ontinue: show ANR dialog");
1483 System.out.println("(k)ill: immediately kill app");
1484 System.out.println("(w)ait: wait some more");
1487 System.out.println("(q)uit: finish monitoring");
1490 void run() throws RemoteException {
1492 printMessageForState();
1494 mAm.setActivityController(this, mMonkey);
1495 mState = STATE_NORMAL;
1497 InputStreamReader converter = new InputStreamReader(System.in);
1498 BufferedReader in = new BufferedReader(converter);
1501 while ((line = in.readLine()) != null) {
1502 boolean addNewline = true;
1503 if (line.length() <= 0) {
1505 } else if ("q".equals(line) || "quit".equals(line)) {
1506 resumeController(RESULT_DEFAULT);
1508 } else if (mState == STATE_CRASHED) {
1509 if ("c".equals(line) || "continue".equals(line)) {
1510 resumeController(RESULT_CRASH_DIALOG);
1511 } else if ("k".equals(line) || "kill".equals(line)) {
1512 resumeController(RESULT_CRASH_KILL);
1514 System.out.println("Invalid command: " + line);
1516 } else if (mState == STATE_ANR) {
1517 if ("c".equals(line) || "continue".equals(line)) {
1518 resumeController(RESULT_ANR_DIALOG);
1519 } else if ("k".equals(line) || "kill".equals(line)) {
1520 resumeController(RESULT_ANR_KILL);
1521 } else if ("w".equals(line) || "wait".equals(line)) {
1522 resumeController(RESULT_ANR_WAIT);
1524 System.out.println("Invalid command: " + line);
1526 } else if (mState == STATE_EARLY_ANR) {
1527 if ("c".equals(line) || "continue".equals(line)) {
1528 resumeController(RESULT_EARLY_ANR_CONTINUE);
1529 } else if ("k".equals(line) || "kill".equals(line)) {
1530 resumeController(RESULT_EARLY_ANR_KILL);
1532 System.out.println("Invalid command: " + line);
1535 System.out.println("Invalid command: " + line);
1538 synchronized (this) {
1540 System.out.println("");
1542 printMessageForState();
1546 } catch (IOException e) {
1547 e.printStackTrace();
1549 mAm.setActivityController(null, mMonkey);
1554 private void runMonitor() throws Exception {
1556 String gdbPort = null;
1557 boolean monkey = false;
1558 while ((opt=nextOption()) != null) {
1559 if (opt.equals("--gdb")) {
1560 gdbPort = nextArgRequired();
1561 } else if (opt.equals("-m")) {
1564 System.err.println("Error: Unknown option: " + opt);
1569 MyActivityController controller = new MyActivityController(gdbPort, monkey);
1573 private void runHang() throws Exception {
1575 boolean allowRestart = false;
1576 while ((opt=nextOption()) != null) {
1577 if (opt.equals("--allow-restart")) {
1578 allowRestart = true;
1580 System.err.println("Error: Unknown option: " + opt);
1585 System.out.println("Hanging the system...");
1586 mAm.hang(new Binder(), allowRestart);
1589 private void runRestart() throws Exception {
1591 while ((opt=nextOption()) != null) {
1592 System.err.println("Error: Unknown option: " + opt);
1596 System.out.println("Restart the system...");
1600 private void runIdleMaintenance() throws Exception {
1602 while ((opt=nextOption()) != null) {
1603 System.err.println("Error: Unknown option: " + opt);
1607 System.out.println("Performing idle maintenance...");
1609 mAm.sendIdleJobTrigger();
1610 } catch (RemoteException e) {
1614 private void runScreenCompat() throws Exception {
1615 String mode = nextArgRequired();
1617 if ("on".equals(mode)) {
1619 } else if ("off".equals(mode)) {
1622 System.err.println("Error: enabled mode must be 'on' or 'off' at " + mode);
1626 String packageName = nextArgRequired();
1629 mAm.setPackageScreenCompatMode(packageName, enabled
1630 ? ActivityManager.COMPAT_MODE_ENABLED
1631 : ActivityManager.COMPAT_MODE_DISABLED);
1632 } catch (RemoteException e) {
1634 packageName = nextArg();
1635 } while (packageName != null);
1638 private void runPackageImportance() throws Exception {
1639 String packageName = nextArgRequired();
1641 int procState = mAm.getPackageProcessState(packageName, "com.android.shell");
1643 ActivityManager.RunningAppProcessInfo.procStateToImportance(procState));
1644 } catch (RemoteException e) {
1648 private void runToUri(int flags) throws Exception {
1649 Intent intent = makeIntent(UserHandle.USER_CURRENT);
1650 System.out.println(intent.toUri(flags));
1653 private class IntentReceiver extends IIntentReceiver.Stub {
1654 private boolean mFinished = false;
1657 public void performReceive(Intent intent, int resultCode, String data, Bundle extras,
1658 boolean ordered, boolean sticky, int sendingUser) {
1659 String line = "Broadcast completed: result=" + resultCode;
1660 if (data != null) line = line + ", data=\"" + data + "\"";
1661 if (extras != null) line = line + ", extras: " + extras;
1662 System.out.println(line);
1663 synchronized (this) {
1669 public synchronized void waitForFinish() {
1671 while (!mFinished) wait();
1672 } catch (InterruptedException e) {
1673 throw new IllegalStateException(e);
1678 private class InstrumentationWatcher extends IInstrumentationWatcher.Stub {
1679 private boolean mFinished = false;
1680 private boolean mRawMode = false;
1683 * Set or reset "raw mode". In "raw mode", all bundles are dumped. In "pretty mode",
1684 * if a bundle includes Instrumentation.REPORT_KEY_STREAMRESULT, just print that.
1685 * @param rawMode true for raw mode, false for pretty mode.
1687 public void setRawOutput(boolean rawMode) {
1692 public void instrumentationStatus(ComponentName name, int resultCode, Bundle results) {
1693 synchronized (this) {
1694 // pretty printer mode?
1695 String pretty = null;
1696 if (!mRawMode && results != null) {
1697 pretty = results.getString(Instrumentation.REPORT_KEY_STREAMRESULT);
1699 if (pretty != null) {
1700 System.out.print(pretty);
1702 if (results != null) {
1703 for (String key : results.keySet()) {
1705 "INSTRUMENTATION_STATUS: " + key + "=" + results.get(key));
1708 System.out.println("INSTRUMENTATION_STATUS_CODE: " + resultCode);
1715 public void instrumentationFinished(ComponentName name, int resultCode,
1717 synchronized (this) {
1718 // pretty printer mode?
1719 String pretty = null;
1720 if (!mRawMode && results != null) {
1721 pretty = results.getString(Instrumentation.REPORT_KEY_STREAMRESULT);
1723 if (pretty != null) {
1724 System.out.println(pretty);
1726 if (results != null) {
1727 for (String key : results.keySet()) {
1729 "INSTRUMENTATION_RESULT: " + key + "=" + results.get(key));
1732 System.out.println("INSTRUMENTATION_CODE: " + resultCode);
1739 public boolean waitForFinish() {
1740 synchronized (this) {
1741 while (!mFinished) {
1743 if (!mAm.asBinder().pingBinder()) {
1747 } catch (InterruptedException e) {
1748 throw new IllegalStateException(e);
1756 private void runStack() throws Exception {
1757 String op = nextArgRequired();
1768 case "resize-animated":
1769 runStackResizeAnimated();
1771 case "resize-docked-stack":
1772 runStackResizeDocked();
1774 case "positiontask":
1775 runStackPositionTask();
1783 case "move-top-activity-to-pinned-stack":
1784 runMoveTopActivityToPinnedStack();
1786 case "size-docked-stack-test":
1787 runStackSizeDockedStackTest();
1793 showError("Error: unknown command '" + op + "'");
1798 private void runStackStart() throws Exception {
1799 String displayIdStr = nextArgRequired();
1800 int displayId = Integer.parseInt(displayIdStr);
1801 Intent intent = makeIntent(UserHandle.USER_CURRENT);
1804 IActivityContainer container = mAm.createStackOnDisplay(displayId);
1805 if (container != null) {
1806 container.startActivity(intent);
1808 } catch (RemoteException e) {
1812 private void runStackMoveTask() throws Exception {
1813 String taskIdStr = nextArgRequired();
1814 int taskId = Integer.parseInt(taskIdStr);
1815 String stackIdStr = nextArgRequired();
1816 int stackId = Integer.parseInt(stackIdStr);
1817 String toTopStr = nextArgRequired();
1818 final boolean toTop;
1819 if ("true".equals(toTopStr)) {
1821 } else if ("false".equals(toTopStr)) {
1824 System.err.println("Error: bad toTop arg: " + toTopStr);
1829 mAm.moveTaskToStack(taskId, stackId, toTop);
1830 } catch (RemoteException e) {
1834 private void runStackResize() throws Exception {
1835 String stackIdStr = nextArgRequired();
1836 int stackId = Integer.parseInt(stackIdStr);
1837 final Rect bounds = getBounds();
1838 if (bounds == null) {
1839 System.err.println("Error: invalid input bounds");
1842 resizeStack(stackId, bounds, 0);
1845 private void runStackResizeAnimated() throws Exception {
1846 String stackIdStr = nextArgRequired();
1847 int stackId = Integer.parseInt(stackIdStr);
1849 if ("null".equals(mArgs.peekNextArg())) {
1852 bounds = getBounds();
1853 if (bounds == null) {
1854 System.err.println("Error: invalid input bounds");
1858 resizeStackUnchecked(stackId, bounds, 0, true);
1861 private void resizeStackUnchecked(int stackId, Rect bounds, int delayMs, boolean animate) {
1863 mAm.resizeStack(stackId, bounds, false, false, animate, -1);
1864 Thread.sleep(delayMs);
1865 } catch (RemoteException e) {
1866 showError("Error: resizing stack " + e);
1867 } catch (InterruptedException e) {
1871 private void runStackResizeDocked() throws Exception {
1872 final Rect bounds = getBounds();
1873 final Rect taskBounds = getBounds();
1874 if (bounds == null || taskBounds == null) {
1875 System.err.println("Error: invalid input bounds");
1879 mAm.resizeDockedStack(bounds, taskBounds, null, null, null);
1880 } catch (RemoteException e) {
1881 showError("Error: resizing docked stack " + e);
1885 private void resizeStack(int stackId, Rect bounds, int delayMs)
1887 if (bounds == null) {
1888 showError("Error: invalid input bounds");
1891 resizeStackUnchecked(stackId, bounds, delayMs, false);
1894 private void runStackPositionTask() throws Exception {
1895 String taskIdStr = nextArgRequired();
1896 int taskId = Integer.parseInt(taskIdStr);
1897 String stackIdStr = nextArgRequired();
1898 int stackId = Integer.parseInt(stackIdStr);
1899 String positionStr = nextArgRequired();
1900 int position = Integer.parseInt(positionStr);
1903 mAm.positionTaskInStack(taskId, stackId, position);
1904 } catch (RemoteException e) {
1908 private void runStackList() throws Exception {
1910 List<StackInfo> stacks = mAm.getAllStackInfos();
1911 for (StackInfo info : stacks) {
1912 System.out.println(info);
1914 } catch (RemoteException e) {
1918 private void runStackInfo() throws Exception {
1920 String stackIdStr = nextArgRequired();
1921 int stackId = Integer.parseInt(stackIdStr);
1922 StackInfo info = mAm.getStackInfo(stackId);
1923 System.out.println(info);
1924 } catch (RemoteException e) {
1928 private void runStackRemove() throws Exception {
1929 String stackIdStr = nextArgRequired();
1930 int stackId = Integer.parseInt(stackIdStr);
1931 mAm.removeStack(stackId);
1934 private void runMoveTopActivityToPinnedStack() throws Exception {
1935 int stackId = Integer.parseInt(nextArgRequired());
1936 final Rect bounds = getBounds();
1937 if (bounds == null) {
1938 System.err.println("Error: invalid input bounds");
1943 if (!mAm.moveTopActivityToPinnedStack(stackId, bounds)) {
1944 showError("Didn't move top activity to pinned stack.");
1946 } catch (RemoteException e) {
1947 showError("Unable to move top activity: " + e);
1952 private void runStackSizeDockedStackTest() throws Exception {
1953 final int stepSize = Integer.parseInt(nextArgRequired());
1954 final String side = nextArgRequired();
1955 final String delayStr = nextArg();
1956 final int delayMs = (delayStr != null) ? Integer.parseInt(delayStr) : 0;
1960 StackInfo info = mAm.getStackInfo(DOCKED_STACK_ID);
1962 showError("Docked stack doesn't exist");
1965 if (info.bounds == null) {
1966 showError("Docked stack doesn't have a bounds");
1969 bounds = info.bounds;
1970 } catch (RemoteException e) {
1971 showError("Unable to get docked stack info:" + e);
1975 final boolean horizontalGrowth = "l".equals(side) || "r".equals(side);
1976 final int changeSize = (horizontalGrowth ? bounds.width() : bounds.height()) / 2;
1980 currentPoint = bounds.left;
1983 currentPoint = bounds.right;
1986 currentPoint = bounds.top;
1989 currentPoint = bounds.bottom;
1992 showError("Unknown growth side: " + side);
1996 final int startPoint = currentPoint;
1997 final int minPoint = currentPoint - changeSize;
1998 final int maxPoint = currentPoint + changeSize;
2001 System.out.println("Shrinking docked stack side=" + side);
2002 while (currentPoint > minPoint) {
2003 maxChange = Math.min(stepSize, currentPoint - minPoint);
2004 currentPoint -= maxChange;
2005 setBoundsSide(bounds, side, currentPoint);
2006 resizeStack(DOCKED_STACK_ID, bounds, delayMs);
2009 System.out.println("Growing docked stack side=" + side);
2010 while (currentPoint < maxPoint) {
2011 maxChange = Math.min(stepSize, maxPoint - currentPoint);
2012 currentPoint += maxChange;
2013 setBoundsSide(bounds, side, currentPoint);
2014 resizeStack(DOCKED_STACK_ID, bounds, delayMs);
2017 System.out.println("Back to Original size side=" + side);
2018 while (currentPoint > startPoint) {
2019 maxChange = Math.min(stepSize, currentPoint - startPoint);
2020 currentPoint -= maxChange;
2021 setBoundsSide(bounds, side, currentPoint);
2022 resizeStack(DOCKED_STACK_ID, bounds, delayMs);
2026 private void setBoundsSide(Rect bounds, String side, int value) {
2029 bounds.left = value;
2032 bounds.right = value;
2038 bounds.bottom = value;
2041 showError("Unknown set side: " + side);
2046 private void runTask() throws Exception {
2047 String op = nextArgRequired();
2048 if (op.equals("lock")) {
2050 } else if (op.equals("resizeable")) {
2051 runTaskResizeable();
2052 } else if (op.equals("resize")) {
2054 } else if (op.equals("drag-task-test")) {
2055 runTaskDragTaskTest();
2056 } else if (op.equals("size-task-test")) {
2057 runTaskSizeTaskTest();
2059 showError("Error: unknown command '" + op + "'");
2064 private void runTaskLock() throws Exception {
2065 String taskIdStr = nextArgRequired();
2067 if (taskIdStr.equals("stop")) {
2068 mAm.stopLockTaskMode();
2070 int taskId = Integer.parseInt(taskIdStr);
2071 mAm.startLockTaskMode(taskId);
2073 System.err.println("Activity manager is " + (mAm.isInLockTaskMode() ? "" : "not ") +
2075 } catch (RemoteException e) {
2079 private void runTaskResizeable() throws Exception {
2080 final String taskIdStr = nextArgRequired();
2081 final int taskId = Integer.parseInt(taskIdStr);
2082 final String resizeableStr = nextArgRequired();
2083 final int resizeableMode = Integer.parseInt(resizeableStr);
2086 mAm.setTaskResizeable(taskId, resizeableMode);
2087 } catch (RemoteException e) {
2091 private void runTaskResize() throws Exception {
2092 final String taskIdStr = nextArgRequired();
2093 final int taskId = Integer.parseInt(taskIdStr);
2094 final Rect bounds = getBounds();
2095 if (bounds == null) {
2096 System.err.println("Error: invalid input bounds");
2099 taskResize(taskId, bounds, 0, false);
2102 private void taskResize(int taskId, Rect bounds, int delay_ms, boolean pretendUserResize) {
2104 final int resizeMode = pretendUserResize ? RESIZE_MODE_USER : RESIZE_MODE_SYSTEM;
2105 mAm.resizeTask(taskId, bounds, resizeMode);
2106 Thread.sleep(delay_ms);
2107 } catch (RemoteException e) {
2108 System.err.println("Error changing task bounds: " + e);
2109 } catch (InterruptedException e) {
2113 private void runTaskDragTaskTest() {
2114 final int taskId = Integer.parseInt(nextArgRequired());
2115 final int stepSize = Integer.parseInt(nextArgRequired());
2116 final String delayStr = nextArg();
2117 final int delay_ms = (delayStr != null) ? Integer.parseInt(delayStr) : 0;
2118 final StackInfo stackInfo;
2121 stackInfo = mAm.getStackInfo(mAm.getFocusedStackId());
2122 taskBounds = mAm.getTaskBounds(taskId);
2123 } catch (RemoteException e) {
2124 System.err.println("Error getting focus stack info or task bounds: " + e);
2127 final Rect stackBounds = stackInfo.bounds;
2128 int travelRight = stackBounds.width() - taskBounds.width();
2129 int travelLeft = -travelRight;
2130 int travelDown = stackBounds.height() - taskBounds.height();
2131 int travelUp = -travelDown;
2134 // We do 2 passes to get back to the original location of the task.
2135 while (passes < 2) {
2137 System.out.println("Moving right...");
2138 travelRight = moveTask(taskId, taskBounds, stackBounds, stepSize,
2139 travelRight, MOVING_FORWARD, MOVING_HORIZONTALLY, delay_ms);
2140 System.out.println("Still need to travel right by " + travelRight);
2143 System.out.println("Moving down...");
2144 travelDown = moveTask(taskId, taskBounds, stackBounds, stepSize,
2145 travelDown, MOVING_FORWARD, !MOVING_HORIZONTALLY, delay_ms);
2146 System.out.println("Still need to travel down by " + travelDown);
2149 System.out.println("Moving left...");
2150 travelLeft = moveTask(taskId, taskBounds, stackBounds, stepSize,
2151 travelLeft, !MOVING_FORWARD, MOVING_HORIZONTALLY, delay_ms);
2152 System.out.println("Still need to travel left by " + travelLeft);
2155 System.out.println("Moving up...");
2156 travelUp = moveTask(taskId, taskBounds, stackBounds, stepSize,
2157 travelUp, !MOVING_FORWARD, !MOVING_HORIZONTALLY, delay_ms);
2158 System.out.println("Still need to travel up by " + travelUp);
2161 taskBounds = mAm.getTaskBounds(taskId);
2162 } catch (RemoteException e) {
2163 System.err.println("Error getting task bounds: " + e);
2170 private int moveTask(int taskId, Rect taskRect, Rect stackRect, int stepSize,
2171 int maxToTravel, boolean movingForward, boolean horizontal, int delay_ms) {
2173 if (movingForward) {
2174 while (maxToTravel > 0
2175 && ((horizontal && taskRect.right < stackRect.right)
2176 ||(!horizontal && taskRect.bottom < stackRect.bottom))) {
2178 maxMove = Math.min(stepSize, stackRect.right - taskRect.right);
2179 maxToTravel -= maxMove;
2180 taskRect.right += maxMove;
2181 taskRect.left += maxMove;
2183 maxMove = Math.min(stepSize, stackRect.bottom - taskRect.bottom);
2184 maxToTravel -= maxMove;
2185 taskRect.top += maxMove;
2186 taskRect.bottom += maxMove;
2188 taskResize(taskId, taskRect, delay_ms, false);
2191 while (maxToTravel < 0
2192 && ((horizontal && taskRect.left > stackRect.left)
2193 ||(!horizontal && taskRect.top > stackRect.top))) {
2195 maxMove = Math.min(stepSize, taskRect.left - stackRect.left);
2196 maxToTravel -= maxMove;
2197 taskRect.right -= maxMove;
2198 taskRect.left -= maxMove;
2200 maxMove = Math.min(stepSize, taskRect.top - stackRect.top);
2201 maxToTravel -= maxMove;
2202 taskRect.top -= maxMove;
2203 taskRect.bottom -= maxMove;
2205 taskResize(taskId, taskRect, delay_ms, false);
2208 // Return the remaining distance we didn't travel because we reached the target location.
2212 private void runTaskSizeTaskTest() {
2213 final int taskId = Integer.parseInt(nextArgRequired());
2214 final int stepSize = Integer.parseInt(nextArgRequired());
2215 final String delayStr = nextArg();
2216 final int delay_ms = (delayStr != null) ? Integer.parseInt(delayStr) : 0;
2217 final StackInfo stackInfo;
2218 final Rect initialTaskBounds;
2220 stackInfo = mAm.getStackInfo(mAm.getFocusedStackId());
2221 initialTaskBounds = mAm.getTaskBounds(taskId);
2222 } catch (RemoteException e) {
2223 System.err.println("Error getting focus stack info or task bounds: " + e);
2226 final Rect stackBounds = stackInfo.bounds;
2227 stackBounds.inset(STACK_BOUNDS_INSET, STACK_BOUNDS_INSET);
2228 final Rect currentTaskBounds = new Rect(initialTaskBounds);
2231 System.out.println("Growing top-left");
2233 currentTaskBounds.top -= getStepSize(
2234 currentTaskBounds.top, stackBounds.top, stepSize, GREATER_THAN_TARGET);
2236 currentTaskBounds.left -= getStepSize(
2237 currentTaskBounds.left, stackBounds.left, stepSize, GREATER_THAN_TARGET);
2239 taskResize(taskId, currentTaskBounds, delay_ms, true);
2240 } while (stackBounds.top < currentTaskBounds.top
2241 || stackBounds.left < currentTaskBounds.left);
2243 // Back to original size
2244 System.out.println("Shrinking top-left");
2246 currentTaskBounds.top += getStepSize(
2247 currentTaskBounds.top, initialTaskBounds.top, stepSize, !GREATER_THAN_TARGET);
2249 currentTaskBounds.left += getStepSize(
2250 currentTaskBounds.left, initialTaskBounds.left, stepSize, !GREATER_THAN_TARGET);
2252 taskResize(taskId, currentTaskBounds, delay_ms, true);
2253 } while (initialTaskBounds.top > currentTaskBounds.top
2254 || initialTaskBounds.left > currentTaskBounds.left);
2256 // Size by top-right
2257 System.out.println("Growing top-right");
2259 currentTaskBounds.top -= getStepSize(
2260 currentTaskBounds.top, stackBounds.top, stepSize, GREATER_THAN_TARGET);
2262 currentTaskBounds.right += getStepSize(
2263 currentTaskBounds.right, stackBounds.right, stepSize, !GREATER_THAN_TARGET);
2265 taskResize(taskId, currentTaskBounds, delay_ms, true);
2266 } while (stackBounds.top < currentTaskBounds.top
2267 || stackBounds.right > currentTaskBounds.right);
2269 // Back to original size
2270 System.out.println("Shrinking top-right");
2272 currentTaskBounds.top += getStepSize(
2273 currentTaskBounds.top, initialTaskBounds.top, stepSize, !GREATER_THAN_TARGET);
2275 currentTaskBounds.right -= getStepSize(currentTaskBounds.right, initialTaskBounds.right,
2276 stepSize, GREATER_THAN_TARGET);
2278 taskResize(taskId, currentTaskBounds, delay_ms, true);
2279 } while (initialTaskBounds.top > currentTaskBounds.top
2280 || initialTaskBounds.right < currentTaskBounds.right);
2282 // Size by bottom-left
2283 System.out.println("Growing bottom-left");
2285 currentTaskBounds.bottom += getStepSize(
2286 currentTaskBounds.bottom, stackBounds.bottom, stepSize, !GREATER_THAN_TARGET);
2288 currentTaskBounds.left -= getStepSize(
2289 currentTaskBounds.left, stackBounds.left, stepSize, GREATER_THAN_TARGET);
2291 taskResize(taskId, currentTaskBounds, delay_ms, true);
2292 } while (stackBounds.bottom > currentTaskBounds.bottom
2293 || stackBounds.left < currentTaskBounds.left);
2295 // Back to original size
2296 System.out.println("Shrinking bottom-left");
2298 currentTaskBounds.bottom -= getStepSize(currentTaskBounds.bottom,
2299 initialTaskBounds.bottom, stepSize, GREATER_THAN_TARGET);
2301 currentTaskBounds.left += getStepSize(
2302 currentTaskBounds.left, initialTaskBounds.left, stepSize, !GREATER_THAN_TARGET);
2304 taskResize(taskId, currentTaskBounds, delay_ms, true);
2305 } while (initialTaskBounds.bottom < currentTaskBounds.bottom
2306 || initialTaskBounds.left > currentTaskBounds.left);
2308 // Size by bottom-right
2309 System.out.println("Growing bottom-right");
2311 currentTaskBounds.bottom += getStepSize(
2312 currentTaskBounds.bottom, stackBounds.bottom, stepSize, !GREATER_THAN_TARGET);
2314 currentTaskBounds.right += getStepSize(
2315 currentTaskBounds.right, stackBounds.right, stepSize, !GREATER_THAN_TARGET);
2317 taskResize(taskId, currentTaskBounds, delay_ms, true);
2318 } while (stackBounds.bottom > currentTaskBounds.bottom
2319 || stackBounds.right > currentTaskBounds.right);
2321 // Back to original size
2322 System.out.println("Shrinking bottom-right");
2324 currentTaskBounds.bottom -= getStepSize(currentTaskBounds.bottom,
2325 initialTaskBounds.bottom, stepSize, GREATER_THAN_TARGET);
2327 currentTaskBounds.right -= getStepSize(currentTaskBounds.right, initialTaskBounds.right,
2328 stepSize, GREATER_THAN_TARGET);
2330 taskResize(taskId, currentTaskBounds, delay_ms, true);
2331 } while (initialTaskBounds.bottom < currentTaskBounds.bottom
2332 || initialTaskBounds.right < currentTaskBounds.right);
2335 private int getStepSize(int current, int target, int inStepSize, boolean greaterThanTarget) {
2337 if (greaterThanTarget && target < current) {
2338 current -= inStepSize;
2339 stepSize = inStepSize;
2340 if (target > current) {
2341 stepSize -= (target - current);
2344 if (!greaterThanTarget && target > current) {
2345 current += inStepSize;
2346 stepSize = inStepSize;
2347 if (target < current) {
2348 stepSize += (current - target);
2354 private List<Configuration> getRecentConfigurations(int days) {
2355 IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
2356 Context.USAGE_STATS_SERVICE));
2357 final long now = System.currentTimeMillis();
2358 final long nDaysAgo = now - (days * 24 * 60 * 60 * 1000);
2360 @SuppressWarnings("unchecked")
2361 ParceledListSlice<ConfigurationStats> configStatsSlice = usm.queryConfigurationStats(
2362 UsageStatsManager.INTERVAL_BEST, nDaysAgo, now, "com.android.shell");
2363 if (configStatsSlice == null) {
2364 return Collections.emptyList();
2367 final ArrayMap<Configuration, Integer> recentConfigs = new ArrayMap<>();
2368 final List<ConfigurationStats> configStatsList = configStatsSlice.getList();
2369 final int configStatsListSize = configStatsList.size();
2370 for (int i = 0; i < configStatsListSize; i++) {
2371 final ConfigurationStats stats = configStatsList.get(i);
2372 final int indexOfKey = recentConfigs.indexOfKey(stats.getConfiguration());
2373 if (indexOfKey < 0) {
2374 recentConfigs.put(stats.getConfiguration(), stats.getActivationCount());
2376 recentConfigs.setValueAt(indexOfKey,
2377 recentConfigs.valueAt(indexOfKey) + stats.getActivationCount());
2381 final Comparator<Configuration> comparator = new Comparator<Configuration>() {
2383 public int compare(Configuration a, Configuration b) {
2384 return recentConfigs.get(b).compareTo(recentConfigs.get(a));
2388 ArrayList<Configuration> configs = new ArrayList<>(recentConfigs.size());
2389 configs.addAll(recentConfigs.keySet());
2390 Collections.sort(configs, comparator);
2393 } catch (RemoteException e) {
2394 return Collections.emptyList();
2398 private void runGetConfig() throws Exception {
2400 String option = nextOption();
2401 if (option != null) {
2402 if (!option.equals("--days")) {
2403 throw new IllegalArgumentException("unrecognized option " + option);
2406 days = Integer.parseInt(nextArgRequired());
2408 throw new IllegalArgumentException("--days must be a positive integer");
2413 Configuration config = mAm.getConfiguration();
2414 if (config == null) {
2415 System.err.println("Activity manager has no configuration");
2419 System.out.println("config: " + Configuration.resourceQualifierString(config));
2420 System.out.println("abi: " + TextUtils.join(",", Build.SUPPORTED_ABIS));
2422 final List<Configuration> recentConfigs = getRecentConfigurations(days);
2423 final int recentConfigSize = recentConfigs.size();
2424 if (recentConfigSize > 0) {
2425 System.out.println("recentConfigs:");
2428 for (int i = 0; i < recentConfigSize; i++) {
2429 System.out.println(" config: " + Configuration.resourceQualifierString(
2430 recentConfigs.get(i)));
2433 } catch (RemoteException e) {
2437 private void runSuppressResizeConfigChanges() throws Exception {
2438 boolean suppress = Boolean.valueOf(nextArgRequired());
2441 mAm.suppressResizeConfigChanges(suppress);
2442 } catch (RemoteException e) {
2443 System.err.println("Error suppressing resize config changes: " + e);
2447 private void runSetInactive() throws Exception {
2448 int userId = UserHandle.USER_CURRENT;
2451 while ((opt=nextOption()) != null) {
2452 if (opt.equals("--user")) {
2453 userId = parseUserArg(nextArgRequired());
2455 System.err.println("Error: Unknown option: " + opt);
2459 String packageName = nextArgRequired();
2460 String value = nextArgRequired();
2462 IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
2463 Context.USAGE_STATS_SERVICE));
2464 usm.setAppInactive(packageName, Boolean.parseBoolean(value), userId);
2467 private void runGetInactive() throws Exception {
2468 int userId = UserHandle.USER_CURRENT;
2471 while ((opt=nextOption()) != null) {
2472 if (opt.equals("--user")) {
2473 userId = parseUserArg(nextArgRequired());
2475 System.err.println("Error: Unknown option: " + opt);
2479 String packageName = nextArgRequired();
2481 IUsageStatsManager usm = IUsageStatsManager.Stub.asInterface(ServiceManager.getService(
2482 Context.USAGE_STATS_SERVICE));
2483 boolean isIdle = usm.isAppInactive(packageName, userId);
2484 System.out.println("Idle=" + isIdle);
2487 private void runSendTrimMemory() throws Exception {
2488 int userId = UserHandle.USER_CURRENT;
2490 while ((opt = nextOption()) != null) {
2491 if (opt.equals("--user")) {
2492 userId = parseUserArg(nextArgRequired());
2493 if (userId == UserHandle.USER_ALL) {
2494 System.err.println("Error: Can't use user 'all'");
2498 System.err.println("Error: Unknown option: " + opt);
2503 String proc = nextArgRequired();
2504 String levelArg = nextArgRequired();
2508 level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
2510 case "RUNNING_MODERATE":
2511 level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
2514 level = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
2517 level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
2520 level = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
2522 case "RUNNING_CRITICAL":
2523 level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
2526 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
2529 System.err.println("Error: Unknown level option: " + levelArg);
2532 if (!mAm.setProcessMemoryTrimLevel(proc, userId, level)) {
2533 System.err.println("Error: Failure to set the level - probably Unknown Process: " +
2538 private void runGetCurrentUser() throws Exception {
2539 UserInfo currentUser = Preconditions.checkNotNull(mAm.getCurrentUser(),
2540 "Current user not set");
2541 System.out.println(currentUser.id);
2544 private void runSupportsMultiwindow() throws Exception {
2545 // system resources does not contain all the device configuration, construct it manually.
2546 Configuration config = mAm.getConfiguration();
2547 if (config == null) {
2548 throw new AndroidException("Activity manager has no configuration");
2551 final DisplayMetrics metrics = new DisplayMetrics();
2552 metrics.setToDefaults();
2554 Resources res = new Resources(AssetManager.getSystem(), metrics, config);
2556 System.out.println(res.getBoolean(com.android.internal.R.bool.config_supportsMultiWindow));
2560 * Open the given file for sending into the system process. This verifies
2561 * with SELinux that the system will have access to the file.
2563 private static ParcelFileDescriptor openForSystemServer(File file, int mode)
2564 throws FileNotFoundException {
2565 final ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, mode);
2566 final String tcon = SELinux.getFileContext(file.getAbsolutePath());
2567 if (!SELinux.checkSELinuxAccess("u:r:system_server:s0", tcon, "file", "read")) {
2568 throw new FileNotFoundException("System server has no access to file context " + tcon);
2573 private Rect getBounds() {
2574 String leftStr = nextArgRequired();
2575 int left = Integer.parseInt(leftStr);
2576 String topStr = nextArgRequired();
2577 int top = Integer.parseInt(topStr);
2578 String rightStr = nextArgRequired();
2579 int right = Integer.parseInt(rightStr);
2580 String bottomStr = nextArgRequired();
2581 int bottom = Integer.parseInt(bottomStr);
2583 System.err.println("Error: bad left arg: " + leftStr);
2587 System.err.println("Error: bad top arg: " + topStr);
2591 System.err.println("Error: bad right arg: " + rightStr);
2595 System.err.println("Error: bad bottom arg: " + bottomStr);
2598 return new Rect(left, top, right, bottom);