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.deviceowner;
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.deviceowner.LOCK_TASK_CREATE";
32     public static final String DESTROY_ACTION = "com.android.cts.deviceowner.LOCK_TASK_DESTROY";
33     public static final String PAUSE_ACTION = "com.android.cts.deviceowner.LOCK_TASK_PAUSE";
34     public static final String RESUME_ACTION = "com.android.cts.deviceowner.LOCK_TASK_RESUME";
35     public static final String INTENT_ACTION = "com.android.cts.deviceowner.LOCK_TASK_INTENT";
36 
37     @Override
onNewIntent(Intent intent)38     protected void onNewIntent(Intent intent) {
39         super.onNewIntent(intent);
40         handleIntent(intent);
41     }
42 
43     @Override
onCreate(android.os.Bundle savedInstanceState)44     protected void onCreate(android.os.Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         sendLocalBroadcast(new Intent(CREATE_ACTION));
47         handleIntent(getIntent());
48     }
49 
50     @Override
onDestroy()51     protected void onDestroy() {
52         sendLocalBroadcast(new Intent(DESTROY_ACTION));
53         super.onDestroy();
54     }
55 
56     @Override
onResume()57     protected void onResume() {
58         sendLocalBroadcast(new Intent(RESUME_ACTION));
59         super.onResume();
60     }
61 
62     @Override
onPause()63     protected void onPause() {
64         sendLocalBroadcast(new Intent(PAUSE_ACTION));
65         super.onPause();
66     }
67 
handleIntent(Intent intent)68     private void handleIntent(Intent intent) {
69         if (intent.getBooleanExtra(START_LOCK_TASK, false)) {
70             startLockTask();
71         }
72         if (intent.getBooleanExtra(STOP_LOCK_TASK, false)) {
73             stopLockTask();
74         }
75         if (intent.hasExtra(START_ACTIVITY)) {
76             Intent i = intent.getParcelableExtra(START_ACTIVITY);
77             startActivity(i);
78         }
79         if (intent.getBooleanExtra(FINISH, false)) {
80             finish();
81         }
82         sendLocalBroadcast(new Intent(INTENT_ACTION));
83     }
84 
sendLocalBroadcast(Intent intent)85     private void sendLocalBroadcast(Intent intent) {
86         Log.d(TAG, "sendLocalBroadcast: " + intent.getAction());
87         intent.setPackage(this.getPackageName());
88         intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
89         sendBroadcast(intent);
90     }
91 }
92