Merge "Add UsbDefaultFragment and tests"
diff --git a/res/layout/instant_app_buttons.xml b/res/layout/instant_app_buttons.xml
index 9c2e915..1ef9f41 100644
--- a/res/layout/instant_app_buttons.xml
+++ b/res/layout/instant_app_buttons.xml
@@ -20,24 +20,39 @@
     android:id="@+id/instant_app_button_container"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:paddingTop="4dp"
-    android:paddingStart="8dp"
-    android:paddingEnd="8dp"
-    android:visibility="gone">
-    <Button
-        android:id="@+id/install"
-        style="@style/ActionPrimaryButton"
-        android:enabled="false"
+    android:gravity="center"
+    android:paddingTop="24dp"
+    android:paddingStart="68dp"
+    android:paddingEnd="24dp"
+    android:orientation="horizontal">
+
+    <FrameLayout
         android:layout_width="0dp"
         android:layout_weight="1"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center"
-        android:text="@string/install_text"/>
+        android:layout_height="wrap_content">
+        <Button
+            android:id="@+id/install"
+            style="@style/ActionPrimaryButton"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginBottom="4dp"
+            android:text="@string/install_text"/>
+        <Button
+            android:id="@+id/launch"
+            style="@style/ActionPrimaryButton"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginBottom="4dp"
+            android:text="@string/launch_instant_app"/>
+    </FrameLayout>
+    <Space
+        android:layout_width="16dp"
+        android:layout_height="wrap_content" />
     <Button
         android:id="@+id/clear_data"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content"
-        android:layout_gravity="center"
+        android:layout_marginBottom="4dp"
         android:text="@string/clear_instant_app_data"/>
 </LinearLayout>
diff --git a/src/com/android/settings/applications/ApplicationFeatureProvider.java b/src/com/android/settings/applications/ApplicationFeatureProvider.java
index eae23d1..3ffacb0 100644
--- a/src/com/android/settings/applications/ApplicationFeatureProvider.java
+++ b/src/com/android/settings/applications/ApplicationFeatureProvider.java
@@ -17,11 +17,7 @@
 package com.android.settings.applications;
 
 import android.annotation.UserIdInt;
-import android.app.Fragment;
 import android.content.Intent;
-import android.view.View;
-
-import com.android.settings.applications.instantapps.InstantAppButtonsController;
 
 import java.util.List;
 import java.util.Set;
