1 /*
2  * Copyright (C) 2020 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.autofill.cts2;
17 
18 import android.app.Activity;
19 import android.app.PendingIntent;
20 import android.app.PendingIntent.CanceledException;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.autofill.AutofillManager;
25 
26 /**
27  * An activity that queries its AutofillService status when started and immediately terminates
28  * itself.
29  */
30 public class QueryAutofillStatusActivity extends Activity {
31 
32     private static final String TAG = "QueryAutofillServiceStatusActivity";
33 
34     // Autofill enable status, the value should be the same with AutofillManagerTest in
35     // CtsAutofillServiceTestCases.
36     private static final int AUTOFILL_ENABLE = 1;
37     private static final int AUTOFILL_DISABLE = 2;
38 
39     private PendingIntent mPendingIntent;
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         final Intent intent = getIntent();
45         mPendingIntent = intent.getParcelableExtra("finishBroadcast");
46     }
47 
48     @Override
onResume()49     protected void onResume() {
50         super.onResume();
51         hasEnabledAutofillServicesAndFinish();
52     }
53 
hasEnabledAutofillServicesAndFinish()54     private void hasEnabledAutofillServicesAndFinish() {
55         // Check if the calling application provides a AutofillService that is enabled
56         final AutofillManager afm = getSystemService(AutofillManager.class);
57         final boolean enabled = afm.hasEnabledAutofillServices();
58         Log.w(TAG, "hasEnabledAutofillServices()= " + enabled);
59 
60         if (mPendingIntent != null) {
61             try {
62                 final int resultCode = enabled ? AUTOFILL_ENABLE : AUTOFILL_DISABLE;
63                 mPendingIntent.send(resultCode);
64             } catch (CanceledException e) {
65                 Log.w(TAG, "Pending intent " + mPendingIntent + " canceled");
66             }
67         }
68         finish();
69     }
70 }
71