Merge "Integrate with SettingsIntelligence"
diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java
index fff7002..741067a 100644
--- a/src/com/android/settings/dashboard/DashboardAdapter.java
+++ b/src/com/android/settings/dashboard/DashboardAdapter.java
@@ -30,6 +30,7 @@
 import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.Log;
+import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -47,6 +48,7 @@
 import com.android.settings.dashboard.suggestions.SuggestionAdapter;
 import com.android.settings.dashboard.suggestions.SuggestionDismissController;
 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
+import com.android.settings.dashboard.suggestions.SuggestionLogHelper;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.Utils;
 import com.android.settingslib.drawer.DashboardCategory;
@@ -159,7 +161,8 @@
                 final String identifier = mSuggestionFeatureProvider.getSuggestionIdentifier(
                         mContext, suggestion);
                 mMetricsFeatureProvider.action(
-                        mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, identifier);
+                        mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, identifier,
+                        getSuggestionTaggedData());
                 mSuggestionsShownLogged.add(identifier);
             }
         }
@@ -289,7 +292,8 @@
                     mContext, suggestion);
             if (mSuggestionsShownLogged.contains(suggestionId)) {
                 mMetricsFeatureProvider.action(
-                        mContext, MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, suggestionId);
+                        mContext, MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION, suggestionId,
+                        getSuggestionTaggedData());
             }
         }
         mSuggestionsShownLogged.clear();
@@ -325,8 +329,8 @@
                     mContext, suggestion);
             if (!mSuggestionsShownLogged.contains(suggestionId)) {
                 mMetricsFeatureProvider.action(
-                        mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                        suggestionId);
+                        mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, suggestionId,
+                        getSuggestionTaggedData());
                 mSuggestionsShownLogged.add(suggestionId);
             }
         }
@@ -507,6 +511,11 @@
         parent.setVisibility(View.VISIBLE);
     }
 
+    private Pair<Integer, Object>[] getSuggestionTaggedData() {
+        return SuggestionLogHelper.getSuggestionTaggedData(
+                mSuggestionFeatureProvider.isSmartSuggestionEnabled(mContext));
+    }
+
     public static class IconCache {
         private final Context mContext;
         private final ArrayMap<Icon, Drawable> mMap = new ArrayMap<>();
diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java b/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java
index 2c9da41..3815211 100644
--- a/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java
+++ b/src/com/android/settings/dashboard/suggestions/SuggestionAdapter.java
@@ -18,6 +18,7 @@
 import android.content.Context;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
+import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -67,11 +68,12 @@
     public void onBindViewHolder(DashboardItemHolder holder, int position) {
         final Tile suggestion = (Tile) mSuggestions.get(position);
         final String suggestionId = mSuggestionFeatureProvider.getSuggestionIdentifier(
-            mContext, suggestion);
+                mContext, suggestion);
         // This is for cases when a suggestion is dismissed and the next one comes to view
         if (!mSuggestionsShownLogged.contains(suggestionId)) {
             mMetricsFeatureProvider.action(
-                mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, suggestionId);
+                    mContext, MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION, suggestionId,
+                    getSuggestionTaggedData());
             mSuggestionsShownLogged.add(suggestionId);
         }
         if (suggestion.remoteViews != null) {
@@ -102,9 +104,11 @@
             // set the item view to disabled to remove any touch effects
             holder.itemView.setEnabled(false);
         }
+
         clickHandler.setOnClickListener(v -> {
             mMetricsFeatureProvider.action(mContext,
-                MetricsEvent.ACTION_SETTINGS_SUGGESTION, suggestionId);
+                    MetricsEvent.ACTION_SETTINGS_SUGGESTION, suggestionId,
+                    getSuggestionTaggedData());
             ((SettingsActivity) mContext).startSuggestion(suggestion.intent);
         });
     }
