Merge "Remove "Hotspot" key from settings search if not needed" into tm-qpr-dev
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index a8bd35c..f8b7f00 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -1216,6 +1216,20 @@
         <item>90</item>
     </string-array>
 
+    <!-- Options for screensaver "When to start" for devices that do not support screensavers
+         while on battery -->
+    <string-array name="when_to_start_screensaver_entries_no_battery" translatable="false">
+        <item>@string/screensaver_settings_summary_sleep</item>
+        <item>@string/screensaver_settings_summary_dock_and_charging</item>
+    </string-array>
+
+    <!-- Values for screensaver "When to start" for devices that do not support screensavers
+         while on battery -->
+    <string-array name="when_to_start_screensaver_values_no_battery" translatable="false">
+        <item>while_charging_only</item>
+        <item>while_docked_only</item>
+    </string-array>
+
     <string-array name="when_to_start_screensaver_entries" translatable="false">
         <item>@string/screensaver_settings_summary_sleep</item>
         <item>@string/screensaver_settings_summary_dock</item>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 42f8e4c..03a103e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3365,6 +3365,8 @@
     <string name="screensaver_settings_toggle_title">Use screen saver</string>
     <!-- Display settings screen, summary fragment for screen saver options, activated when docked or asleep and charging [CHAR LIMIT=35] -->
     <string name="screensaver_settings_summary_either_long">While charging or docked</string>
+    <!-- Display settings screen, summary fragment for screen saver options, actived while docked and charging [CHAR LIMIT=35] -->
+    <string name="screensaver_settings_summary_dock_and_charging">While docked and charging</string>
     <!-- Display settings screen, summary fragment for screen saver options, activated when asleep and charging [CHAR LIMIT=35] -->
     <string name="screensaver_settings_summary_sleep">While charging</string>
     <!-- Display settings screen, summary fragment for screen saver options, activated when docked [CHAR LIMIT=35] -->
@@ -9895,6 +9897,9 @@
     <!-- [CHAR LIMIT=NONE] Text appearing when channel group notifications are off -->
     <string name="channel_group_notifications_off_desc">At your request, Android is blocking this group of notifications from appearing on this device</string>
 
+    <!-- [CHAR LIMIT=NONE] Text appearing when app does not send notifications -->
+    <string name="app_notifications_not_send_desc">This app does not send notifications</string>
+
     <!-- [CHAR LIMIT=NONE] App notification settings: channels title -->
     <string name="notification_channels">Categories</string>
 
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index d92fb7f..70fbefc 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -60,6 +60,7 @@
 import com.android.settings.development.bluetooth.BluetoothSampleRateDialogPreferenceController;
 import com.android.settings.development.qstile.DevelopmentTiles;
 import com.android.settings.development.storage.SharedDataPreferenceController;
+import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.actionbar.SearchMenuController;
 import com.android.settings.widget.SettingsMainSwitchBar;
@@ -308,6 +309,8 @@
                     DisableDevSettingsDialogFragment.show(this /* host */);
                 }
             }
+            FeatureFactory.getFactory(
+                    getContext()).getSearchFeatureProvider().sendPreIndexIntent(getContext());
         }
     }
 
@@ -369,12 +372,12 @@
 
         final BluetoothLeAudioHwOffloadPreferenceController leAudioController =
                 getDevelopmentOptionsController(
-                    BluetoothLeAudioHwOffloadPreferenceController.class);
+                        BluetoothLeAudioHwOffloadPreferenceController.class);
         leAudioController.onRebootDialogConfirmed();
 
         final BluetoothLeAudioPreferenceController leAudioFeatureController =
                 getDevelopmentOptionsController(
-                    BluetoothLeAudioPreferenceController.class);
+                        BluetoothLeAudioPreferenceController.class);
         leAudioFeatureController.onRebootDialogConfirmed();
     }
 
@@ -386,12 +389,12 @@
 
         final BluetoothLeAudioHwOffloadPreferenceController leAudioController =
                 getDevelopmentOptionsController(
-                    BluetoothLeAudioHwOffloadPreferenceController.class);
+                        BluetoothLeAudioHwOffloadPreferenceController.class);
         leAudioController.onRebootDialogCanceled();
 
         final BluetoothLeAudioPreferenceController leAudioFeatureController =
                 getDevelopmentOptionsController(
-                    BluetoothLeAudioPreferenceController.class);
+                        BluetoothLeAudioPreferenceController.class);
         leAudioFeatureController.onRebootDialogCanceled();
     }
 
