1 /* 2 * Copyright (C) 2018 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 com.android.tv.settings.autofill; 17 18 import android.app.Activity; 19 import android.content.ComponentName; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.view.autofill.AutofillManager; 23 24 /** 25 * Standalone activity used to launch a {@link AutofillPickerActivity" from a 26 * {@link android.provider.Settings#ACTION_REQUEST_SET_AUTOFILL_SERVICE} intent. 27 * 28 * <p>It first checks for cases that can fail fast, then forwards to {@link AutofillPickerActivity} 29 * if necessary. 30 */ 31 public class AutofillPickerTrampolineActivity extends Activity { 32 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 // First check if the current user's service already belongs to the app... 38 final Intent intent = getIntent(); 39 final String packageName = intent.getData().getSchemeSpecificPart(); 40 final ComponentName currentService = AutofillHelper.getCurrentAutofillAsComponentName(this); 41 if (currentService != null && currentService.getPackageName().equals(packageName)) { 42 // ...and succeed right away if it does. 43 setResult(RESULT_OK); 44 finish(); 45 return; 46 } 47 48 // Then check if the Autofill is available for the current user... 49 final AutofillManager afm = getSystemService(AutofillManager.class); 50 if (afm == null || !afm.hasAutofillFeature() || !afm.isAutofillSupported()) { 51 // ... and fail right away if it is not. 52 setResult(RESULT_CANCELED); 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 66