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 
17 package com.android.cts.deviceandprofileowner;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.util.Log;
22 
23 public class LockTaskUtilityActivity extends Activity {
24     private static final String TAG = "LockTaskUtilityActivity";
25 
26     public static final String START_LOCK_TASK = "startLockTask";
27     public static final String STOP_LOCK_TASK = "stopLockTask";
28     public static final String START_ACTIVITY = "startActivity";
29     public static final String FINISH = "finish";
30 
31     public static final String CREATE_ACTION = "com.android.cts.deviceandprofileowner.LOCK_TASK_CREATE";
32     public static final String DESTROY_ACTION = "com.android.cts.deviceandprofileowner.LOCK_TASK_DESTROY";
33     public static final String PAUSE_ACTION = "com.android.cts.deviceandprofileowner.LOCK_TASK_PAUSE";
34     public static final String RESUME_ACTION = "com.android.cts.deviceandprofileowner.LOCK_TASK_RESUME";
35     public static final String INTENT_ACTION = "com.android.cts.deviceandprofileowner.LOCK_TASK_INTENT";
36 
37     private static volatile boolean isActivityResumed;
38     private static final Object ACTIVITY_RESUMED_LOCK = new Object();
39 
setIsActivityResumed(boolean newValue)40     private static void setIsActivityResumed(boolean newValue) {
41         synchronized (ACTIVITY_RESUMED_LOCK) {
42             isActivityResumed = newValue;
43             ACTIVITY_RESUMED_LOCK.notify();
44         }
45     }
46 
47     /** Returns true if it's successful. */
waitUntilActivityResumed(long timeoutMs)48     public static boolean waitUntilActivityResumed(long timeoutMs) throws InterruptedException {
49         synchronized (ACTIVITY_RESUMED_LOCK) {
50             if (!isActivityResumed) {
51                 ACTIVITY_RESUMED_LOCK.wait(timeoutMs);
52             }
53             return isActivityResumed;
54         }
55     }
56 
57     /** Returns true if it's successful. */
waitUntilActivityPaused(long timeoutMs)58     public static boolean waitUntilActivityPaused(long timeoutMs) throws InterruptedException {
59         synchronized (ACTIVITY_RESUMED_LOCK) {
60             if (isActivityResumed) {
61                 ACTIVITY_RESUMED_LOCK.wait(timeoutMs);
62             }
63             return !isActivityResumed;
64         }
65     }
66 
67     @Override
onNewIntent(Intent intent)68     protected void onNewIntent(Intent intent) {
69         super.onNewIntent(intent);
70         handleIntent(intent);
71     }
72 
73     @Override
onCreate(android.os.Bundle savedInstanceState)74     protected void onCreate(android.os.Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         sendLocalBroadcast(new Intent(CREATE_ACTION));
77         handleIntent(getIntent());
78     }
79 
80     @Override
onDestroy()81     protected void onDestroy() {
82         sendLocalBroadcast(new Intent(DESTROY_ACTION));
83         super.onDestroy();
84     }
85 
86     @Override
onResume()87     protected void onResume() {
88         setIsActivityResumed(true);
89         sendLocalBroadcast(new Intent(RESUME_ACTION));
90         super.onResume();
91     }
92 
93     @Override
onPause()94     protected void onPause() {
95         setIsActivityResumed(false);
96         sendLocalBroadcast(new Intent(PAUSE_ACTION));
97         super.onPause();
98     }
99 
handleIntent(Intent intent)100     private void handleIntent(Intent intent) {
101         if (intent.getBooleanExtra(START_LOCK_TASK, false)) {
102             startLockTask();
103         }
104         if (intent.getBooleanExtra(STOP_LOCK_TASK, false)) {
105             stopLockTask();
106         }
107         if (intent.hasExtra(START_ACTIVITY)) {
108             Intent i = intent.getParcelableExtra(START_ACTIVITY);
109             startActivity(i);
110         }
111         if (intent.getBooleanExtra(FINISH, false)) {
112             finish();
113         }
114         sendLocalBroadcast(new Intent(INTENT_ACTION));
115     }
116 
sendLocalBroadcast(Intent intent)117     private void sendLocalBroadcast(Intent intent) {
118         Log.d(TAG, "sendLocalBroadcast: " + intent.getAction());
119         intent.setPackage(this.getPackageName());
120         intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
121         sendBroadcast(intent);
122     }
123 }
124