1 /*
2  * Copyright (C) 2019 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.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.os.Bundle;
26 import android.util.Log;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * Wrapper class used to call the activity in the non-test APK and wait for its result.
33  */
34 public class ContentCaptureActivity extends Activity {
35     private static final String TAG = "ContentCaptureActivity";
36 
37     public static final String CONTENT_CAPTURE_PACKAGE_NAME =
38             "com.android.cts.devicepolicy.contentcaptureapp";
39     public static final String CONTENT_CAPTURE_ACTIVITY_NAME = CONTENT_CAPTURE_PACKAGE_NAME
40             + ".SimpleActivity";
41 
42     private CountDownLatch mEnabledLatch = new CountDownLatch(1);
43     private CountDownLatch mDisabledLatch = new CountDownLatch(1);
44 
45     private ContentCaptureActivityReceiver mReceiver;
46 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50         mReceiver = new ContentCaptureActivityReceiver();
51         registerReceiver(mReceiver, new IntentFilter(ContentCaptureActivityReceiver.ACTION),
52                 Context.RECEIVER_EXPORTED);
53 
54         final Intent launchIntent = new Intent();
55         launchIntent.setComponent(
56                 new ComponentName(CONTENT_CAPTURE_PACKAGE_NAME, CONTENT_CAPTURE_ACTIVITY_NAME));
57         startActivityForResult(launchIntent, 42);
58     }
59 
waitContentCaptureEnabled(boolean enabled)60     public void waitContentCaptureEnabled(boolean enabled) throws InterruptedException {
61         final Intent broadcastIntent = new Intent().setAction("SimpleActivityReceiver");
62         sendBroadcast(broadcastIntent);
63 
64         final CountDownLatch latch = enabled ? mEnabledLatch : mDisabledLatch;
65         final boolean called = latch.await(2, TimeUnit.SECONDS);
66 
67         if (!called) {
68             Log.e(TAG, CONTENT_CAPTURE_PACKAGE_NAME
69                     + " didn't get " + (enabled ? "enabled" : "disabled") + " state in 2 seconds");
70         }
71     }
72 
73     @Override
onDestroy()74     public void onDestroy() {
75         super.onDestroy();
76         unregisterReceiver(mReceiver);
77     }
78 
79     private class ContentCaptureActivityReceiver extends BroadcastReceiver {
80         private static final String ACTION = "ContentCaptureActivityReceiver";
81 
82         @Override
onReceive(Context context, Intent intent)83         public void onReceive(Context context, Intent intent) {
84             boolean enabled = intent.getBooleanExtra("enabled", false);
85             if (enabled) {
86                 if (mEnabledLatch.getCount() == 0) {
87                     Log.e(TAG, "already received enabled broadcast");
88                 }
89                 mEnabledLatch.countDown();
90             } else {
91                 if (mDisabledLatch.getCount() == 0) {
92                     Log.e(TAG, "already received disabled broadcast");
93                 }
94                 mDisabledLatch.countDown();
95             }
96         }
97     }
98 }
99