blob: 416943b0b9eb427b560cedf21747ee9e1d60c464 [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
Justin Klaassendd32d902016-07-31 10:25:36 -070019import android.app.ActivityManager;
Justin Klaassen13790902016-06-21 20:28:12 -070020import android.content.Intent;
21import android.provider.Settings;
22import android.widget.Switch;
23
24import com.android.internal.app.NightDisplayController;
25import com.android.internal.logging.MetricsLogger;
Tamas Berghammercbd3f0c2016-06-22 15:21:38 +010026import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Justin Klaassen13790902016-06-21 20:28:12 -070027import com.android.systemui.R;
28import com.android.systemui.qs.QSTile;
29
30public class NightDisplayTile extends QSTile<QSTile.BooleanState>
31 implements NightDisplayController.Callback {
32
Justin Klaassendd32d902016-07-31 10:25:36 -070033 private NightDisplayController mController;
34 private boolean mIsListening;
Justin Klaassen13790902016-06-21 20:28:12 -070035
36 public NightDisplayTile(Host host) {
37 super(host);
Justin Klaassendd32d902016-07-31 10:25:36 -070038 mController = new NightDisplayController(mContext, ActivityManager.getCurrentUser());
Justin Klaassen13790902016-06-21 20:28:12 -070039 }
40
41 @Override
42 public boolean isAvailable() {
43 return NightDisplayController.isAvailable(mContext);
44 }
45
46 @Override
47 public BooleanState newTileState() {
48 return new BooleanState();
49 }
50
51 @Override
52 protected void handleClick() {
53 final boolean activated = !mState.value;
54 MetricsLogger.action(mContext, getMetricsCategory(), activated);
55 mController.setActivated(activated);
56 }
57
58 @Override
Justin Klaassendd32d902016-07-31 10:25:36 -070059 protected void handleUserSwitch(int newUserId) {
60 // Stop listening to the old controller.
61 if (mIsListening) {
62 mController.setListener(null);
63 }
64
65 // Make a new controller for the new user.
66 mController = new NightDisplayController(mContext, newUserId);
67 if (mIsListening) {
68 mController.setListener(this);
69 }
70
71 super.handleUserSwitch(newUserId);
72 }
73
74 @Override
Justin Klaassen13790902016-06-21 20:28:12 -070075 protected void handleUpdateState(BooleanState state, Object arg) {
76 final boolean isActivated = mController.isActivated();
77 state.value = isActivated;
78 state.label = mContext.getString(R.string.quick_settings_night_display_label);
79 state.icon = ResourceIcon.get(isActivated ? R.drawable.ic_qs_night_display_on
80 : R.drawable.ic_qs_night_display_off);
81 state.contentDescription = mContext.getString(isActivated
82 ? R.string.quick_settings_night_display_summary_on
83 : R.string.quick_settings_night_display_summary_off);
84 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
85 = Switch.class.getName();
86 }
87
88 @Override
89 public int getMetricsCategory() {
90 return MetricsEvent.QS_NIGHT_DISPLAY;
91 }
92
93 @Override
94 public Intent getLongClickIntent() {
Justin Klaassen06c0cb72016-07-21 19:22:09 -070095 return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS);
Justin Klaassen13790902016-06-21 20:28:12 -070096 }
97
98 @Override
99 protected void setListening(boolean listening) {
Justin Klaassendd32d902016-07-31 10:25:36 -0700100 mIsListening = listening;
Justin Klaassen13790902016-06-21 20:28:12 -0700101 if (listening) {
102 mController.setListener(this);
103 refreshState();
104 } else {
105 mController.setListener(null);
106 }
107 }
108
109 @Override
110 public CharSequence getTileLabel() {
111 return mContext.getString(R.string.quick_settings_night_display_label);
112 }
113
114 @Override
115 public void onActivated(boolean activated) {
116 refreshState();
117 }
118}