diff --git a/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java b/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java
index 7ddca79..6af58b5 100644
--- a/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java
+++ b/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java
@@ -240,6 +240,8 @@
         mDevHitToast = Toast.makeText(mContext, R.string.show_dev_on,
                 Toast.LENGTH_LONG);
         mDevHitToast.show();
+
+        FeatureFactory.getFactory(mContext).getSearchFeatureProvider().sendPreIndexIntent(mContext);
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/dream/DreamSettings.java b/src/com/android/settings/dream/DreamSettings.java
index 1d12c1a..580211a 100644
--- a/src/com/android/settings/dream/DreamSettings.java
+++ b/src/com/android/settings/dream/DreamSettings.java
@@ -90,12 +90,14 @@
         }
     }
 
-    static int getDreamSettingDescriptionResId(@WhenToDream int dreamSetting) {
+    static int getDreamSettingDescriptionResId(@WhenToDream int dreamSetting,
+            boolean enabledOnBattery) {
         switch (dreamSetting) {
             case WHILE_CHARGING:
                 return R.string.screensaver_settings_summary_sleep;
             case WHILE_DOCKED:
-                return R.string.screensaver_settings_summary_dock;
+                return enabledOnBattery ? R.string.screensaver_settings_summary_dock
+                        : R.string.screensaver_settings_summary_dock_and_charging;
             case EITHER:
                 return R.string.screensaver_settings_summary_either_long;
             case NEVER:
diff --git a/src/com/android/settings/dream/WhenToDreamPicker.java b/src/com/android/settings/dream/WhenToDreamPicker.java
index 1c5e25e..13cdadf 100644
--- a/src/com/android/settings/dream/WhenToDreamPicker.java
+++ b/src/com/android/settings/dream/WhenToDreamPicker.java
@@ -32,12 +32,15 @@
 
     private static final String TAG = "WhenToDreamPicker";
     private DreamBackend mBackend;
+    private boolean mDreamsSupportedOnBattery;
 
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
 
         mBackend = DreamBackend.getInstance(context);
+        mDreamsSupportedOnBattery = getResources().getBoolean(
+                com.android.internal.R.bool.config_dreamsEnabledOnBattery);
     }
 
     @Override
@@ -69,11 +72,17 @@
     }
 
     private String[] entries() {
-        return getResources().getStringArray(R.array.when_to_start_screensaver_entries);
+        if (mDreamsSupportedOnBattery) {
+            return getResources().getStringArray(R.array.when_to_start_screensaver_entries);
+        }
+        return getResources().getStringArray(R.array.when_to_start_screensaver_entries_no_battery);
     }
 
     private String[] keys() {
-        return getResources().getStringArray(R.array.when_to_start_screensaver_values);
+        if (mDreamsSupportedOnBattery) {
+            return getResources().getStringArray(R.array.when_to_start_screensaver_values);
+        }
+        return getResources().getStringArray(R.array.when_to_start_screensaver_values_no_battery);
     }
 
     @Override
diff --git a/src/com/android/settings/dream/WhenToDreamPreferenceController.java b/src/com/android/settings/dream/WhenToDreamPreferenceController.java
index 02ae6a7..c3bae00 100644
--- a/src/com/android/settings/dream/WhenToDreamPreferenceController.java
+++ b/src/com/android/settings/dream/WhenToDreamPreferenceController.java
@@ -33,19 +33,24 @@
     private static final String WHEN_TO_START = "when_to_start";
     private final DreamBackend mBackend;
     private final boolean mDreamsDisabledByAmbientModeSuppression;
+    private final boolean mDreamsEnabledOnBattery;
 
     WhenToDreamPreferenceController(Context context) {
         this(context, context.getResources().getBoolean(
-                com.android.internal.R.bool.config_dreamsDisabledByAmbientModeSuppressionConfig));
+                com.android.internal.R.bool.config_dreamsDisabledByAmbientModeSuppressionConfig),
+                context.getResources().getBoolean(
+                        com.android.internal.R.bool.config_dreamsEnabledOnBattery));
     }
 
     @VisibleForTesting
     WhenToDreamPreferenceController(Context context,
-            boolean dreamsDisabledByAmbientModeSuppression) {
+            boolean dreamsDisabledByAmbientModeSuppression,
+            boolean dreamsEnabledOnBattery) {
         super(context);
 
         mBackend = DreamBackend.getInstance(context);
         mDreamsDisabledByAmbientModeSuppression = dreamsDisabledByAmbientModeSuppression;
+        mDreamsEnabledOnBattery = dreamsEnabledOnBattery;
     }
 
     @Override
