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 | |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.provider.Settings; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 23 | import android.service.quicksettings.Tile; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 24 | import android.widget.Switch; |
| 25 | |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 26 | import com.android.internal.app.ColorDisplayController; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 27 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 28 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 29 | import com.android.systemui.qs.QSHost; |
| 30 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
| 31 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 32 | |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 33 | import java.time.format.DateTimeFormatter; |
| 34 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 35 | public class NightDisplayTile extends QSTileImpl<BooleanState> |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 36 | implements ColorDisplayController.Callback { |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 37 | |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 38 | /** |
| 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 Shah | d3aa56c | 2018-01-24 11:17:39 -0800 | [diff] [blame] | 42 | private static final String APPROXIMATE_HOUR_DATE_TIME_PATTERN = "h a"; |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 43 | |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 44 | private ColorDisplayController mController; |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 45 | private boolean mIsListening; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 46 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 47 | public NightDisplayTile(QSHost host) { |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 48 | super(host); |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 49 | mController = new ColorDisplayController(mContext, ActivityManager.getCurrentUser()); |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public boolean isAvailable() { |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 54 | return ColorDisplayController.isAvailable(mContext); |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 55 | } |
| 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 Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 65 | mController.setActivated(activated); |
| 66 | } |
| 67 | |
| 68 | @Override |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 69 | 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 Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 76 | mController = new ColorDisplayController(mContext, newUserId); |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 77 | if (mIsListening) { |
| 78 | mController.setListener(this); |
| 79 | } |
| 80 | |
| 81 | super.handleUserSwitch(newUserId); |
| 82 | } |
| 83 | |
| 84 | @Override |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 85 | protected void handleUpdateState(BooleanState state, Object arg) { |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 86 | state.value = mController.isActivated(); |
Justin Klaassen | 8cbf1fb | 2016-10-11 14:40:16 -0700 | [diff] [blame] | 87 | state.label = state.contentDescription = |
| 88 | mContext.getString(R.string.quick_settings_night_display_label); |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 89 | state.icon = ResourceIcon.get(R.drawable.ic_qs_night_display_on); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 90 | state.expandedAccessibilityClassName = Switch.class.getName(); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 91 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Rohan Shah | 785f3d7 | 2018-01-18 09:54:20 -0800 | [diff] [blame] | 92 | 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 Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int getMetricsCategory() { |
| 133 | return MetricsEvent.QS_NIGHT_DISPLAY; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Intent getLongClickIntent() { |
Justin Klaassen | 06c0cb7 | 2016-07-21 19:22:09 -0700 | [diff] [blame] | 138 | return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS); |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 142 | protected void handleSetListening(boolean listening) { |
Justin Klaassen | dd32d90 | 2016-07-31 10:25:36 -0700 | [diff] [blame] | 143 | mIsListening = listening; |
Justin Klaassen | 1379090 | 2016-06-21 20:28:12 -0700 | [diff] [blame] | 144 | 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 | } |