Merge "Improvements on ACTION_REQUEST_SET_AUTOFILL_SERVICE:" into oc-dev
diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java
index 951a26b..a9ca458 100644
--- a/src/com/android/settings/DisplaySettings.java
+++ b/src/com/android/settings/DisplaySettings.java
@@ -27,7 +27,6 @@
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.display.AutoBrightnessPreferenceController;
 import com.android.settings.display.AutoRotatePreferenceController;
-import com.android.settings.display.BrightnessLevelPreferenceController;
 import com.android.settings.display.CameraGesturePreferenceController;
 import com.android.settings.display.DozePreferenceController;
 import com.android.settings.display.FontSizePreferenceController;
@@ -107,7 +106,6 @@
         controllers.add(new VrDisplayPreferenceController(context));
         controllers.add(new WallpaperPreferenceController(context));
         controllers.add(new ThemePreferenceController(context));
-        controllers.add(new BrightnessLevelPreferenceController(context));
         return controllers;
     }
 
diff --git a/src/com/android/settings/SetupChooseLockPattern.java b/src/com/android/settings/SetupChooseLockPattern.java
index 30a3c7c..762d3b3 100644
--- a/src/com/android/settings/SetupChooseLockPattern.java
+++ b/src/com/android/settings/SetupChooseLockPattern.java
@@ -75,7 +75,7 @@
         protected Intent getRedactionInterstitialIntent(Context context) {
             // Setup wizard's redaction interstitial is deferred to optional step. Enable that
             // optional step if the lock screen was set up.
-            SetupRedactionInterstitial.setEnabled(context, false);
+            SetupRedactionInterstitial.setEnabled(context, true);
             return null;
         }
     }
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
index 78d6122..342888e 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
@@ -20,9 +20,9 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.provider.Settings;
-import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.Preference;
 import android.text.TextUtils;
 import android.util.Log;
@@ -51,22 +51,17 @@
     private static final String DASHBOARD_TILE_PREF_KEY_PREFIX = "dashboard_tile_pref_";
     private static final String META_DATA_KEY_INTENT_ACTION = "com.android.settings.intent.action";
 
-
     protected final Context mContext;
+
     private final MetricsFeatureProvider mMetricsFeatureProvider;
     private final CategoryManager mCategoryManager;
+    private final PackageManager mPackageManager;
 
     public DashboardFeatureProviderImpl(Context context) {
         mContext = context.getApplicationContext();
         mCategoryManager = CategoryManager.get(context, getExtraIntentAction());
         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
-    }
-
-    @VisibleForTesting
-    DashboardFeatureProviderImpl(Context context, CategoryManager categoryManager) {
-        mContext = context.getApplicationContext();
-        mCategoryManager = categoryManager;
-        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+        mPackageManager = context.getPackageManager();
     }
 
     @Override
@@ -208,6 +203,10 @@
 
     private void launchIntentOrSelectProfile(Activity activity, Tile tile, Intent intent,
             int sourceMetricCategory) {
+        if (!isIntentResolvable(intent)) {
+            Log.w(TAG, "Cannot resolve intent, skipping. " + intent);
+            return;
+        }
         ProfileSelectDialog.updateUserHandlesIfNeeded(mContext, tile);
         if (tile.userHandle == null) {
             mMetricsFeatureProvider.logDashboardStartIntent(mContext, intent, sourceMetricCategory);
@@ -219,4 +218,8 @@
             ProfileSelectDialog.show(activity.getFragmentManager(), tile);
         }
     }
+
+    private boolean isIntentResolvable(Intent intent) {
+        return mPackageManager.resolveActivity(intent, 0) != null;
+    }
 }