@@ -29,13 +25,6 @@
 public interface ApplicationFeatureProvider {
 
     /**
-     * Returns a new {@link InstantAppButtonsController} instance for showing buttons
-     * only relevant to instant apps.
-     */
-    InstantAppButtonsController newInstantAppButtonsController(Fragment fragment,
-            View view, InstantAppButtonsController.ShowDialogDelegate showDialogDelegate);
-
-    /**
      * Calculates the total number of apps installed on the device via policy in the current user
      * and all its managed profiles.
      *
diff --git a/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java b/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java
index 5323cd5..e1f434e 100644
--- a/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java
+++ b/src/com/android/settings/applications/ApplicationFeatureProviderImpl.java
@@ -16,7 +16,6 @@
 
 package com.android.settings.applications;
 
-import android.app.Fragment;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ComponentInfo;
@@ -26,9 +25,7 @@
 import android.os.RemoteException;
 import android.os.UserManager;
 import android.util.ArraySet;
-import android.view.View;
 
-import com.android.settings.applications.instantapps.InstantAppButtonsController;
 import com.android.settings.wrapper.DevicePolicyManagerWrapper;
 import com.android.settings.wrapper.IPackageManagerWrapper;
 import com.android.settingslib.wrapper.PackageManagerWrapper;
@@ -55,12 +52,6 @@
     }
 
     @Override
-    public InstantAppButtonsController newInstantAppButtonsController(Fragment fragment,
-            View view, InstantAppButtonsController.ShowDialogDelegate showDialogDelegate) {
-        return new InstantAppButtonsController(mContext, fragment, view, showDialogDelegate);
-    }
-
-    @Override
     public void calculateNumberOfPolicyInstalledApps(boolean async, NumberOfAppsCallback callback) {
         final CurrentUserAndManagedProfilePolicyInstalledAppCounter counter =
                 new CurrentUserAndManagedProfilePolicyInstalledAppCounter(mContext, mPm, callback);
diff --git a/src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java b/src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java
index 90df547..8c998e9 100755
--- a/src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java
+++ b/src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java
@@ -86,6 +86,7 @@
     @VisibleForTesting static final int UNINSTALL_ALL_USERS_MENU = 1;
     @VisibleForTesting static final int UNINSTALL_UPDATES = 2;
     static final int FORCE_STOP_MENU = 3;
+    static final int INSTALL_INSTANT_APP_MENU = 4;
 
     // Result code identifiers
     @VisibleForTesting
@@ -103,6 +104,7 @@
     static final int DLG_FORCE_STOP = DLG_BASE + 1;
     private static final int DLG_DISABLE = DLG_BASE + 2;
     private static final int DLG_SPECIAL_DISABLE = DLG_BASE + 3;
+    static final int DLG_CLEAR_INSTANT_APP = DLG_BASE + 4;
 
     private static final String KEY_ADVANCED_APP_INFO_CATEGORY = "advanced_app_info";
 
@@ -244,7 +246,7 @@
         // The following are controllers for preferences that don't need to refresh the preference
         // state when app state changes.
         mInstantAppButtonPreferenceController =
-                new InstantAppButtonsPreferenceController(context, this, packageName);
+                new InstantAppButtonsPreferenceController(context, this, packageName, lifecycle);
         controllers.add(mInstantAppButtonPreferenceController);
         controllers.add(new AppBatteryPreferenceController(context, this, packageName, lifecycle));
         controllers.add(new AppMemoryPreferenceController(context, this, lifecycle));
diff --git a/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceController.java b/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceController.java
index b9fe003..dcae5ef 100644
--- a/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceController.java
@@ -18,30 +18,62 @@
 
 import android.app.AlertDialog;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.UserHandle;
 import android.support.annotation.VisibleForTesting;
 import android.support.v7.preference.PreferenceScreen;
+import android.text.TextUtils;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.widget.Button;
 
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
-import com.android.settings.applications.ApplicationFeatureProvider;
+import com.android.settings.applications.AppStoreUtil;
 import com.android.settings.applications.LayoutPreference;
-import com.android.settings.applications.instantapps.InstantAppButtonsController;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.applications.AppUtils;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnCreateOptionsMenu;
+import com.android.settingslib.core.lifecycle.events.OnOptionsItemSelected;
+import com.android.settingslib.core.lifecycle.events.OnPrepareOptionsMenu;
+import com.android.settingslib.wrapper.PackageManagerWrapper;
 
-public class InstantAppButtonsPreferenceController extends BasePreferenceController {
+import java.util.List;
+
+public class InstantAppButtonsPreferenceController extends BasePreferenceController implements
+        LifecycleObserver, OnCreateOptionsMenu, OnPrepareOptionsMenu, OnOptionsItemSelected,
+        DialogInterface.OnClickListener {
 
     private static final String KEY_INSTANT_APP_BUTTONS = "instant_app_buttons";
+    private static final String META_DATA_DEFAULT_URI = "default-url";
 
     private final AppInfoDashboardFragment mParent;
     private final String mPackageName;
-    private InstantAppButtonsController mInstantAppButtonsController;
+    private final PackageManagerWrapper mPackageManagerWrapper;
+    private String mLaunchUri;
+    private LayoutPreference mPreference;
+    private MenuItem mInstallMenu;
 
     public InstantAppButtonsPreferenceController(Context context, AppInfoDashboardFragment parent,
-            String packageName) {
+            String packageName, Lifecycle lifecycle) {
         super(context, KEY_INSTANT_APP_BUTTONS);
         mParent = parent;
         mPackageName = packageName;
+        mPackageManagerWrapper = new PackageManagerWrapper(context.getPackageManager());
+        mLaunchUri = getDefaultLaunchUri();
+        if (lifecycle != null) {
+            lifecycle.addObserver(this);
+        }
     }
 
     @Override
@@ -53,22 +85,98 @@
     @Override
     public void displayPreference(PreferenceScreen screen) {
         super.displayPreference(screen);
-        LayoutPreference buttons =
-                (LayoutPreference) screen.findPreference(KEY_INSTANT_APP_BUTTONS);
-        mInstantAppButtonsController = getApplicationFeatureProvider()
-                .newInstantAppButtonsController(mParent,
-                        buttons.findViewById(R.id.instant_app_button_container),
-                        id -> mParent.showDialogInner(id, 0))
-                .setPackageName(mPackageName)
-                .show();
+        mPreference = (LayoutPreference) screen.findPreference(KEY_INSTANT_APP_BUTTONS);
+        initButtons(mPreference.findViewById(R.id.instant_app_button_container));
     }
 
-    public AlertDialog createDialog(int id) {
-        return mInstantAppButtonsController.createDialog(id);
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        if (!TextUtils.isEmpty(mLaunchUri)) {
+            menu.add(0, AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU, 2, R.string.install_text)
+                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+        }
     }
 
-    @VisibleForTesting
-    ApplicationFeatureProvider getApplicationFeatureProvider() {
-        return FeatureFactory.getFactory(mContext).getApplicationFeatureProvider(mContext);
+    @Override
+    public boolean onOptionsItemSelected(MenuItem menuItem) {
+        if (menuItem.getItemId() == AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU) {
+            final Intent appStoreIntent = AppStoreUtil.getAppStoreLink(mContext, mPackageName);
+            if (appStoreIntent != null) {
+                mParent.startActivity(appStoreIntent);
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public void onPrepareOptionsMenu(Menu menu) {
+        mInstallMenu = menu.findItem(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU);
+        final Intent appStoreIntent = AppStoreUtil.getAppStoreLink(mContext, mPackageName);
+        if (appStoreIntent == null) {
+            mInstallMenu.setEnabled(false);
+        }
+    }
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        FeatureFactory.getFactory(mContext).getMetricsFeatureProvider()
+            .action(mContext, MetricsEvent.ACTION_SETTINGS_CLEAR_INSTANT_APP, mPackageName);
+        mPackageManagerWrapper.deletePackageAsUser(
+            mPackageName, null, 0, UserHandle.myUserId());
+    }
+
+    AlertDialog createDialog(int id) {
+        if (id == AppInfoDashboardFragment.DLG_CLEAR_INSTANT_APP) {
+            AlertDialog confirmDialog = new AlertDialog.Builder(mContext)
+                .setPositiveButton(R.string.clear_instant_app_data, this)
+                .setNegativeButton(R.string.cancel, null)
+                .setTitle(R.string.clear_instant_app_data)
+                .setMessage(mContext.getString(R.string.clear_instant_app_confirmation))
+                .create();
+            return confirmDialog;
+        }
+        return null;
+    }
+
+    private void initButtons(View view) {
+        final Button installButton = view.findViewById(R.id.install);
+        final Button clearDataButton = view.findViewById(R.id.clear_data);
+        final Button launchButton = view.findViewById(R.id.launch);
+        if (!TextUtils.isEmpty(mLaunchUri)) {
+            installButton.setVisibility(View.GONE);
+            final Intent intent = new Intent(Intent.ACTION_VIEW);
+            intent.setData(Uri.parse(mLaunchUri));
+            launchButton.setOnClickListener(v -> mParent.startActivity(intent));
+        } else {
+            launchButton.setVisibility(View.GONE);
+            final Intent appStoreIntent = AppStoreUtil.getAppStoreLink(mContext, mPackageName);
+            if (appStoreIntent != null) {
+                installButton.setOnClickListener(v -> mParent.startActivity(appStoreIntent));
+            } else {
+                installButton.setEnabled(false);
+            }
+        }
+        clearDataButton.setOnClickListener(
+            v -> mParent.showDialogInner(mParent.DLG_CLEAR_INSTANT_APP, 0));
+    }
+
+    private String getDefaultLaunchUri() {
+        final PackageManager manager = mContext.getPackageManager();
+        final Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.addCategory(Intent.CATEGORY_LAUNCHER);
+        intent.setPackage(mPackageName);
+        final List<ResolveInfo> infos = manager.queryIntentActivities(
+            intent, PackageManager.GET_META_DATA | PackageManager.MATCH_INSTANT);
+        for (ResolveInfo info : infos) {
+            final Bundle metaData = info.activityInfo.metaData;
+            if (metaData != null) {
+                final String launchUri = metaData.getString(META_DATA_DEFAULT_URI);
+                if (!TextUtils.isEmpty(launchUri)) {
+                    return launchUri;
+                }
+            }
+        }
+        return null;
     }
 }
diff --git a/src/com/android/settings/applications/instantapps/InstantAppButtonsController.java b/src/com/android/settings/applications/instantapps/InstantAppButtonsController.java
deleted file mode 100644
index 42474a8..0000000
--- a/src/com/android/settings/applications/instantapps/InstantAppButtonsController.java
+++ /dev/null
@@ -1,113 +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.applications.instantapps;
-
-import android.app.AlertDialog;
-import android.app.Fragment;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.Intent;
-import android.os.UserHandle;
-import android.view.View;
-import android.widget.Button;
-
-import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
-import com.android.settings.R;
-import com.android.settings.applications.AppStoreUtil;
-import com.android.settings.overlay.FeatureFactory;
-import com.android.settingslib.wrapper.PackageManagerWrapper;
-
-/** Encapsulates a container for buttons relevant to instant apps */
-public class InstantAppButtonsController implements DialogInterface.OnClickListener {
-
-    public interface ShowDialogDelegate {
-        /**
-         * Delegate that should be called when this controller wants to show a dialog.
-         */
-        void showDialog(int id);
-    }
-
-    private final Context mContext;
-    private final Fragment mFragment;
-    private final View mView;
-    private final PackageManagerWrapper mPackageManagerWrapper;
-    private final ShowDialogDelegate mShowDialogDelegate;
-    private String mPackageName;
-
-    public static final int DLG_BASE = 0x5032;
-    public static final int DLG_CLEAR_APP = DLG_BASE + 1;
-
-    public InstantAppButtonsController(
-            Context context,
-            Fragment fragment,
-            View view,
-            ShowDialogDelegate showDialogDelegate) {
-      mContext = context;
-      mFragment = fragment;
-      mView = view;
-      mShowDialogDelegate = showDialogDelegate;
-      mPackageManagerWrapper = new PackageManagerWrapper(context.getPackageManager());
-    }
-
-    public InstantAppButtonsController setPackageName(String packageName) {
-        mPackageName = packageName;
-        return this;
-    }
-
-    public void bindButtons() {
-        Button installButton = (Button)mView.findViewById(R.id.install);
-        Button clearDataButton = (Button)mView.findViewById(R.id.clear_data);
-        Intent appStoreIntent = AppStoreUtil.getAppStoreLink(mContext, mPackageName);
-        if (appStoreIntent != null) {
-            installButton.setEnabled(true);
-            installButton.setOnClickListener(v -> mFragment.startActivity(appStoreIntent));
-        }
-
-        clearDataButton.setOnClickListener(v -> mShowDialogDelegate.showDialog(DLG_CLEAR_APP));
-    }
-
-    public AlertDialog createDialog(int id) {
-        if (id == DLG_CLEAR_APP) {
-            AlertDialog dialog = new AlertDialog.Builder(mFragment.getActivity())
-                    .setPositiveButton(R.string.clear_instant_app_data, this)
-                    .setNegativeButton(R.string.cancel, null)
-                    .setTitle(R.string.clear_instant_app_data)
-                    .setMessage(mContext.getString(R.string.clear_instant_app_confirmation))
-                    .create();
-            return dialog;
-        }
-        return null;
-    }
-
-    public void onClick(DialogInterface dialog, int which) {
-        if (which == DialogInterface.BUTTON_POSITIVE) {
-            FeatureFactory.getFactory(mContext)
-                    .getMetricsFeatureProvider()
-                    .action(mContext,
-                            MetricsEvent.ACTION_SETTINGS_CLEAR_INSTANT_APP,
-                            mPackageName);
-            mPackageManagerWrapper.deletePackageAsUser(
-                    mPackageName, null, 0, UserHandle.myUserId());
-        }
-    }
-
-    public InstantAppButtonsController show() {
-        bindButtons();
-        mView.setVisibility(View.VISIBLE);
-        return this;
-    }
-}
diff --git a/src/com/android/settings/core/BasePreferenceController.java b/src/com/android/settings/core/BasePreferenceController.java
index 777f3dd..1bbee0c 100644
--- a/src/com/android/settings/core/BasePreferenceController.java
+++ b/src/com/android/settings/core/BasePreferenceController.java
@@ -25,6 +25,8 @@
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.List;
 
 /**
@@ -73,6 +75,44 @@
 
     protected Lifecycle mLifecycle;
 
+    /**
+     * Instantiate a controller as specified controller type and user-defined key.
+     * <p/>
+     * This is done through reflection. Do not use this method unless you know what you are doing.
+     */
+    public static BasePreferenceController createInstance(Context context,
+            String controllerName, String key) {
+        try {
+            final Class<?> clazz = Class.forName(controllerName);
+            final Constructor<?> preferenceConstructor =
+                    clazz.getConstructor(Context.class, String.class);
+            final Object[] params = new Object[] {context, key};
+            return (BasePreferenceController) preferenceConstructor.newInstance(params);
+        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
+                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
+            throw new IllegalStateException(
+                    "Invalid preference controller: " + controllerName, e);
+        }
+    }
+
+    /**
+     * Instantiate a controller as specified controller type.
+     * <p/>
+     * This is done through reflection. Do not use this method unless you know what you are doing.
+     */
+    public static BasePreferenceController createInstance(Context context, String controllerName) {
+        try {
+            final Class<?> clazz = Class.forName(controllerName);
+            final Constructor<?> preferenceConstructor = clazz.getConstructor(Context.class);
+            final Object[] params = new Object[] {context};
+            return (BasePreferenceController) preferenceConstructor.newInstance(params);
+        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
+                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
+            throw new IllegalStateException(
+                    "Invalid preference controller: " + controllerName, e);
+        }
+    }
+
     public BasePreferenceController(Context context, String preferenceKey) {
         super(context);
         mPreferenceKey = preferenceKey;
diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java
index 6a88a38..a4f9a6b 100644
--- a/src/com/android/settings/dashboard/DashboardFragment.java
+++ b/src/com/android/settings/dashboard/DashboardFragment.java
@@ -27,9 +27,6 @@
 import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.Log;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
 
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.overlay.FeatureFactory;
@@ -58,7 +55,7 @@
             new ArrayMap<>();
     private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();
 
-    protected DashboardFeatureProvider mDashboardFeatureProvider;
+    private DashboardFeatureProvider mDashboardFeatureProvider;
     private DashboardTilePlaceholderPreferenceController mPlaceholderPreferenceController;
     private boolean mListeningToCategoryChange;
     private SummaryLoader mSummaryLoader;
@@ -95,13 +92,6 @@
     }
 
     @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,
-            Bundle savedInstanceState) {
-        final View view = super.onCreateView(inflater, container, savedInstanceState);
-        return view;
-    }
-
-    @Override
     public void onCategoriesChanged() {
         final DashboardCategory category =
                 mDashboardFeatureProvider.getTilesForCategory(getCategoryKey());
diff --git a/src/com/android/settings/dashboard/conditional/WorkModeCondition.java b/src/com/android/settings/dashboard/conditional/WorkModeCondition.java
index 5c47be6..cb1b60a 100644
--- a/src/com/android/settings/dashboard/conditional/WorkModeCondition.java
+++ b/src/com/android/settings/dashboard/conditional/WorkModeCondition.java
@@ -85,7 +85,7 @@
     @Override
     public void onPrimaryClick() {
         mManager.getContext().startActivity(new Intent(mManager.getContext(),
-                Settings.UserSettingsActivity.class)
+                Settings.AccountDashboardActivity.class)
                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
     }
 
diff --git a/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceController.java b/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceController.java
index 4d3dd31..bf8cd07 100644
--- a/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceController.java
@@ -29,13 +29,15 @@
  */
 public class AutoBatterySaverPreferenceController extends TogglePreferenceController implements
         Preference.OnPreferenceChangeListener {
-    private static final int LOW_POWER_MODE_TRIGGER_THRESHOLD = 15;
+    private final int mDefWarnLevel;
 
     @VisibleForTesting
     static final String KEY_AUTO_BATTERY_SAVER = "auto_battery_saver";
 
     public AutoBatterySaverPreferenceController(Context context) {
         super(context, KEY_AUTO_BATTERY_SAVER);
+        mDefWarnLevel = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_lowBatteryWarningLevel);
     }
 
     @Override
@@ -46,7 +48,7 @@
     @Override
     public boolean isChecked() {
         return Settings.Global.getInt(mContext.getContentResolver(),
-                Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0) != 0;
+                Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, mDefWarnLevel) != 0;
     }
 
     @Override