@@ -129,7 +133,7 @@
 
     public Tile getSuggestion(int position) {
         final long itemId = getItemId(position);
-        for (Tile tile: mSuggestions) {
+        for (Tile tile : mSuggestions) {
             if (Objects.hash(tile.title) == itemId) {
                 return tile;
             }
@@ -141,4 +145,10 @@
         mSuggestions.remove(suggestion);
         notifyDataSetChanged();
     }
+
+    private Pair<Integer, Object>[] getSuggestionTaggedData() {
+        return SuggestionLogHelper.getSuggestionTaggedData(
+                mSuggestionFeatureProvider.isSmartSuggestionEnabled(mContext));
+    }
+
 }
diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
index f8b5a8b..3d40d96 100644
--- a/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImpl.java
@@ -26,8 +26,9 @@
 import android.support.annotation.NonNull;
 import android.support.annotation.VisibleForTesting;
 import android.util.Log;
+import android.util.Pair;
 
-import com.android.internal.logging.nano.MetricsProto;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.Settings.AmbientDisplayPickupSuggestionActivity;
 import com.android.settings.Settings.AmbientDisplaySuggestionActivity;
 import com.android.settings.Settings.DoubleTapPowerSuggestionActivity;
@@ -45,6 +46,7 @@
 import com.android.settingslib.drawer.Tile;
 import com.android.settingslib.suggestions.SuggestionParser;
 
+import java.util.ArrayList;
 import java.util.List;
 
 public class SuggestionFeatureProviderImpl implements SuggestionFeatureProvider {
@@ -130,10 +132,13 @@
         if (parser == null || suggestion == null || context == null) {
             return;
         }
-        mMetricsFeatureProvider.action(
-                context, MetricsProto.MetricsEvent.ACTION_SETTINGS_DISMISS_SUGGESTION,
-                getSuggestionIdentifier(context, suggestion));
+        final Pair<Integer, Object>[] taggedData =
+                SuggestionLogHelper.getSuggestionTaggedData(isSmartSuggestionEnabled(context));
 
+        mMetricsFeatureProvider.action(
+                context, MetricsEvent.ACTION_SETTINGS_DISMISS_SUGGESTION,
+                getSuggestionIdentifier(context, suggestion),
+                taggedData);
         if (!parser.dismissSuggestion(suggestion)) {
             return;
         }
diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionLogHelper.java b/src/com/android/settings/dashboard/suggestions/SuggestionLogHelper.java
new file mode 100644
index 0000000..339392f
--- /dev/null
+++ b/src/com/android/settings/dashboard/suggestions/SuggestionLogHelper.java
@@ -0,0 +1,29 @@
+/*
+ * 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.dashboard.suggestions;
+
+import android.util.Pair;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+
+public class SuggestionLogHelper {
+
+    public static Pair<Integer, Object>[] getSuggestionTaggedData(boolean enabled) {
+        return new Pair[]{
+                Pair.create(
+                        MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, enabled ? 1 : 0)};
+    }
+}
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 919dc3a..5768299 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -32,8 +32,10 @@
 import com.android.settings.search.Indexable;
 import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
 import com.android.settingslib.development.DevelopmentSettingsEnabler;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -116,7 +118,7 @@
 
     @Override
     protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
-        return buildPreferenceControllers(context);
+        return buildPreferenceControllers(context, getLifecycle());
     }
 
     void onEnableDevelopmentOptionsConfirmed() {
@@ -129,8 +131,12 @@
         mSwitchBar.setChecked(false);
     }
 
-    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
-        return null;
+    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
+            Lifecycle lifecycle) {
+        final List<AbstractPreferenceController> controllers = new ArrayList<>();
+        controllers.add(new StayAwakePreferenceController(context, lifecycle));
+
+        return controllers;
     }
 
     /**
@@ -156,7 +162,7 @@
                 @Override
                 public List<AbstractPreferenceController> getPreferenceControllers(Context
                         context) {
-                    return buildPreferenceControllers(context);
+                    return buildPreferenceControllers(context, null /* lifecycle */);
                 }
             };
 }
