OSDN Git Service

Change Display summary to sleep timeout description.
authorStephen Chen <stewchen@google.com>
Tue, 20 Sep 2016 20:15:11 +0000 (13:15 -0700)
committerStephen Chen <stewchen@google.com>
Thu, 22 Sep 2016 20:55:32 +0000 (13:55 -0700)
Bug: 29579542
Fixes: 29579542
Change-Id: I4e122bd6972fde933229768cba86302eb4d3f7c6
Test: Added Robo test, manually tested

res/values/strings.xml
src/com/android/settings/DisplaySettings.java
tests/robotests/src/com/android/settings/DisplaySettingsTest.java [new file with mode: 0644]

index 893bc47..07cd527 100644 (file)
     <!-- Summary of storage usage [CHAR LIMIT=NONE] -->
     <string name="storage_summary"><xliff:g id="size1" example="8GB">%1$s</xliff:g> of <xliff:g id="size2" example="32GB">%2$s</xliff:g> used</string>
 
-    <!-- Summary of display with adaptive brightness on [CHAR LIMIT=NONE] -->
-    <string name="display_summary_on">Adaptive brightness is ON</string>
-
-    <!-- Summary of display with adaptive brightness off [CHAR LIMIT=NONE] -->
-    <string name="display_summary_off">Adaptive brightness is OFF</string>
+    <!-- Summary of display with screen sleep timeout [CHAR LIMIT=NONE] -->
+    <string name="display_summary">Sleep after <xliff:g id="timeout_description" example="10 minutes">%1$s</xliff:g> of inactivity</string>
 
     <!-- Summary of memory screen [CHAR LIMIT=NONE] -->
     <string name="memory_summary">Avg <xliff:g id="used_memory" example="1.7GB">%1$s</xliff:g> of <xliff:g id="total_memory" example="2GB">%2$s</xliff:g> memory used</string>
index 67a4b15..33ce5ad 100644 (file)
@@ -289,31 +289,36 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
 
     private void updateTimeoutPreferenceDescription(long currentTimeout) {
         TimeoutListPreference preference = mScreenTimeoutPreference;
+        final CharSequence[] entries = preference.getEntries();
+        final CharSequence[] values = preference.getEntryValues();
         String summary;
         if (preference.isDisabledByAdmin()) {
             summary = getString(R.string.disabled_by_policy_title);
-        } else if (currentTimeout < 0) {
-            // Unsupported value
-            summary = "";
         } else {
-            final CharSequence[] entries = preference.getEntries();
-            final CharSequence[] values = preference.getEntryValues();
-            if (entries == null || entries.length == 0) {
-                summary = "";
-            } else {
-                int best = 0;
-                for (int i = 0; i < values.length; i++) {
-                    long timeout = Long.parseLong(values[i].toString());
-                    if (currentTimeout >= timeout) {
-                        best = i;
-                    }
-                }
-                summary = getString(R.string.screen_timeout_summary, entries[best]);
-            }
+            CharSequence timeoutDescription = getTimeoutDescription(
+                    currentTimeout, entries, values);
+            summary = timeoutDescription == null ? ""
+                    : getString(R.string.screen_timeout_summary, timeoutDescription);
         }
         preference.setSummary(summary);
     }
 
+    private static CharSequence getTimeoutDescription(
+        long currentTimeout, CharSequence[] entries, CharSequence[] values) {
+        if (currentTimeout < 0 || entries == null || values == null
+                || values.length != entries.length) {
+            return null;
+        }
+
+        for (int i = 0; i < values.length; i++) {
+            long timeout = Long.parseLong(values[i].toString());
+            if (currentTimeout == timeout) {
+                return entries[i];
+            }
+        }
+        return null;
+    }
+
     @Override
     public void onResume() {
         super.onResume();
@@ -472,11 +477,17 @@ public class DisplaySettings extends SettingsPreferenceFragment implements
         }
 
         private void updateSummary() {
-            boolean auto = Settings.System.getInt(mContext.getContentResolver(),
-                    SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
-                    == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
-            mLoader.setSummary(this, mContext.getString(auto ? R.string.display_summary_on
-                    : R.string.display_summary_off));
+            final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(),
+                    SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
+            final CharSequence[] entries =
+                    mContext.getResources().getTextArray(R.array.screen_timeout_entries);
+            final CharSequence[] values =
+                    mContext.getResources().getTextArray(R.array.screen_timeout_values);
+            final CharSequence timeoutDescription = getTimeoutDescription(
+                    currentTimeout, entries, values);
+            final String summary = timeoutDescription == null ? ""
+                    : mContext.getString(R.string.display_summary, timeoutDescription);
+            mLoader.setSummary(this, summary);
         }
     }
 
diff --git a/tests/robotests/src/com/android/settings/DisplaySettingsTest.java b/tests/robotests/src/com/android/settings/DisplaySettingsTest.java
new file mode 100644 (file)
index 0000000..5af8544
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.settings;
+
+import android.app.Activity;
+import android.provider.Settings.System;
+
+import com.android.settings.dashboard.SummaryLoader;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class DisplaySettingsTest {
+
+  private Activity mActivity;
+  @Mock private SummaryLoader mSummaryLoader;
+  private SummaryLoader.SummaryProvider mSummaryProvider;
+
+  @Before
+  public void setUp() {
+    MockitoAnnotations.initMocks(this);
+    mActivity = Robolectric.buildActivity(Activity.class).get();
+    mSummaryProvider = DisplaySettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(
+            mActivity, mSummaryLoader);
+  }
+
+  @Test
+  public void testInvalidTimeouts_summaryShouldBeEmpty() {
+    System.putLong(mActivity.getContentResolver(), SCREEN_OFF_TIMEOUT, -1);
+    assertEquals(System.getLong(mActivity.getContentResolver(), SCREEN_OFF_TIMEOUT, 0), -1);
+    mSummaryProvider.setListening(true);
+
+    System.putLong(mActivity.getContentResolver(), SCREEN_OFF_TIMEOUT, 1234);
+    mSummaryProvider.setListening(true);
+
+    verify(mSummaryLoader, times(2)).setSummary(mSummaryProvider, "");
+  }
+
+  @Test
+  public void testValidTimeouts_summaryShouldNotBeEmpty() {
+    final CharSequence[] values =
+            mActivity.getResources().getTextArray(R.array.screen_timeout_values);
+
+    for (CharSequence value : values) {
+      long timeout = Long.parseLong(value.toString());
+      System.putLong(mActivity.getContentResolver(), SCREEN_OFF_TIMEOUT, timeout);
+      assertEquals(System.getLong(mActivity.getContentResolver(), SCREEN_OFF_TIMEOUT, 0), timeout);
+      mSummaryProvider.setListening(true);
+    }
+
+    verify(mSummaryLoader, never()).setSummary(mSummaryProvider, "");
+  }
+}