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 package android.contentcaptureservice.cts2;
17 
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.contentcapture.ContentCaptureManager;
23 
24 /**
25  * This activity is used to test temporary Content Capture Service interactions with activities
26  * outside of its own package.
27  */
28 public class OutsideOfPackageActivity extends Activity {
29 
30     private static final String TAG = "OutsideOfPackageActivity";
31     boolean mFinishActivity;
32 
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         Intent intent = getIntent();
37         mFinishActivity = intent.getBooleanExtra("finishActivity", false);
38 
39     }
40 
41     @Override
onNewIntent(Intent intent)42     protected void onNewIntent(Intent intent) {
43         Log.d(TAG, "onNewIntent()");
44         super.onNewIntent(intent);
45         mFinishActivity = intent.getBooleanExtra("finishActivity", false);
46     }
47 
48     @Override
onResume()49     protected void onResume() {
50         super.onResume();
51         Log.d(TAG, "onCreate(), mFinishActivity=" + mFinishActivity);
52         sendContentCaptureEnableStatussult();
53         if (mFinishActivity) {
54             finish();
55         }
56         Log.d(TAG, "finish onResume()");
57     }
58 
isContentCaptureEnabled()59     private boolean isContentCaptureEnabled() {
60         ContentCaptureManager captureManager = getSystemService(ContentCaptureManager.class);
61         if (captureManager == null) {
62             return false;
63         }
64         return captureManager.isContentCaptureEnabled();
65     }
66 
sendContentCaptureEnableStatussult()67     private void sendContentCaptureEnableStatussult() {
68         boolean isEnable = isContentCaptureEnabled();
69         Log.d(TAG, "send enable: " + isEnable + " for " + getPackageName());
70         Intent intent = new Intent("ACTION_ACTIVITY_CC_STATUS_TEST")
71                 .addFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REGISTERED_ONLY)
72                 .putExtra("cc_enable", isEnable);
73         sendBroadcast(intent);
74     }
75 }
76