diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9f3f0291716..4b93d8ced17 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -8161,4 +8161,11 @@
Make %1$s your autofill app? %1$s will be able to read your screen and fill fields in other apps.
+
+ Device theme
+
+ Default
+
+ Changing the theme requires a restart.
+
diff --git a/res/xml/ia_display_settings.xml b/res/xml/ia_display_settings.xml
index 946466b502f..ed026f10ff9 100644
--- a/res/xml/ia_display_settings.xml
+++ b/res/xml/ia_display_settings.xml
@@ -105,6 +105,11 @@
android:title="@string/tap_to_wake"
android:summary="@string/tap_to_wake_summary" />
+
+
{
+ mUiModeManager.setTheme(defaultToNull((String) newValue));
+ ((ListPreference) preference).setValue((String) newValue);
+ };
+ new AlertDialog.Builder(mContext)
+ .setTitle(R.string.change_theme_reboot)
+ .setPositiveButton(com.android.internal.R.string.global_action_restart, onConfirm)
+ .setNegativeButton(android.R.string.cancel, null)
+ .show();
+ return false;
+ }
+
+ @Override
+ public boolean isAvailable() {
+ String[] themes = mUiModeManager.getAvailableThemes();
+ return themes != null && themes.length > 1;
+ }
+
+ private String nullToDefault(String input) {
+ if (input == null) {
+ return mContext.getString(R.string.default_theme);
+ }
+ return input;
+ }
+
+ private String defaultToNull(String input) {
+ if (mContext.getString(R.string.default_theme).equals(input)) {
+ return null;
+ }
+ return input;
+ }
+}
diff --git a/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java b/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java
new file mode 100644
index 00000000000..231787e5e12
--- /dev/null
+++ b/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.core;
+
+import static junit.framework.TestCase.assertNotNull;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.v7.preference.ListPreference;
+
+import com.android.settings.R;
+import com.android.settings.display.ThemePreferenceController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class ThemePreferenceControllerTest {
+
+ private UiModeManager mMockUiModeManager;
+ private ContextWrapper mContext;
+ private ThemePreferenceController mPreferenceController;
+
+ @Before
+ public void setup() {
+ mMockUiModeManager = mock(UiModeManager.class);
+ mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
+ @Override
+ public Object getSystemService(String name) {
+ if (Context.UI_MODE_SERVICE.equals(name)) {
+ return mMockUiModeManager;
+ }
+ return super.getSystemService(name);
+ }
+ };
+ mPreferenceController = new ThemePreferenceController(mContext);
+ }
+
+ @Test
+ public void testUpdateState() {
+ when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[] {
+ null,
+ "Theme1",
+ "Theme2",
+ });
+ when(mMockUiModeManager.getTheme()).thenReturn("Theme1");
+ ListPreference pref = mock(ListPreference.class);
+ mPreferenceController.updateState(pref);
+ ArgumentCaptor arg = ArgumentCaptor.forClass(String[].class);
+ verify(pref).setEntries(arg.capture());
+
+ String[] entries = arg.getValue();
+ assertEquals(3, entries.length);
+ assertNotNull(entries[0]);
+ assertEquals("Theme1", entries[1]);
+ assertEquals("Theme2", entries[2]);
+
+ verify(pref).setEntryValues(arg.capture());
+ String[] entryValues = arg.getValue();
+ assertEquals(3, entryValues.length);
+ assertNotNull(entryValues[0]);
+ assertEquals("Theme1", entryValues[1]);
+ assertEquals("Theme2", entryValues[2]);
+
+ verify(pref).setValue(eq("Theme1"));
+ }
+
+ @Test
+ public void testAvailable_false() {
+ when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[1]);
+ assertFalse(mPreferenceController.isAvailable());
+ }
+
+ @Test
+ public void testAvailable_true() {
+ when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[2]);
+ assertTrue(mPreferenceController.isAvailable());
+ }
+}