@@ -54,7 +56,7 @@
         Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL,
                 isChecked
-                        ? LOW_POWER_MODE_TRIGGER_THRESHOLD
+                        ? mDefWarnLevel
                         : 0);
         return true;
     }
diff --git a/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceController.java b/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceController.java
index 1cc72a7..05b3503 100644
--- a/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceController.java
@@ -42,12 +42,15 @@
         LifecycleObserver, OnStart, OnStop, SeekBarPreference.OnPreferenceChangeListener {
     @VisibleForTesting
     static final String KEY_AUTO_BATTERY_SEEK_BAR = "battery_saver_seek_bar";
+    private final int mDefWarnLevel;
     private SeekBarPreference mPreference;
     private AutoBatterySaverSettingObserver mContentObserver;
 
     public AutoBatterySeekBarPreferenceController(Context context, Lifecycle lifecycle) {
         super(context, KEY_AUTO_BATTERY_SEEK_BAR);
         mContentObserver = new AutoBatterySaverSettingObserver(new Handler(Looper.getMainLooper()));
+        mDefWarnLevel = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_lowBatteryWarningLevel);
         if (lifecycle != null) {
             lifecycle.addObserver(this);
         }
@@ -94,7 +97,7 @@
     void updatePreference(Preference preference) {
         final ContentResolver contentResolver = mContext.getContentResolver();
         final int level = Settings.Global.getInt(contentResolver,
-                Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0);
+                Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, mDefWarnLevel);
         if (level == 0) {
             preference.setVisible(false);
         } else {
diff --git a/src/com/android/settings/slices/SliceBuilderUtils.java b/src/com/android/settings/slices/SliceBuilderUtils.java
index 11ff1c1..a01ea1b 100644
--- a/src/com/android/settings/slices/SliceBuilderUtils.java
+++ b/src/com/android/settings/slices/SliceBuilderUtils.java
@@ -33,9 +33,6 @@
 import com.android.settings.search.DatabaseIndexingUtils;
 import com.android.settingslib.core.AbstractPreferenceController;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-
 import androidx.app.slice.Slice;
 import androidx.app.slice.builders.ListBuilder;
 import androidx.app.slice.builders.ListBuilder.RowBuilder;
@@ -87,46 +84,16 @@
     public static BasePreferenceController getPreferenceController(Context context,
             SliceData sliceData) {
         try {
-            return getController(context, sliceData, true /* isContextOnly */);
+            return BasePreferenceController.createInstance(context,
+                    sliceData.getPreferenceController());
         } catch (IllegalStateException e) {
             // Do nothing
             Log.d(TAG, "Could not find Context-only controller for preference controller: "
                     + sliceData.getKey());
         }
 
-        return getController(context, sliceData, false /* isContextOnly */);
-    }
-
-    /**
-     * Attempts to build a {@link BasePreferenceController} from {@param SliceData}.
-     *
-     * @param sliceData     Backing data for the Slice.
-     * @param contextOnlyCtor {@code true} when the constructor for the
-     *                      {@link BasePreferenceController}
-     *                      only takes a {@link Context}. Else the constructor will be ({@link
-     *                      Context}, {@code String}.
-     */
-    private static BasePreferenceController getController(Context context, SliceData sliceData,
-            boolean contextOnlyCtor) {
-        try {
-            Class<?> clazz = Class.forName(sliceData.getPreferenceController());
-            Constructor<?> preferenceConstructor;
-            Object[] params;
-
-            if (contextOnlyCtor) {
-                preferenceConstructor = clazz.getConstructor(Context.class);
-                params = new Object[]{context};
-            } else {
-                preferenceConstructor = clazz.getConstructor(Context.class, String.class);
-                params = new Object[]{context, sliceData.getKey()};
-            }
-
-            return (BasePreferenceController) preferenceConstructor.newInstance(params);
-        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
-                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
-            throw new IllegalStateException(
-                    "Invalid preference controller: " + sliceData.getPreferenceController(), e);
-        }
+        return BasePreferenceController.createInstance(context, sliceData.getPreferenceController(),
+                sliceData.getKey());
     }
 
     private static void addToggleAction(Context context, RowBuilder builder, boolean isChecked,
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 9fbeabe..3972b85 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -353,32 +353,6 @@
     }
 
     /**
-     * Only update the AP list if there are not any APs currently shown.
-     *
-     * <p>Thus forceUpdate will only be called during cold start or when toggling between wifi on
-     * and off. In other use cases, the previous APs will remain until the next update is received
-     * from {@link WifiTracker}.
-     */
-    private void conditionallyForceUpdateAPs() {
-        if (mAccessPointsPreferenceCategory.getPreferenceCount() > 0
-                && mAccessPointsPreferenceCategory.getPreference(0) instanceof
-                        AccessPointPreference) {
-            // Make sure we don't update due to callbacks initiated by sticky broadcasts in
-            // WifiTracker.
-            Log.d(TAG, "Did not force update APs due to existing APs displayed");
-            getView().removeCallbacks(mUpdateAccessPointsRunnable);
-            return;
-        }
-        setProgressBarVisible(true);
-        mWifiTracker.forceUpdate();
-        if (isVerboseLoggingEnabled()) {
-            Log.i(TAG, "WifiSettings force update APs: " + mWifiTracker.getAccessPoints());
-        }
-        getView().removeCallbacks(mUpdateAccessPointsRunnable);
-        updateAccessPointPreferences();
-    }
-
-    /**
      * @return new WifiEnabler or null (as overridden by WifiSettingsForSetupWizard)
      */
     private WifiEnabler createWifiEnabler() {
@@ -682,7 +656,7 @@
         final int wifiState = mWifiManager.getWifiState();
         switch (wifiState) {
             case WifiManager.WIFI_STATE_ENABLED:
-                conditionallyForceUpdateAPs();
+                updateAccessPointPreferences();
                 break;
 
             case WifiManager.WIFI_STATE_ENABLING:
diff --git a/tests/robotests/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceControllerTest.java
index eb8a082..935389c 100644
--- a/tests/robotests/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/applications/appinfo/InstantAppButtonsPreferenceControllerTest.java
@@ -18,28 +18,40 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import android.app.AlertDialog;
-import android.app.Fragment;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.support.v7.preference.PreferenceManager;
 import android.support.v7.preference.PreferenceScreen;
+import android.text.TextUtils;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.view.View;
+import android.widget.Button;
 
+import com.android.settings.R;
 import com.android.settings.TestConfig;
 import com.android.settings.applications.LayoutPreference;
-import com.android.settings.applications.instantapps.InstantAppButtonsController;
-import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settingslib.applications.AppUtils;
 import com.android.settingslib.applications.instantapps.InstantAppDataProvider;
+import com.android.settingslib.wrapper.PackageManagerWrapper;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -54,27 +66,48 @@
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class InstantAppButtonsPreferenceControllerTest {
 
+    private static final String TEST_INSTALLER_PACKAGE_NAME = "com.installer";
+    private static final String TEST_INSTALLER_ACTIVITY_NAME = "com.installer.InstallerActivity";
+    private static final String TEST_AIA_PACKAGE_NAME = "test.aia.package";
+
     @Mock
     private PackageManager mPackageManager;
     @Mock
     private ApplicationInfo mAppInfo;
     @Mock
     private AppInfoDashboardFragment mFragment;
+    @Mock
+    private LayoutPreference mPreference;
 
     private Context mContext;
+    private PreferenceScreen mScreen;
+    private PreferenceManager mPreferenceManager;
+    private Button mLaunchButton;
+    private Button mInstallButton;
+    private Button mClearAppButton;
     private InstantAppButtonsPreferenceController mController;
-    private FakeFeatureFactory mFeatureFactory;
 
     @Before
     public void setUp() throws PackageManager.NameNotFoundException {
         MockitoAnnotations.initMocks(this);
-        mFeatureFactory = FakeFeatureFactory.setupForTest();
         mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getPackageManager()).thenReturn(mPackageManager);
         final PackageInfo packageInfo = mock(PackageInfo.class);
         packageInfo.applicationInfo = mAppInfo;
         when(mFragment.getPackageInfo()).thenReturn(packageInfo);
-        mController =
-                spy(new InstantAppButtonsPreferenceController(mContext, mFragment, "Package1"));
+        mPreferenceManager = new PreferenceManager(mContext);
+        mScreen = mPreferenceManager.createPreferenceScreen(mContext);
+        when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
+        final View buttons = View.inflate(
+            RuntimeEnvironment.application, R.layout.instant_app_buttons, null /* parent */);
+        mLaunchButton = buttons.findViewById(R.id.launch);
+        mInstallButton = buttons.findViewById(R.id.install);
+        mClearAppButton = buttons.findViewById(R.id.clear_data);
+        mController = spy(new InstantAppButtonsPreferenceController(
+            mContext, mFragment, TEST_AIA_PACKAGE_NAME, null /* lifecycle */));
+        when(mPreference.getKey()).thenReturn("instant_app_buttons");
+        mScreen.addPreference(mPreference);
+        when(mPreference.findViewById(R.id.instant_app_button_container)).thenReturn(buttons);
     }
 
     @Test
@@ -94,39 +127,164 @@
     }
 
     @Test
-    public void displayPreference_shouldSetPreferenceTitle() {
-        final PreferenceScreen screen = mock(PreferenceScreen.class);
-        final LayoutPreference preference = mock(LayoutPreference.class);
-        when(screen.findPreference(mController.getPreferenceKey())).thenReturn(preference);
-        when(mController.getApplicationFeatureProvider())
-                .thenReturn(mFeatureFactory.applicationFeatureProvider);
-        final InstantAppButtonsController buttonsController =
-                mock(InstantAppButtonsController.class);
-        when(buttonsController.setPackageName(nullable(String.class)))
-                .thenReturn(buttonsController);
-        when(mFeatureFactory.applicationFeatureProvider.newInstantAppButtonsController(
-                nullable(Fragment.class), nullable(View.class),
-                nullable(InstantAppButtonsController.ShowDialogDelegate.class)))
-                .thenReturn(buttonsController);
+    public void onCreateOptionsMenu_noLaunchUri_shouldNotAddInstallInstantAppMenu() {
+        final Menu menu = mock(Menu.class);
+        when(menu.add(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(mock(MenuItem.class));
 
-        mController.displayPreference(screen);
+        mController.onCreateOptionsMenu(menu, null /* inflater */);
 
-        verify(buttonsController).setPackageName(nullable(String.class));
-        verify(buttonsController).show();
+        verify(menu, never()).add(anyInt(), eq(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU),
+            anyInt(), eq(R.string.install_text));
     }
 
     @Test
-    public void createDialog_shouldReturnDialogFromButtonController() {
-        final InstantAppButtonsController buttonsController =
-                mock(InstantAppButtonsController.class);
-        ReflectionHelpers.setField(
-                mController, "mInstantAppButtonsController", buttonsController);
-        final AlertDialog mockDialog = mock(AlertDialog.class);
-        when(buttonsController.createDialog(InstantAppButtonsController.DLG_CLEAR_APP))
-                .thenReturn(mockDialog);
+    public void onCreateOptionsMenu_hasLaunchUri_shouldAddForceStop() {
+        ReflectionHelpers.setField(mController, "mLaunchUri", "www.test.launch");
+        final Menu menu = mock(Menu.class);
+        when(menu.add(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(mock(MenuItem.class));
 
-        assertThat(mController.createDialog(InstantAppButtonsController.DLG_CLEAR_APP))
-                .isEqualTo(mockDialog);
+        mController.onCreateOptionsMenu(menu, null /* inflater */);
+
+        verify(menu).add(anyInt(), eq(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU),
+            anyInt(), eq(R.string.install_text));
+    }
+
+    @Test
+    public void onPrepareOptionsMenu_noAppStoreLink_shoulDisableInstallInstantAppMenu() {
+        ReflectionHelpers.setField(mController, "mLaunchUri", "www.test.launch");
+        final Menu menu = mock(Menu.class);
+        final MenuItem menuItem = mock(MenuItem.class);
+        when(menu.findItem(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU)).thenReturn(menuItem);
+
+        mController.onPrepareOptionsMenu(menu);
+
+        verify(menuItem).setEnabled(false);
+    }
+
+    @Test
+    public void onPrepareOptionsMenu_hasAppStoreLink_shoulNotDisableInstallInstantAppMenu() {
+        ReflectionHelpers.setField(mController, "mLaunchUri", "www.test.launch");
+        final ResolveInfo resolveInfo = mock(ResolveInfo.class);
+        final ActivityInfo activityInfo = mock(ActivityInfo.class);
+        resolveInfo.activityInfo = activityInfo;
+        activityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
+        activityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
+        when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
+        final Menu menu = mock(Menu.class);
+        final MenuItem menuItem = mock(MenuItem.class);
+        when(menu.findItem(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU)).thenReturn(menuItem);
+
+        mController.onPrepareOptionsMenu(menu);
+
+        verify(menuItem, never()).setEnabled(false);
+    }
+
+    @Test
+    public void onOptionsItemSelected_shouldOpenAppStore() {
+        final ResolveInfo resolveInfo = mock(ResolveInfo.class);
+        final ActivityInfo activityInfo = mock(ActivityInfo.class);
+        resolveInfo.activityInfo = activityInfo;
+        activityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
+        activityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
+        when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
+        mController.displayPreference(mScreen);
+        final ComponentName componentName =
+            new ComponentName(TEST_INSTALLER_PACKAGE_NAME, TEST_INSTALLER_ACTIVITY_NAME);
+        final MenuItem menu = mock(MenuItem.class);
+        when(menu.getItemId()).thenReturn(AppInfoDashboardFragment.INSTALL_INSTANT_APP_MENU);
+
+        mController.onOptionsItemSelected(menu);
+
+        verify(mFragment).startActivity(argThat(intent-> intent != null
+            && intent.getAction().equals(Intent.ACTION_SHOW_APP_INFO)
+            && intent.getComponent().equals(componentName)));
+    }
+
+    @Test
+    public void displayPreference_noLaunchUri_shouldShowHideLaunchButton() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mLaunchButton.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void displayPreference_hasLaunchUri_shouldShowHideInstallButton() {
+        ReflectionHelpers.setField(mController, "mLaunchUri", "www.test.launch");
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mInstallButton.getVisibility()).isEqualTo(View.GONE);
+    }
+
+    @Test
+    public void displayPreference_noAppStoreLink_shoulDisableInstallButton() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mInstallButton.isEnabled()).isFalse();
+    }
+
+    @Test
+    public void displayPreference_hasAppStoreLink_shoulSetClickListenerForInstallButton() {
+        final ResolveInfo resolveInfo = mock(ResolveInfo.class);
+        final ActivityInfo activityInfo = mock(ActivityInfo.class);
+        resolveInfo.activityInfo = activityInfo;
+        activityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
+        activityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
+        when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mInstallButton.hasOnClickListeners()).isTrue();
+    }
+
+    @Test
+    public void displayPreference_shoulSetClickListenerForClearButton() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mClearAppButton.hasOnClickListeners()).isTrue();
+    }
+
+    @Test
+    public void clickLaunchButton_shouldLaunchViewIntent() {
+        final String launchUri = "www.test.launch";
+        ReflectionHelpers.setField(mController, "mLaunchUri", launchUri);
+        mController.displayPreference(mScreen);
+
+        mLaunchButton.callOnClick();
+
+        verify(mFragment).startActivity(argThat(intent-> intent != null
+            && intent.getAction().equals(Intent.ACTION_VIEW)
+            && TextUtils.equals(intent.getDataString(), launchUri)));
+    }
+
+    @Test
+    public void clickInstallButton_shouldOpenAppStore() {
+        final ResolveInfo resolveInfo = mock(ResolveInfo.class);
+        final ActivityInfo activityInfo = mock(ActivityInfo.class);
+        resolveInfo.activityInfo = activityInfo;
+        activityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
+        activityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
+        when(mPackageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
+        mController.displayPreference(mScreen);
+        final ComponentName componentName =
+            new ComponentName(TEST_INSTALLER_PACKAGE_NAME, TEST_INSTALLER_ACTIVITY_NAME);
+
+        mInstallButton.callOnClick();
+
+        verify(mFragment).startActivity(argThat(intent-> intent != null
+            && intent.getAction().equals(Intent.ACTION_SHOW_APP_INFO)
+            && intent.getComponent().equals(componentName)));
+    }
+
+    @Test
+    public void onClick_shouldDeleteApp() {
+        PackageManagerWrapper packageManagerWrapper = mock(PackageManagerWrapper.class);
+        ReflectionHelpers.setField(mController, "mPackageManagerWrapper", packageManagerWrapper);
+
+        mController.onClick(mock(DialogInterface.class), DialogInterface.BUTTON_POSITIVE);
+
+        verify(packageManagerWrapper)
+            .deletePackageAsUser(eq(TEST_AIA_PACKAGE_NAME), any(), anyInt(),anyInt());
     }
 
 }
diff --git a/tests/robotests/src/com/android/settings/applications/instantapps/InstantAppButtonsControllerTest.java b/tests/robotests/src/com/android/settings/applications/instantapps/InstantAppButtonsControllerTest.java
deleted file mode 100644
index f85d43a..0000000
--- a/tests/robotests/src/com/android/settings/applications/instantapps/InstantAppButtonsControllerTest.java
+++ /dev/null
@@ -1,181 +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.applications.instantapps;
-
-import static com.android.settings.applications.instantapps.InstantAppButtonsController
-        .ShowDialogDelegate;
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
-
-import android.annotation.SuppressLint;
-import android.app.Fragment;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.DialogInterface;
-import android.content.Intent;
-import android.content.pm.ActivityInfo;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
-import android.view.View;
-import android.widget.Button;
-
-import com.android.settings.R;
-import com.android.settings.TestConfig;
-import com.android.settings.testutils.FakeFeatureFactory;
-import com.android.settings.testutils.SettingsRobolectricTestRunner;
-import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
-import com.android.settingslib.wrapper.PackageManagerWrapper;
-
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.robolectric.annotation.Config;
-import org.robolectric.util.ReflectionHelpers;
-
-/** Tests for the InstantAppButtonsController. */
-@RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = 23)
-public class InstantAppButtonsControllerTest {
-
-    private static final String TEST_INSTALLER_PACKAGE_NAME = "com.installer";
-    private static final String TEST_INSTALLER_ACTIVITY_NAME = "com.installer.InstallerActivity";
-    private static final String TEST_AIA_PACKAGE_NAME = "test.aia.package";
-    private static ComponentName sTestInstallerComponent;
-
-    @BeforeClass
-    public static void beforeClass() {
-        sTestInstallerComponent =
-                new ComponentName(
-                        TEST_INSTALLER_PACKAGE_NAME,
-                        TEST_INSTALLER_ACTIVITY_NAME);
-    }
-
-    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
-    Context mockContext;
-    @Mock
-    PackageManager mockPackageManager;
-    @Mock
-    PackageManagerWrapper mockPackageManagerWrapper;
-    @Mock
-    View mockView;
-    @Mock
-    ShowDialogDelegate mockShowDialogDelegate;
-    @Mock
-    Button mockInstallButton;
-    @Mock
-    Button mockClearButton;
-    @Mock
-    MetricsFeatureProvider mockMetricsFeatureProvider;
-    @Mock
-    ResolveInfo mockResolveInfo;
-    @Mock
-    ActivityInfo mockActivityInfo;
-
-    private PackageManager stubPackageManager;
-
-    private FakeFeatureFactory fakeFeatureFactory;
-    private TestFragment testFragment;
-    private InstantAppButtonsController controller;
-
-
-    private View.OnClickListener receivedListener;
-
-    @Before
-    public void init() {
-        MockitoAnnotations.initMocks(this);
-        testFragment = new TestFragment();
-        when(mockView.findViewById(R.id.install)).thenReturn(mockInstallButton);
-        when(mockView.findViewById(R.id.clear_data)).thenReturn(mockClearButton);
-        mockResolveInfo.activityInfo = mockActivityInfo;
-        mockActivityInfo.packageName = TEST_INSTALLER_PACKAGE_NAME;
-        mockActivityInfo.name = TEST_INSTALLER_ACTIVITY_NAME;
-        when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
-        when(mockPackageManager.resolveActivity(any(), anyInt())).thenReturn(mockResolveInfo);
-        controller = new InstantAppButtonsController(
-                mockContext, testFragment, mockView, mockShowDialogDelegate);
-        controller.setPackageName(TEST_AIA_PACKAGE_NAME);
-        ReflectionHelpers.setField(
-                controller, "mPackageManagerWrapper", mockPackageManagerWrapper);
-        FakeFeatureFactory.setupForTest();
-    }
-
-    @Test
-    public void testInstallListenerTriggersInstall() {
-        doAnswer(invocation -> {
-            receivedListener = (View.OnClickListener) invocation.getArguments()[0];
-            return null;
-        }).when(mockInstallButton).setOnClickListener(any());
-        controller.bindButtons();
-
-        assertThat(receivedListener).isNotNull();
-        receivedListener.onClick(mockInstallButton);
-        assertThat(testFragment.getStartActivityIntent()).isNotNull();
-        assertThat(testFragment.getStartActivityIntent().getComponent())
-                .isEqualTo(sTestInstallerComponent);
-    }
-
-    @Test
-    public void testClearListenerShowsDialog() {
-        doAnswer(invocation -> {
-            receivedListener = (View.OnClickListener) invocation.getArguments()[0];
-            return null;
-        }).when(mockClearButton).setOnClickListener(any());
-        controller.bindButtons();
-        assertThat(receivedListener).isNotNull();
-        receivedListener.onClick(mockClearButton);
-        verify(mockShowDialogDelegate).showDialog(InstantAppButtonsController.DLG_CLEAR_APP);
-    }
-
-    @Test
-    public void testDialogInterfaceOnClick_positiveClearsApp() {
-        controller.onClick(mock(DialogInterface.class), DialogInterface.BUTTON_POSITIVE);
-        verify(mockPackageManagerWrapper)
-                .deletePackageAsUser(eq(TEST_AIA_PACKAGE_NAME), any(), anyInt(),anyInt());
-    }
-
-    @Test
-    public void testDialogInterfaceOnClick_nonPositiveDoesNothing() {
-        controller.onClick(mock(DialogInterface.class), DialogInterface.BUTTON_NEGATIVE);
-        controller.onClick(mock(DialogInterface.class), DialogInterface.BUTTON_NEUTRAL);
-        verifyZeroInteractions(mockPackageManagerWrapper);
-    }
-    @SuppressLint("ValidFragment")
-    private class TestFragment extends Fragment {
-
-        private Intent startActivityIntent;
-
-        public Intent getStartActivityIntent() {
-            return startActivityIntent;
-        }
-
-        @Override
-        public void startActivity(Intent intent) {
-            startActivityIntent = intent;
-        }
-    }
-}
diff --git a/tests/robotests/src/com/android/settings/dashboard/conditional/WorkModeConditionTest.java b/tests/robotests/src/com/android/settings/dashboard/conditional/WorkModeConditionTest.java
new file mode 100644
index 0000000..dff7700
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/dashboard/conditional/WorkModeConditionTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018 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.dashboard.conditional;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.ComponentName;
+import android.content.Context;
+
+import com.android.settings.Settings;
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WorkModeConditionTest {
+
+    @Mock
+    private ConditionManager mConditionManager;
+
+    private Context mContext;
+    private WorkModeCondition mCondition;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        FakeFeatureFactory.setupForTest();
+        when(mConditionManager.getContext()).thenReturn(mContext);
+        mCondition = new WorkModeCondition(mConditionManager);
+    }
+
+    @Test
+    public void onPrimaryClick_shouldLaunchAccountsSetting() {
+        final ComponentName componentName =
+            new ComponentName(mContext, Settings.AccountDashboardActivity.class);
+
+        mCondition.onPrimaryClick();
+
+        verify(mContext).startActivity(
+            argThat(intent-> intent.getComponent().equals(componentName)));
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java
index cabcdcf..af0f855 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySaverPreferenceControllerTest.java
@@ -25,6 +25,7 @@
 
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -34,7 +35,8 @@
 import org.robolectric.annotation.Config;
 
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows =
+        SettingsShadowResources.class)
 public class AutoBatterySaverPreferenceControllerTest {
 
     private AutoBatterySaverPreferenceController mController;
@@ -45,6 +47,8 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
 
+        SettingsShadowResources.overrideResource(
+                com.android.internal.R.integer.config_lowBatteryWarningLevel, 15);
         mContext = RuntimeEnvironment.application;
         mPreference = new SwitchPreference(mContext);
         mController = new AutoBatterySaverPreferenceController(mContext);
@@ -84,4 +88,9 @@
                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0)).isEqualTo(0);
     }
 
