blob: ea6e174d786e1b5c4237ba1ffce007587e92433d [file] [log] [blame]
Justin Klaassen13790902016-06-21 20:28:12 -07001/*
2 * Copyright (c) 2016, 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
17package com.android.systemui.qs.tiles;
18
Rohan Shah785f3d72018-01-18 09:54:20 -080019import android.annotation.Nullable;
Justin Klaassendd32d902016-07-31 10:25:36 -070020import android.app.ActivityManager;
Justin Klaassen13790902016-06-21 20:28:12 -070021import android.content.Intent;
22import android.provider.Settings;
Jason Monk32508852017-01-18 09:17:13 -050023import android.service.quicksettings.Tile;
Justin Klaassen13790902016-06-21 20:28:12 -070024import android.widget.Switch;
25
Christine Franks5397f032017-11-01 18:35:16 -070026import com.android.internal.app.ColorDisplayController;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010027import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Justin Klaassen13790902016-06-21 20:28:12 -070028import com.android.systemui.R;
Jason Monk702e2eb2017-03-03 16:53:44 -050029import com.android.systemui.qs.QSHost;
30import com.android.systemui.plugins.qs.QSTile.BooleanState;
31import com.android.systemui.qs.tileimpl.QSTileImpl;
Justin Klaassen13790902016-06-21 20:28:12 -070032
Rohan Shah785f3d72018-01-18 09:54:20 -080033import java.time.format.DateTimeFormatter;
34
Jason Monk702e2eb2017-03-03 16:53:44 -050035public class NightDisplayTile extends QSTileImpl<BooleanState>
Christine Franks5397f032017-11-01 18:35:16 -070036 implements ColorDisplayController.Callback {
Justin Klaassen13790902016-06-21 20:28:12 -070037
Rohan Shah785f3d72018-01-18 09:54:20 -080038 /**
39 * Pattern for {@link java.time.format.DateTimeFormatter} used to approximate the time to the
40 * nearest hour and add on the AM/PM indicator.
41 */
Rohan Shahd3aa56c2018-01-24 11:17:39 -080042 private static final String APPROXIMATE_HOUR_DATE_TIME_PATTERN = "h a";
Rohan Shah785f3d72018-01-18 09:54:20 -080043
Christine Franks5397f032017-11-01 18:35:16 -070044 private ColorDisplayController mController;
Justin Klaassendd32d902016-07-31 10:25:36 -070045 private boolean mIsListening;
Justin Klaassen13790902016-06-21 20:28:12 -070046
Jason Monk702e2eb2017-03-03 16:53:44 -050047 public NightDisplayTile(QSHost host) {
Justin Klaassen13790902016-06-21 20:28:12 -070048 super(host);
Christine Franks5397f032017-11-01 18:35:16 -070049 mController = new ColorDisplayController(mContext, ActivityManager.getCurrentUser());
Justin Klaassen13790902016-06-21 20:28:12 -070050 }
51
52 @Override
53 public boolean isAvailable() {
Christine Franks5397f032017-11-01 18:35:16 -070054 return ColorDisplayController.isAvailable(mContext);
Justin Klaassen13790902016-06-21 20:28:12 -070055 }
56
57 @Override
58 public BooleanState newTileState() {
59 return new BooleanState();
60 }
61
62 @Override
63 protected void handleClick() {
64 final boolean activated = !mState.value;
Justin Klaassen13790902016-06-21 20:28:12 -070065 mController.setActivated(activated);
66 }
67
68 @Override
Justin Klaassendd32d902016-07-31 10:25:36 -070069 protected void handleUserSwitch(int newUserId) {
70 // Stop listening to the old controller.
71 if (mIsListening) {
72 mController.setListener(null);
73 }
74
75 // Make a new controller for the new user.
Christine Franks5397f032017-11-01 18:35:16 -070076 mController = new ColorDisplayController(mContext, newUserId);
Justin Klaassendd32d902016-07-31 10:25:36 -070077 if (mIsListening) {
78 mController.setListener(this);
79 }
80
81 super.handleUserSwitch(newUserId);
82 }
83
84 @Override
Justin Klaassen13790902016-06-21 20:28:12 -070085 protected void handleUpdateState(BooleanState state, Object arg) {
Rohan Shah785f3d72018-01-18 09:54:20 -080086 state.value = mController.isActivated();
Justin Klaassen8cbf1fb2016-10-11 14:40:16 -070087 state.label = state.contentDescription =
88 mContext.getString(R.string.quick_settings_night_display_label);
Evan Lairdbcdd0462017-05-15 12:51:34 -040089 state.icon = ResourceIcon.get(R.drawable.ic_qs_night_display_on);
Jason Monk702e2eb2017-03-03 16:53:44 -050090 state.expandedAccessibilityClassName = Switch.class.getName();
Jason Monk32508852017-01-18 09:17:13 -050091 state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
Rohan Shah785f3d72018-01-18 09:54:20 -080092 state.secondaryLabel = getSecondaryLabel(state.value);
93 }
94
95 /**
96 * Returns a {@link String} for the secondary label that reflects when the light will be turned
97 * on or off based on the current auto mode and night light activated status.
98 */
99 @Nullable
100 private String getSecondaryLabel(boolean isNightLightActivated) {
101 switch(mController.getAutoMode()) {
102 case ColorDisplayController.AUTO_MODE_TWILIGHT:
103 // Auto mode related to sunrise & sunset. If the light is on, it's guaranteed to be
104 // turned off at sunrise. If it's off, it's guaranteed to be turned on at sunset.
105 return isNightLightActivated
106 ? mContext.getString(
107 R.string.quick_settings_night_secondary_label_until_sunrise)
108 : mContext.getString(
109 R.string.quick_settings_night_secondary_label_on_at_sunset);
110
111 case ColorDisplayController.AUTO_MODE_CUSTOM:
112 // User-specified time, approximated to the nearest hour.
113 return isNightLightActivated
114 ? mContext.getString(
115 R.string.quick_settings_night_secondary_label_until,
116 mController.getCustomEndTime().format(
117 DateTimeFormatter.ofPattern(
118 APPROXIMATE_HOUR_DATE_TIME_PATTERN)))
119 : mContext.getString(
120 R.string.quick_settings_night_secondary_label_on_at,
121 mController.getCustomStartTime().format(
122 DateTimeFormatter.ofPattern(
123 APPROXIMATE_HOUR_DATE_TIME_PATTERN)));
124
125 default:
126 // No secondary label when auto mode is disabled.
127 return null;
128 }
Justin Klaassen13790902016-06-21 20:28:12 -0700129 }
130
131 @Override
132 public int getMetricsCategory() {
133 return MetricsEvent.QS_NIGHT_DISPLAY;
134 }
135
136 @Override
137 public Intent getLongClickIntent() {
Justin Klaassen06c0cb72016-07-21 19:22:09 -0700138 return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS);
Justin Klaassen13790902016-06-21 20:28:12 -0700139 }
140
141 @Override
Jason Monk1c6116c2017-09-06 17:33:01 -0400142 protected void handleSetListening(boolean listening) {
Justin Klaassendd32d902016-07-31 10:25:36 -0700143 mIsListening = listening;
Justin Klaassen13790902016-06-21 20:28:12 -0700144 if (listening) {
145 mController.setListener(this);
146 refreshState();
147 } else {
148 mController.setListener(null);
149 }
150 }
151
152 @Override
153 public CharSequence getTileLabel() {
154 return mContext.getString(R.string.quick_settings_night_display_label);
155 }
156
157 @Override
158 public void onActivated(boolean activated) {
159 refreshState();
160 }
161}