diff --git a/src/com/android/settings/development/StayAwakePreferenceController.java b/src/com/android/settings/development/StayAwakePreferenceController.java
new file mode 100644
index 0000000..ebba9e5
--- /dev/null
+++ b/src/com/android/settings/development/StayAwakePreferenceController.java
@@ -0,0 +1,139 @@
+package com.android.settings.development;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.BatteryManager;
+import android.os.Handler;
+import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedSwitchPreference;
+import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnPause;
+import com.android.settingslib.core.lifecycle.events.OnResume;
+
+
+public class StayAwakePreferenceController extends AbstractPreferenceController implements
+        PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
+        OnResume, OnPause {
+
+    private static final String TAG = "StayAwakeCtrl";
+    private static final String PREFERENCE_KEY = "keep_screen_on";
+    @VisibleForTesting
+    static final int SETTING_VALUE_OFF = 0;
+    @VisibleForTesting
+    static final int SETTING_VALUE_ON =
+            BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB
+                    | BatteryManager.BATTERY_PLUGGED_WIRELESS;
+    @VisibleForTesting
+    SettingsObserver mSettingsObserver;
+
+    private RestrictedSwitchPreference mPreference;
+
+    public StayAwakePreferenceController(Context context, Lifecycle lifecycle) {
+        super(context);
+        mSettingsObserver = new SettingsObserver();
+
+        if (lifecycle != null) {
+            lifecycle.addObserver(this);
+        }
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return true;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return PREFERENCE_KEY;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mPreference = (RestrictedSwitchPreference) screen.findPreference(getPreferenceKey());
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        final boolean stayAwake = (Boolean) newValue;
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                stayAwake ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
+        return true;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        final RestrictedLockUtils.EnforcedAdmin admin = checkIfMaximumTimeToLockSetByAdmin();
+        if (admin != null) {
+            mPreference.setDisabledByAdmin(admin);
+            return;
+        }
+
+        final int stayAwakeMode = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                SETTING_VALUE_OFF);
+        mPreference.setChecked(stayAwakeMode != SETTING_VALUE_OFF);
+    }
+
+    @Override
+    public void onResume() {
+        if (mPreference != null) {
+            mSettingsObserver.register(true /* register */);
+        }
+    }
+
+    @Override
+    public void onPause() {
+        if (mPreference != null) {
+            mSettingsObserver.register(false /* unregister */);
+        }
+    }
+
+    @VisibleForTesting
+    RestrictedLockUtils.EnforcedAdmin checkIfMaximumTimeToLockSetByAdmin() {
+        // A DeviceAdmin has specified a maximum time until the device
+        // will lock...  in this case we can't allow the user to turn
+        // on "stay awake when plugged in" because that would defeat the
+        // restriction.
+        return RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(mContext);
+    }
+
+    @VisibleForTesting
+    class SettingsObserver extends ContentObserver {
+        private final Uri mStayAwakeUri = Settings.Global.getUriFor(
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN);
+
+        public SettingsObserver() {
+            super(new Handler());
+        }
+
+        public void register(boolean register) {
+            final ContentResolver cr = mContext.getContentResolver();
+            if (register) {
+                cr.registerContentObserver(
+                        mStayAwakeUri, false, this);
+            } else {
+                cr.unregisterContentObserver(this);
+            }
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            super.onChange(selfChange, uri);
+            if (mStayAwakeUri.equals(uri)) {
+                updateState(mPreference);
+            }
+        }
+    }
+}
diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java
index 0c401b5..4829ca0 100644
--- a/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java
+++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java
@@ -122,7 +122,7 @@
         anomalyDetectionEnabled = mParserWrapper.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, true);
         wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true);
         wakeupAlarmDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,
