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 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.content.SharedPreferences; |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 24 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 25 | import android.os.UserManager; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 26 | import android.provider.Settings; |
| 27 | import android.provider.Settings.Global; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.View.OnAttachStateChangeListener; |
| 31 | import android.view.ViewGroup; |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 32 | import android.widget.Switch; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 33 | import android.widget.Toast; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 34 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 35 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 36 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 37 | import com.android.systemui.Prefs; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 38 | import com.android.systemui.R; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 39 | import com.android.systemui.SysUIToast; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 40 | import com.android.systemui.qs.QSTile; |
| 41 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 42 | import com.android.systemui.volume.ZenModePanel; |
| 43 | |
| 44 | /** Quick settings tile: Do not disturb **/ |
| 45 | public class DndTile extends QSTile<QSTile.BooleanState> { |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 46 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 47 | private static final Intent ZEN_SETTINGS = |
| 48 | new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); |
| 49 | |
| 50 | private static final Intent ZEN_PRIORITY_SETTINGS = |
| 51 | new Intent(Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 52 | |
| 53 | private static final String ACTION_SET_VISIBLE = "com.android.systemui.dndtile.SET_VISIBLE"; |
| 54 | private static final String EXTRA_VISIBLE = "visible"; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 55 | |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 56 | private static final QSTile.Icon TOTAL_SILENCE = |
| 57 | ResourceIcon.get(R.drawable.ic_qs_dnd_on_total_silence); |
| 58 | |
| 59 | private final AnimationIcon mDisable = |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 60 | new AnimationIcon(R.drawable.ic_dnd_disable_animation, |
| 61 | R.drawable.ic_qs_dnd_off); |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 62 | private final AnimationIcon mDisableTotalSilence = |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 63 | new AnimationIcon(R.drawable.ic_dnd_total_silence_disable_animation, |
| 64 | R.drawable.ic_qs_dnd_off); |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 65 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 66 | private final ZenModeController mController; |
| 67 | private final DndDetailAdapter mDetailAdapter; |
| 68 | |
| 69 | private boolean mListening; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 70 | private boolean mShowingDetail; |
| 71 | |
| 72 | public DndTile(Host host) { |
| 73 | super(host); |
| 74 | mController = host.getZenModeController(); |
| 75 | mDetailAdapter = new DndDetailAdapter(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 76 | mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_SET_VISIBLE)); |
| 77 | } |
| 78 | |
| 79 | public static void setVisible(Context context, boolean visible) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 80 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_VISIBLE, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | public static boolean isVisible(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 84 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_VISIBLE, false /* defaultValue */); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 85 | } |
| 86 | |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 87 | public static void setCombinedIcon(Context context, boolean combined) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 88 | Prefs.putBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, combined); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public static boolean isCombinedIcon(Context context) { |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 92 | return Prefs.getBoolean(context, Prefs.Key.DND_TILE_COMBINED_ICON, |
| 93 | false /* defaultValue */); |
John Spurlock | f88d808 | 2015-03-25 18:09:51 -0400 | [diff] [blame] | 94 | } |
| 95 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 96 | @Override |
| 97 | public DetailAdapter getDetailAdapter() { |
| 98 | return mDetailAdapter; |
| 99 | } |
| 100 | |
| 101 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 102 | public BooleanState newTileState() { |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 103 | return new BooleanState(); |
| 104 | } |
| 105 | |
| 106 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 107 | public Intent getLongClickIntent() { |
| 108 | return ZEN_SETTINGS; |
| 109 | } |
| 110 | |
| 111 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 112 | public void handleClick() { |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 113 | if (mController.isVolumeRestricted()) { |
| 114 | // Collapse the panels, so the user can see the toast. |
| 115 | mHost.collapsePanels(); |
| 116 | SysUIToast.makeText(mContext, mContext.getString( |
| 117 | com.android.internal.R.string.error_message_change_not_allowed), |
| 118 | Toast.LENGTH_LONG).show(); |
| 119 | return; |
| 120 | } |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 121 | MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 122 | if (mState.value) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 123 | mController.setZen(Global.ZEN_MODE_OFF, null, TAG); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 124 | } else { |
Jason Monk | 48e093e | 2016-07-15 10:37:07 -0400 | [diff] [blame] | 125 | showDetail(true); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 126 | int zen = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN, Global.ZEN_MODE_ALARMS); |
| 127 | mController.setZen(zen, null, TAG); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 132 | public CharSequence getTileLabel() { |
| 133 | return mContext.getString(R.string.quick_settings_dnd_label); |
| 134 | } |
| 135 | |
| 136 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 137 | protected void handleUpdateState(BooleanState state, Object arg) { |
| 138 | final int zen = arg instanceof Integer ? (Integer) arg : mController.getZen(); |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 139 | final boolean newValue = zen != Global.ZEN_MODE_OFF; |
| 140 | final boolean valueChanged = state.value != newValue; |
| 141 | state.value = newValue; |
Sudheer Shanka | a8fbbb3 | 2016-02-11 17:17:57 +0000 | [diff] [blame] | 142 | checkIfRestrictionEnforcedByAdminOnly(state, UserManager.DISALLOW_ADJUST_VOLUME); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 143 | switch (zen) { |
| 144 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 145 | state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on); |
| 146 | state.label = mContext.getString(R.string.quick_settings_dnd_priority_label); |
| 147 | state.contentDescription = mContext.getString( |
| 148 | R.string.accessibility_quick_settings_dnd_priority_on); |
| 149 | break; |
| 150 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 151 | state.icon = TOTAL_SILENCE; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 152 | state.label = mContext.getString(R.string.quick_settings_dnd_none_label); |
| 153 | state.contentDescription = mContext.getString( |
| 154 | R.string.accessibility_quick_settings_dnd_none_on); |
| 155 | break; |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 156 | case Global.ZEN_MODE_ALARMS: |
| 157 | state.icon = ResourceIcon.get(R.drawable.ic_qs_dnd_on); |
| 158 | state.label = mContext.getString(R.string.quick_settings_dnd_alarms_label); |
| 159 | state.contentDescription = mContext.getString( |
| 160 | R.string.accessibility_quick_settings_dnd_alarms_on); |
| 161 | break; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 162 | default: |
John Spurlock | 036e7cb | 2015-06-15 13:49:57 -0400 | [diff] [blame] | 163 | state.icon = TOTAL_SILENCE.equals(state.icon) ? mDisableTotalSilence : mDisable; |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 164 | state.label = mContext.getString(R.string.quick_settings_dnd_label); |
| 165 | state.contentDescription = mContext.getString( |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 166 | R.string.accessibility_quick_settings_dnd); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | if (mShowingDetail && !state.value) { |
| 170 | showDetail(false); |
| 171 | } |
John Spurlock | b1688f6 | 2015-05-12 15:52:38 -0400 | [diff] [blame] | 172 | if (valueChanged) { |
| 173 | fireToggleStateChanged(state.value); |
| 174 | } |
Julia Reynolds | 20aef8a | 2016-05-04 16:44:08 -0400 | [diff] [blame] | 175 | state.minimalAccessibilityClassName = state.expandedAccessibilityClassName |
| 176 | = Switch.class.getName(); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 180 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 181 | return MetricsEvent.QS_DND; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 185 | protected String composeChangeAnnouncement() { |
| 186 | if (mState.value) { |
| 187 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_on); |
| 188 | } else { |
| 189 | return mContext.getString(R.string.accessibility_quick_settings_dnd_changed_off); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void setListening(boolean listening) { |
| 195 | if (mListening == listening) return; |
| 196 | mListening = listening; |
| 197 | if (mListening) { |
| 198 | mController.addCallback(mZenCallback); |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 199 | Prefs.registerListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 200 | } else { |
| 201 | mController.removeCallback(mZenCallback); |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 202 | Prefs.unregisterListener(mContext, mPrefListener); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 206 | @Override |
| 207 | public boolean isAvailable() { |
| 208 | return isVisible(mContext); |
| 209 | } |
| 210 | |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 211 | private final OnSharedPreferenceChangeListener mPrefListener |
| 212 | = new OnSharedPreferenceChangeListener() { |
| 213 | @Override |
Andrew Flynn | 8286257 | 2015-04-01 14:22:37 -0400 | [diff] [blame] | 214 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, |
| 215 | @Prefs.Key String key) { |
| 216 | if (Prefs.Key.DND_TILE_COMBINED_ICON.equals(key) || |
| 217 | Prefs.Key.DND_TILE_VISIBLE.equals(key)) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 218 | refreshState(); |
| 219 | } |
| 220 | } |
| 221 | }; |
| 222 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 223 | private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { |
| 224 | public void onZenChanged(int zen) { |
| 225 | refreshState(zen); |
| 226 | } |
| 227 | }; |
| 228 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 229 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 230 | @Override |
| 231 | public void onReceive(Context context, Intent intent) { |
John Spurlock | cd863ad | 2015-04-07 14:01:28 -0400 | [diff] [blame] | 232 | final boolean visible = intent.getBooleanExtra(EXTRA_VISIBLE, false); |
| 233 | setVisible(mContext, visible); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 234 | refreshState(); |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | private final class DndDetailAdapter implements DetailAdapter, OnAttachStateChangeListener { |
| 239 | |
| 240 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 241 | public CharSequence getTitle() { |
| 242 | return mContext.getString(R.string.quick_settings_dnd_label); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Boolean getToggleState() { |
| 247 | return mState.value; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Intent getSettingsIntent() { |
| 252 | return ZEN_SETTINGS; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 257 | MetricsLogger.action(mContext, MetricsEvent.QS_DND_TOGGLE, state); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 258 | if (!state) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 259 | mController.setZen(Global.ZEN_MODE_OFF, null, TAG); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 260 | showDetail(false); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 265 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 266 | return MetricsEvent.QS_DND_DETAILS; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 270 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 271 | final ZenModePanel zmp = convertView != null ? (ZenModePanel) convertView |
| 272 | : (ZenModePanel) LayoutInflater.from(context).inflate( |
| 273 | R.layout.zen_mode_panel, parent, false); |
| 274 | if (convertView == null) { |
| 275 | zmp.init(mController); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 276 | zmp.addOnAttachStateChangeListener(this); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 277 | zmp.setCallback(mZenModePanelCallback); |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 278 | } |
| 279 | return zmp; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void onViewAttachedToWindow(View v) { |
| 284 | mShowingDetail = true; |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void onViewDetachedFromWindow(View v) { |
| 289 | mShowingDetail = false; |
| 290 | } |
| 291 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 292 | |
| 293 | private final ZenModePanel.Callback mZenModePanelCallback = new ZenModePanel.Callback() { |
| 294 | @Override |
| 295 | public void onPrioritySettings() { |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 296 | mHost.startActivityDismissingKeyguard(ZEN_PRIORITY_SETTINGS); |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void onInteraction() { |
| 301 | // noop |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void onExpanded(boolean expanded) { |
| 306 | // noop |
| 307 | } |
| 308 | }; |
| 309 | |
John Spurlock | 3c4076a | 2015-02-24 12:12:25 -0500 | [diff] [blame] | 310 | } |