John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 19 | import static android.provider.Settings.Global.ZEN_MODE_ALARMS; |
| 20 | import static android.provider.Settings.Global.ZEN_MODE_OFF; |
| 21 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.content.SharedPreferences; |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 27 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 30 | import android.os.UserManager; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 31 | import android.provider.Settings; |
| 32 | import android.provider.Settings.Global; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 33 | import android.service.notification.ZenModeConfig; |
| 34 | import android.service.notification.ZenModeConfig.ZenRule; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 35 | import android.service.quicksettings.Tile; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 36 | import android.util.Slog; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.view.View.OnAttachStateChangeListener; |
| 40 | import android.view.ViewGroup; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 41 | import android.widget.Switch; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 42 | import android.widget.Toast; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 43 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 44 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 45 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 46 | import com.android.systemui.Dependency; |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 47 | import com.android.systemui.Prefs; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 48 | import com.android.systemui.R; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 49 | import com.android.systemui.SysUIToast; |
Jason Monk | ec34da8 | 2017-02-24 15:57:05 -0500 | [diff] [blame] | 50 | import com.android.systemui.plugins.ActivityStarter; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 51 | import com.android.systemui.plugins.qs.DetailAdapter; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 52 | import com.android.systemui.plugins.qs.QSTile; |
| 53 | import com.android.systemui.plugins.qs.QSTile.BooleanState; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 54 | import com.android.systemui.qs.QSHost; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 55 | import com.android.systemui.qs.tileimpl.QSTileImpl; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 56 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 57 | import com.android.systemui.volume.ZenModePanel; |
| 58 | |
| 59 | /** Quick settings tile: Do not disturb **/ |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 60 | public class DndTile extends QSTileImpl<BooleanState> { |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 61 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 62 | private static final Intent ZEN_SETTINGS = |
| 63 | new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); |
| 64 | |
| 65 | private static final Intent ZEN_PRIORITY_SETTINGS = |
| 66 | new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 67 | |
| 68 | private static final String ACTION_SET_VISIBLE = "com.android.systemui.dndtile.SET_VISIBLE"; |
| 69 | private static final String EXTRA_VISIBLE = "visible"; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 70 | |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 71 | private static final QSTile.Icon TOTAL_SILENCE = |
| 72 | ResourceIcon.get(R.drawable.ic_qs_dnd_on_total_silence); |
| 73 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 74 | private final ZenModeController mController; |
| 75 | private final DndDetailAdapter mDetailAdapter; |
| 76 | |
| 77 | private boolean mListening; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 78 | private boolean mShowingDetail; |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 79 | private boolean mReceiverRegistered; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 80 | |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 81 | public DndTile(QSHost host) { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 82 | super(host); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 83 | mController = Dependency.get(ZenModeController.class); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 84 | mDetailAdapter = new DndDetailAdapter(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 85 | mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_SET_VISIBLE)); |
Yuta Yamada | 99fd277 | 2016-10-06 17:40:19 +0900 | [diff] [blame] | 86 | mReceiverRegistered = true; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void handleDestroy() { |
| 91 | super.handleDestroy(); |
| 92 | if (mReceiverRegistered) { |
| 93 | mContext.unregisterReceiver(mReceiver); |
| 94 | mReceiverRegistered = false; |
| 95 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public static void setVisible(Context context, boolean visible) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 99 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_VISIBLE, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public static boolean isVisible(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 103 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_VISIBLE, false /* defaultValue */); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 104 | } |
| 105 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 106 | public static void setCombinedIcon(Context context, boolean combined) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 107 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, combined); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public static boolean isCombinedIcon(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 111 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, |
| 112 | false /* defaultValue */); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 113 | } |
| 114 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 115 | @Override |
| 116 | public DetailAdapter getDetailAdapter() { |
| 117 | return mDetailAdapter; |
| 118 | } |
| 119 | |
| 120 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 121 | public BooleanState newTileState() { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 122 | return new BooleanState(); |
| 123 | } |
| 124 | |
| 125 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 126 | public Intent getLongClickIntent() { |
| 127 | return ZEN_SETTINGS; |
| 128 | } |
| 129 | |
| 130 | @Override |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 131 | protected void handleClick() { |
| 132 | if (mState.value) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 133 | mController.setZen(ZEN_MODE_OFF, null, TAG); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 134 | } else { |
Beverly | 3e906c5 | 2018-01-22 10:49:43 -0500 | [diff] [blame] | 135 | mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected void handleSecondaryClick() { |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 141 | if (mController.isVolumeRestricted()) { |
| 142 | // Collapse the panels, so the user can see the toast. |
| 143 | mHost.collapsePanels(); |
| 144 | SysUIToast.makeText(mContext, mContext.getString( |
| 145 | com.android.internal.R.string.error_message_change_not_allowed), |
| 146 | Toast.LENGTH_LONG).show(); |
| 147 | return; |
| 148 | } |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 149 | if (!mState.value) { |
| 150 | // Because of the complexity of the zen panel, it needs to be shown after |
| 151 | // we turn on zen below. |
| 152 | mController.addCallback(new ZenModeController.Callback() { |
| 153 | @Override |
| 154 | public void onZenChanged(int zen) { |
| 155 | mController.removeCallback(this); |
| 156 | showDetail(true); |
| 157 | } |
| 158 | }); |
Beverly | 3e906c5 | 2018-01-22 10:49:43 -0500 | [diff] [blame] | 159 | mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
Jason Monk | 3c3b650 | 2017-07-27 15:41:26 -0400 | [diff] [blame] | 160 | } else { |
| 161 | showDetail(true); |
| 162 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 166 | public CharSequence getTileLabel() { |
| 167 | return mContext.getString(R.string.quick_settings_dnd_label); |
| 168 | } |
| 169 | |
| 170 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 171 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 172 | final int zen = arg instanceof Integer ? (Integer) arg : mController.getZen(); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 173 | final boolean newValue = zen != ZEN_MODE_OFF; |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 174 | final boolean valueChanged = state.value != newValue; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 175 | if (state.slash == null) state.slash = new SlashState(); |
Jason Monk | 2db4619 | 2017-02-01 17:18:22 -0500 | [diff] [blame] | 176 | state.dualTarget = true; |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 177 | state.value = newValue; |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 178 | state.state = state.value ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 179 | state.slash.isSlashed = !state.value; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 180 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_ADJUST_VOLUME); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 181 | switch (zen) { |
| 182 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 183 | state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on); |
| 184 | state.label = mContext.getString(R.string.quick_settings_dnd_priority_label); |
| 185 | state.contentDescription = mContext.getString( |
| 186 | R.string.accessibility_quick_settings_dnd_priority_on); |
| 187 | break; |
| 188 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 189 | state.icon = TOTAL_SILENCE; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 190 | state.label = mContext.getString(R.string.quick_settings_dnd_none_label); |
| 191 | state.contentDescription = mContext.getString( |
| 192 | R.string.accessibility_quick_settings_dnd_none_on); |
| 193 | break; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 194 | case ZEN_MODE_ALARMS: |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 195 | state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on); |
| 196 | state.label = mContext.getString(R.string.quick_settings_dnd_alarms_label); |
| 197 | state.contentDescription = mContext.getString( |
| 198 | R.string.accessibility_quick_settings_dnd_alarms_on); |
| 199 | break; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 200 | default: |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 201 | state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 202 | state.label = mContext.getString(R.string.quick_settings_dnd_label); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 203 | state.contentDescription = mContext.getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 204 | R.string.accessibility_quick_settings_dnd); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 205 | break; |
| 206 | } |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 207 | if (valueChanged) { |
| 208 | fireToggleStateChanged(state.value); |
| 209 | } |
Jason Monk | 28df4ed | 2017-03-23 10:25:37 -0400 | [diff] [blame] | 210 | state.dualLabelContentDescription = mContext.getResources().getString( |
| 211 | R.string.accessibility_quick_settings_open_settings, getTileLabel()); |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 212 | state.expandedAccessibilityClassName = Switch.class.getName(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 216 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 217 | return MetricsEvent.QS_DND; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 221 | protected String composeChangeAnnouncement() { |
| 222 | if (mState.value) { |
| 223 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_on); |
| 224 | } else { |
| 225 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_off); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
Jason Monk | 1c6116c | 2017-09-06 17:33:01 -0400 | [diff] [blame] | 230 | public void handleSetListening(boolean listening) { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 231 | if (mListening == listening) return; |
| 232 | mListening = listening; |
| 233 | if (mListening) { |
| 234 | mController.addCallback(mZenCallback); |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 235 | Prefs.registerListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 236 | } else { |
| 237 | mController.removeCallback(mZenCallback); |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 238 | Prefs.unregisterListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 242 | @Override |
| 243 | public boolean isAvailable() { |
| 244 | return isVisible(mContext); |
| 245 | } |
| 246 | |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 247 | private final OnSharedPreferenceChangeListener mPrefListener |
| 248 | = new OnSharedPreferenceChangeListener() { |
| 249 | @Override |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 250 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, |
| 251 | @Prefs.Key String key) { |
| 252 | if (Prefs.Key.DND_TILE_COMBINED_ICON.equals(key) || |
| 253 | Prefs.Key.DND_TILE_VISIBLE.equals(key)) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 254 | refreshState(); |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 259 | private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { |
| 260 | public void onZenChanged(int zen) { |
| 261 | refreshState(zen); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 262 | if (isShowingDetail()) { |
| 263 | mDetailAdapter.updatePanel(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void onConfigChanged(ZenModeConfig config) { |
| 269 | if (isShowingDetail()) { |
| 270 | mDetailAdapter.updatePanel(); |
| 271 | } |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 272 | } |
| 273 | }; |
| 274 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 275 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 276 | @Override |
| 277 | public void onReceive(Context context, Intent intent) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 278 | final boolean visible = intent.getBooleanExtra(EXTRA_VISIBLE, false); |
| 279 | setVisible(mContext, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 280 | refreshState(); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | private final class DndDetailAdapter implements DetailAdapter, OnAttachStateChangeListener { |
| 285 | |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 286 | private ZenModePanel mZenPanel; |
| 287 | private boolean mAuto; |
| 288 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 289 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 290 | public CharSequence getTitle() { |
| 291 | return mContext.getString(R.string.quick_settings_dnd_label); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public Boolean getToggleState() { |
| 296 | return mState.value; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public Intent getSettingsIntent() { |
| 301 | return ZEN_SETTINGS; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 306 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_TOGGLE, state); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 307 | if (!state) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 308 | mController.setZen(ZEN_MODE_OFF, null, TAG); |
| 309 | mAuto = false; |
| 310 | } else { |
Beverly | 3e906c5 | 2018-01-22 10:49:43 -0500 | [diff] [blame] | 311 | mController.setZen(Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, TAG); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 316 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 317 | return MetricsEvent.QS_DND_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 321 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 322 | mZenPanel = convertView != null ? (ZenModePanel) convertView |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 323 | : (ZenModePanel) LayoutInflater.from(context).inflate( |
| 324 | R.layout.zen_mode_panel, parent, false); |
| 325 | if (convertView == null) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 326 | mZenPanel.init(mController); |
| 327 | mZenPanel.addOnAttachStateChangeListener(this); |
| 328 | mZenPanel.setCallback(mZenModePanelCallback); |
Evan Laird | bcdd046 | 2017-05-15 12:51:34 -0400 | [diff] [blame] | 329 | mZenPanel.setEmptyState(R.drawable.ic_qs_dnd_detail_empty, R.string.dnd_is_off); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 330 | } |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 331 | updatePanel(); |
| 332 | return mZenPanel; |
| 333 | } |
| 334 | |
| 335 | private void updatePanel() { |
| 336 | if (mZenPanel == null) return; |
| 337 | mAuto = false; |
| 338 | if (mController.getZen() == ZEN_MODE_OFF) { |
| 339 | mZenPanel.setState(ZenModePanel.STATE_OFF); |
| 340 | } else { |
| 341 | ZenModeConfig config = mController.getConfig(); |
| 342 | String summary = ""; |
| 343 | if (config.manualRule != null && config.manualRule.enabler != null) { |
| 344 | summary = getOwnerCaption(config.manualRule.enabler); |
| 345 | } |
| 346 | for (ZenRule automaticRule : config.automaticRules.values()) { |
| 347 | if (automaticRule.isAutomaticActive()) { |
| 348 | if (summary.isEmpty()) { |
| 349 | summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule, |
| 350 | automaticRule.name); |
| 351 | } else { |
| 352 | summary = mContext.getString(R.string.qs_dnd_prompt_auto_rule_app); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | if (summary.isEmpty()) { |
| 357 | mZenPanel.setState(ZenModePanel.STATE_MODIFY); |
| 358 | } else { |
| 359 | mAuto = true; |
| 360 | mZenPanel.setState(ZenModePanel.STATE_AUTO_RULE); |
| 361 | mZenPanel.setAutoText(summary); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private String getOwnerCaption(String owner) { |
| 367 | final PackageManager pm = mContext.getPackageManager(); |
| 368 | try { |
| 369 | final ApplicationInfo info = pm.getApplicationInfo(owner, 0); |
| 370 | if (info != null) { |
| 371 | final CharSequence seq = info.loadLabel(pm); |
| 372 | if (seq != null) { |
| 373 | final String str = seq.toString().trim(); |
| 374 | return mContext.getString(R.string.qs_dnd_prompt_app, str); |
| 375 | } |
| 376 | } |
| 377 | } catch (Throwable e) { |
| 378 | Slog.w(TAG, "Error loading owner caption", e); |
| 379 | } |
| 380 | return ""; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void onViewAttachedToWindow(View v) { |
| 385 | mShowingDetail = true; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void onViewDetachedFromWindow(View v) { |
| 390 | mShowingDetail = false; |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame] | 391 | mZenPanel = null; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 392 | } |
| 393 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 394 | |
| 395 | private final ZenModePanel.Callback mZenModePanelCallback = new ZenModePanel.Callback() { |
| 396 | @Override |
| 397 | public void onPrioritySettings() { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 398 | Dependency.get(ActivityStarter.class).postStartActivityDismissingKeyguard( |
| 399 | ZEN_PRIORITY_SETTINGS, 0); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public void onInteraction() { |
| 404 | // noop |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void onExpanded(boolean expanded) { |
| 409 | // noop |
| 410 | } |
| 411 | }; |
| 412 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 413 | } |