diff --git a/src/com/android/settings/display/BrightnessLevelPreferenceController.java b/src/com/android/settings/display/BrightnessLevelPreferenceController.java
deleted file mode 100644
index e0000d6..0000000
--- a/src/com/android/settings/display/BrightnessLevelPreferenceController.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.display;
-
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
-
-import android.content.Context;
-import android.provider.Settings;
-import android.support.v7.preference.Preference;
-import com.android.settings.core.PreferenceController;
-import java.text.NumberFormat;
-
-public class BrightnessLevelPreferenceController extends PreferenceController {
-
-    private static final String KEY_BRIGHTNESS = "brightness";
-
-    public BrightnessLevelPreferenceController(Context context) {
-        super(context);
-    }
-
-    @Override
-    public boolean isAvailable() {
-        return true;
-    }
-
-    @Override
-    public String getPreferenceKey() {
-        return KEY_BRIGHTNESS;
-    }
-
-    @Override
-    public void updateState(Preference preference) {
-        final double brightness = Settings.System.getInt(mContext.getContentResolver(),
-            SCREEN_BRIGHTNESS, 0);
-        preference.setSummary(NumberFormat.getPercentInstance().format(brightness / 255));
-    }
-
-}
diff --git a/tests/robotests/src/com/android/settings/SettingsRobolectricTestRunner.java b/tests/robotests/src/com/android/settings/SettingsRobolectricTestRunner.java
index 668fc88..e063f88 100644
--- a/tests/robotests/src/com/android/settings/SettingsRobolectricTestRunner.java
+++ b/tests/robotests/src/com/android/settings/SettingsRobolectricTestRunner.java
@@ -77,6 +77,22 @@
                         getPackageName(),
                         Fs.fileFromPath("./frameworks/base/core/res/res"),
                         null));
+                paths.add(new ResourcePath(
+                        getPackageName(),
+                        Fs.fileFromPath("./frameworks/opt/setupwizard/library/main/res"),
+                        null));
+                paths.add(new ResourcePath(
+                        getPackageName(),
+                        Fs.fileFromPath("./frameworks/opt/setupwizard/library/gingerbread/res"),
+                        null));
+                paths.add(new ResourcePath(
+                        getPackageName(),
+                        Fs.fileFromPath("./frameworks/opt/setupwizard/library/recyclerview/res"),
+                        null));
+                paths.add(new ResourcePath(
+                        getPackageName(),
+                        Fs.fileFromPath("./frameworks/support/v7/appcompat/res"),
+                        null));
                 return paths;
             }
         };