+    @Test
+    public void testIsChecked_useDefaultValue_returnTrue() {
+        assertThat(mController.isChecked()).isTrue();
+    }
+
 }
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java
index 32a4fac..d3c2752 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterysaver/AutoBatterySeekBarPreferenceControllerTest.java
@@ -24,6 +24,7 @@
 
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
 import com.android.settings.widget.SeekBarPreference;
 import com.android.settingslib.core.lifecycle.Lifecycle;
 
@@ -35,9 +36,11 @@
 import org.robolectric.annotation.Config;
 
 @RunWith(SettingsRobolectricTestRunner.class)
-@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, shadows =
+        SettingsShadowResources.class)
 public class AutoBatterySeekBarPreferenceControllerTest {
-    private static final int TRIGGER_LEVEL = 15;
+    private static final int TRIGGER_LEVEL = 20;
+    private static final int DEFAULT_LEVEL = 15;
 
     private AutoBatterySeekBarPreferenceController mController;
     private Context mContext;
@@ -51,6 +54,8 @@
         mLifecycleOwner = () -> mLifecycle;
         mLifecycle = new Lifecycle(mLifecycleOwner);
 
+        SettingsShadowResources.overrideResource(
+                com.android.internal.R.integer.config_lowBatteryWarningLevel, DEFAULT_LEVEL);
         mContext = RuntimeEnvironment.application;
         mPreference = new SeekBarPreference(mContext);
         mPreference.setMax(100);
@@ -68,13 +73,21 @@
     }
 
     @Test
