Merge "[Settings] Fix conflict of AOSP merge on wifi call" am: 95e1c8d099
am: d7f0fb6cc1
Change-Id: Ic6268671ff092174866dbd6341a9d7113bd2a4bd
diff --git a/res/layout/battery_header.xml b/res/layout/battery_header.xml
index b3b699a..dca0972 100644
--- a/res/layout/battery_header.xml
+++ b/res/layout/battery_header.xml
@@ -44,16 +44,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
- android:minLines="2"
+ android:minLines="3"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small"/>
android:textColor="?android:attr/textColorPrimary"/>
- <TextView
- android:id="@+id/summary2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small"/>
- android:textColor="?android:attr/textColorPrimary"/>
</LinearLayout>
<com.android.settings.fuelgauge.BatteryMeterView
diff --git a/res/layout/dark_ui_activation_button.xml b/res/layout/dark_ui_activation_button.xml
new file mode 100644
index 0000000..5f9eefc
--- /dev/null
+++ b/res/layout/dark_ui_activation_button.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2019 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.
+ -->
+
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:minHeight="?android:attr/listPreferredItemHeight">
+
+ <Button
+ android:id="@+id/dark_ui_turn_on_button"
+ style="@style/ActionPrimaryButton"
+ android:layout_marginStart="@dimen/screen_margin_sides"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical" />
+
+ <Button
+ android:id="@+id/dark_ui_turn_off_button"
+ style="@style/ActionSecondaryButton"
+ android:layout_marginStart="@dimen/screen_margin_sides"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_vertical" />
+
+</LinearLayout>
\ No newline at end of file
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 65d9c2e..ef75d03 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -100,6 +100,14 @@
<item>1800000</item>
</string-array>
+ <!-- Dark theme scheduling preferences [CHAR LIMIT=NONE] -->
+ <string-array name="dark_ui_scheduler_preference_titles">
+ <!-- 1: None -->
+ <item>@string/dark_ui_auto_mode_never</item>
+ <!-- 2: Auto -->
+ <item>@string/dark_ui_auto_mode_auto</item>
+ </string-array>
+
<!-- Security settings. The delay after screen is turned off until device locks.
These are shown in a list dialog. -->
<string-array name="lock_after_timeout_entries">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 47ca38e..b25ec1a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -989,7 +989,7 @@
<!-- Button text in face settings which lets the user enroll their face [CHAR LIMIT=40] -->
<string name="security_settings_face_settings_enroll">Set up face unlock</string>
<!-- Text shown in face settings explaining what your face can be used for. [CHAR LIMIT=NONE] -->
- <string name="security_settings_face_settings_footer">Use face unlock to unlock your device, sign in to apps, and confirm payments.\n\nKeep in mind:\nLooking at the phone can unlock it when you don\u2019t intend to.\n\nYour phone can be unlocked by someone else if it\u2019s held up to your face, even if your eyes are closed.\n\nYour phone can be unlocked by someone who looks a lot like you, say, an identical sibling.</string>
+ <string name="security_settings_face_settings_footer">Use face unlock to unlock your device, sign in to apps, and confirm payments.\n\nKeep in mind:\nLooking at the phone can unlock it when you don\u2019t intend to.\n\nYour phone can be unlocked by someone else if it\u2019s held up to your face while your eyes are open.\n\nYour phone can be unlocked by someone who looks a lot like you, say, an identical sibling.</string>
<!-- Dialog title shown when the user removes an enrollment [CHAR LIMIT=35] -->
<string name="security_settings_face_settings_remove_dialog_title">Delete face data?</string>
<!-- Dialog contents shown when the user removes an enrollment [CHAR LIMIT=NONE] -->
@@ -2919,6 +2919,40 @@
<!-- Night display slice screen, subtitle of intensity setting when night light is off. [CHAR LIMIT=30] -->
<string name="night_display_not_currently_on">Night Light not currently on</string>
+ <!-- Dark ui screen-->
+ <!-- Display settings screen, activation button action for manual mode. [CHAR LIMIT=40] -->
+ <string name="dark_ui_activation_on_manual">Turn on now</string>
+ <!-- Display settings screen, deactivation button action for manual mode. [CHAR LIMIT=40] -->
+ <string name="dark_ui_activation_off_manual">Turn off now</string>
+ <!-- Display settings screen, activation button action for sunset-to-sunrise schedule [CHAR LIMIT=40] -->
+ <string name="dark_ui_activation_on_auto">Turn on until sunrise</string>
+ <!-- Display settings screen, deactivation button action for sunset-to-sunrise schedule [CHAR LIMIT=40] -->
+ <string name="dark_ui_activation_off_auto">Turn off until sunset</string>
+ <!-- Dark UI screen, setting option name to enable Dark UI [CHAR LIMIT=30] -->
+ <string name="dark_ui_title">Dark Mode</string>
+ <!-- Dark UI screen, setting option name to configure whether Dark UI turn on/off automatically. [CHAR LIMIT=30] -->
+ <string name="dark_ui_auto_mode_title">Schedule</string>
+ <!-- Dark UI screen, setting option value for Dark UI to *never* turn on/off automatically. [CHAR LIMIT=30] -->
+ <string name="dark_ui_auto_mode_never">None</string>
+ <!-- Dark UIscreen, setting option value for Dark UI to turn on/off automatically at sunset/sunrise. [CHAR LIMIT=32] -->
+ <string name="dark_ui_auto_mode_auto">Turns on from sunset to sunrise</string>
+ <!-- Dark UIscreen, setting option name controlling the current activation status. [CHAR LIMIT=30] -->
+ <string name="dark_ui_status_title">Status</string>
+ <!-- Display settings screen, summary format of Dark UI when off. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_off">Off / <xliff:g name="auto_mode_summary" example="Never turn on automatically">%1$s</xliff:g></string>
+ <!-- Display settings screen, summary of Dark UI when off and will *never* turn on automatically. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_off_auto_mode_never">Will never turn on automatically</string>
+ <!-- Display settings screen, summary of Dark UI when off and will turn on automatically at sunset. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_off_auto_mode_auto">Will turn on automatically at sunset</string>
+ <!-- Display settings screen, summary format of Dark UI when on. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_on">On / <xliff:g name="auto_mode_summary" example="Never turn off automatically">%1$s</xliff:g></string>
+ <!-- Display settings screen, summary of Dark UI when on and will *never* turn off automatically. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_on_auto_mode_never">Will never turn off automatically</string>
+ <!-- Display settings screen, summary of Dark UI when on and will turn off automatically at sunrise. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_summary_on_auto_mode_auto">Will turn off automatically at sunrise</string>
+ <!-- Dark theme screen, description of Dark theme feature. [CHAR LIMIT=NONE] -->
+ <string name="dark_ui_text">Dark theme uses true black to help keep your battery alive longer. Dark theme schedules wait to turn on until your screen is off.</string>
+
<!-- Sound & display settings screen, setting option name to change screen timeout -->
<string name="screen_timeout">Screen timeout</string>
<!-- Sound & display settings screen, setting option name to change screen timeout [CHAR LIMIT=30] -->
@@ -7042,6 +7076,7 @@
<string name="help_url_connected_devices" translatable="false"></string>
<string name="help_url_apps_and_notifications" translatable="false"></string>
<string name="help_url_night_display" translatable="false"></string>
+ <string name="help_url_dark_theme" translatable="false"></string>
<string name="help_url_screen_saver" translatable="false"></string>
<string name="help_url_pickup_gesture" translatable="false"></string>
<string name="help_url_storage_dashboard" translatable="false"></string>
@@ -10137,6 +10172,12 @@
<!-- [CHAR_LIMIT=40] Positive button text in dark theme notification -->
<string name="dark_ui_settings_dialog_acknowledge">Got it</string>
+ <!-- [CHAR_LIMIT=50] Title string in the dark theme slice(suggestion) -->
+ <string name="dark_theme_slice_title">Try Dark theme</string>
+
+ <!-- [CHAR_LIMIT=50] Subtitle string in the dark theme slice(suggestion) -->
+ <string name="dark_theme_slice_subtitle">Helps extend battery life</string>
+
<!-- [CHAR LIMIT=60] Name of dev option to enable extra quick settings tiles -->
<string name="quick_settings_developer_tiles">Quick settings developer tiles</string>
diff --git a/res/xml/dark_mode_settings.xml b/res/xml/dark_mode_settings.xml
new file mode 100644
index 0000000..9247a0c
--- /dev/null
+++ b/res/xml/dark_mode_settings.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 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.
+-->
+
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:settings="http://schemas.android.com/apk/res-auto"
+ android:title="@string/dark_ui_mode"
+ android:key="dark_ui_mode_screen"
+ settings:keywords="@string/keywords_dark_ui_mode">
+
+ <DropDownPreference
+ android:key="dark_ui_auto_mode"
+ android:title="@string/dark_ui_auto_mode_title"
+ android:summary="%s"
+ android:entries="@array/dark_ui_scheduler_preference_titles"
+ android:entryValues="@array/dark_ui_scheduler_preference_titles"
+ settings:controller="com.android.settings.display.darkmode.DarkModeScheduleSelectorController"
+ settings:keywords="@string/keywords_dark_ui_mode"/>
+
+ <com.android.settingslib.widget.LayoutPreference
+ android:key="dark_ui_activated"
+ android:title="@string/dark_ui_title"
+ android:selectable="false"
+ android:layout="@layout/dark_ui_activation_button"
+ settings:allowDividerBelow="true"
+ settings:controller="com.android.settings.display.darkmode.DarkModeActivationPreferenceController"
+ settings:keywords="@string/keywords_dark_ui_mode"/>
+
+ <com.android.settingslib.widget.FooterPreference
+ android:key="dark_ui_footer"
+ android:title="@string/dark_ui_text"
+ android:selectable="false"
+ settings:searchable="false"
+ settings:allowDividerAbove="true"/>
+
+</PreferenceScreen>
\ No newline at end of file
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index c45e6a2..9bc6710 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -31,6 +31,15 @@
<intent android:action="com.android.intent.action.SHOW_BRIGHTNESS_DIALOG" />
</com.android.settingslib.RestrictedPreference>
+ <com.android.settings.display.darkmode.DarkModePreference
+ android:key="dark_ui_mode"
+ android:title="@string/dark_ui_mode"
+ android:fragment="com.android.settings.display.darkmode.DarkModeSettingsFragment"
+ android:widgetLayout="@null"
+ settings:widgetLayout="@null"
+ settings:controller="com.android.settings.display.DarkUIPreferenceController"
+ settings:searchable="false"/>
+
<com.android.settings.display.NightDisplayPreference
android:key="night_display"
android:title="@string/night_display_title"
@@ -54,13 +63,6 @@
settings:controller="com.android.settings.display.WallpaperPreferenceController">
</com.android.settingslib.RestrictedPreference>
-
- <SwitchPreference
- android:key="dark_ui_mode"
- android:title="@string/dark_ui_mode"
- settings:keywords="@string/keywords_dark_ui_mode"
- settings:controller="com.android.settings.display.DarkUIPreferenceController"/>
-
<!-- Cross-listed item, if you change this, also change it in power_usage_summary.xml -->
<com.android.settings.display.TimeoutListPreference
android:key="screen_timeout"
diff --git a/res/xml/security_settings_face.xml b/res/xml/security_settings_face.xml
index 1561e8c..7d59f30 100644
--- a/res/xml/security_settings_face.xml
+++ b/res/xml/security_settings_face.xml
@@ -52,6 +52,13 @@
android:key="security_settings_face_manage_category"
android:title="@string/security_settings_face_settings_require_category">
<SwitchPreference
+ android:key="security_settings_face_require_attention"
+ android:title="@string/security_settings_face_settings_require_attention"
+ android:summary="@string/security_settings_face_settings_require_attention_details"
+ app:keywords="@string/keywords_face_unlock"
+ app:controller="com.android.settings.biometrics.face.FaceSettingsAttentionPreferenceController"/>
+
+ <SwitchPreference
android:key="security_settings_face_require_confirmation"
android:title="@string/security_settings_face_settings_require_confirmation"
android:summary="@string/security_settings_face_settings_require_confirmation_details"
diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java
index eb77d4a..06e4694 100644
--- a/src/com/android/settings/DisplaySettings.java
+++ b/src/com/android/settings/DisplaySettings.java
@@ -24,7 +24,6 @@
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.display.BrightnessLevelPreferenceController;
import com.android.settings.display.CameraGesturePreferenceController;
-import com.android.settings.display.DarkUIPreferenceController;
import com.android.settings.display.LiftToWakePreferenceController;
import com.android.settings.display.NightDisplayPreferenceController;
import com.android.settings.display.NightModePreferenceController;
@@ -67,7 +66,6 @@
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
- use(DarkUIPreferenceController.class).setParentFragment(this);
}
@Override
diff --git a/src/com/android/settings/biometrics/face/FaceSettings.java b/src/com/android/settings/biometrics/face/FaceSettings.java
index b8bb030..902d9a1 100644
--- a/src/com/android/settings/biometrics/face/FaceSettings.java
+++ b/src/com/android/settings/biometrics/face/FaceSettings.java
@@ -60,6 +60,7 @@
private FaceManager mFaceManager;
private int mUserId;
private byte[] mToken;
+ private FaceSettingsAttentionPreferenceController mAttentionController;
private FaceSettingsRemoveButtonPreferenceController mRemoveController;
private FaceSettingsEnrollButtonPreferenceController mEnrollController;
private List<AbstractPreferenceController> mControllers;
@@ -125,11 +126,12 @@
Preference keyguardPref = findPreference(FaceSettingsKeyguardPreferenceController.KEY);
Preference appPref = findPreference(FaceSettingsAppPreferenceController.KEY);
+ Preference attentionPref = findPreference(FaceSettingsAttentionPreferenceController.KEY);
Preference confirmPref = findPreference(FaceSettingsConfirmPreferenceController.KEY);
Preference bypassPref =
findPreference(FaceSettingsLockscreenBypassPreferenceController.KEY);
mTogglePreferences = new ArrayList<>(
- Arrays.asList(keyguardPref, appPref, confirmPref, bypassPref));
+ Arrays.asList(keyguardPref, appPref, attentionPref, confirmPref, bypassPref));
mRemoveButton = findPreference(FaceSettingsRemoveButtonPreferenceController.KEY);
mEnrollButton = findPreference(FaceSettingsEnrollButtonPreferenceController.KEY);
@@ -173,6 +175,7 @@
finish();
}
} else {
+ mAttentionController.setToken(mToken);
mEnrollController.setToken(mToken);
}
@@ -193,6 +196,7 @@
mToken = data.getByteArrayExtra(
ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
if (mToken != null) {
+ mAttentionController.setToken(mToken);
mEnrollController.setToken(mToken);
}
}
@@ -236,7 +240,9 @@
mControllers = buildPreferenceControllers(context, getSettingsLifecycle());
// There's no great way of doing this right now :/
for (AbstractPreferenceController controller : mControllers) {
- if (controller instanceof FaceSettingsRemoveButtonPreferenceController) {
+ if (controller instanceof FaceSettingsAttentionPreferenceController) {
+ mAttentionController = (FaceSettingsAttentionPreferenceController) controller;
+ } else if (controller instanceof FaceSettingsRemoveButtonPreferenceController) {
mRemoveController = (FaceSettingsRemoveButtonPreferenceController) controller;
mRemoveController.setListener(mRemovalListener);
mRemoveController.setActivity((SettingsActivity) getActivity());
@@ -255,6 +261,7 @@
controllers.add(new FaceSettingsVideoPreferenceController(context));
controllers.add(new FaceSettingsKeyguardPreferenceController(context));
controllers.add(new FaceSettingsAppPreferenceController(context));
+ controllers.add(new FaceSettingsAttentionPreferenceController(context));
controllers.add(new FaceSettingsRemoveButtonPreferenceController(context));
controllers.add(new FaceSettingsFooterPreferenceController(context));
controllers.add(new FaceSettingsConfirmPreferenceController(context));
diff --git a/src/com/android/settings/bluetooth/BluetoothSliceBuilder.java b/src/com/android/settings/bluetooth/BluetoothSliceBuilder.java
index 18df872..5a4ee1f 100644
--- a/src/com/android/settings/bluetooth/BluetoothSliceBuilder.java
+++ b/src/com/android/settings/bluetooth/BluetoothSliceBuilder.java
@@ -136,6 +136,6 @@
final Intent intent = new Intent(ACTION_BLUETOOTH_SLICE_CHANGED)
.setClass(context, SliceBroadcastReceiver.class);
return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
- PendingIntent.FLAG_CANCEL_CURRENT);
+ PendingIntent.FLAG_UPDATE_CURRENT);
}
}
diff --git a/src/com/android/settings/display/DarkUIPreferenceController.java b/src/com/android/settings/display/DarkUIPreferenceController.java
index d3d30b5..7e550d7 100644
--- a/src/com/android/settings/display/DarkUIPreferenceController.java
+++ b/src/com/android/settings/display/DarkUIPreferenceController.java
@@ -21,6 +21,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.res.Configuration;
import android.os.PowerManager;
import android.provider.Settings;
@@ -28,10 +29,10 @@
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
-import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.display.darkmode.DarkModePreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
@@ -44,7 +45,7 @@
public static final int DIALOG_SEEN = 1;
@VisibleForTesting
- SwitchPreference mPreference;
+ Preference mPreference;
private UiModeManager mUiModeManager;
private PowerManager mPowerManager;
@@ -68,7 +69,8 @@
@Override
public boolean isChecked() {
- return mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES;
+ return (mContext.getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
}
@Override
@@ -92,15 +94,12 @@
showDarkModeDialog();
return false;
}
- mUiModeManager.setNightMode(isChecked
- ? UiModeManager.MODE_NIGHT_YES
- : UiModeManager.MODE_NIGHT_NO);
- return true;
+ return mUiModeManager.setNightModeActivated(isChecked);
}
private void showDarkModeDialog() {
final DarkUIInfoDialogFragment frag = new DarkUIInfoDialogFragment();
- if (mFragment.getFragmentManager() != null) {
+ if (mFragment != null && mFragment.getFragmentManager() != null) {
frag.show(mFragment.getFragmentManager(), getClass().getName());
}
}
@@ -113,12 +112,10 @@
boolean isBatterySaver = isPowerSaveMode();
mPreference.setEnabled(!isBatterySaver);
if (isBatterySaver) {
- int stringId = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES
+ int stringId = isChecked()
? R.string.dark_ui_mode_disabled_summary_dark_theme_on
: R.string.dark_ui_mode_disabled_summary_dark_theme_off;
mPreference.setSummary(mContext.getString(stringId));
- } else {
- mPreference.setSummary(null);
}
}
@@ -127,22 +124,17 @@
return mPowerManager.isPowerSaveMode();
}
-
- @VisibleForTesting
- void setUiModeManager(UiModeManager uiModeManager) {
- mUiModeManager = uiModeManager;
- }
-
- public void setParentFragment(Fragment fragment) {
- mFragment = fragment;
- }
-
@Override
public void onStart() {
mContext.registerReceiver(mReceiver,
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
}
+ // used by AccessibilitySettings
+ public void setParentFragment(Fragment fragment) {
+ mFragment = fragment;
+ }
+
@Override
public void onStop() {
mContext.unregisterReceiver(mReceiver);
diff --git a/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceController.java b/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceController.java
new file mode 100644
index 0000000..98d4b14
--- /dev/null
+++ b/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceController.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.view.View;
+import android.widget.Button;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settingslib.widget.LayoutPreference;
+
+/**
+ * Controller for activate/deactivate night mode button
+ */
+public class DarkModeActivationPreferenceController extends BasePreferenceController {
+ private final UiModeManager mUiModeManager;
+ private Button mTurnOffButton;
+ private Button mTurnOnButton;
+
+ public DarkModeActivationPreferenceController(Context context,
+ String preferenceKey) {
+ super(context, preferenceKey);
+
+ mUiModeManager = context.getSystemService(UiModeManager.class);
+ }
+
+ @Override
+ public final void updateState(Preference preference) {
+ final boolean active = (mContext.getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
+ updateNightMode(active);
+ }
+
+ private void updateNightMode(boolean active) {
+ final int autoMode = mUiModeManager.getNightMode();
+ String buttonText;
+
+ if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
+ buttonText = mContext.getString(active
+ ? R.string.dark_ui_activation_off_auto
+ : R.string.dark_ui_activation_on_auto);
+ } else {
+ buttonText = mContext.getString(active
+ ? R.string.dark_ui_activation_off_manual
+ : R.string.dark_ui_activation_on_manual);
+ }
+ if (active) {
+ mTurnOnButton.setVisibility(View.GONE);
+ mTurnOffButton.setVisibility(View.VISIBLE);
+ mTurnOffButton.setText(buttonText);
+ } else {
+ mTurnOnButton.setVisibility(View.VISIBLE);
+ mTurnOffButton.setVisibility(View.GONE);
+ mTurnOnButton.setText(buttonText);
+ }
+ }
+
+ @Override
+ public CharSequence getSummary() {
+ final boolean isActivated = (mContext.getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
+ final int autoMode = mUiModeManager.getNightMode();
+ if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
+ return mContext.getString(isActivated
+ ? R.string.dark_ui_summary_on_auto_mode_auto
+ : R.string.dark_ui_summary_off_auto_mode_auto);
+ } else {
+ return mContext.getString(isActivated
+ ? R.string.dark_ui_summary_on_auto_mode_never
+ : R.string.dark_ui_summary_off_auto_mode_never);
+ }
+ }
+
+ private final View.OnClickListener mListener = new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ final boolean active = (mContext.getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
+ mUiModeManager.setNightModeActivated(!active);
+ updateNightMode(!active);
+ }
+ };
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+
+ final LayoutPreference preference = screen.findPreference(getPreferenceKey());
+ mTurnOnButton = preference.findViewById(R.id.dark_ui_turn_on_button);
+ mTurnOnButton.setOnClickListener(mListener);
+ mTurnOffButton = preference.findViewById(R.id.dark_ui_turn_off_button);
+ mTurnOffButton.setOnClickListener(mListener);
+ }
+
+ @Override
+ public int getAvailabilityStatus() {
+ return AVAILABLE;
+ }
+}
diff --git a/src/com/android/settings/display/darkmode/DarkModeObserver.java b/src/com/android/settings/display/darkmode/DarkModeObserver.java
new file mode 100644
index 0000000..1c076f7
--- /dev/null
+++ b/src/com/android/settings/display/darkmode/DarkModeObserver.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.provider.Settings;
+import com.android.internal.annotations.VisibleForTesting;
+
+/**
+ * Observes changes for dark night settings*/
+public class DarkModeObserver {
+ private ContentObserver mContentObserver;
+ private Runnable mCallback;
+ private Context mContext;
+
+ public DarkModeObserver(Context context) {
+ mContext = context;
+ mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
+ @Override
+ public void onChange(boolean selfChange, Uri uri) {
+ super.onChange(selfChange, uri);
+ final String setting = uri == null ? null : uri.getLastPathSegment();
+ if (setting != null && mCallback != null) {
+ switch (setting) {
+ case Settings.Secure.UI_NIGHT_MODE:
+ mCallback.run();
+ break;
+ default:
+ }
+ }
+ }
+ };
+ }
+
+ /**
+ * subscribe callback when night mode changed in the database
+ *
+ * @param callback the callback that gets triggered when subscribed
+ */
+ public void subscribe(Runnable callback) {
+ callback.run();
+ mCallback = callback;
+ final Uri uri = Settings.Secure.getUriFor(Settings.Secure.UI_NIGHT_MODE);
+ mContext.getContentResolver().registerContentObserver(uri, false, mContentObserver);
+ }
+
+ /**
+ * unsubscribe from dark ui database changes
+ */
+ public void unsubscribe() {
+ mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+ mCallback = null;
+ }
+
+ @VisibleForTesting
+ protected void setContentObserver(ContentObserver co) {
+ mContentObserver = co;
+ }
+
+ @VisibleForTesting
+ protected ContentObserver getContentObserver() {
+ return mContentObserver;
+ }
+}
diff --git a/src/com/android/settings/display/darkmode/DarkModePreference.java b/src/com/android/settings/display/darkmode/DarkModePreference.java
new file mode 100644
index 0000000..2a8909f
--- /dev/null
+++ b/src/com/android/settings/display/darkmode/DarkModePreference.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.util.AttributeSet;
+import com.android.settings.R;
+import com.android.settings.widget.MasterSwitchPreference;
+
+/**
+ * component for the display settings dark ui summary*/
+public class DarkModePreference extends MasterSwitchPreference {
+
+ private UiModeManager mUiModeManager;
+ private DarkModeObserver mDarkModeObserver;
+ private Runnable mCallback;
+
+ public DarkModePreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ mDarkModeObserver = new DarkModeObserver(context);
+ mUiModeManager = context.getSystemService(UiModeManager.class);
+ mCallback = () -> {
+ updateSummary();
+ };
+ mDarkModeObserver.subscribe(mCallback);
+ }
+
+ @Override
+ public void onAttached() {
+ super.onAttached();
+ mDarkModeObserver.subscribe(mCallback);
+ }
+
+ @Override
+ public void onDetached() {
+ super.onDetached();
+ mDarkModeObserver.unsubscribe();
+ }
+
+ private void updateSummary() {
+ final boolean active = (getContext().getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
+ final boolean auto = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO;
+
+ String detail;
+ if (active) {
+ detail = getContext().getString(auto
+ ? R.string.dark_ui_summary_on_auto_mode_auto
+ : R.string.dark_ui_summary_on_auto_mode_never);
+ } else {
+ detail = getContext().getString(auto
+ ? R.string.dark_ui_summary_off_auto_mode_auto
+ : R.string.dark_ui_summary_off_auto_mode_never);
+ }
+ String summary = getContext().getString(active
+ ? R.string.dark_ui_summary_on
+ : R.string.dark_ui_summary_off, detail);
+
+ setSummary(summary);
+ }
+}
diff --git a/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorController.java b/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorController.java
new file mode 100644
index 0000000..ab7ea92
--- /dev/null
+++ b/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorController.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.res.Configuration;
+import androidx.preference.DropDownPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+/**
+ * Controller for the dark ui option dropdown
+ */
+public class DarkModeScheduleSelectorController extends BasePreferenceController
+ implements Preference.OnPreferenceChangeListener {
+
+ private final UiModeManager mUiModeManager;
+ private boolean mPreferenceSet = false;
+ private DropDownPreference mPreference;
+ private String mCurrentMode;
+
+ public DarkModeScheduleSelectorController(Context context, String key) {
+ super(context, key);
+ mUiModeManager = context.getSystemService(UiModeManager.class);
+ }
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+
+ mPreference = screen.findPreference(getPreferenceKey());
+ }
+
+ @Override
+ public int getAvailabilityStatus() {
+ return BasePreferenceController.AVAILABLE;
+ }
+
+ @Override
+ public final void updateState(Preference preference) {
+ mCurrentMode =
+ mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO
+ ? mContext.getString(R.string.dark_ui_auto_mode_auto)
+ : mContext.getString(R.string.dark_ui_auto_mode_never);
+ mPreference.setValue(mCurrentMode);
+ }
+ @Override
+ public final boolean onPreferenceChange(Preference preference, Object newValue) {
+ String newMode = (String) newValue;
+ if (newMode == mCurrentMode) {
+ return false;
+ }
+ mCurrentMode = newMode;
+ if (mCurrentMode == mContext.getString(R.string.dark_ui_auto_mode_never)) {
+ boolean active = (mContext.getResources().getConfiguration().uiMode
+ & Configuration.UI_MODE_NIGHT_YES) != 0;
+ int mode = active ? UiModeManager.MODE_NIGHT_YES
+ : UiModeManager.MODE_NIGHT_NO;
+ mUiModeManager.setNightMode(mode);
+
+ } else if (mCurrentMode ==
+ mContext.getString(R.string.dark_ui_auto_mode_auto)) {
+ mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO);
+ }
+ return true;
+ }
+}
diff --git a/src/com/android/settings/display/darkmode/DarkModeSettingsFragment.java b/src/com/android/settings/display/darkmode/DarkModeSettingsFragment.java
new file mode 100644
index 0000000..045c2bb
--- /dev/null
+++ b/src/com/android/settings/display/darkmode/DarkModeSettingsFragment.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.app.settings.SettingsEnums;
+import com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settingslib.search.SearchIndexable;
+
+/**
+ * Settings screen for Dark UI Mode
+ */
+@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
+public class DarkModeSettingsFragment extends DashboardFragment {
+
+ private static final String TAG = "DarkModeSettingsFragment";
+ private DarkModeObserver mContentObserver;
+ private Runnable mCallback = () -> {
+ updatePreferenceStates();
+ };
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ final Context context = getContext();
+ mContentObserver = new DarkModeObserver(context);
+ }
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ // Listen for changes only while visible.
+ mContentObserver.subscribe(mCallback);
+ }
+
+ @Override
+ public void onStop() {
+ super.onStop();
+ // Stop listening for state changes.
+ mContentObserver.unsubscribe();
+ }
+
+ @Override
+ protected int getPreferenceScreenResId() {
+ return R.xml.dark_mode_settings;
+ }
+
+ @Override
+ public int getHelpResource() {
+ return R.string.help_url_dark_theme;
+ }
+
+ @Override
+ protected String getLogTag() {
+ return TAG;
+ }
+
+ @Override
+ public int getMetricsCategory() {
+ return SettingsEnums.DARK_UI_SETTINGS;
+ }
+
+ public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+ new BaseSearchIndexProvider();
+}
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
index 3064d4f..bfa43d1 100644
--- a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
@@ -55,8 +55,6 @@
TextView mBatteryPercentText;
@VisibleForTesting
TextView mSummary1;
- @VisibleForTesting
- TextView mSummary2;
private Activity mActivity;
private PreferenceFragmentCompat mHost;
@@ -90,7 +88,6 @@
.findViewById(R.id.battery_header_icon);
mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent);
mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
- mSummary2 = mBatteryLayoutPref.findViewById(R.id.summary2);
quickUpdateHeaderPreference();
}
@@ -115,9 +112,6 @@
} else {
mSummary1.setText(info.remainingLabel);
}
- // Clear this just to be sure we don't get UI jank on re-entering this view from another
- // activity.
- mSummary2.setText("");
mBatteryMeterView.setBatteryLevel(info.batteryLevel);
mBatteryMeterView.setCharging(!info.discharging);
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 880255b..d25c2d4 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -19,7 +19,6 @@
import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType;
import android.app.settings.SettingsEnums;
-import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
@@ -27,7 +26,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.provider.SearchIndexableResource;
-import android.provider.Settings;
import android.provider.Settings.Global;
import android.text.format.Formatter;
import android.view.Menu;
@@ -162,7 +160,6 @@
final TextView percentRemaining =
mBatteryLayoutPref.findViewById(R.id.battery_percent);
final TextView summary1 = mBatteryLayoutPref.findViewById(R.id.summary1);
- final TextView summary2 = mBatteryLayoutPref.findViewById(R.id.summary2);
BatteryInfo oldInfo = batteryInfos.get(0);
BatteryInfo newInfo = batteryInfos.get(1);
percentRemaining.setText(Utils.formatPercentage(oldInfo.batteryLevel));
@@ -170,14 +167,13 @@
// set the text to the old estimate (copied from battery info). Note that this
// can sometimes say 0 time remaining because battery stats requires the phone
// be unplugged for a period of time before being willing ot make an estimate.
- summary1.setText(mPowerFeatureProvider.getOldEstimateDebugString(
+ final String OldEstimateString = mPowerFeatureProvider.getOldEstimateDebugString(
Formatter.formatShortElapsedTime(getContext(),
- PowerUtil.convertUsToMs(oldInfo.remainingTimeUs))));
-
- // for this one we can just set the string directly
- summary2.setText(mPowerFeatureProvider.getEnhancedEstimateDebugString(
+ PowerUtil.convertUsToMs(oldInfo.remainingTimeUs)));
+ final String NewEstimateString = mPowerFeatureProvider.getEnhancedEstimateDebugString(
Formatter.formatShortElapsedTime(getContext(),
- PowerUtil.convertUsToMs(newInfo.remainingTimeUs))));
+ PowerUtil.convertUsToMs(newInfo.remainingTimeUs)));
+ summary1.setText(OldEstimateString + "\n" + NewEstimateString);
batteryView.setBatteryLevel(oldInfo.batteryLevel);
batteryView.setCharging(!oldInfo.discharging);
diff --git a/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java b/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java
index 6d3649d..5a04143 100644
--- a/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java
+++ b/src/com/android/settings/homepage/contextualcards/ContextualCardLoader.java
@@ -37,12 +37,11 @@
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.utils.AsyncLoaderCompat;
+import com.android.settingslib.utils.ThreadUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -56,7 +55,6 @@
private static final String TAG = "ContextualCardLoader";
private static final long ELIGIBILITY_CHECKER_TIMEOUT_MS = 250;
- private final ExecutorService mExecutorService;
private final ContentObserver mObserver = new ContentObserver(
new Handler(Looper.getMainLooper())) {
@Override
@@ -76,7 +74,6 @@
ContextualCardLoader(Context context) {
super(context);
mContext = context.getApplicationContext();
- mExecutorService = Executors.newCachedThreadPool();
}
@Override
@@ -163,8 +160,8 @@
final List<Future<ContextualCard>> eligibleCards = new ArrayList<>();
for (ContextualCard card : candidates) {
- final EligibleCardChecker future = new EligibleCardChecker(mContext, card);
- eligibleCards.add(mExecutorService.submit(future));
+ final EligibleCardChecker checker = new EligibleCardChecker(mContext, card);
+ eligibleCards.add(ThreadUtils.postOnBackgroundThread(checker));
}
// Collect future and eligible cards
for (Future<ContextualCard> cardFuture : eligibleCards) {
diff --git a/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java b/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
index 385f8cd..1494293 100644
--- a/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
+++ b/src/com/android/settings/homepage/contextualcards/SettingsContextualCardProvider.java
@@ -78,6 +78,12 @@
.setCardName(CustomSliceRegistry.FACE_ENROLL_SLICE_URI.toString())
.setCardCategory(ContextualCard.Category.DEFAULT)
.build();
+ final ContextualCard darkThemeCard =
+ ContextualCard.newBuilder()
+ .setSliceUri(CustomSliceRegistry.DARK_THEME_SLICE_URI.toString())
+ .setCardName(CustomSliceRegistry.DARK_THEME_SLICE_URI.toString())
+ .setCardCategory(ContextualCard.Category.IMPORTANT)
+ .build();
final ContextualCardList cards = ContextualCardList.newBuilder()
.addCard(wifiCard)
.addCard(connectedDeviceCard)
@@ -86,6 +92,7 @@
.addCard(notificationChannelCard)
.addCard(contextualAdaptiveSleepCard)
.addCard(contextualFaceSettingsCard)
+ .addCard(darkThemeCard)
.build();
return cards;
diff --git a/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java b/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
index 66f6c81..1beac9b 100644
--- a/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
+++ b/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
@@ -23,13 +23,12 @@
import androidx.annotation.VisibleForTesting;
import com.android.settings.homepage.contextualcards.ContextualCard;
+import com.android.settingslib.utils.ThreadUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -42,7 +41,6 @@
private static final long DISPLAYABLE_CHECKER_TIMEOUT_MS = 20;
- private final ExecutorService mExecutorService;
private final Context mAppContext;
private final ConditionListener mListener;
@@ -50,7 +48,6 @@
public ConditionManager(Context context, ConditionListener listener) {
mAppContext = context.getApplicationContext();
- mExecutorService = Executors.newCachedThreadPool();
mCardControllers = new ArrayList<>();
mListener = listener;
initCandidates();
@@ -64,8 +61,8 @@
final List<Future<ContextualCard>> displayableCards = new ArrayList<>();
// Check displayable future
for (ConditionalCardController card : mCardControllers) {
- final DisplayableChecker future = new DisplayableChecker(getController(card.getId()));
- displayableCards.add(mExecutorService.submit(future));
+ final DisplayableChecker checker = new DisplayableChecker(getController(card.getId()));
+ displayableCards.add(ThreadUtils.postOnBackgroundThread(checker));
}
// Collect future and add displayable cards
for (Future<ContextualCard> cardFuture : displayableCards) {
diff --git a/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSlice.java b/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSlice.java
new file mode 100644
index 0000000..36a3980
--- /dev/null
+++ b/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSlice.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2019 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.homepage.contextualcards.slices;
+
+import static androidx.slice.builders.ListBuilder.ICON_IMAGE;
+
+import android.annotation.ColorInt;
+import android.app.PendingIntent;
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.BatteryManager;
+import android.os.Handler;
+import android.os.Looper;
+import android.util.Log;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.core.graphics.drawable.IconCompat;
+import androidx.slice.Slice;
+import androidx.slice.builders.ListBuilder;
+import androidx.slice.builders.SliceAction;
+
+import com.android.settings.R;
+import com.android.settings.Utils;
+import com.android.settings.overlay.FeatureFactory;
+import com.android.settings.slices.CustomSliceRegistry;
+import com.android.settings.slices.CustomSliceable;
+
+public class DarkThemeSlice implements CustomSliceable {
+ private static final String TAG = "DarkThemeSlice";
+ private static final int BATTERY_LEVEL_THRESHOLD = 50;
+ private static final int DELAY_TIME_EXECUTING_DARK_THEME = 200;
+
+ // Keep the slice even Dark theme mode changed when it is on HomePage
+ @VisibleForTesting
+ static boolean sKeepSliceShow;
+ @VisibleForTesting
+ static long sActiveUiSession = -1000;
+
+ private final Context mContext;
+ private final UiModeManager mUiModeManager;
+
+ public DarkThemeSlice(Context context) {
+ mContext = context;
+ mUiModeManager = context.getSystemService(UiModeManager.class);
+ }
+
+ @Override
+ public Slice getSlice() {
+ final long currentUiSession = FeatureFactory.getFactory(mContext)
+ .getSlicesFeatureProvider().getUiSessionToken();
+ if (currentUiSession != sActiveUiSession) {
+ sActiveUiSession = currentUiSession;
+ sKeepSliceShow = false;
+ }
+ if (!sKeepSliceShow && !isAvailable(mContext)) {
+ return null;
+ }
+ sKeepSliceShow = true;
+ final PendingIntent toggleAction = getBroadcastIntent(mContext);
+ @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);
+ final IconCompat icon =
+ IconCompat.createWithResource(mContext, R.drawable.dark_theme);
+ final boolean isChecked = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES;
+ return new ListBuilder(mContext, CustomSliceRegistry.DARK_THEME_SLICE_URI,
+ ListBuilder.INFINITY)
+ .setAccentColor(color)
+ .addRow(new ListBuilder.RowBuilder()
+ .setTitle(mContext.getText(R.string.dark_theme_slice_title))
+ .setTitleItem(icon, ICON_IMAGE)
+ .setSubtitle(mContext.getText(R.string.dark_theme_slice_subtitle))
+ .setPrimaryAction(
+ SliceAction.createToggle(toggleAction, null /* actionTitle */,
+ isChecked)))
+ .build();
+ }
+
+ @Override
+ public Uri getUri() {
+ return CustomSliceRegistry.DARK_THEME_SLICE_URI;
+ }
+
+ @Override
+ public void onNotifyChange(Intent intent) {
+ final boolean isChecked = intent.getBooleanExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE,
+ false);
+ // make toggle transition more smooth before dark theme takes effect
+ new Handler(Looper.getMainLooper()).postDelayed(() -> {
+ mUiModeManager.setNightMode(
+ isChecked ? UiModeManager.MODE_NIGHT_YES : UiModeManager.MODE_NIGHT_NO);
+ }, DELAY_TIME_EXECUTING_DARK_THEME);
+ }
+
+ @Override
+ public Intent getIntent() {
+ return null;
+ }
+
+ @VisibleForTesting
+ boolean isAvailable(Context context) {
+ // checking dark theme mode.
+ if (mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
+ return false;
+ }
+
+ // checking the current battery level
+ final BatteryManager batteryManager = context.getSystemService(BatteryManager.class);
+ final int level = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
+ Log.d(TAG, "battery level=" + level);
+
+ return level <= BATTERY_LEVEL_THRESHOLD;
+ }
+}
diff --git a/src/com/android/settings/homepage/contextualcards/slices/NotificationChannelSlice.java b/src/com/android/settings/homepage/contextualcards/slices/NotificationChannelSlice.java
index ff76779..bdf97a8 100644
--- a/src/com/android/settings/homepage/contextualcards/slices/NotificationChannelSlice.java
+++ b/src/com/android/settings/homepage/contextualcards/slices/NotificationChannelSlice.java
@@ -20,8 +20,6 @@
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
-import static com.android.settings.notification.ChannelListPreferenceController.ARG_FROM_SETTINGS;
-
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
@@ -62,13 +60,12 @@
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.applications.ApplicationsState;
+import com.android.settingslib.utils.ThreadUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -135,7 +132,6 @@
};
protected final Context mContext;
- private final ExecutorService mExecutorService;
@VisibleForTesting
NotificationBackend mNotificationBackend;
private NotificationBackend.AppRow mAppRow;
@@ -145,7 +141,6 @@
public NotificationChannelSlice(Context context) {
mContext = context;
mNotificationBackend = new NotificationBackend();
- mExecutorService = Executors.newCachedThreadPool();
}
@Override
@@ -376,9 +371,9 @@
// Create tasks to get notification data for multi-channel packages.
final List<Future<NotificationBackend.AppRow>> appRowTasks = new ArrayList<>();
for (PackageInfo packageInfo : packageInfoList) {
- final NotificationMultiChannelAppRow future = new NotificationMultiChannelAppRow(
+ final NotificationMultiChannelAppRow appRow = new NotificationMultiChannelAppRow(
mContext, mNotificationBackend, packageInfo);
- appRowTasks.add(mExecutorService.submit(future));
+ appRowTasks.add(ThreadUtils.postOnBackgroundThread(appRow));
}
// Get the package which has sent at least ~10 notifications and not turn off channels.
diff --git a/src/com/android/settings/network/telephony/MmsMessagePreferenceController.java b/src/com/android/settings/network/telephony/MmsMessagePreferenceController.java
index b8d2081..4d3986b 100644
--- a/src/com/android/settings/network/telephony/MmsMessagePreferenceController.java
+++ b/src/com/android/settings/network/telephony/MmsMessagePreferenceController.java
@@ -22,11 +22,8 @@
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.data.ApnSetting;
-import android.util.Log;
-import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
-import androidx.preference.SwitchPreference;
import com.android.settings.network.MobileDataContentObserver;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -41,14 +38,14 @@
private TelephonyManager mTelephonyManager;
private SubscriptionManager mSubscriptionManager;
private MobileDataContentObserver mMobileDataContentObserver;
- private SwitchPreference mPreference;
+ private PreferenceScreen mScreen;
public MmsMessagePreferenceController(Context context, String key) {
super(context, key);
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
mMobileDataContentObserver = new MobileDataContentObserver(
new Handler(Looper.getMainLooper()));
- mMobileDataContentObserver.setOnMobileDataChangedListener(()->updateState(mPreference));
+ mMobileDataContentObserver.setOnMobileDataChangedListener(()->refreshPreference());
}
@Override
@@ -79,15 +76,9 @@
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
- mPreference = screen.findPreference(getPreferenceKey());
+ mScreen = screen;
}
- @Override
- public void updateState(Preference preference) {
- super.updateState(preference);
- preference.setVisible(isAvailable());
- ((SwitchPreference) preference).setChecked(isChecked());
- }
public void init(int subId) {
mSubId = subId;
@@ -103,4 +94,10 @@
public boolean isChecked() {
return mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS);
}
+
+ private void refreshPreference() {
+ if (mScreen != null) {
+ super.displayPreference(mScreen);
+ }
+ }
}
diff --git a/src/com/android/settings/slices/CustomSliceRegistry.java b/src/com/android/settings/slices/CustomSliceRegistry.java
index 946a9d3..42d81e9 100644
--- a/src/com/android/settings/slices/CustomSliceRegistry.java
+++ b/src/com/android/settings/slices/CustomSliceRegistry.java
@@ -37,6 +37,7 @@
import com.android.settings.homepage.contextualcards.slices.BluetoothDevicesSlice;
import com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice;
import com.android.settings.homepage.contextualcards.slices.ContextualNotificationChannelSlice;
+import com.android.settings.homepage.contextualcards.slices.DarkThemeSlice;
import com.android.settings.homepage.contextualcards.slices.FaceSetupSlice;
import com.android.settings.homepage.contextualcards.slices.LowStorageSlice;
import com.android.settings.homepage.contextualcards.slices.NotificationChannelSlice;
@@ -342,6 +343,16 @@
.appendPath("media_output_indicator")
.build();
+ /**
+ * Backing Uri for the Dark theme Slice.
+ */
+ public static final Uri DARK_THEME_SLICE_URI = new Uri.Builder()
+ .scheme(ContentResolver.SCHEME_CONTENT)
+ .authority(SettingsSliceProvider.SLICE_AUTHORITY)
+ .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
+ .appendPath("dark_theme")
+ .build();
+
@VisibleForTesting
static final Map<Uri, Class<? extends CustomSliceable>> sUriToSlice;
@@ -367,6 +378,7 @@
sUriToSlice.put(NOTIFICATION_CHANNEL_SLICE_URI, NotificationChannelSlice.class);
sUriToSlice.put(STORAGE_SLICE_URI, StorageSlice.class);
sUriToSlice.put(WIFI_SLICE_URI, WifiSlice.class);
+ sUriToSlice.put(DARK_THEME_SLICE_URI, DarkThemeSlice.class);
}
public static Class<? extends CustomSliceable> getSliceClassByUri(Uri uri) {
diff --git a/src/com/android/settings/slices/CustomSliceable.java b/src/com/android/settings/slices/CustomSliceable.java
index 9566be1..92c604e 100644
--- a/src/com/android/settings/slices/CustomSliceable.java
+++ b/src/com/android/settings/slices/CustomSliceable.java
@@ -95,7 +95,7 @@
.setData(getUri())
.setClass(context, SliceBroadcastReceiver.class);
return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
- PendingIntent.FLAG_CANCEL_CURRENT);
+ PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
diff --git a/src/com/android/settings/slices/SliceBuilderUtils.java b/src/com/android/settings/slices/SliceBuilderUtils.java
index 0e51032..5585be3 100644
--- a/src/com/android/settings/slices/SliceBuilderUtils.java
+++ b/src/com/android/settings/slices/SliceBuilderUtils.java
@@ -173,7 +173,7 @@
.putExtra(EXTRA_SLICE_KEY, data.getKey())
.putExtra(EXTRA_SLICE_PLATFORM_DEFINED, data.isPlatformDefined());
return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
- PendingIntent.FLAG_CANCEL_CURRENT);
+ PendingIntent.FLAG_UPDATE_CURRENT);
}
/**
diff --git a/tests/robotests/src/com/android/settings/display/DarkUIPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/DarkUIPreferenceControllerTest.java
index 3659803..9924cb4 100644
--- a/tests/robotests/src/com/android/settings/display/DarkUIPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/DarkUIPreferenceControllerTest.java
@@ -16,25 +16,9 @@
package com.android.settings.display;
-import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
import android.content.Context;
-import android.os.Handler;
-import android.os.IPowerManager;
-import android.os.PowerManager;
-
import androidx.fragment.app.Fragment;
-import androidx.preference.PreferenceScreen;
-import androidx.preference.SwitchPreference;
-
-import org.junit.Assert;
+import com.android.settings.display.darkmode.DarkModePreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -43,6 +27,10 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
@RunWith(RobolectricTestRunner.class)
public class DarkUIPreferenceControllerTest {
@@ -57,7 +45,7 @@
mContext = spy(RuntimeEnvironment.application);
mController = spy(new DarkUIPreferenceController(mContext, "dark_ui_mode"));
mController.setParentFragment(mFragment);
- mController.mPreference = new SwitchPreference(mContext);
+ mController.mPreference = new DarkModePreference(mContext, null /* AttributeSet attrs */);
mController.onStart();
}
diff --git a/tests/robotests/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceControllerTest.java
new file mode 100644
index 0000000..24cbd07
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeActivationPreferenceControllerTest.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.content.res.Resources;
+import android.view.View;
+import android.widget.Button;
+import androidx.preference.PreferenceScreen;
+import com.android.settings.R;
+import com.android.settingslib.widget.LayoutPreference;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.eq;
+
+@RunWith(RobolectricTestRunner.class)
+public class DarkModeActivationPreferenceControllerTest {
+ private DarkModeActivationPreferenceController mController;
+ private String mPreferenceKey = "key";
+ @Mock
+ private LayoutPreference mPreference;
+ @Mock
+ private PreferenceScreen mScreen;
+ @Mock
+ private Resources res;
+ @Mock
+ private Context mContext;
+ @Mock
+ private UiModeManager mService;
+ @Mock
+ private Button mTurnOffButton;
+ @Mock
+ private Button mTurnOnButton;
+
+ private Configuration configNightYes = new Configuration();
+ private Configuration configNightNo = new Configuration();;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mService = mock(UiModeManager.class);
+ when(mContext.getResources()).thenReturn(res);
+ when(mContext.getSystemService(UiModeManager.class)).thenReturn(mService);
+ when(mScreen.findPreference(anyString())).thenReturn(mPreference);
+ when(mPreference.findViewById(
+ eq(R.id.dark_ui_turn_on_button))).thenReturn(mTurnOnButton);
+ when(mPreference.findViewById(
+ eq(R.id.dark_ui_turn_off_button))).thenReturn(mTurnOffButton);
+ when(mService.setNightModeActivated(anyBoolean())).thenReturn(true);
+ when(mContext.getString(
+ R.string.dark_ui_activation_off_auto)).thenReturn("off_auto");
+ when(mContext.getString(
+ R.string.dark_ui_activation_on_auto)).thenReturn("on_auto");
+ when(mContext.getString(
+ R.string.dark_ui_activation_off_manual)).thenReturn("off_manual");
+ when(mContext.getString(
+ R.string.dark_ui_activation_on_manual)).thenReturn("on_manual");
+ when(mContext.getString(
+ R.string.dark_ui_summary_off_auto_mode_auto)).thenReturn("summary_off_auto");
+ when(mContext.getString(
+ R.string.dark_ui_summary_on_auto_mode_auto)).thenReturn("summary_on_auto");
+ when(mContext.getString(
+ R.string.dark_ui_summary_off_auto_mode_never)).thenReturn("summary_off_manual");
+ when(mContext.getString(
+ R.string.dark_ui_summary_on_auto_mode_never)).thenReturn("summary_on_manual");
+ mController = new DarkModeActivationPreferenceController(mContext, mPreferenceKey);
+ mController.displayPreference(mScreen);
+ configNightNo.uiMode = Configuration.UI_MODE_NIGHT_NO;
+ configNightYes.uiMode = Configuration.UI_MODE_NIGHT_YES;
+ }
+
+ @Test
+ public void nightMode_toggleButton_offManual() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+ when(res.getConfiguration()).thenReturn(configNightYes);
+
+ mController.updateState(mPreference);
+
+ verify(mTurnOnButton).setVisibility(eq(View.GONE));
+ verify(mTurnOffButton).setVisibility(eq(View.VISIBLE));
+ verify(mTurnOffButton).setText(eq(mContext.getString(
+ R.string.dark_ui_activation_off_manual)));
+ }
+
+
+ @Test
+ public void nightMode_toggleButton_onAutoWhenModeIsYes() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+ when(res.getConfiguration()).thenReturn(configNightNo);
+
+ mController.updateState(mPreference);
+
+ verify(mTurnOffButton).setVisibility(eq(View.GONE));
+ verify(mTurnOnButton).setVisibility(eq(View.VISIBLE));
+ verify(mTurnOnButton).setText(eq(mContext.getString(
+ R.string.dark_ui_activation_on_manual)));
+ }
+
+ @Test
+ public void nightMode_toggleButton_onAutoWhenModeIsAuto() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
+ when(res.getConfiguration()).thenReturn(configNightNo);
+
+ mController.updateState(mPreference);
+
+ verify(mTurnOffButton).setVisibility(eq(View.GONE));
+ verify(mTurnOnButton).setVisibility(eq(View.VISIBLE));
+ verify(mTurnOnButton).setText(eq(mContext.getString(
+ R.string.dark_ui_activation_on_auto)));
+ }
+
+ @Test
+ public void nightModeSummary_buttonText_onManual() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_NO);
+ when(res.getConfiguration()).thenReturn(configNightYes);
+
+ assertEquals(mController.getSummary(), mContext.getString(
+ R.string.dark_ui_summary_on_auto_mode_never));
+ }
+
+ @Test
+ public void nightModeSummary_buttonText_offAuto() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
+ when(res.getConfiguration()).thenReturn(configNightNo);
+
+ assertEquals(mController.getSummary(), mContext.getString(
+ R.string.dark_ui_summary_off_auto_mode_auto));
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/display/darkmode/DarkModeObserverTest.java b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeObserverTest.java
new file mode 100644
index 0000000..dfa8ba1
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeObserverTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.provider.Settings;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+
+
+@RunWith(RobolectricTestRunner.class)
+public class DarkModeObserverTest {
+ private Context mContext;
+ private ContentObserver mContentObserver;
+ private DarkModeObserver mDarkModeObserver;
+ private Runnable mCallback;
+ private Uri mUri = Settings.Secure.getUriFor(Settings.Secure.UI_NIGHT_MODE);
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = spy(RuntimeEnvironment.application);
+ mDarkModeObserver = new DarkModeObserver(mContext);
+ mContentObserver = mDarkModeObserver.getContentObserver();
+ mCallback = mock(Runnable.class);
+ }
+
+ @Test
+ public void callbackTest_subscribedCallbackCalled() {
+ mDarkModeObserver.subscribe(mCallback);
+ mContentObserver.onChange(false, mUri);
+ Mockito.verify(mCallback, times(2)).run();
+ }
+
+ @Test
+ public void callbackTest_unsubscribedCallNotbackCalled() {
+ mDarkModeObserver.subscribe(mCallback);
+ mContentObserver.onChange(false, mUri);
+ mDarkModeObserver.unsubscribe();
+ mContentObserver.onChange(false, mUri);
+ Mockito.verify(mCallback, times(2)).run();
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java
new file mode 100644
index 0000000..a844fec
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/darkmode/DarkModeScheduleSelectorControllerTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2019 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.display.darkmode;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import androidx.preference.DropDownPreference;
+import androidx.preference.PreferenceScreen;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import com.android.settings.R;
+
+import static junit.framework.TestCase.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(RobolectricTestRunner.class)
+public class DarkModeScheduleSelectorControllerTest {
+ private DarkModeScheduleSelectorController mController;
+ private String mPreferenceKey = "key";
+ @Mock
+ private DropDownPreference mPreference;
+ @Mock
+ private PreferenceScreen mScreen;
+ private Context mContext;
+ @Mock
+ private UiModeManager mService;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = spy(RuntimeEnvironment.application);
+ when(mContext.getSystemService(UiModeManager.class)).thenReturn(mService);
+ when(mContext.getString(R.string.dark_ui_auto_mode_never)).thenReturn("never");
+ when(mContext.getString(R.string.dark_ui_auto_mode_auto)).thenReturn("auto");
+ mPreference = spy(new DropDownPreference(mContext));
+ when(mScreen.findPreference(anyString())).thenReturn(mPreference);
+ when(mService.setNightModeActivated(anyBoolean())).thenReturn(true);
+ mController = new DarkModeScheduleSelectorController(mContext, mPreferenceKey);
+ }
+
+ @Test
+ public void nightMode_preferenceChange_preferenceChangeTrueWhenChangedOnly() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+ mController.displayPreference(mScreen);
+ boolean changed = mController
+ .onPreferenceChange(mScreen, mContext.getString(R.string.dark_ui_auto_mode_auto));
+ assertTrue(changed);
+ changed = mController
+ .onPreferenceChange(mScreen, mContext.getString(R.string.dark_ui_auto_mode_auto));
+ assertFalse(changed);
+ }
+
+ @Test
+ public void nightMode_updateStateNone_dropDownValueChangedToNone() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+ mController.displayPreference(mScreen);
+ mController.updateState(mPreference);
+ verify(mPreference).setValue(mContext.getString(R.string.dark_ui_auto_mode_never));
+ }
+
+ @Test
+ public void nightMode_updateStateNone_dropDownValueChangedToAuto() {
+ when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
+ mController.displayPreference(mScreen);
+ mController.updateState(mPreference);
+ verify(mPreference).setValue(mContext.getString(R.string.dark_ui_auto_mode_auto));
+ }
+}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
index 5be7274..63e8a80 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
@@ -85,7 +85,6 @@
private BatteryMeterView mBatteryMeterView;
private TextView mBatteryPercentText;
private TextView mSummary;
- private TextView mSummary2;
private LayoutPreference mBatteryLayoutPref;
private Intent mBatteryIntent;
private LifecycleOwner mLifecycleOwner;
@@ -102,7 +101,6 @@
mBatteryPercentText = new TextView(mContext);
mSummary = new TextView(mContext);
ShadowEntityHeaderController.setUseMock(mEntityHeaderController);
- mSummary2 = new TextView(mContext);
mBatteryIntent = new Intent();
mBatteryIntent.putExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL);
@@ -126,7 +124,6 @@
mController.mBatteryMeterView = mBatteryMeterView;
mController.mBatteryPercentText = mBatteryPercentText;
mController.mSummary1 = mSummary;
- mController.mSummary2 = mSummary2;
}
@After
@@ -190,7 +187,6 @@
@Test
public void quickUpdateHeaderPreference_onlyUpdateBatteryLevelAndChargingState() {
mSummary.setText(BATTERY_STATUS);
- mSummary2.setText(BATTERY_STATUS);
mController.quickUpdateHeaderPreference();
@@ -198,7 +194,6 @@
assertThat(mBatteryMeterView.getCharging()).isTrue();
assertThat(mBatteryPercentText.getText().toString()).isEqualTo("60 %");
assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS);
- assertThat(mSummary2.getText()).isEqualTo(BATTERY_STATUS);
}
@Test
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
index 4d77bdd..7839e1a 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -265,20 +265,17 @@
}
}).when(mFeatureFactory.powerUsageFeatureProvider).getEnhancedEstimateDebugString(any());
- doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary1);
mFragment.onLongClick(new View(mRealContext));
TextView summary1 = mFragment.mBatteryLayoutPref.findViewById(R.id.summary1);
- TextView summary2 = mFragment.mBatteryLayoutPref.findViewById(R.id.summary2);
Robolectric.flushBackgroundThreadScheduler();
- assertThat(summary2.getText().toString()).contains(NEW_ML_EST_SUFFIX);
+ assertThat(summary1.getText().toString()).contains(NEW_ML_EST_SUFFIX);
assertThat(summary1.getText().toString()).contains(OLD_EST_SUFFIX);
}
@Test
public void debugMode() {
doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isEstimateDebugEnabled();
- doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
mFragment.restartBatteryInfoLoader();
ArgumentCaptor<View.OnLongClickListener> listener = ArgumentCaptor.forClass(
diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSliceTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSliceTest.java
new file mode 100644
index 0000000..bb21332
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/slices/DarkThemeSliceTest.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2019 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.homepage.contextualcards.slices;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.net.Uri;
+import android.os.BatteryManager;
+
+import androidx.slice.Slice;
+import androidx.slice.SliceMetadata;
+import androidx.slice.SliceProvider;
+import androidx.slice.widget.SliceLiveData;
+
+import com.android.settings.R;
+import com.android.settings.slices.CustomSliceRegistry;
+import com.android.settings.slices.SlicesFeatureProviderImpl;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class DarkThemeSliceTest {
+ @Mock
+ private UiModeManager mUiModeManager;
+ @Mock
+ private BatteryManager mBatteryManager;
+
+ private Context mContext;
+ private DarkThemeSlice mDarkThemeSlice;
+ private FakeFeatureFactory mFeatureFactory;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = spy(RuntimeEnvironment.application);
+ mFeatureFactory = FakeFeatureFactory.setupForTest();
+ mFeatureFactory.slicesFeatureProvider = new SlicesFeatureProviderImpl();
+ mFeatureFactory.slicesFeatureProvider.newUiSession();
+ doReturn(mUiModeManager).when(mContext).getSystemService(UiModeManager.class);
+
+ // Set-up specs for SliceMetadata.
+ SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
+ mDarkThemeSlice = new DarkThemeSlice(mContext);
+ mDarkThemeSlice.sKeepSliceShow = false;
+ }
+
+ @Test
+ public void getUri_shouldBeDarkThemeSliceUri() {
+ final Uri uri = mDarkThemeSlice.getUri();
+
+ assertThat(uri).isEqualTo(CustomSliceRegistry.DARK_THEME_SLICE_URI);
+ }
+
+ @Test
+ public void isAvailable_inDarkThemeMode_returnFalse() {
+ when(mUiModeManager.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+
+ assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
+ }
+
+ @Test
+ public void isAvailable_nonDarkThemeBatteryCapacityEq100_returnFalse() {
+ setBatteryCapacityLevel(100);
+
+ assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
+ }
+
+ @Test
+ public void isAvailable_nonDarkThemeBatteryCapacityLt50_returnTrue() {
+ setBatteryCapacityLevel(40);
+
+ assertThat(mDarkThemeSlice.isAvailable(mContext)).isTrue();
+ }
+
+ @Test
+ public void getSlice_notAvailable_returnNull() {
+ when(mUiModeManager.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+
+ assertThat(mDarkThemeSlice.getSlice()).isNull();
+ }
+
+ @Test
+ public void getSlice_newSession_notAvailable_returnNull() {
+ // previous displayed: yes
+ mDarkThemeSlice.sKeepSliceShow = true;
+ // Session: use original value + 1 to become a new session
+ mDarkThemeSlice.sActiveUiSession =
+ mFeatureFactory.slicesFeatureProvider.getUiSessionToken() + 1;
+
+ when(mUiModeManager.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
+
+ assertThat(mDarkThemeSlice.getSlice()).isNull();
+ }
+
+ @Test
+ public void getSlice_previouslyDisplayed_isAvailable_returnSlice() {
+ mDarkThemeSlice.sActiveUiSession =
+ mFeatureFactory.slicesFeatureProvider.getUiSessionToken();
+ mDarkThemeSlice.sKeepSliceShow = true;
+ setBatteryCapacityLevel(40);
+
+ assertThat(mDarkThemeSlice.getSlice()).isNotNull();
+ }
+
+ @Test
+ public void getSlice_isAvailable_returnSlice() {
+ setBatteryCapacityLevel(40);
+
+ assertThat(mDarkThemeSlice.getSlice()).isNotNull();
+ }
+
+ @Test
+ public void getSlice_isAvailable_showTitleSubtitle() {
+ setBatteryCapacityLevel(40);
+
+ final Slice slice = mDarkThemeSlice.getSlice();
+ final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
+ assertThat(metadata.getTitle()).isEqualTo(
+ mContext.getString(R.string.dark_theme_slice_title));
+ assertThat(metadata.getSubtitle()).isEqualTo(
+ mContext.getString(R.string.dark_theme_slice_subtitle));
+ }
+
+ private void setBatteryCapacityLevel(int power_level) {
+ when(mUiModeManager.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_NO);
+ doReturn(mBatteryManager).when(mContext).getSystemService(BatteryManager.class);
+ when(mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY))
+ .thenReturn(power_level);
+ }
+}