@@ -57,7 +62,7 @@
             preference.setSummary(R.string.screensaver_settings_when_to_dream_bedtime);
         } else {
             final int resId = DreamSettings.getDreamSettingDescriptionResId(
-                    mBackend.getWhenToDreamSetting());
+                    mBackend.getWhenToDreamSetting(), mDreamsEnabledOnBattery);
             preference.setSummary(resId);
         }
     }
diff --git a/src/com/android/settings/homepage/TopLevelSettings.java b/src/com/android/settings/homepage/TopLevelSettings.java
index 70530fc..8c122ef 100644
--- a/src/com/android/settings/homepage/TopLevelSettings.java
+++ b/src/com/android/settings/homepage/TopLevelSettings.java
@@ -43,6 +43,7 @@
 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.overlay.FeatureFactory;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.support.SupportPreferenceController;
 import com.android.settings.widget.HomepagePreference;
@@ -157,6 +158,8 @@
     public void onStart() {
         if (mFirstStarted) {
             mFirstStarted = false;
+            FeatureFactory.getFactory(getContext()).getSearchFeatureProvider().sendPreIndexIntent(
+                    getContext());
         } else if (mIsEmbeddingActivityEnabled && isOnlyOneActivityInTask()
                 && !SplitController.getInstance().isActivityEmbedded(getActivity())) {
             // Set default highlight menu key for 1-pane homepage since it will show the placeholder
@@ -346,7 +349,9 @@
     }
 
     private interface PreferenceJob {
-        default void init() {}
+        default void init() {
+        }
+
         void doForEach(Preference preference);
     }
 
diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java
index 150dbe0..68f5d08 100644
--- a/src/com/android/settings/notification/NotificationBackend.java
+++ b/src/com/android/settings/notification/NotificationBackend.java
@@ -126,6 +126,7 @@
             if (app.requestedPermissions == null || Arrays.stream(app.requestedPermissions)
                     .noneMatch(p -> p.equals(android.Manifest.permission.POST_NOTIFICATIONS))) {
                 row.lockedImportance = true;
+                row.permissionStateLocked = true;
             }
         }
     }
@@ -684,6 +685,9 @@
         public boolean systemApp;
         public boolean lockedImportance;
         public boolean showBadge;
+        // For apps target T but have not but has not requested the permission
+        // we cannot change the permission state
+        public boolean permissionStateLocked;
         public int bubblePreference = NotificationManager.BUBBLE_PREFERENCE_NONE;
         public int userId;
         public int blockedChannelCount;
diff --git a/src/com/android/settings/notification/VibrateIconPreferenceController.java b/src/com/android/settings/notification/VibrateIconPreferenceController.java
index 25d2326..d772b47 100644
--- a/src/com/android/settings/notification/VibrateIconPreferenceController.java
+++ b/src/com/android/settings/notification/VibrateIconPreferenceController.java
@@ -19,6 +19,7 @@
 import static com.android.settings.notification.SettingPref.TYPE_SECURE;
 
 import android.content.Context;
+import android.os.Vibrator;
 import android.provider.Settings.Secure;
 
 import com.android.settings.SettingsPreferenceFragment;
@@ -27,16 +28,18 @@
 public class VibrateIconPreferenceController extends SettingPrefController {
 
     private static final String KEY_VIBRATE_ICON = "vibrate_icon";
+    private final boolean mHasVibrator;
 
     public VibrateIconPreferenceController(Context context, SettingsPreferenceFragment parent,
             Lifecycle lifecycle) {
         super(context, parent, lifecycle);
+        mHasVibrator = context.getSystemService(Vibrator.class).hasVibrator();
         mPreference = new SettingPref(
             TYPE_SECURE, KEY_VIBRATE_ICON, Secure.STATUS_BAR_SHOW_VIBRATE_ICON, 0 /*default off*/);
     }
 
     @Override
     public boolean isAvailable() {
-        return true;
+        return mHasVibrator;
     }
 }