+    public void testPreference_defaultValue_preferenceVisible() {
+        mController.updateState(mPreference);
+
+        assertThat(mPreference.isVisible()).isTrue();
+        assertThat(mPreference.getProgress()).isEqualTo(DEFAULT_LEVEL);
+    }
+
+    @Test
     public void testPreference_lowPowerLevelNotZero_updatePreference() {
         Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.LOW_POWER_MODE_TRIGGER_LEVEL, TRIGGER_LEVEL);
         mController.updateState(mPreference);
 
         assertThat(mPreference.isVisible()).isTrue();
-        assertThat(mPreference.getTitle()).isEqualTo("Turn on automatically at 15%");
+        assertThat(mPreference.getTitle()).isEqualTo("Turn on automatically at 20%");
         assertThat(mPreference.getProgress()).isEqualTo(TRIGGER_LEVEL);
     }
 
diff --git a/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java b/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java
index e44a596..e1005ea 100644
--- a/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java
+++ b/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java
@@ -51,6 +51,7 @@
 import android.support.test.runner.AndroidJUnit4;
 
 import com.android.settings.Settings.WifiSettingsActivity;
+import com.android.settingslib.utils.ThreadUtils;
 import com.android.settingslib.wifi.AccessPoint;
 import com.android.settingslib.wifi.TestAccessPointBuilder;
 import com.android.settingslib.wifi.WifiTracker;
