Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.provider.Settings; |
| 22 | import android.widget.Switch; |
| 23 | |
| 24 | import com.android.internal.app.NightDisplayController; |
| 25 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | cbd3f0c | 2016-06-22 15:21:38 +0100 | [diff] [blame^] | 26 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | import com.android.systemui.qs.QSTile; |
| 29 | |
| 30 | public class NightDisplayTile extends QSTile<QSTile.BooleanState> |
| 31 | implements NightDisplayController.Callback { |
| 32 | |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 33 | private NightDisplayController mController; |
| 34 | private boolean mIsListening; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 35 | |
| 36 | public NightDisplayTile(Host host) { |
| 37 | super(host); |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 38 | mController = new NightDisplayController(mContext, ActivityManager.getCurrentUser()); |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 39 | } |
| 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 Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 59 | 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 Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 75 | 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 Klaassen | 06c0cb7 | 2016-07-21 19:22:09 -0700 | [diff] [blame] | 95 | return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS); |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void setListening(boolean listening) { |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 100 | mIsListening = listening; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 101 | 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 | } |