diff --git a/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java b/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java
index 0c7cd23..04e3f0e 100644
--- a/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java
+++ b/src/com/android/settings/notification/app/NotificationsOffPreferenceController.java
@@ -62,6 +62,8 @@
                 preference.setTitle(R.string.channel_notifications_off_desc);
             } else if (mChannelGroup != null) {
                 preference.setTitle(R.string.channel_group_notifications_off_desc);
+            } else if (mAppRow.permissionStateLocked) {
+                preference.setTitle(R.string.app_notifications_not_send_desc);
             } else {
                 preference.setTitle(R.string.app_notifications_off_desc);
             }
diff --git a/src/com/android/settings/search/SearchFeatureProvider.java b/src/com/android/settings/search/SearchFeatureProvider.java
index 1785361..b14a4d7 100644
--- a/src/com/android/settings/search/SearchFeatureProvider.java
+++ b/src/com/android/settings/search/SearchFeatureProvider.java
@@ -72,6 +72,12 @@
     }
 
     /**
+     * Send the pre-index intent.
+     */
+    default void sendPreIndexIntent(Context context){
+    }
+
+    /**
      * Initializes the search toolbar.
      */
     default void initSearchToolbar(FragmentActivity activity, Toolbar toolbar, int pageId) {
diff --git a/tests/robotests/src/com/android/settings/dream/DreamSettingsTest.java b/tests/robotests/src/com/android/settings/dream/DreamSettingsTest.java
index a554564..c1cc6ae 100644
--- a/tests/robotests/src/com/android/settings/dream/DreamSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/dream/DreamSettingsTest.java
@@ -29,33 +29,40 @@
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
 
 import java.util.Arrays;
 import java.util.List;
-import org.robolectric.RobolectricTestRunner;
 
 @RunWith(RobolectricTestRunner.class)
 public class DreamSettingsTest {
 
     private static final List<String> KEYS = Arrays.asList(
-        DreamSettings.WHILE_CHARGING_ONLY,
-        DreamSettings.WHILE_DOCKED_ONLY,
-        DreamSettings.EITHER_CHARGING_OR_DOCKED,
-        DreamSettings.NEVER_DREAM
+            DreamSettings.WHILE_CHARGING_ONLY,
+            DreamSettings.WHILE_DOCKED_ONLY,
+            DreamSettings.EITHER_CHARGING_OR_DOCKED,
+            DreamSettings.NEVER_DREAM
     );
 
     private static final @WhenToDream int[] SETTINGS = {
-        DreamBackend.WHILE_CHARGING,
-        DreamBackend.WHILE_DOCKED,
-        DreamBackend.EITHER,
-        DreamBackend.NEVER,
+            DreamBackend.WHILE_CHARGING,
+            DreamBackend.WHILE_DOCKED,
+            DreamBackend.EITHER,
+            DreamBackend.NEVER,
     };
 
     private static final int[] RES_IDS = {
-        R.string.screensaver_settings_summary_sleep,
-        R.string.screensaver_settings_summary_dock,
-        R.string.screensaver_settings_summary_either_long,
-        R.string.screensaver_settings_summary_never
+            R.string.screensaver_settings_summary_sleep,
+            R.string.screensaver_settings_summary_dock,
+            R.string.screensaver_settings_summary_either_long,
+            R.string.screensaver_settings_summary_never
+    };
+
+    private static final int[] RES_IDS_NO_BATTERY = {
+            R.string.screensaver_settings_summary_sleep,
+            R.string.screensaver_settings_summary_dock_and_charging,
+            R.string.screensaver_settings_summary_either_long,
+            R.string.screensaver_settings_summary_never
     };
 
     @Test
@@ -81,11 +88,17 @@
     @Test
     public void getDreamSettingDescriptionResId() {
         for (int i = 0; i < SETTINGS.length; i++) {
-            assertThat(DreamSettings.getDreamSettingDescriptionResId(SETTINGS[i]))
+            assertThat(DreamSettings.getDreamSettingDescriptionResId(
+                    SETTINGS[i], /* enabledOnBattery= */ false))
+                    .isEqualTo(RES_IDS_NO_BATTERY[i]);
+            assertThat(DreamSettings.getDreamSettingDescriptionResId(
+                    SETTINGS[i], /* enabledOnBattery= */ true))
                     .isEqualTo(RES_IDS[i]);
         }
         // Default
-        assertThat(DreamSettings.getDreamSettingDescriptionResId(-1))
+        assertThat(DreamSettings.getDreamSettingDescriptionResId(-1, /* enabledOnBattery= */ false))
+                .isEqualTo(R.string.screensaver_settings_summary_never);
+        assertThat(DreamSettings.getDreamSettingDescriptionResId(-1, /* enabledOnBattery= */ true))
                 .isEqualTo(R.string.screensaver_settings_summary_never);
     }
 
diff --git a/tests/robotests/src/com/android/settings/dream/WhenToDreamPickerTest.java b/tests/robotests/src/com/android/settings/dream/WhenToDreamPickerTest.java
index 16f8599..91ec299 100644
--- a/tests/robotests/src/com/android/settings/dream/WhenToDreamPickerTest.java
+++ b/tests/robotests/src/com/android/settings/dream/WhenToDreamPickerTest.java
@@ -28,6 +28,7 @@
 import androidx.test.core.app.ApplicationProvider;
 
 import com.android.settings.testutils.FakeFeatureFactory;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
 import com.android.settingslib.dream.DreamBackend;
 
 import org.junit.Before;
@@ -37,9 +38,11 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
 import org.robolectric.util.ReflectionHelpers;
 
 @RunWith(RobolectricTestRunner.class)
+@Config(shadows = SettingsShadowResources.class)
 public class WhenToDreamPickerTest {
 
     private WhenToDreamPicker mPicker;
@@ -53,10 +56,15 @@
         MockitoAnnotations.initMocks(this);
         final Context context = spy(ApplicationProvider.getApplicationContext());
 
+        SettingsShadowResources.overrideResource(
+                com.android.internal.R.bool.config_dreamsEnabledOnBattery,
+                true);
+
         when(context.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
         FakeFeatureFactory.setupForTest();
 
-        mPicker = new WhenToDreamPicker();
+        mPicker = spy(new WhenToDreamPicker());
+        when(mPicker.getContext()).thenReturn(context);
         mPicker.onAttach(context);
 
         ReflectionHelpers.setField(mPicker, "mBackend", mBackend);
diff --git a/tests/robotests/src/com/android/settings/dream/WhenToDreamPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/dream/WhenToDreamPreferenceControllerTest.java
index 458c5c6..6e687bb 100644
--- a/tests/robotests/src/com/android/settings/dream/WhenToDreamPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/dream/WhenToDreamPreferenceControllerTest.java
@@ -64,7 +64,7 @@
     public void setup() throws Exception {
         MockitoAnnotations.initMocks(this);
         mContext = spy(ApplicationProvider.getApplicationContext());
-        mController = new WhenToDreamPreferenceController(mContext, true);
+        mController = new WhenToDreamPreferenceController(mContext, true, true);
         ReflectionHelpers.setField(mController, "mBackend", mBackend);
         when(mContext.getSystemService(PowerManager.class)).thenReturn(mPowerManager);
         when(mPowerManager.isAmbientDisplaySuppressedForTokenByApp(anyString(), anyInt()))
@@ -88,7 +88,7 @@
         final Preference mockPref = mock(Preference.class);
         when(mockPref.getContext()).thenReturn(mContext);
         when(mBackend.getWhenToDreamSetting()).thenReturn(testSetting);
-        final int expectedResId = DreamSettings.getDreamSettingDescriptionResId(testSetting);
+        final int expectedResId = DreamSettings.getDreamSettingDescriptionResId(testSetting, true);
 
         mController.updateState(mockPref);
         verify(mockPref).setSummary(expectedResId);
diff --git a/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java
index 34e94a0..2eebd3a 100644
--- a/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/app/NotificationsOffPreferenceControllerTest.java
@@ -20,6 +20,7 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
@@ -32,6 +33,7 @@
 
 import androidx.preference.Preference;
 
+import com.android.settings.R;
 import com.android.settings.notification.NotificationBackend;
 
 import com.google.common.collect.ImmutableList;
@@ -157,4 +159,23 @@
         assertThat(pref.getTitle().toString()).contains("app");
         assertThat(pref.isSelectable()).isFalse();
     }
+
+    @Test
+    public void testUpdateState_whenToggleDisabled() {
+        // Given: the app does not request to post notifications
+        // and it's preference toggle is disabled
+        NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
+        appRow.banned = true;
+        appRow.permissionStateLocked = true;
+        mController.onResume(appRow, null, null, null, null, null, null);
+        Preference pref = new Preference(RuntimeEnvironment.application);
+
+        // When: updateState(Preference preference) is called
+        mController.updateState(pref);
+
+        // Then: title of pref should be app_notifications_not_send_desc
+        assertEquals(
+                RuntimeEnvironment.application.getString(R.string.app_notifications_not_send_desc),
+                pref.getTitle().toString());
+    }
 }