1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.applications.autofill;
15 
16 import android.app.Activity;
17 import android.content.ComponentName;
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.view.autofill.AutofillManager;
21 import com.android.settings.applications.credentials.DefaultCombinedPicker;
22 
23 /**
24  * Standalone activity used to launch a {@link DefaultCombinedPicker} fragment from a
25  * {@link android.provider.Settings#ACTION_REQUEST_SET_AUTOFILL_SERVICE} intent.
26  *
27  * <p>It first check for cases that can fail fast, then forward to {@link AutofillPickerActivity}
28  * if necessary.
29  */
30 public class AutofillPickerTrampolineActivity extends Activity {
31 
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         final AutofillManager afm = getSystemService(AutofillManager.class);
37 
38         // First check if the Autofill is available for the current user...
39         if (afm == null || !afm.hasAutofillFeature() || !afm.isAutofillSupported()) {
40             // ... and fail right away if it is not.
41             setResult(RESULT_CANCELED);
42             finish();
43             return;
44         }
45 
46         // Then check if the current user's service already belongs to the app...
47         final Intent intent = getIntent();
48         final String packageName = intent.getData().getSchemeSpecificPart();
49         final ComponentName currentService = afm.getAutofillServiceComponentName();
50         if (currentService != null && currentService.getPackageName().equals(packageName)) {
51             // ...and succeed right away if it does.
52             setResult(RESULT_OK);
53             finish();
54             return;
55         }
56 
57         // Otherwise, go ahead and show the real UI...
58         final Intent newIntent = new Intent(this, AutofillPickerActivity.class)
59                 .setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
60                 .setData(intent.getData());
61         startActivity(newIntent);
62         finish();
63     }
64 }
65