diff --git a/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java
new file mode 100644
index 0000000..4b78ee7
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/SetupChooseLockPatternTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.robolectric.RuntimeEnvironment.application;
+
+import android.app.Activity;
+import android.content.ComponentName;
+import android.content.pm.PackageManager;
+import android.os.UserHandle;
+
+import com.android.settings.ChooseLockPattern.ChooseLockPatternFragment;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
+import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
+import com.android.settings.testutils.shadow.ShadowEventLogWriter;
+import com.android.settings.testutils.shadow.ShadowUtils;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.res.builder.RobolectricPackageManager.ComponentState;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(
+        manifest = TestConfig.MANIFEST_PATH,
+        sdk = TestConfig.SDK_VERSION,
+        shadows = {
+                SettingsShadowResources.class,
+                SettingsShadowResources.SettingsShadowTheme.class,
+                ShadowDynamicIndexableContentMonitor.class,
+                ShadowEventLogWriter.class,
+                ShadowUtils.class
+        })
+public class SetupChooseLockPatternTest {
+
+    private SetupChooseLockPattern mActivity;
+
+    @Before
+    public void setUp() {
+        RuntimeEnvironment.getRobolectricPackageManager().setComponentEnabledSetting(
+                new ComponentName(application, SetupRedactionInterstitial.class),
+                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+                PackageManager.DONT_KILL_APP);
+
+        mActivity =  Robolectric.buildActivity(
+                SetupChooseLockPattern.class,
+                SetupChooseLockPattern.createIntent(
+                        application,
+                        false, /* requirePassword */
+                        false, /* confirmCredentials */
+                        UserHandle.myUserId()))
+                .setup().get();
+    }
+
+    @Test
+    public void chooseLockSaved_shouldEnableRedactionInterstitial() {
+        findFragment(mActivity).onChosenLockSaveFinished(false, null);
+
+        ComponentState redactionComponentState =
+                RuntimeEnvironment.getRobolectricPackageManager().getComponentState(
+                        new ComponentName(application, SetupRedactionInterstitial.class));
+        assertThat(redactionComponentState.newState).named("Redaction component state")
+                .isEqualTo(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+    }
+
+    private ChooseLockPatternFragment findFragment(Activity activity) {
+        return (ChooseLockPatternFragment)
+                activity.getFragmentManager().findFragmentById(R.id.main_content);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
index 28bed3e..e7587d5 100644
--- a/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/DashboardFeatureProviderImplTest.java
@@ -50,6 +50,7 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.shadows.ShadowActivity;
 import org.robolectric.shadows.ShadowApplication;
+import org.robolectric.util.ReflectionHelpers;
 
 import java.util.ArrayList;
 
@@ -294,8 +295,34 @@
     }
 
     @Test
+    public void clickPreference_withUnresolvableIntent_shouldNotLaunchAnything() {
+        ReflectionHelpers.setField(
+                mImpl, "mPackageManager", RuntimeEnvironment.getPackageManager());
+        Activity activity = Robolectric.buildActivity(Activity.class).get();
+        final ShadowApplication application = ShadowApplication.getInstance();
+        final Preference preference = new Preference(application.getApplicationContext());
+        final Tile tile = new Tile();
+        tile.key = "key";
+        tile.intent = new Intent();
+        tile.intent.setComponent(new ComponentName("pkg", "class"));
+        tile.metaData = new Bundle();
+        tile.metaData.putString("com.android.settings.intent.action", "TestAction");
+        tile.userHandle = null;
+
+        mImpl.bindPreferenceToTile(activity, MetricsProto.MetricsEvent.SETTINGS_GESTURES,
+                preference, tile, "123", Preference.DEFAULT_ORDER);
+        preference.performClick();
+
+        final ShadowActivity.IntentForResult launchIntent =
+                shadowOf(activity).getNextStartedActivityForResult();
+
+        assertThat(launchIntent).isNull();
+    }
+
+    @Test
     public void getPreferences_noCategory_shouldReturnNull() {
-        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
+        mImpl = new DashboardFeatureProviderImpl(mActivity);
+        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
         when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
                 .thenReturn(null);
 
@@ -306,7 +333,8 @@
 
     @Test
     public void getPreferences_noTileForCategory_shouldReturnNull() {
-        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
+        mImpl = new DashboardFeatureProviderImpl(mActivity);
+        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
         when(mCategoryManager.getTilesByCategory(mActivity, CategoryKey.CATEGORY_HOMEPAGE))
                 .thenReturn(new DashboardCategory());
 
@@ -317,7 +345,8 @@
 
     @Test
     public void getPreferences_hasTileForCategory_shouldReturnPrefList() {
-        mImpl = new DashboardFeatureProviderImpl(mActivity, mCategoryManager);
+        mImpl = new DashboardFeatureProviderImpl(mActivity);
+        ReflectionHelpers.setField(mImpl, "mCategoryManager", mCategoryManager);
         final DashboardCategory category = new DashboardCategory();
         category.tiles.add(new Tile());
         when(mCategoryManager
diff --git a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java
deleted file mode 100644
index 025e1ae..0000000
--- a/tests/robotests/src/com/android/settings/display/BrightnessLevelPreferenceControllerTest.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.display;
-
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import android.content.ContentResolver;
-import android.content.Context;
-import android.provider.Settings;
-import android.support.v7.preference.Preference;
-
-import com.android.settings.SettingsRobolectricTestRunner;
-import com.android.settings.TestConfig;
-import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
-
-import java.text.NumberFormat;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.annotation.Config;
-
-@RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
-public class BrightnessLevelPreferenceControllerTest {
-    @Mock
-    private Context mContext;
-    @Mock
-    private ContentResolver mContentResolver;
-    @Mock
-    private Preference mPreference;
-
-    private BrightnessLevelPreferenceController mController;
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-
-        mController = new BrightnessLevelPreferenceController(mContext);
-    }
-
-    @Test
-    public void isAvailable_shouldAlwaysReturnTrue() {
-        assertThat(mController.isAvailable()).isTrue();
-    }
-
-    @Test
-    public void updateState_shouldSetSummary() {
-        final NumberFormat formatter = NumberFormat.getPercentInstance();
-        when(mContext.getContentResolver()).thenReturn(mContentResolver);
-        Settings.System.putInt(mContentResolver, SCREEN_BRIGHTNESS, 45);
-
-        mController.updateState(mPreference);
-
-        verify(mPreference).setSummary(formatter.format(45.0 / 255));
-    }
-
-}
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowUtils.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowUtils.java
new file mode 100644
index 0000000..81cc607
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowUtils.java
@@ -0,0 +1,33 @@
+/*
+ * 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.testutils.shadow;
+
+import android.content.Context;
+
+import com.android.settings.Utils;
+
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
+
+@Implements(Utils.class)
+public class ShadowUtils {
+
+    @Implementation
+    public static int enforceSameOwner(Context context, int userId) {
+        return userId;
+    }
+}