Implemented ACTION_REQUEST_SET_AUTOFILL_SERVICE.

Bug: 37673809
Fixes: 37576671
Test: manual verification
Test: make RunSettingsRoboTests -j90

Change-Id: I36170cb715473dfbf598deedd9719a46168aabc8
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 8535b2f..2d8e06a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -3077,6 +3077,16 @@
             android:permission="android.permission.DUMP"
             android:enabled="@bool/config_has_help" />
 
+        <activity android:name=".applications.defaultapps.AutofillPickerActivity"
+                android:taskAffinity=""
+                android:noHistory="true">
+            <intent-filter android:priority="1">
+                <action android:name="android.settings.REQUEST_SET_AUTOFILL_SERVICE" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:scheme="package" />
+            </intent-filter>
+        </activity>
+
         <!-- This is the longest AndroidManifest.xml ever. -->
     </application>
 </manifest>
diff --git a/src/com/android/settings/applications/defaultapps/AutofillPickerActivity.java b/src/com/android/settings/applications/defaultapps/AutofillPickerActivity.java
new file mode 100644
index 0000000..a173909
--- /dev/null
+++ b/src/com/android/settings/applications/defaultapps/AutofillPickerActivity.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+package com.android.settings.applications.defaultapps;
+
+import android.content.Intent;
+import android.os.Bundle;
+
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+
+/**
+ * Standalone activity used to launch {@link DefaultAppPickerFragment} from a
+ * {@link android.provider.Settings#ACTION_REQUEST_SET_AUTOFILL_SERVICE} intent.
+ */
+public class AutofillPickerActivity extends SettingsActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        final Intent intent = getIntent();
+        final String packageName = intent.getData().getSchemeSpecificPart();
+        intent.putExtra(EXTRA_SHOW_FRAGMENT, DefaultAutofillPicker.class.getName());
+        intent.putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.autofill_app);
+        intent.putExtra(DefaultAutofillPicker.EXTRA_PACKAGE_NAME, packageName);
+
+        super.onCreate(savedInstanceState);
+    }
+
+    @Override
+    protected boolean isValidFragment(String fragmentName) {
+        return super.isValidFragment(fragmentName)
+                || DefaultAutofillPicker.class.getName().equals(fragmentName);
+    }
+}
diff --git a/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java b/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java
index 8eb1fc6..037cddf 100644
--- a/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java
+++ b/src/com/android/settings/applications/defaultapps/DefaultAutofillPicker.java
@@ -16,6 +16,7 @@
 
 package com.android.settings.applications.defaultapps;
 
+import android.app.Activity;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -38,6 +39,8 @@
     static final String SETTING = Settings.Secure.AUTOFILL_SERVICE;
     static final Intent AUTOFILL_PROBE = new Intent(AutofillService.SERVICE_INTERFACE);
 
+    static final String EXTRA_PACKAGE_NAME = "package_name";
+
     @Override
     public int getMetricsCategory() {
         return MetricsProto.MetricsEvent.DEFAULT_AUTOFILL_PICKER;
@@ -79,11 +82,24 @@
     @Override
     protected boolean setDefaultKey(String key) {
         Settings.Secure.putString(getContext().getContentResolver(), SETTING, key);
+
+        // Check if activity was launched from Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE
+        // intent, and set proper result if so...
+        final Activity activity = getActivity();
+        if (activity != null) {
+            final String packageName = activity.getIntent().getStringExtra(EXTRA_PACKAGE_NAME);
+            if (packageName != null) {
+                final int result = key != null && key.startsWith(packageName) ? Activity.RESULT_OK
+                        : Activity.RESULT_CANCELED;
+                activity.setResult(result);
+                activity.finish();
+            }
+        }
         return true;
     }
 
     /**
-     * Provides Intent to setting activity for the specified auto-fill service.
+     * Provides Intent to setting activity for the specified autofill service.
      */
     static final class AutofillSettingIntentProvider implements SettingIntentProvider {