• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 package com.android.cts.deviceowner;
17 
18 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS;
19 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_HOME;
20 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_KEYGUARD;
21 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_NONE;
22 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS;
23 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW;
24 import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_SYSTEM_INFO;
25 
26 import static junit.framework.Assert.assertEquals;
27 import static junit.framework.Assert.assertFalse;
28 import static junit.framework.Assert.assertTrue;
29 
30 import static org.junit.Assert.assertArrayEquals;
31 import static org.testng.Assert.assertThrows;
32 
33 import android.app.ActivityManager;
34 import android.app.ActivityOptions;
35 import android.app.admin.DevicePolicyManager;
36 import android.content.BroadcastReceiver;
37 import android.content.ComponentName;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.content.IntentFilter;
41 import android.os.Bundle;
42 import android.util.Log;
43 
44 import androidx.test.InstrumentationRegistry;
45 import androidx.test.runner.AndroidJUnit4;
46 
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 
52 import java.util.concurrent.TimeUnit;
53 
54 @RunWith(AndroidJUnit4.class)
55 public class LockTaskTest {
56 
57     private static final String TAG = "LockTaskTest";
58 
59     private static final String PACKAGE_NAME = LockTaskTest.class.getPackage().getName();
60     private static final ComponentName ADMIN_COMPONENT =
61             new ComponentName(PACKAGE_NAME, BasicAdminReceiver.class.getName());
62     private static final String TEST_PACKAGE = "com.google.android.example.somepackage";
63 
64     private static final String UTILITY_ACTIVITY
65             = "com.android.cts.deviceowner.LockTaskUtilityActivity";
66     private static final String UTILITY_ACTIVITY_IF_WHITELISTED
67             = "com.android.cts.deviceowner.LockTaskUtilityActivityIfWhitelisted";
68 
69     private static final String RECEIVER_ACTIVITY_PACKAGE_NAME =
70             "com.android.cts.intent.receiver";
71     private static final String RECEIVER_ACTIVITY_NAME =
72             "com.android.cts.intent.receiver.IntentReceiverActivity";
73     private static final String ACTION_JUST_CREATE =
74             "com.android.cts.action.JUST_CREATE";
75     private static final String ACTION_CREATE_AND_WAIT =
76             "com.android.cts.action.CREATE_AND_WAIT";
77     private static final String RECEIVER_ACTIVITY_CREATED_ACTION =
78             "com.android.cts.deviceowner.action.RECEIVER_ACTIVITY_CREATED";
79     private static final String RECEIVER_ACTIVITY_DESTROYED_ACTION =
80             "com.android.cts.deviceowner.action.RECEIVER_ACTIVITY_DESTROYED";
81 
82     private static final long ACTIVITY_RESUMED_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(20);
83     private static final long ACTIVITY_RUNNING_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(10);
84     private static final long ACTIVITY_DESTROYED_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(60);
85 
86     /**
87      * The tests below need to keep detailed track of the state of the activity
88      * that is started and stopped frequently.  To do this it sends a number of
89      * broadcasts that are caught here and translated into booleans (as well as
90      * notify some locks in case we are waiting).  There is also an action used
91      * to specify that the activity has finished handling the current command
92      * (INTENT_ACTION).
93      */
94     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
95         @Override
96         public void onReceive(Context context, Intent intent) {
97             String action = intent.getAction();
98             Log.d(TAG, "onReceive: " + action);
99             if (LockTaskUtilityActivity.CREATE_ACTION.equals(action)) {
100                 synchronized (mActivityRunningLock) {
101                     mIsActivityRunning = true;
102                     mActivityRunningLock.notify();
103                 }
104             } else if (LockTaskUtilityActivity.DESTROY_ACTION.equals(action)) {
105                 synchronized (mActivityRunningLock) {
106                     mIsActivityRunning = false;
107                     mActivityRunningLock.notify();
108                 }
109             } else if (LockTaskUtilityActivity.RESUME_ACTION.equals(action)) {
110                 synchronized (mActivityResumedLock) {
111                     mIsActivityResumed = true;
112                     mActivityResumedLock.notify();
113                 }
114             } else if (LockTaskUtilityActivity.PAUSE_ACTION.equals(action)) {
115                 synchronized (mActivityResumedLock) {
116                     mIsActivityResumed = false;
117                     mActivityResumedLock.notify();
118                 }
119             } else if (LockTaskUtilityActivity.INTENT_ACTION.equals(action)) {
120                 // Notify that intent has been handled.
121                 synchronized (LockTaskTest.this) {
122                     mIntentHandled = true;
123                     LockTaskTest.this.notify();
124                 }
125             } else if (RECEIVER_ACTIVITY_CREATED_ACTION.equals(action)) {
126                 synchronized(mReceiverActivityRunningLock) {
127                     mIsReceiverActivityRunning = true;
128                     mReceiverActivityRunningLock.notify();
129                 }
130             } else if (RECEIVER_ACTIVITY_DESTROYED_ACTION.equals(action)) {
131                 synchronized (mReceiverActivityRunningLock) {
132                     mIsReceiverActivityRunning = false;
133                     mReceiverActivityRunningLock.notify();
134                 }
135             }
136         }
137     };
138 
139     private volatile boolean mIsActivityRunning;
140     private volatile boolean mIsActivityResumed;
141     private volatile boolean mIsReceiverActivityRunning;
142     private volatile boolean mIntentHandled;
143     private final Object mActivityRunningLock = new Object();
144     private final Object mActivityResumedLock = new Object();
145     private final Object mReceiverActivityRunningLock = new Object();
146 
147     private Context mContext;
148     private ActivityManager mActivityManager;
149     private DevicePolicyManager mDevicePolicyManager;
150 
151     @Before
setUp()152     public void setUp() {
153         mContext = InstrumentationRegistry.getContext();
154 
155         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
156         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
157         mActivityManager = mContext.getSystemService(ActivityManager.class);
158         IntentFilter filter = new IntentFilter();
159         filter.addAction(LockTaskUtilityActivity.CREATE_ACTION);
160         filter.addAction(LockTaskUtilityActivity.DESTROY_ACTION);
161         filter.addAction(LockTaskUtilityActivity.INTENT_ACTION);
162         filter.addAction(LockTaskUtilityActivity.RESUME_ACTION);
163         filter.addAction(LockTaskUtilityActivity.PAUSE_ACTION);
164         filter.addAction(RECEIVER_ACTIVITY_CREATED_ACTION);
165         filter.addAction(RECEIVER_ACTIVITY_DESTROYED_ACTION);
166         mContext.registerReceiver(mReceiver, filter);
167     }
168 
169     @After
tearDown()170     public void tearDown() {
171         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
172         mContext.unregisterReceiver(mReceiver);
173     }
174 
175     // Setting and unsetting the lock task packages.
176     @Test
testSetLockTaskPackages()177     public void testSetLockTaskPackages() {
178         final String[] packages = new String[] { TEST_PACKAGE, "some.other.package" };
179         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, packages);
180         assertArrayEquals(packages, mDevicePolicyManager.getLockTaskPackages(ADMIN_COMPONENT));
181         assertTrue(mDevicePolicyManager.isLockTaskPermitted(TEST_PACKAGE));
182 
183         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
184         assertEquals(0, mDevicePolicyManager.getLockTaskPackages(ADMIN_COMPONENT).length);
185         assertFalse(mDevicePolicyManager.isLockTaskPermitted(TEST_PACKAGE));
186     }
187 
188     // Setting and unsetting the lock task features. The actual UI behavior is tested with CTS
189     // verifier.
190     @Test
testSetLockTaskFeatures()191     public void testSetLockTaskFeatures() {
192         final int[] flags = new int[] {
193                 LOCK_TASK_FEATURE_SYSTEM_INFO,
194                 LOCK_TASK_FEATURE_HOME,
195                 LOCK_TASK_FEATURE_NOTIFICATIONS,
196                 LOCK_TASK_FEATURE_OVERVIEW,
197                 LOCK_TASK_FEATURE_GLOBAL_ACTIONS,
198                 LOCK_TASK_FEATURE_KEYGUARD
199         };
200 
201         int cumulative = LOCK_TASK_FEATURE_NONE;
202         for (int flag : flags) {
203             if (flag == LOCK_TASK_FEATURE_OVERVIEW || flag == LOCK_TASK_FEATURE_NOTIFICATIONS) {
204                 // Those flags can only be used in combination with HOME
205                 assertThrows(
206                         IllegalArgumentException.class,
207                         () -> mDevicePolicyManager.setLockTaskFeatures(ADMIN_COMPONENT, flag));
208             } else {
209                 mDevicePolicyManager.setLockTaskFeatures(ADMIN_COMPONENT, flag);
210                 assertEquals(flag, mDevicePolicyManager.getLockTaskFeatures(ADMIN_COMPONENT));
211             }
212 
213             cumulative |= flag;
214             mDevicePolicyManager.setLockTaskFeatures(ADMIN_COMPONENT, cumulative);
215             assertEquals(cumulative, mDevicePolicyManager.getLockTaskFeatures(ADMIN_COMPONENT));
216 
217             mDevicePolicyManager.setLockTaskFeatures(ADMIN_COMPONENT, LOCK_TASK_FEATURE_NONE);
218             assertEquals(LOCK_TASK_FEATURE_NONE,
219                     mDevicePolicyManager.getLockTaskFeatures(ADMIN_COMPONENT));
220         }
221     }
222 
223     // Start lock task, verify that ActivityManager knows thats what is going on.
224     @Test
testStartLockTask()225     public void testStartLockTask() throws Exception {
226         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
227         startLockTask(UTILITY_ACTIVITY);
228         waitForResume();
229 
230         // Verify that activity open and activity manager is in lock task.
231         assertLockTaskModeActive();
232         assertTrue(mIsActivityRunning);
233         assertTrue(mIsActivityResumed);
234 
235         stopAndFinish(UTILITY_ACTIVITY);
236     }
237 
238     // Verifies that the act of finishing is blocked by ActivityManager in lock task.
239     // This results in onDestroy not being called until stopLockTask is called before finish.
240     @Test
testCannotFinish()241     public void testCannotFinish() throws Exception {
242         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
243         startLockTask(UTILITY_ACTIVITY);
244 
245         // If lock task has not exited then the activity shouldn't actually receive onDestroy.
246         finishAndWait(UTILITY_ACTIVITY);
247         assertLockTaskModeActive();
248         assertTrue(mIsActivityRunning);
249 
250         stopAndFinish(UTILITY_ACTIVITY);
251     }
252 
253     // Verifies that updating the whitelisting during lock task mode finishes the locked task.
254     @Test
testUpdateWhitelisting()255     public void testUpdateWhitelisting() throws Exception {
256         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
257         startLockTask(UTILITY_ACTIVITY);
258 
259         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
260 
261         synchronized (mActivityRunningLock) {
262             mActivityRunningLock.wait(ACTIVITY_DESTROYED_TIMEOUT_MILLIS);
263         }
264 
265         assertLockTaskModeInactive();
266         assertFalse(mIsActivityRunning);
267         assertFalse(mIsActivityResumed);
268     }
269 
270     // Verifies that removing the whitelist authorization immediately finishes the corresponding
271     // locked task. The other locked task(s) should remain locked.
272     @Test
testUpdateWhitelisting_twoTasks()273     public void testUpdateWhitelisting_twoTasks() throws Exception {
274         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME,
275                 RECEIVER_ACTIVITY_PACKAGE_NAME});
276 
277         // Start first locked task
278         startLockTask(UTILITY_ACTIVITY);
279         waitForResume();
280 
281         // Start the other task from the running activity
282         mIsReceiverActivityRunning = false;
283         Intent launchIntent = createReceiverActivityIntent(true /*newTask*/, true /*shouldWait*/);
284         mContext.startActivity(launchIntent);
285         synchronized (mReceiverActivityRunningLock) {
286             mReceiverActivityRunningLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
287             assertTrue(mIsReceiverActivityRunning);
288         }
289 
290         // Remove whitelist authorization of the second task
291         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
292         synchronized (mReceiverActivityRunningLock) {
293             mReceiverActivityRunningLock.wait(ACTIVITY_DESTROYED_TIMEOUT_MILLIS);
294             assertFalse(mIsReceiverActivityRunning);
295         }
296 
297         assertLockTaskModeActive();
298         assertTrue(mIsActivityRunning);
299         assertTrue(mIsActivityResumed);
300 
301         stopAndFinish(UTILITY_ACTIVITY);
302     }
303 
304     // This launches an activity that is in the current task.
305     // This should always be permitted as a part of lock task (since it isn't a new task).
306     @Test
testStartActivity_withinTask()307     public void testStartActivity_withinTask() throws Exception {
308         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
309         startLockTask(UTILITY_ACTIVITY);
310         waitForResume();
311 
312         mIsReceiverActivityRunning = false;
313         Intent launchIntent = createReceiverActivityIntent(false /*newTask*/, false /*shouldWait*/);
314         Intent lockTaskUtility = getLockTaskUtility(UTILITY_ACTIVITY);
315         lockTaskUtility.putExtra(LockTaskUtilityActivity.START_ACTIVITY, launchIntent);
316         mContext.startActivity(lockTaskUtility);
317 
318         synchronized (mReceiverActivityRunningLock) {
319             mReceiverActivityRunningLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
320             assertTrue(mIsReceiverActivityRunning);
321         }
322         stopAndFinish(UTILITY_ACTIVITY);
323     }
324 
325     // This launches a whitelisted activity that is not part of the current task.
326     // This should be permitted as a part of lock task.
327     @Test
testStartActivity_outsideTaskWhitelisted()328     public void testStartActivity_outsideTaskWhitelisted() throws Exception {
329         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME,
330                 RECEIVER_ACTIVITY_PACKAGE_NAME});
331         startLockTask(UTILITY_ACTIVITY);
332         waitForResume();
333 
334         mIsReceiverActivityRunning = false;
335         Intent launchIntent = createReceiverActivityIntent(true /*newTask*/, false /*shouldWait*/);
336         mContext.startActivity(launchIntent);
337         synchronized (mReceiverActivityRunningLock) {
338             mReceiverActivityRunningLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
339             assertTrue(mIsReceiverActivityRunning);
340         }
341         stopAndFinish(UTILITY_ACTIVITY);
342     }
343 
344     // This launches a non-whitelisted activity that is not part of the current task.
345     // This should be blocked.
346     @Test
testStartActivity_outsideTaskNonWhitelisted()347     public void testStartActivity_outsideTaskNonWhitelisted() throws Exception {
348         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
349         startLockTask(UTILITY_ACTIVITY);
350         waitForResume();
351 
352         Intent launchIntent = createReceiverActivityIntent(true /*newTask*/, false /*shouldWait*/);
353         mContext.startActivity(launchIntent);
354         synchronized (mActivityResumedLock) {
355             mActivityResumedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
356             assertFalse(mIsReceiverActivityRunning);
357         }
358         stopAndFinish(UTILITY_ACTIVITY);
359     }
360 
361     // Test the lockTaskMode flag for an activity declaring if_whitelisted.
362     // Whitelist the activity and verify that lock task mode is started.
363     @Test
testManifestArgument_whitelisted()364     public void testManifestArgument_whitelisted() throws Exception {
365         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
366         startAndWait(getLockTaskUtility(UTILITY_ACTIVITY_IF_WHITELISTED));
367         waitForResume();
368 
369         assertLockTaskModeActive();
370         assertTrue(mIsActivityRunning);
371         assertTrue(mIsActivityResumed);
372 
373         stopAndFinish(UTILITY_ACTIVITY_IF_WHITELISTED);
374     }
375 
376     // Test the lockTaskMode flag for an activity declaring if_whitelisted.
377     // Don't whitelist the activity and verify that lock task mode is not started.
378     @Test
testManifestArgument_nonWhitelisted()379     public void testManifestArgument_nonWhitelisted() throws Exception {
380         startAndWait(getLockTaskUtility(UTILITY_ACTIVITY_IF_WHITELISTED));
381         waitForResume();
382 
383         assertLockTaskModeInactive();
384         assertTrue(mIsActivityRunning);
385         assertTrue(mIsActivityResumed);
386 
387         stopAndFinish(UTILITY_ACTIVITY_IF_WHITELISTED);
388     }
389 
390     // Test the lockTaskMode flag for an activity declaring if_whitelisted.
391     // An activity locked via manifest argument cannot finish without calling stopLockTask.
392     @Test
testManifestArgument_cannotFinish()393     public void testManifestArgument_cannotFinish() throws Exception {
394         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
395         startAndWait(getLockTaskUtility(UTILITY_ACTIVITY_IF_WHITELISTED));
396         waitForResume();
397 
398         // If lock task has not exited then the activity shouldn't actually receive onDestroy.
399         finishAndWait(UTILITY_ACTIVITY_IF_WHITELISTED);
400         assertLockTaskModeActive();
401         assertTrue(mIsActivityRunning);
402 
403         stopAndFinish(UTILITY_ACTIVITY_IF_WHITELISTED);
404     }
405 
406     // Test the lockTaskMode flag for an activity declaring if_whitelisted.
407     // Verifies that updating the whitelisting during lock task mode finishes the locked task.
408     @Test
testManifestArgument_updateWhitelisting()409     public void testManifestArgument_updateWhitelisting() throws Exception {
410         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
411         startAndWait(getLockTaskUtility(UTILITY_ACTIVITY_IF_WHITELISTED));
412         waitForResume();
413 
414         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
415 
416         synchronized (mActivityRunningLock) {
417             mActivityRunningLock.wait(ACTIVITY_DESTROYED_TIMEOUT_MILLIS);
418         }
419 
420         assertLockTaskModeInactive();
421         assertFalse(mIsActivityRunning);
422         assertFalse(mIsActivityResumed);
423     }
424 
425     // Start lock task with ActivityOptions
426     @Test
testActivityOptions_whitelisted()427     public void testActivityOptions_whitelisted() throws Exception {
428         mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
429         startLockTaskWithOptions(UTILITY_ACTIVITY);
430         waitForResume();
431 
432         // Verify that activity open and activity manager is in lock task.
433         assertLockTaskModeActive();
434         assertTrue(mIsActivityRunning);
435         assertTrue(mIsActivityResumed);
436 
437         stopAndFinish(UTILITY_ACTIVITY);
438     }
439 
440     // Starting a non-whitelisted activity with ActivityOptions is not allowed
441     @Test(expected = SecurityException.class)
testActivityOptions_nonWhitelisted()442     public void testActivityOptions_nonWhitelisted() throws Exception {
443         startLockTaskWithOptions(UTILITY_ACTIVITY);
444     }
445 
446     /**
447      * Checks that lock task mode is active and fails the test if it isn't.
448      */
assertLockTaskModeActive()449     private void assertLockTaskModeActive() {
450         assertTrue(mActivityManager.isInLockTaskMode());
451         assertEquals(ActivityManager.LOCK_TASK_MODE_LOCKED,
452                 mActivityManager.getLockTaskModeState());
453     }
454 
455     /**
456      * Checks that lock task mode is not active and fails the test if it is.
457      */
assertLockTaskModeInactive()458     private void assertLockTaskModeInactive() throws InterruptedException {
459         // Retry 10 times with 200 ms interval.
460         for (int i = 0; i < 10 && mActivityManager.isInLockTaskMode(); i++) {
461             Thread.sleep(200);
462         }
463         assertFalse(mActivityManager.isInLockTaskMode());
464         assertEquals(ActivityManager.LOCK_TASK_MODE_NONE, mActivityManager.getLockTaskModeState());
465     }
466 
467     /**
468      * Call stopLockTask and finish on the LockTaskUtilityActivity.
469      *
470      * Verify that the activity is no longer running.
471      *
472      * If activityManager is not null then verify that the ActivityManager
473      * is no longer in lock task mode.
474      */
stopAndFinish(String className)475     private void stopAndFinish(String className) throws InterruptedException {
476         stopLockTask(className);
477         finishAndWait(className);
478         assertLockTaskModeInactive();
479         assertFalse(mIsActivityRunning);
480     }
481 
482     /**
483      * Call finish on the LockTaskUtilityActivity and wait for
484      * onDestroy to be called.
485      */
finishAndWait(String className)486     private void finishAndWait(String className) throws InterruptedException {
487         synchronized (mActivityRunningLock) {
488             finish(className);
489             if (mIsActivityRunning) {
490                 mActivityRunningLock.wait(ACTIVITY_DESTROYED_TIMEOUT_MILLIS);
491             }
492         }
493     }
494 
495     /**
496      * Wait for onResume to be called on the LockTaskUtilityActivity.
497      */
waitForResume()498     private void waitForResume() throws InterruptedException {
499         // It may take a moment for the resume to come in.
500         synchronized (mActivityResumedLock) {
501             if (!mIsActivityResumed) {
502                 mActivityResumedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
503             }
504         }
505     }
506 
507     /**
508      * Calls startLockTask on the LockTaskUtilityActivity
509      */
startLockTask(String className)510     private void startLockTask(String className) throws InterruptedException {
511         Intent intent = getLockTaskUtility(className);
512         intent.putExtra(LockTaskUtilityActivity.START_LOCK_TASK, true);
513         startAndWait(intent);
514     }
515 
516     /**
517      * Starts LockTaskUtilityActivity with {@link ActivityOptions#setLockTaskEnabled(boolean)}
518      */
startLockTaskWithOptions(String className)519     private void startLockTaskWithOptions(String className) throws InterruptedException {
520         Intent intent = getLockTaskUtility(className);
521         Bundle options = ActivityOptions.makeBasic().setLockTaskEnabled(true).toBundle();
522         startAndWait(intent, options);
523     }
524 
525     /**
526      * Calls stopLockTask on the LockTaskUtilityActivity
527      */
stopLockTask(String className)528     private void stopLockTask(String className) throws InterruptedException {
529         Intent intent = getLockTaskUtility(className);
530         intent.putExtra(LockTaskUtilityActivity.STOP_LOCK_TASK, true);
531         startAndWait(intent);
532     }
533 
534     /**
535      * Calls finish on the LockTaskUtilityActivity
536      */
finish(String className)537     private void finish(String className) throws InterruptedException {
538         Intent intent = getLockTaskUtility(className);
539         intent.putExtra(LockTaskUtilityActivity.FINISH, true);
540         startAndWait(intent);
541     }
542 
543     /**
544      * Sends a command intent to the LockTaskUtilityActivity and waits
545      * to receive the broadcast back confirming it has finished processing
546      * the command.
547      */
startAndWait(Intent intent)548     private void startAndWait(Intent intent) throws InterruptedException {
549         startAndWait(intent, null);
550     }
551 
552     /**
553      * Same as {@link #startAndWait(Intent)}, but with additional {@link ActivityOptions}.
554      */
startAndWait(Intent intent, Bundle options)555     private void startAndWait(Intent intent, Bundle options) throws InterruptedException {
556         mIntentHandled = false;
557         synchronized (this) {
558             mContext.startActivity(intent, options);
559             // Give 20 secs to finish.
560             wait(ACTIVITY_RUNNING_TIMEOUT_MILLIS);
561             assertTrue(mIntentHandled);
562         }
563     }
564 
565     /**
566      * Get basic intent that points at the LockTaskUtilityActivity.
567      *
568      * This intent includes the flags to make it act as single top.
569      */
getLockTaskUtility(String className)570     private Intent getLockTaskUtility(String className) {
571         Intent intent = new Intent();
572         intent.setClassName(PACKAGE_NAME, className);
573         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
574         return intent;
575     }
576 
577     /** Create an intent to launch {@link #RECEIVER_ACTIVITY_NAME}. */
createReceiverActivityIntent(boolean newTask, boolean shouldWait)578     private Intent createReceiverActivityIntent(boolean newTask, boolean shouldWait) {
579         final Intent intent = new Intent();
580         intent.setComponent(
581                 new ComponentName(RECEIVER_ACTIVITY_PACKAGE_NAME, RECEIVER_ACTIVITY_NAME));
582         intent.setAction(shouldWait ? ACTION_CREATE_AND_WAIT : ACTION_JUST_CREATE);
583         intent.setFlags(newTask ? Intent.FLAG_ACTIVITY_NEW_TASK : 0);
584         return intent;
585     }
586 }
587