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 53 final Intent launchIntent = new Intent(); 54 launchIntent.setComponent( 55 new ComponentName(CONTENT_CAPTURE_PACKAGE_NAME, CONTENT_CAPTURE_ACTIVITY_NAME)); 56 startActivityForResult(launchIntent, 42); 57 } 58 waitContentCaptureEnabled(boolean enabled)59 public void waitContentCaptureEnabled(boolean enabled) throws InterruptedException { 60 final Intent broadcastIntent = new Intent().setAction("SimpleActivityReceiver"); 61 sendBroadcast(broadcastIntent); 62 63 final CountDownLatch latch = enabled ? mEnabledLatch : mDisabledLatch; 64 final boolean called = latch.await(2, TimeUnit.SECONDS); 65 66 if (!called) { 67 Log.e(TAG, CONTENT_CAPTURE_PACKAGE_NAME 68 + " didn't get " + (enabled ? "enabled" : "disabled") + " state in 2 seconds"); 69 } 70 } 71 72 @Override onDestroy()73 public void onDestroy() { 74 super.onDestroy(); 75 unregisterReceiver(mReceiver); 76 } 77 78 private class ContentCaptureActivityReceiver extends BroadcastReceiver { 79 private static final String ACTION = "ContentCaptureActivityReceiver"; 80 81 @Override onReceive(Context context, Intent intent)82 public void onReceive(Context context, Intent intent) { 83 boolean enabled = intent.getBooleanExtra("enabled", false); 84 if (enabled) { 85 if (mEnabledLatch.getCount() == 0) { 86 Log.e(TAG, "already received enabled broadcast"); 87 } 88 mEnabledLatch.countDown(); 89 } else { 90 if (mDisabledLatch.getCount() == 0) { 91 Log.e(TAG, "already received disabled broadcast"); 92 } 93 mDisabledLatch.countDown(); 94 } 95 } 96 } 97 } 98