Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
| 19 | import android.app.UiModeManager; |
| 20 | import android.content.Intent; |
| 21 | import android.content.res.Configuration; |
| 22 | import android.provider.Settings; |
| 23 | import android.service.quicksettings.Tile; |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 24 | import android.text.TextUtils; |
Fabian Kozynski | 6f5e82d | 2019-12-02 09:52:00 -0500 | [diff] [blame] | 25 | import android.widget.Switch; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 26 | |
| 27 | import com.android.internal.logging.nano.MetricsProto; |
| 28 | import com.android.systemui.R; |
| 29 | import com.android.systemui.plugins.qs.QSTile; |
| 30 | import com.android.systemui.qs.QSHost; |
| 31 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 32 | import com.android.systemui.statusbar.policy.BatteryController; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 34 | |
| 35 | import javax.inject.Inject; |
| 36 | |
| 37 | /** |
| 38 | * Quick Settings tile for: Night Mode / Dark Theme / Dark Mode. |
| 39 | * |
| 40 | * The string id of this tile is "dark" because "night" was already |
| 41 | * taken by {@link NightDisplayTile}. |
| 42 | */ |
| 43 | public class UiModeNightTile extends QSTileImpl<QSTile.BooleanState> implements |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 44 | ConfigurationController.ConfigurationListener, |
| 45 | BatteryController.BatteryStateChangeCallback { |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 46 | |
| 47 | private final Icon mIcon = ResourceIcon.get( |
| 48 | com.android.internal.R.drawable.ic_qs_ui_mode_night); |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 49 | private final UiModeManager mUiModeManager; |
| 50 | private final BatteryController mBatteryController; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 51 | |
| 52 | @Inject |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 53 | public UiModeNightTile(QSHost host, ConfigurationController configurationController, |
| 54 | BatteryController batteryController) { |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 55 | super(host); |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 56 | mBatteryController = batteryController; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 57 | mUiModeManager = mContext.getSystemService(UiModeManager.class); |
| 58 | configurationController.observe(getLifecycle(), this); |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 59 | batteryController.observe(getLifecycle(), this); |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onUiModeChanged() { |
| 64 | refreshState(); |
| 65 | } |
| 66 | |
| 67 | @Override |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 68 | public void onPowerSaveChanged(boolean isPowerSave) { |
| 69 | refreshState(); |
| 70 | } |
| 71 | |
| 72 | @Override |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 73 | public BooleanState newTileState() { |
| 74 | return new BooleanState(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void handleClick() { |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 79 | if (getState().state == Tile.STATE_UNAVAILABLE) { |
| 80 | return; |
| 81 | } |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 82 | boolean newState = !mState.value; |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 83 | mUiModeManager.setNightModeActivated(newState); |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 84 | refreshState(newState); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected void handleUpdateState(BooleanState state, Object arg) { |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 89 | int uiMode = mUiModeManager.getNightMode(); |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 90 | boolean powerSave = mBatteryController.isPowerSave(); |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 91 | boolean isAuto = uiMode == UiModeManager.MODE_NIGHT_AUTO; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 92 | boolean nightMode = (mContext.getResources().getConfiguration().uiMode |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 93 | & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 94 | |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 95 | if (isAuto) { |
| 96 | state.secondaryLabel = mContext.getResources().getString(nightMode |
| 97 | ? R.string.quick_settings_dark_mode_secondary_label_until_sunrise |
| 98 | : R.string.quick_settings_dark_mode_secondary_label_on_at_sunset); |
| 99 | } else { |
| 100 | state.secondaryLabel = null; |
| 101 | } |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 102 | state.value = nightMode; |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 103 | state.label = mContext.getString(powerSave |
| 104 | ? R.string.quick_settings_ui_mode_night_label_battery_saver |
| 105 | : R.string.quick_settings_ui_mode_night_label); |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 106 | state.icon = mIcon; |
Jay Aliomer | 8b2671b | 2019-10-24 13:18:06 -0400 | [diff] [blame] | 107 | state.contentDescription = TextUtils.isEmpty(state.secondaryLabel) |
| 108 | ? state.label |
| 109 | : TextUtils.concat(state.label, ", ", state.secondaryLabel); |
Lucas Dupin | e7acd0f | 2019-05-10 14:45:42 -0700 | [diff] [blame] | 110 | if (powerSave) { |
| 111 | state.state = Tile.STATE_UNAVAILABLE; |
| 112 | } else { |
| 113 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
| 114 | } |
Fabian Kozynski | 49614c7 | 2019-04-18 09:05:15 -0400 | [diff] [blame] | 115 | state.showRippleEffect = false; |
Fabian Kozynski | 6f5e82d | 2019-12-02 09:52:00 -0500 | [diff] [blame] | 116 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Lucas Dupin | 046282e | 2019-04-10 20:17:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public int getMetricsCategory() { |
| 121 | return MetricsProto.MetricsEvent.QS_UI_MODE_NIGHT; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Intent getLongClickIntent() { |
| 126 | return new Intent(Settings.ACTION_DISPLAY_SETTINGS); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected void handleSetListening(boolean listening) { |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CharSequence getTileLabel() { |
| 135 | return getState().label; |
| 136 | } |
| 137 | } |