1 package com.android.cts.deviceandprofileowner;
2 
3 import android.app.Activity;
4 import android.content.BroadcastReceiver;
5 import android.content.ComponentName;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.graphics.Bitmap;
9 import android.os.Handler;
10 import android.os.Looper;
11 import android.support.test.InstrumentationRegistry;
12 import android.support.test.runner.AndroidJUnit4;
13 import android.util.Log;
14 
15 import com.android.compatibility.common.util.BlockingBroadcastReceiver;
16 
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 
22 import java.util.concurrent.LinkedBlockingQueue;
23 import java.util.concurrent.TimeUnit;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 /**
28  * Testing
29  * {@link android.app.admin.DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}
30  * is enforced in {@link android.service.voice.VoiceInteractionSession#onHandleScreenshot(Bitmap)}.
31  */
32 @RunWith(AndroidJUnit4.class)
33 public class AssistScreenCaptureDisabledTest {
34     private static final String TAG = "DevicePolicyAssistTest";
35 
36     private static final String ACTION_CHECK_IS_READY = "voice_interaction_service.is_ready";
37     private static final String ACTION_SHOW_SESSION = "voice_interaction_service.show_session";
38     private static final String ACTION_HANDLE_SCREENSHOT =
39             "voice_interaction_session_service.handle_screenshot";
40     private static final String KEY_HAS_SCREENSHOT = "has_screenshot";
41     private static final String ASSIST_PACKAGE = "com.android.cts.devicepolicy.assistapp";
42 
43     private static final int MAX_ATTEMPTS_COUNT = 5;
44     private static final int WAIT_IN_SECOND = 5;
45     private Context mContext;
46 
47     @Before
setup()48     public void setup() {
49         mContext = InstrumentationRegistry.getContext();
50     }
51 
52     @Test
testScreenCaptureImpossible_assist()53     public void testScreenCaptureImpossible_assist() throws Exception {
54         assertScreenCapturePossible(false);
55     }
56 
57     @Test
testScreenCapturePossible_assist()58     public void testScreenCapturePossible_assist() throws Exception {
59         assertScreenCapturePossible(true);
60     }
61 
assertScreenCapturePossible(boolean possible)62     private void assertScreenCapturePossible(boolean possible) throws InterruptedException {
63         // Wait until voice interaction service is ready by sending broadcast to ask for status.
64         Intent checkIsReadyIntent = new Intent(ACTION_CHECK_IS_READY);
65         checkIsReadyIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
66         checkIsReadyIntent.setPackage(ASSIST_PACKAGE);
67         boolean isAssistReady = false;
68         for (int i = 0; i < MAX_ATTEMPTS_COUNT && !isAssistReady; i++) {
69             Log.d(TAG, "assertScreenCapturePossible: wait for assist service ready, attempt " + i);
70             final LinkedBlockingQueue<Boolean> q = new LinkedBlockingQueue<>();
71             mContext.sendOrderedBroadcast(checkIsReadyIntent, null, new BroadcastReceiver() {
72                 @Override
73                 public void onReceive(Context context, Intent intent) {
74                     q.offer(getResultCode() == Activity.RESULT_OK);
75                 }
76             }, null, Activity.RESULT_CANCELED, null, null);
77             Boolean result = q.poll(WAIT_IN_SECOND, TimeUnit.SECONDS);
78             isAssistReady = result != null && result;
79         }
80         Assert.assertTrue(isAssistReady);
81 
82         // Send broadcast to voice interaction service and ask for screnshot.
83         BlockingBroadcastReceiver receiver = new BlockingBroadcastReceiver(
84                 mContext, ACTION_HANDLE_SCREENSHOT);
85         try {
86             receiver.register();
87             Intent showSessionIntent = new Intent(ACTION_SHOW_SESSION);
88             showSessionIntent.setPackage(ASSIST_PACKAGE);
89             mContext.sendBroadcast(showSessionIntent);
90             Intent screenShotIntent = null;
91             for (int i = 0; i < MAX_ATTEMPTS_COUNT && (screenShotIntent == null); ++ i) {
92                 Log.d(TAG, "has not received intent yet: wait for intent, attempt " + i);
93                 screenShotIntent = receiver.awaitForBroadcast();
94             }
95             Assert.assertNotNull(screenShotIntent);
96             Assert.assertTrue(screenShotIntent.hasExtra(KEY_HAS_SCREENSHOT));
97             assertEquals(possible, screenShotIntent.getBooleanExtra(KEY_HAS_SCREENSHOT, false));
98         } finally {
99             receiver.unregisterQuietly();
100         }
101     }
102 }
103