2626import android .app .IActivityManager ;
2727import android .app .IInstrumentationWatcher ;
2828import android .app .Instrumentation ;
29+ import android .app .IStopUserCallback ;
2930import android .app .ProfilerInfo ;
3031import android .app .UiAutomationConnection ;
3132import android .app .usage .ConfigurationStats ;
@@ -133,7 +134,7 @@ public void onShowUsage(PrintStream out) {
133134 " am to-app-uri [INTENT]\n " +
134135 " am switch-user <USER_ID>\n " +
135136 " am start-user <USER_ID>\n " +
136- " am stop-user <USER_ID>\n " +
137+ " am stop-user [-w] <USER_ID>\n " +
137138 " am stack start <DISPLAY_ID> <INTENT>\n " +
138139 " am stack movetask <TASK_ID> <STACK_ID> [true|false]\n " +
139140 " am stack resize <STACK_ID> <LEFT,TOP,RIGHT,BOTTOM>\n " +
@@ -257,6 +258,7 @@ public void onShowUsage(PrintStream out) {
257258 "\n " +
258259 "am stop-user: stop execution of USER_ID, not allowing it to run any\n " +
259260 " code until a later explicit start or switch to it.\n " +
261+ " -w: wait for stop-user to complete.\n " +
260262 "\n " +
261263 "am stack start: start a new activity on <DISPLAY_ID> using <INTENT>.\n " +
262264 "\n " +
@@ -1303,9 +1305,45 @@ private void runStartUserInBackground() throws Exception {
13031305 }
13041306 }
13051307
1308+ private static class StopUserCallback extends IStopUserCallback .Stub {
1309+ private boolean mFinished = false ;
1310+
1311+ public synchronized void waitForFinish () {
1312+ try {
1313+ while (!mFinished ) wait ();
1314+ } catch (InterruptedException e ) {
1315+ throw new IllegalStateException (e );
1316+ }
1317+ }
1318+
1319+ @ Override
1320+ public synchronized void userStopped (int userId ) {
1321+ mFinished = true ;
1322+ notifyAll ();
1323+ }
1324+
1325+ @ Override
1326+ public synchronized void userStopAborted (int userId ) {
1327+ mFinished = true ;
1328+ notifyAll ();
1329+ }
1330+ }
1331+
13061332 private void runStopUser () throws Exception {
1307- String user = nextArgRequired ();
1308- int res = mAm .stopUser (Integer .parseInt (user ), null );
1333+ boolean wait = false ;
1334+ String opt = null ;
1335+ while ((opt = nextOption ()) != null ) {
1336+ if ("-w" .equals (opt )) {
1337+ wait = true ;
1338+ } else {
1339+ System .err .println ("Error: unknown option: " + opt );
1340+ return ;
1341+ }
1342+ }
1343+ int user = Integer .parseInt (nextArgRequired ());
1344+ StopUserCallback callback = wait ? new StopUserCallback () : null ;
1345+
1346+ int res = mAm .stopUser (user , callback );
13091347 if (res != ActivityManager .USER_OP_SUCCESS ) {
13101348 String txt = "" ;
13111349 switch (res ) {
@@ -1317,6 +1355,8 @@ private void runStopUser() throws Exception {
13171355 break ;
13181356 }
13191357 System .err .println ("Switch failed: " + res + txt );
1358+ } else if (callback != null ) {
1359+ callback .waitForFinish ();
13201360 }
13211361 }
13221362
0 commit comments