-                false);
+                true);
         bluetoothScanDetectionEnabled = mParserWrapper.getBoolean(
                 KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, true);
         wakeLockThreshold = mParserWrapper.getLong(KEY_WAKELOCK_THRESHOLD,
diff --git a/src/com/android/settings/password/ConfirmDeviceCredentialBaseFragment.java b/src/com/android/settings/password/ConfirmDeviceCredentialBaseFragment.java
index a67184a..2cb337b 100644
--- a/src/com/android/settings/password/ConfirmDeviceCredentialBaseFragment.java
+++ b/src/com/android/settings/password/ConfirmDeviceCredentialBaseFragment.java
@@ -243,7 +243,6 @@
             try {
                 IActivityManager activityManager = ActivityManager.getService();
                 final ActivityOptions options = ActivityOptions.makeBasic();
-                options.setLaunchStackId(ActivityManager.StackId.INVALID_STACK_ID);
                 activityManager.startActivityFromRecents(taskId, options.toBundle());
                 return;
             } catch (RemoteException e) {
diff --git a/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java b/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java
index da13126..2f05274 100644
--- a/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java
+++ b/src/com/android/settings/wifi/tether/WifiTetherPreferenceController.java
@@ -138,6 +138,7 @@
 
     private void handleWifiApStateChanged(int state, int reason) {
         switch (state) {
+            case WifiManager.WIFI_AP_STATE_ENABLING:
             case WifiManager.WIFI_AP_STATE_ENABLED:
                 /**
                  * Summary on enable is handled by tether
diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java
index e00908e..4b345d0 100644
--- a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java
@@ -37,6 +37,7 @@
 import android.os.Bundle;
 import android.support.v7.widget.RecyclerView;
 import android.util.DisplayMetrics;
+import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.RelativeLayout;
@@ -89,6 +90,8 @@
     private ArgumentCaptor<Integer> mActionCategoryCaptor = ArgumentCaptor.forClass(Integer.class);
     @Captor
     private ArgumentCaptor<String> mActionPackageCaptor = ArgumentCaptor.forClass(String.class);
+    @Captor
+    private ArgumentCaptor<Pair> mTaggedDataCaptor = ArgumentCaptor.forClass(Pair.class);
     private FakeFeatureFactory mFactory;
     private DashboardAdapter mDashboardAdapter;
     private DashboardAdapter.SuggestionAndConditionHeaderHolder mSuggestionHolder;
@@ -123,112 +126,143 @@
     @Test
     public void testSuggestionsLogs_NotExpanded() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         verify(mFactory.metricsFeatureProvider, times(2)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg2"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly("pkg1", "pkg2");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_NotExpandedAndPaused() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         mDashboardAdapter.onPause();
+
         verify(mFactory.metricsFeatureProvider, times(4)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg1", "pkg2"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION};
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg2", "pkg1", "pkg2");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_Expanded() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
+
         verify(mFactory.metricsFeatureProvider, times(3)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg2", "pkg3");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedAndPaused() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
         mDashboardAdapter.onPause();
+
         verify(mFactory.metricsFeatureProvider, times(6)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedAfterPause() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         mDashboardAdapter.onPause();
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
+
         verify(mFactory.metricsFeatureProvider, times(7)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{
-                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedAfterPauseAndPausedAgain() {
         setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3"));
+
         mDashboardAdapter.onPause();
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
         mDashboardAdapter.onPause();
+
         verify(mFactory.metricsFeatureProvider, times(10)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{
-                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
@@ -238,63 +272,82 @@
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg2", "pkg1", "pkg2", "pkg1", "pkg2", "pkg3", "pkg1", "pkg2", "pkg3");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedWithLessThanDefaultShown() {
         setupSuggestions(makeSuggestions("pkg1"));
+
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
+
         verify(mFactory.metricsFeatureProvider, times(1)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1"};
-        Integer[] expectedActions = new Integer[]{
-                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly("pkg1");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAndPaused() {
         setupSuggestions(makeSuggestions("pkg1"));
+
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
         mDashboardAdapter.onPause();
+
         verify(mFactory.metricsFeatureProvider, times(2)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg1"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly("pkg1", "pkg1");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
     public void testSuggestionsLogs_ExpandedWithLessThanDefaultShownAfterPause() {
         setupSuggestions(makeSuggestions("pkg1"));
+
         mDashboardAdapter.onPause();
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
+
         verify(mFactory.metricsFeatureProvider, times(3)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly("pkg1", "pkg1", "pkg1");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
     }
 
     @Test
@@ -304,18 +357,46 @@
         mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
         mSuggestionHolder.itemView.callOnClick();
         mDashboardAdapter.onPause();
+
         verify(mFactory.metricsFeatureProvider, times(4)).action(
                 any(Context.class), mActionCategoryCaptor.capture(),
-                mActionPackageCaptor.capture());
-        String[] expectedPackages = new String[]{"pkg1", "pkg1", "pkg1", "pkg1"};
-        Integer[] expectedActions = new Integer[]{
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION,
                 MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
-                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION
-        };
-        assertThat(mActionPackageCaptor.getAllValues().toArray()).isEqualTo(expectedPackages);
-        assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions);
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly(
+                "pkg1", "pkg1", "pkg1", "pkg1");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
+    }
+
+    @Test
+    public void testSuggestionsLogs_SmartSuggestionEnabled() {
+        when(mFactory.suggestionsFeatureProvider
+                .isSmartSuggestionEnabled(any(Context.class))).thenReturn(true);
+        setupSuggestions(makeSuggestions("pkg1"));
+
+        mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData);
+        mSuggestionHolder.itemView.callOnClick();
+        mDashboardAdapter.onPause();
+
+        verify(mFactory.metricsFeatureProvider, times(2)).action(
+                any(Context.class), mActionCategoryCaptor.capture(),
+                mActionPackageCaptor.capture(),
+                mTaggedDataCaptor.capture());
+        assertThat(mActionCategoryCaptor.getAllValues()).containsExactly(
+                MetricsEvent.ACTION_SHOW_SETTINGS_SUGGESTION,
+                MetricsEvent.ACTION_HIDE_SETTINGS_SUGGESTION);
+        assertThat(mActionPackageCaptor.getAllValues()).containsExactly("pkg1", "pkg1");
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 1),
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 1));
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java
index 45d04a4..c343f97 100644
--- a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java
+++ b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionFeatureProviderImplTest.java
@@ -35,7 +35,9 @@
 import android.content.pm.PackageManager;
 import android.hardware.fingerprint.FingerprintManager;
 import android.provider.Settings.Secure;
+import android.util.Pair;
 
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.internal.logging.nano.MetricsProto;
 import com.android.settings.R;
 import com.android.settings.Settings.AmbientDisplayPickupSuggestionActivity;
@@ -60,6 +62,8 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Answers;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
@@ -93,6 +97,8 @@
     private FingerprintManager mFingerprintManager;
     @Mock
     private SharedPreferences mSharedPreferences;
+    @Captor
+    private ArgumentCaptor<Pair> mTaggedDataCaptor = ArgumentCaptor.forClass(Pair.class);
 
     private FakeFeatureFactory mFactory;
     private SuggestionFeatureProviderImpl mProvider;
@@ -335,7 +341,10 @@
         verify(mFactory.metricsFeatureProvider).action(
                 eq(mContext),
                 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_DISMISS_SUGGESTION),
-                anyString());
+                anyString(),
+                mTaggedDataCaptor.capture());
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
         verify(mContext, never()).getPackageManager();
     }
 
@@ -356,8 +365,10 @@
         verify(mFactory.metricsFeatureProvider).action(
                 eq(mContext),
                 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_DISMISS_SUGGESTION),
-                anyString());
-
+                anyString(),
+                mTaggedDataCaptor.capture());
+        assertThat(mTaggedDataCaptor.getAllValues()).containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
         verify(mContext.getPackageManager())
                 .setComponentEnabledSetting(mSuggestion.intent.getComponent(),
                         PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
diff --git a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionLogHelperTest.java b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionLogHelperTest.java
new file mode 100644
index 0000000..8eb4273
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionLogHelperTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.dashboard.suggestions;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.util.Pair;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+
+import org.junit.Test;
+
+public class SuggestionLogHelperTest {
+
+    @Test
+    public void testGetSmartSuggestionEnabledTaggedData_disabled() {
+        assertThat(SuggestionLogHelper.getSuggestionTaggedData(false)).asList().containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 0));
+    }
+
+    @Test
+    public void testGetSmartSuggestionEnabledTaggedData_enabled() {
+        assertThat(SuggestionLogHelper.getSuggestionTaggedData(true)).asList().containsExactly(
+                Pair.create(MetricsEvent.FIELD_SETTINGS_SMART_SUGGESTIONS_ENABLED, 1));
+    }
+}
+
diff --git a/tests/robotests/src/com/android/settings/development/StayAwakePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/StayAwakePreferenceControllerTest.java
new file mode 100644
index 0000000..cbef531
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/development/StayAwakePreferenceControllerTest.java
@@ -0,0 +1,117 @@
+package com.android.settings.development;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+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.content.ContentResolver;
+import android.content.Context;
+import android.provider.Settings;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedSwitchPreference;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+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 StayAwakePreferenceControllerTest {
+
+    @Mock
+    private Context mContext;
+    @Mock
+    private RestrictedSwitchPreference mPreference;
+    @Mock
+    private PreferenceScreen mPreferenceScreen;
+    @Mock
+    private Lifecycle mLifecycle;
+    private ContentResolver mContentResolver;
+    private StayAwakePreferenceController mController;
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        mContentResolver = RuntimeEnvironment.application.getContentResolver();
+        mController = new StayAwakePreferenceController(mContext, mLifecycle);
+        when(mContext.getContentResolver()).thenReturn(mContentResolver);
+        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
+                mPreference);
+        mController.displayPreference(mPreferenceScreen);
+    }
+
+    @Test
+    public void onPreferenceChanged_turnOnStayAwake() {
+        mController.onPreferenceChange(null, true);
+
+        final int mode = Settings.System.getInt(mContentResolver,
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, -1);
+        assertThat(mode).isEqualTo(StayAwakePreferenceController.SETTING_VALUE_ON);
+    }
+
+    @Test
+    public void onPreferenceChanged_turnOffStayAwake() {
+        mController.onPreferenceChange(null, false);
+
+        final int mode = Settings.System.getInt(mContentResolver,
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, -1);
+        assertThat(mode).isEqualTo(StayAwakePreferenceController.SETTING_VALUE_OFF);
+    }
+
+    @Test
+    public void updateState_preferenceShouldBeChecked() {
+        Settings.System.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                StayAwakePreferenceController.SETTING_VALUE_ON);
+        mController.updateState(mPreference);
+        verify(mPreference).setChecked(true);
+    }
+
+    @Test
+    public void updateState_preferenceShouldNotBeChecked() {
+        Settings.System.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                StayAwakePreferenceController.SETTING_VALUE_OFF);
+        mController.updateState(mPreference);
+        verify(mPreference).setChecked(false);
+    }
+
+    @Test
+    public void displayPreference_expectSetDisabledByAdminToBeCalled() {
+        mController = spy(mController);
+        RestrictedLockUtils.EnforcedAdmin admin = Mockito.mock(
+                RestrictedLockUtils.EnforcedAdmin.class);
+        doReturn(admin).when(mController).checkIfMaximumTimeToLockSetByAdmin();
+        mController.updateState(mPreference);
+        verify(mPreference).setDisabledByAdmin(admin);
+    }
+
+    @Test
+    public void observerOnChangeCalledWithSameUri_preferenceShouldBeUpdated() {
+        Settings.System.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                StayAwakePreferenceController.SETTING_VALUE_ON);
+        mController.mSettingsObserver.onChange(false,
+                Settings.Global.getUriFor(Settings.Global.STAY_ON_WHILE_PLUGGED_IN));
+        verify(mPreference).setChecked(true);
+    }
+
+    @Test
+    public void observerOnChangeCalledWithDifferentUri_preferenceShouldNotBeUpdated() {
+        Settings.System.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
+                StayAwakePreferenceController.SETTING_VALUE_ON);
+        mController.mSettingsObserver.onChange(false, null);
+        verify(mPreference, never()).setChecked(true);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java
index 914cc2f..169cba8 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java
@@ -84,7 +84,7 @@
         assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue();
         assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isTrue();
         assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS);
-        assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isFalse();
+        assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue();
         assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(60);
         assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isTrue();
         assertThat(anomalyDetectionPolicy.bluetoothScanThreshold).isEqualTo(
@@ -112,7 +112,7 @@
         doReturn(false).when(mKeyValueListParserWrapper).getBoolean(
                 AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true);
         doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
-                AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, false);
+                AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, true);
         doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
                 AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, true);
 
diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java
index 860a914..a23347a 100644
--- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherPreferenceControllerTest.java
@@ -161,12 +161,7 @@
     @Test
     public void testReceiver_apStateChangedToDisabled_shouldUpdatePreferenceSummary() {
         mController.displayPreference(mScreen);
-        final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
-        final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
-        broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_DISABLED);
-
-        receiver.onReceive(RuntimeEnvironment.application, broadcast);
-
+        receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
         assertThat(mPreference.getSummary().toString()).isEqualTo(
                 RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
     }
@@ -174,17 +169,29 @@
     @Test
     public void testReceiver_apStateChangedToDisabling_shouldUpdatePreferenceSummary() {
         mController.displayPreference(mScreen);
-        final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
-        final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
-        broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_DISABLING);
-
-        receiver.onReceive(RuntimeEnvironment.application, broadcast);
-
+        receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLING);
         assertThat(mPreference.getSummary().toString()).isEqualTo(
                 RuntimeEnvironment.application.getString(R.string.wifi_tether_stopping));
     }
 
     @Test
+    public void testReceiver_apStateChangedToEnablingOrEnabled_shouldNotUpdatePreferenceSummary() {
+        mController.displayPreference(mScreen);
+        receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
+        assertThat(mPreference.getSummary().toString()).isEqualTo(
+                RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
+
+        // When turning on the hotspot, we receive STATE_ENABLING followed by STATE_ENABLED. Neither
+        // of these should change the summary.
+        receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLING);
+        assertThat(mPreference.getSummary().toString()).isEqualTo(
+                RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
+        receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLED);
+        assertThat(mPreference.getSummary().toString()).isEqualTo(
+                RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
+    }
+
+    @Test
     public void testReceiver_goingToAirplaneMode_shouldClearPreferenceSummary() {
         final ContentResolver cr = mock(ContentResolver.class);
         when(mContext.getContentResolver()).thenReturn(cr);
@@ -248,4 +255,16 @@
             onStopCalled = true;
         }
     }
+
+    /**
+     * Helper to cause the controller to receive a WIFI_AP_STATE_CHANGED_ACTION with a specific
+     * state.
+     * @param state - the state, as specified by one of the WifiManager.WIFI_AP_STATE_* values
+     */
+    private void receiveApStateChangedBroadcast(int state) {
+        final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
+        final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
+        broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, state);
+        receiver.onReceive(RuntimeEnvironment.application, broadcast);
+    }
 }