@@ -261,26 +262,6 @@
     }
 
     @Test
-    public void resumingAp_shouldNotForceUpdateWhenExistingAPsAreListed() {
-        setWifiState(WifiManager.WIFI_STATE_ENABLED);
-        setupConnectedAccessPoint();
-        when(mWifiTracker.isConnected()).thenReturn(true);
-
-        launchActivity();
-
-        onView(withText(resourceString(WIFI_DISPLAY_STATUS_CONNECTED))).check(
-                matches(isDisplayed()));
-        verify(mWifiTracker).forceUpdate();
-
-        Activity activity = mActivityRule.getActivity();
-        activity.finish();
-        getInstrumentation().waitForIdleSync();
-
-        getInstrumentation().callActivityOnStart(activity);
-        verify(mWifiTracker, atMost(1)).forceUpdate();
-    }
-
-    @Test
     public void changingSecurityStateOnApShouldNotCauseMultipleListItems() {
         setWifiState(WifiManager.WIFI_STATE_ENABLED);
         TestAccessPointBuilder builder = new TestAccessPointBuilder(mContext)
@@ -305,10 +286,10 @@
 
         onView(withText(TEST_SSID)).check(matches(isDisplayed()));
 
-        mWifiListener.onAccessPointsChanged();
+        ThreadUtils.postOnMainThread(() -> mWifiListener.onAccessPointsChanged());
         onView(withText(TEST_SSID)).check(matches(isDisplayed()));
 
-        mWifiListener.onAccessPointsChanged();
+        ThreadUtils.postOnMainThread(() -> mWifiListener.onAccessPointsChanged());
         onView(withText(TEST_SSID)).check(matches(isDisplayed()));
     }
 
@@ -367,7 +348,6 @@
 
         launchActivity();
 
-        verify(mWifiTracker, atMost(1)).forceUpdate();
         verify(mWifiTracker, times(1)).getAccessPoints();
         onView(withText(WIFI_DISPLAY_STATUS_CONNECTED)).check(matches(isDisplayed()));