John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.volume; |
| 18 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
| 24 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
John Spurlock | b71e68f | 2014-07-17 23:09:40 -0400 | [diff] [blame] | 25 | import android.content.res.Resources; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
| 30 | import android.provider.Settings; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 31 | import android.provider.Settings.Global; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 32 | import android.service.notification.Condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 33 | import android.service.notification.ZenModeConfig; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 34 | import android.util.AttributeSet; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 35 | import android.util.Log; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 36 | import android.util.MathUtils; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 39 | import android.view.animation.AnimationUtils; |
| 40 | import android.view.animation.Interpolator; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 41 | import android.widget.CompoundButton; |
| 42 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| 43 | import android.widget.ImageView; |
| 44 | import android.widget.LinearLayout; |
| 45 | import android.widget.RadioButton; |
| 46 | import android.widget.TextView; |
| 47 | |
| 48 | import com.android.systemui.R; |
| 49 | import com.android.systemui.statusbar.policy.ZenModeController; |
| 50 | |
| 51 | import java.util.Arrays; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 52 | import java.util.Objects; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 53 | |
| 54 | public class ZenModePanel extends LinearLayout { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 55 | private static final String TAG = "ZenModePanel"; |
| 56 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 57 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 58 | private static final int SECONDS_MS = 1000; |
| 59 | private static final int MINUTES_MS = 60 * SECONDS_MS; |
| 60 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 61 | private static final int[] MINUTE_BUCKETS = DEBUG |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 62 | ? new int[] { 0, 1, 2, 5, 15, 30, 45, 60, 120, 180, 240, 480 } |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 63 | : ZenModeConfig.MINUTE_BUCKETS; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 64 | private static final int MIN_BUCKET_MINUTES = MINUTE_BUCKETS[0]; |
| 65 | private static final int MAX_BUCKET_MINUTES = MINUTE_BUCKETS[MINUTE_BUCKETS.length - 1]; |
| 66 | private static final int DEFAULT_BUCKET_INDEX = Arrays.binarySearch(MINUTE_BUCKETS, 60); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 67 | private static final int FOREVER_CONDITION_INDEX = 0; |
| 68 | private static final int TIME_CONDITION_INDEX = 1; |
| 69 | private static final int FIRST_CONDITION_INDEX = 2; |
| 70 | private static final float SILENT_HINT_PULSE_SCALE = 1.1f; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 71 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 72 | public static final Intent ZEN_SETTINGS = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); |
| 73 | |
John Spurlock | 7f1df5e | 2014-05-31 19:11:40 -0400 | [diff] [blame] | 74 | private final Context mContext; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 75 | private final LayoutInflater mInflater; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 76 | private final H mHandler = new H(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 77 | private final Prefs mPrefs; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 78 | private final Interpolator mFastOutSlowInInterpolator; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 79 | private final int mSubheadWarningColor; |
| 80 | private final int mSubheadColor; |
John Spurlock | 27c7b92 | 2014-09-23 14:59:51 -0400 | [diff] [blame] | 81 | private final ZenToast mZenToast; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 82 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 83 | private String mTag = TAG + "/" + Integer.toHexString(System.identityHashCode(this)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 84 | |
| 85 | private SegmentedButtons mZenButtons; |
| 86 | private View mZenSubhead; |
| 87 | private TextView mZenSubheadCollapsed; |
| 88 | private TextView mZenSubheadExpanded; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 89 | private View mMoreSettings; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 90 | private LinearLayout mZenConditions; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 91 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 92 | private Callback mCallback; |
| 93 | private ZenModeController mController; |
| 94 | private boolean mRequestingConditions; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 95 | private Condition mExitCondition; |
| 96 | private String mExitConditionText; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 97 | private int mBucketIndex = -1; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 98 | private boolean mExpanded; |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 99 | private boolean mHidden = false; |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 100 | private int mSessionZen; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 101 | private int mAttachedZen; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 102 | private Condition mSessionExitCondition; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 103 | private Condition[] mConditions; |
| 104 | private Condition mTimeCondition; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 105 | |
| 106 | public ZenModePanel(Context context, AttributeSet attrs) { |
| 107 | super(context, attrs); |
John Spurlock | 7f1df5e | 2014-05-31 19:11:40 -0400 | [diff] [blame] | 108 | mContext = context; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 109 | mPrefs = new Prefs(); |
John Spurlock | f7d2213 | 2014-07-10 19:03:00 -0400 | [diff] [blame] | 110 | mInflater = LayoutInflater.from(mContext.getApplicationContext()); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 111 | mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(mContext, |
| 112 | android.R.interpolator.fast_out_slow_in); |
John Spurlock | b71e68f | 2014-07-17 23:09:40 -0400 | [diff] [blame] | 113 | final Resources res = mContext.getResources(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 114 | mSubheadWarningColor = res.getColor(R.color.system_warning_color); |
| 115 | mSubheadColor = res.getColor(R.color.qs_subhead); |
John Spurlock | 27c7b92 | 2014-09-23 14:59:51 -0400 | [diff] [blame] | 116 | mZenToast = new ZenToast(mContext); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 117 | if (DEBUG) Log.d(mTag, "new ZenModePanel"); |
| 118 | } |
| 119 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 120 | @Override |
| 121 | protected void onFinishInflate() { |
| 122 | super.onFinishInflate(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 123 | |
| 124 | mZenButtons = (SegmentedButtons) findViewById(R.id.zen_buttons); |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 125 | mZenButtons.addButton(R.string.interruption_level_none, Global.ZEN_MODE_NO_INTERRUPTIONS); |
| 126 | mZenButtons.addButton(R.string.interruption_level_priority, |
| 127 | Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); |
| 128 | mZenButtons.addButton(R.string.interruption_level_all, Global.ZEN_MODE_OFF); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 129 | mZenButtons.setCallback(mZenButtonsCallback); |
| 130 | |
| 131 | mZenSubhead = findViewById(R.id.zen_subhead); |
| 132 | |
| 133 | mZenSubheadCollapsed = (TextView) findViewById(R.id.zen_subhead_collapsed); |
| 134 | mZenSubheadCollapsed.setOnClickListener(new View.OnClickListener() { |
| 135 | @Override |
| 136 | public void onClick(View v) { |
| 137 | setExpanded(true); |
| 138 | fireInteraction(); |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | mZenSubheadExpanded = (TextView) findViewById(R.id.zen_subhead_expanded); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 143 | |
| 144 | mMoreSettings = findViewById(R.id.zen_more_settings); |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 145 | mMoreSettings.setOnClickListener(new View.OnClickListener() { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 146 | @Override |
| 147 | public void onClick(View v) { |
| 148 | fireMoreSettings(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 149 | fireInteraction(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 150 | } |
| 151 | }); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 152 | |
| 153 | mZenConditions = (LinearLayout) findViewById(R.id.zen_conditions); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 157 | protected void onAttachedToWindow() { |
| 158 | super.onAttachedToWindow(); |
| 159 | if (DEBUG) Log.d(mTag, "onAttachedToWindow"); |
John Spurlock | 27c7b92 | 2014-09-23 14:59:51 -0400 | [diff] [blame] | 160 | mZenToast.hide(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 161 | mAttachedZen = getSelectedZen(-1); |
| 162 | mSessionZen = mAttachedZen; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 163 | mSessionExitCondition = copy(mExitCondition); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 164 | refreshExitConditionText(); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 165 | updateWidgets(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | protected void onDetachedFromWindow() { |
| 170 | super.onDetachedFromWindow(); |
| 171 | if (DEBUG) Log.d(mTag, "onDetachedFromWindow"); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 172 | checkForAttachedZenChange(); |
| 173 | mAttachedZen = -1; |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 174 | mSessionZen = -1; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 175 | mSessionExitCondition = null; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 176 | setExpanded(false); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 177 | } |
| 178 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 179 | public void setHidden(boolean hidden) { |
| 180 | if (mHidden == hidden) return; |
| 181 | mHidden = hidden; |
| 182 | updateWidgets(); |
| 183 | } |
| 184 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 185 | private void checkForAttachedZenChange() { |
| 186 | final int selectedZen = getSelectedZen(-1); |
| 187 | if (DEBUG) Log.d(mTag, "selectedZen=" + selectedZen); |
| 188 | if (selectedZen != mAttachedZen) { |
| 189 | if (DEBUG) Log.d(mTag, "attachedZen: " + mAttachedZen + " -> " + selectedZen); |
| 190 | if (selectedZen == Global.ZEN_MODE_NO_INTERRUPTIONS) { |
| 191 | mPrefs.trackNoneSelected(); |
| 192 | } |
John Spurlock | 27c7b92 | 2014-09-23 14:59:51 -0400 | [diff] [blame] | 193 | if (selectedZen == Global.ZEN_MODE_NO_INTERRUPTIONS |
| 194 | || selectedZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) { |
| 195 | mZenToast.show(selectedZen); |
| 196 | } |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 200 | private void setExpanded(boolean expanded) { |
| 201 | if (expanded == mExpanded) return; |
| 202 | mExpanded = expanded; |
| 203 | updateWidgets(); |
| 204 | setRequestingConditions(mExpanded); |
| 205 | fireExpanded(); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** Start or stop requesting relevant zen mode exit conditions */ |
| 209 | private void setRequestingConditions(boolean requesting) { |
| 210 | if (mRequestingConditions == requesting) return; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 211 | if (DEBUG) Log.d(mTag, "setRequestingConditions " + requesting); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 212 | mRequestingConditions = requesting; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 213 | if (mController != null) { |
| 214 | mController.requestConditions(mRequestingConditions); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 215 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 216 | if (mRequestingConditions) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 217 | mTimeCondition = parseExistingTimeCondition(mExitCondition); |
| 218 | if (mTimeCondition != null) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 219 | mBucketIndex = -1; |
| 220 | } else { |
| 221 | mBucketIndex = DEFAULT_BUCKET_INDEX; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 222 | mTimeCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 223 | } |
| 224 | if (DEBUG) Log.d(mTag, "Initial bucket index: " + mBucketIndex); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 225 | mConditions = null; // reset conditions |
| 226 | handleUpdateConditions(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 227 | } else { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 228 | mZenConditions.removeAllViews(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 229 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 230 | } |
| 231 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 232 | public void init(ZenModeController controller) { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 233 | mController = controller; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 234 | setExitCondition(mController.getExitCondition()); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 235 | refreshExitConditionText(); |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 236 | mSessionZen = getSelectedZen(-1); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 237 | handleUpdateZen(mController.getZen()); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 238 | if (DEBUG) Log.d(mTag, "init mExitCondition=" + mExitCondition); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 239 | mZenConditions.removeAllViews(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 240 | mController.addCallback(mZenCallback); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Jason Monk | e2f4771 | 2014-09-09 09:35:55 -0400 | [diff] [blame] | 243 | public void updateLocale() { |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 244 | mZenButtons.updateLocale(); |
Jason Monk | e2f4771 | 2014-09-09 09:35:55 -0400 | [diff] [blame] | 245 | } |
| 246 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 247 | private void setExitCondition(Condition exitCondition) { |
| 248 | if (sameConditionId(mExitCondition, exitCondition)) return; |
| 249 | mExitCondition = exitCondition; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 250 | refreshExitConditionText(); |
John Spurlock | 89f060a | 2014-07-16 21:03:15 -0400 | [diff] [blame] | 251 | updateWidgets(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 252 | } |
| 253 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 254 | private static Uri getConditionId(Condition condition) { |
| 255 | return condition != null ? condition.id : null; |
| 256 | } |
| 257 | |
| 258 | private static boolean sameConditionId(Condition lhs, Condition rhs) { |
| 259 | return lhs == null ? rhs == null : rhs != null && lhs.id.equals(rhs.id); |
| 260 | } |
| 261 | |
| 262 | private static Condition copy(Condition condition) { |
| 263 | return condition == null ? null : condition.copy(); |
| 264 | } |
| 265 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 266 | private void refreshExitConditionText() { |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 267 | final String forever = mContext.getString(com.android.internal.R.string.zen_mode_forever); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 268 | if (mExitCondition == null) { |
John Spurlock | bc5858f | 2014-07-11 14:33:18 -0400 | [diff] [blame] | 269 | mExitConditionText = forever; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 270 | } else if (ZenModeConfig.isValidCountdownConditionId(mExitCondition.id)) { |
| 271 | final Condition condition = parseExistingTimeCondition(mExitCondition); |
John Spurlock | bc5858f | 2014-07-11 14:33:18 -0400 | [diff] [blame] | 272 | mExitConditionText = condition != null ? condition.summary : forever; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 273 | } else { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 274 | mExitConditionText = mExitCondition.summary; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 275 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | public void setCallback(Callback callback) { |
| 279 | mCallback = callback; |
| 280 | } |
| 281 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 282 | public void showSilentHint() { |
| 283 | if (DEBUG) Log.d(mTag, "showSilentHint"); |
| 284 | if (mZenButtons == null || mZenButtons.getChildCount() == 0) return; |
| 285 | final View noneButton = mZenButtons.getChildAt(0); |
| 286 | if (noneButton.getScaleX() != 1) return; // already running |
| 287 | noneButton.animate().cancel(); |
| 288 | noneButton.animate().scaleX(SILENT_HINT_PULSE_SCALE).scaleY(SILENT_HINT_PULSE_SCALE) |
| 289 | .setInterpolator(mFastOutSlowInInterpolator) |
| 290 | .setListener(new AnimatorListenerAdapter() { |
| 291 | @Override |
| 292 | public void onAnimationEnd(Animator animation) { |
| 293 | noneButton.animate().scaleX(1).scaleY(1).setListener(null); |
| 294 | } |
| 295 | }); |
| 296 | } |
| 297 | |
| 298 | private void handleUpdateZen(int zen) { |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 299 | if (mSessionZen != -1 && mSessionZen != zen) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 300 | setExpanded(zen != Global.ZEN_MODE_OFF); |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 301 | mSessionZen = zen; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 302 | } |
| 303 | mZenButtons.setSelectedValue(zen); |
| 304 | updateWidgets(); |
| 305 | } |
| 306 | |
| 307 | private int getSelectedZen(int defValue) { |
| 308 | final Object zen = mZenButtons.getSelectedValue(); |
| 309 | return zen != null ? (Integer) zen : defValue; |
| 310 | } |
| 311 | |
| 312 | private void updateWidgets() { |
| 313 | final int zen = getSelectedZen(Global.ZEN_MODE_OFF); |
| 314 | final boolean zenOff = zen == Global.ZEN_MODE_OFF; |
| 315 | final boolean zenImportant = zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 316 | final boolean zenNone = zen == Global.ZEN_MODE_NO_INTERRUPTIONS; |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 317 | final boolean expanded = !mHidden && mExpanded; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 318 | |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 319 | mZenButtons.setVisibility(mHidden ? GONE : VISIBLE); |
John Spurlock | 89fe1cb | 2014-08-06 10:30:42 -0400 | [diff] [blame] | 320 | mZenSubhead.setVisibility(!mHidden && !zenOff ? VISIBLE : GONE); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 321 | mZenSubheadExpanded.setVisibility(expanded ? VISIBLE : GONE); |
| 322 | mZenSubheadCollapsed.setVisibility(!expanded ? VISIBLE : GONE); |
| 323 | mMoreSettings.setVisibility(zenImportant && expanded ? VISIBLE : GONE); |
| 324 | mZenConditions.setVisibility(!zenOff && expanded ? VISIBLE : GONE); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 325 | |
| 326 | if (zenNone) { |
John Spurlock | 86d04fd | 2014-07-10 18:37:45 -0400 | [diff] [blame] | 327 | mZenSubheadExpanded.setText(R.string.zen_no_interruptions_with_warning); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 328 | mZenSubheadCollapsed.setText(mExitConditionText); |
| 329 | } else if (zenImportant) { |
| 330 | mZenSubheadExpanded.setText(R.string.zen_important_interruptions); |
| 331 | mZenSubheadCollapsed.setText(mExitConditionText); |
| 332 | } |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 333 | mZenSubheadExpanded.setTextColor(zenNone && mPrefs.isNoneDangerous() |
| 334 | ? mSubheadWarningColor : mSubheadColor); |
John Spurlock | 89f060a | 2014-07-16 21:03:15 -0400 | [diff] [blame] | 335 | } |
| 336 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 337 | private Condition parseExistingTimeCondition(Condition condition) { |
| 338 | if (condition == null) return null; |
| 339 | final long time = ZenModeConfig.tryParseCountdownConditionId(condition.id); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 340 | if (time == 0) return null; |
| 341 | final long span = time - System.currentTimeMillis(); |
| 342 | if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 343 | return ZenModeConfig.toTimeCondition(time, Math.round(span / (float) MINUTES_MS)); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | private void handleUpdateConditions(Condition[] conditions) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 347 | mConditions = conditions; |
| 348 | handleUpdateConditions(); |
| 349 | } |
| 350 | |
| 351 | private void handleUpdateConditions() { |
| 352 | final int conditionCount = mConditions == null ? 0 : mConditions.length; |
| 353 | if (DEBUG) Log.d(mTag, "handleUpdateConditions conditionCount=" + conditionCount); |
| 354 | for (int i = mZenConditions.getChildCount() - 1; i >= FIRST_CONDITION_INDEX; i--) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 355 | mZenConditions.removeViewAt(i); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 356 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 357 | // forever |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 358 | bind(null, mZenConditions.getChildAt(FOREVER_CONDITION_INDEX)); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 359 | // countdown |
| 360 | bind(mTimeCondition, mZenConditions.getChildAt(TIME_CONDITION_INDEX)); |
| 361 | // provider conditions |
| 362 | boolean foundDowntime = false; |
| 363 | for (int i = 0; i < conditionCount; i++) { |
| 364 | bind(mConditions[i], mZenConditions.getChildAt(FIRST_CONDITION_INDEX + i)); |
| 365 | foundDowntime |= isDowntime(mConditions[i]); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 366 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 367 | // ensure downtime exists, if active |
| 368 | if (isDowntime(mSessionExitCondition) && !foundDowntime) { |
| 369 | bind(mSessionExitCondition, null); |
| 370 | } |
| 371 | // ensure something is selected |
| 372 | checkForDefault(); |
| 373 | } |
| 374 | |
| 375 | private static boolean isDowntime(Condition c) { |
| 376 | return ZenModeConfig.isValidDowntimeConditionId(getConditionId(c)); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 377 | } |
| 378 | |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 379 | private ConditionTag getConditionTagAt(int index) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 380 | return (ConditionTag) mZenConditions.getChildAt(index).getTag(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | private void checkForDefault() { |
| 384 | // are we left without anything selected? if so, set a default |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 385 | for (int i = 0; i < mZenConditions.getChildCount(); i++) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 386 | if (getConditionTagAt(i).rb.isChecked()) { |
John Spurlock | fbb3d6f | 2014-07-17 11:17:11 -0400 | [diff] [blame] | 387 | if (DEBUG) Log.d(mTag, "Not selecting a default, checked=" |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 388 | + getConditionTagAt(i).condition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | } |
| 392 | if (DEBUG) Log.d(mTag, "Selecting a default"); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 393 | final int favoriteIndex = mPrefs.getMinuteIndex(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 394 | if (favoriteIndex == -1) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 395 | getConditionTagAt(FOREVER_CONDITION_INDEX).rb.setChecked(true); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 396 | } else { |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 397 | mTimeCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[favoriteIndex]); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 398 | mBucketIndex = favoriteIndex; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 399 | bind(mTimeCondition, mZenConditions.getChildAt(TIME_CONDITION_INDEX)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 400 | getConditionTagAt(TIME_CONDITION_INDEX).rb.setChecked(true); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 404 | private void handleExitConditionChanged(Condition exitCondition) { |
| 405 | setExitCondition(exitCondition); |
| 406 | if (DEBUG) Log.d(mTag, "handleExitConditionChanged " + mExitCondition); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 407 | final int N = mZenConditions.getChildCount(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 408 | for (int i = 0; i < N; i++) { |
| 409 | final ConditionTag tag = getConditionTagAt(i); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 410 | tag.rb.setChecked(sameConditionId(tag.condition, mExitCondition)); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 411 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | private void bind(final Condition condition, View convertView) { |
| 415 | final boolean enabled = condition == null || condition.state == Condition.STATE_TRUE; |
| 416 | final View row; |
| 417 | if (convertView == null) { |
| 418 | row = mInflater.inflate(R.layout.zen_mode_condition, this, false); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 419 | if (DEBUG) Log.d(mTag, "Adding new condition view for: " + condition); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 420 | mZenConditions.addView(row); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 421 | } else { |
| 422 | row = convertView; |
| 423 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 424 | final ConditionTag tag = |
| 425 | row.getTag() != null ? (ConditionTag) row.getTag() : new ConditionTag(); |
| 426 | row.setTag(tag); |
| 427 | if (tag.rb == null) { |
| 428 | tag.rb = (RadioButton) row.findViewById(android.R.id.checkbox); |
| 429 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 430 | tag.condition = condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 431 | tag.rb.setEnabled(enabled); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 432 | if (sameConditionId(mSessionExitCondition, tag.condition)) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 433 | tag.rb.setChecked(true); |
| 434 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 435 | tag.rb.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 436 | @Override |
| 437 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 438 | if (mExpanded && isChecked) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 439 | if (DEBUG) Log.d(mTag, "onCheckedChanged " + tag.condition); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 440 | final int N = mZenConditions.getChildCount(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 441 | for (int i = 0; i < N; i++) { |
| 442 | ConditionTag childTag = getConditionTagAt(i); |
| 443 | if (childTag == tag) continue; |
| 444 | childTag.rb.setChecked(false); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 445 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 446 | select(tag.condition); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 447 | fireInteraction(); |
| 448 | } |
| 449 | } |
| 450 | }); |
| 451 | final TextView title = (TextView) row.findViewById(android.R.id.title); |
| 452 | if (condition == null) { |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 453 | title.setText(mContext.getString(com.android.internal.R.string.zen_mode_forever)); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 454 | } else { |
| 455 | title.setText(condition.summary); |
| 456 | } |
| 457 | title.setEnabled(enabled); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 458 | title.setAlpha(enabled ? 1 : .4f); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 459 | final ImageView button1 = (ImageView) row.findViewById(android.R.id.button1); |
| 460 | button1.setOnClickListener(new OnClickListener() { |
| 461 | @Override |
| 462 | public void onClick(View v) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 463 | onClickTimeButton(row, tag, false /*down*/); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 464 | } |
| 465 | }); |
| 466 | |
| 467 | final ImageView button2 = (ImageView) row.findViewById(android.R.id.button2); |
| 468 | button2.setOnClickListener(new OnClickListener() { |
| 469 | @Override |
| 470 | public void onClick(View v) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 471 | onClickTimeButton(row, tag, true /*up*/); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 472 | } |
| 473 | }); |
| 474 | title.setOnClickListener(new OnClickListener() { |
| 475 | @Override |
| 476 | public void onClick(View v) { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 477 | tag.rb.setChecked(true); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 478 | fireInteraction(); |
| 479 | } |
| 480 | }); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 481 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 482 | final long time = ZenModeConfig.tryParseCountdownConditionId(getConditionId(tag.condition)); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 483 | if (time > 0) { |
| 484 | if (mBucketIndex > -1) { |
| 485 | button1.setEnabled(mBucketIndex > 0); |
| 486 | button2.setEnabled(mBucketIndex < MINUTE_BUCKETS.length - 1); |
| 487 | } else { |
| 488 | final long span = time - System.currentTimeMillis(); |
| 489 | button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS); |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 490 | final Condition maxCondition = ZenModeConfig.toTimeCondition(MAX_BUCKET_MINUTES); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 491 | button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary)); |
| 492 | } |
| 493 | |
| 494 | button1.setAlpha(button1.isEnabled() ? 1f : .5f); |
| 495 | button2.setAlpha(button2.isEnabled() ? 1f : .5f); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 496 | } else { |
| 497 | button1.setVisibility(View.GONE); |
| 498 | button2.setVisibility(View.GONE); |
| 499 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | private void onClickTimeButton(View row, ConditionTag tag, boolean up) { |
| 503 | Condition newCondition = null; |
| 504 | final int N = MINUTE_BUCKETS.length; |
| 505 | if (mBucketIndex == -1) { |
| 506 | // not on a known index, search for the next or prev bucket by time |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 507 | final Uri conditionId = getConditionId(tag.condition); |
| 508 | final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 509 | final long now = System.currentTimeMillis(); |
| 510 | for (int i = 0; i < N; i++) { |
| 511 | int j = up ? i : N - 1 - i; |
| 512 | final int bucketMinutes = MINUTE_BUCKETS[j]; |
| 513 | final long bucketTime = now + bucketMinutes * MINUTES_MS; |
| 514 | if (up && bucketTime > time || !up && bucketTime < time) { |
| 515 | mBucketIndex = j; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 516 | newCondition = ZenModeConfig.toTimeCondition(bucketTime, bucketMinutes); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | } |
| 520 | if (newCondition == null) { |
| 521 | mBucketIndex = DEFAULT_BUCKET_INDEX; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 522 | newCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 523 | } |
| 524 | } else { |
| 525 | // on a known index, simply increment or decrement |
| 526 | mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1))); |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 527 | newCondition = ZenModeConfig.toTimeCondition(MINUTE_BUCKETS[mBucketIndex]); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 528 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 529 | mTimeCondition = newCondition; |
| 530 | bind(mTimeCondition, row); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 531 | tag.rb.setChecked(true); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 532 | select(mTimeCondition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 533 | fireInteraction(); |
| 534 | } |
| 535 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 536 | private void select(Condition condition) { |
| 537 | if (DEBUG) Log.d(mTag, "select " + condition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 538 | if (mController != null) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 539 | mController.setExitCondition(condition); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 540 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 541 | setExitCondition(condition); |
| 542 | if (condition == null) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 543 | mPrefs.setMinuteIndex(-1); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 544 | } else if (ZenModeConfig.isValidCountdownConditionId(condition.id) && mBucketIndex != -1) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 545 | mPrefs.setMinuteIndex(mBucketIndex); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 546 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 547 | mSessionExitCondition = copy(condition); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | private void fireMoreSettings() { |
| 551 | if (mCallback != null) { |
| 552 | mCallback.onMoreSettings(); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | private void fireInteraction() { |
| 557 | if (mCallback != null) { |
| 558 | mCallback.onInteraction(); |
| 559 | } |
| 560 | } |
| 561 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 562 | private void fireExpanded() { |
| 563 | if (mCallback != null) { |
| 564 | mCallback.onExpanded(mExpanded); |
| 565 | } |
| 566 | } |
| 567 | |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 568 | private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { |
| 569 | @Override |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 570 | public void onZenChanged(int zen) { |
| 571 | mHandler.obtainMessage(H.UPDATE_ZEN, zen, 0).sendToTarget(); |
| 572 | } |
| 573 | @Override |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 574 | public void onConditionsChanged(Condition[] conditions) { |
| 575 | mHandler.obtainMessage(H.UPDATE_CONDITIONS, conditions).sendToTarget(); |
| 576 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 577 | |
| 578 | @Override |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 579 | public void onExitConditionChanged(Condition exitCondition) { |
| 580 | mHandler.obtainMessage(H.EXIT_CONDITION_CHANGED, exitCondition).sendToTarget(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 581 | } |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | private final class H extends Handler { |
| 585 | private static final int UPDATE_CONDITIONS = 1; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 586 | private static final int EXIT_CONDITION_CHANGED = 2; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 587 | private static final int UPDATE_ZEN = 3; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 588 | |
| 589 | private H() { |
| 590 | super(Looper.getMainLooper()); |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public void handleMessage(Message msg) { |
| 595 | if (msg.what == UPDATE_CONDITIONS) { |
John Spurlock | 89f060a | 2014-07-16 21:03:15 -0400 | [diff] [blame] | 596 | handleUpdateConditions((Condition[]) msg.obj); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 597 | } else if (msg.what == EXIT_CONDITION_CHANGED) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 598 | handleExitConditionChanged((Condition) msg.obj); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 599 | } else if (msg.what == UPDATE_ZEN) { |
| 600 | handleUpdateZen(msg.arg1); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | public interface Callback { |
| 606 | void onMoreSettings(); |
| 607 | void onInteraction(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 608 | void onExpanded(boolean expanded); |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 609 | } |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 610 | |
| 611 | // used as the view tag on condition rows |
| 612 | private static class ConditionTag { |
| 613 | RadioButton rb; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 614 | Condition condition; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 615 | } |
| 616 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 617 | private final class Prefs implements OnSharedPreferenceChangeListener { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 618 | private static final String KEY_MINUTE_INDEX = "minuteIndex"; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 619 | private static final String KEY_NONE_SELECTED = "noneSelected"; |
| 620 | |
| 621 | private final int mNoneDangerousThreshold; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 622 | |
| 623 | private int mMinuteIndex; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 624 | private int mNoneSelected; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 625 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 626 | private Prefs() { |
| 627 | mNoneDangerousThreshold = mContext.getResources() |
| 628 | .getInteger(R.integer.zen_mode_alarm_warning_threshold); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 629 | prefs().registerOnSharedPreferenceChangeListener(this); |
| 630 | updateMinuteIndex(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 631 | updateNoneSelected(); |
| 632 | } |
| 633 | |
| 634 | public boolean isNoneDangerous() { |
| 635 | return mNoneSelected < mNoneDangerousThreshold; |
| 636 | } |
| 637 | |
| 638 | public void trackNoneSelected() { |
| 639 | mNoneSelected = clampNoneSelected(mNoneSelected + 1); |
| 640 | if (DEBUG) Log.d(mTag, "Setting none selected: " + mNoneSelected + " threshold=" |
| 641 | + mNoneDangerousThreshold); |
| 642 | prefs().edit().putInt(KEY_NONE_SELECTED, mNoneSelected).apply(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | public int getMinuteIndex() { |
| 646 | return mMinuteIndex; |
| 647 | } |
| 648 | |
| 649 | public void setMinuteIndex(int minuteIndex) { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 650 | minuteIndex = clampIndex(minuteIndex); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 651 | if (minuteIndex == mMinuteIndex) return; |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 652 | mMinuteIndex = clampIndex(minuteIndex); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 653 | if (DEBUG) Log.d(mTag, "Setting favorite minute index: " + mMinuteIndex); |
| 654 | prefs().edit().putInt(KEY_MINUTE_INDEX, mMinuteIndex).apply(); |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
| 659 | updateMinuteIndex(); |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 660 | updateNoneSelected(); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | private SharedPreferences prefs() { |
| 664 | return mContext.getSharedPreferences(ZenModePanel.class.getSimpleName(), 0); |
| 665 | } |
| 666 | |
| 667 | private void updateMinuteIndex() { |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 668 | mMinuteIndex = clampIndex(prefs().getInt(KEY_MINUTE_INDEX, DEFAULT_BUCKET_INDEX)); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 669 | if (DEBUG) Log.d(mTag, "Favorite minute index: " + mMinuteIndex); |
| 670 | } |
| 671 | |
John Spurlock | 8f8ecd6 | 2014-08-27 17:46:03 -0400 | [diff] [blame] | 672 | private int clampIndex(int index) { |
| 673 | return MathUtils.constrain(index, -1, MINUTE_BUCKETS.length - 1); |
| 674 | } |
| 675 | |
| 676 | private void updateNoneSelected() { |
| 677 | mNoneSelected = clampNoneSelected(prefs().getInt(KEY_NONE_SELECTED, 0)); |
| 678 | if (DEBUG) Log.d(mTag, "None selected: " + mNoneSelected); |
| 679 | } |
| 680 | |
| 681 | private int clampNoneSelected(int noneSelected) { |
| 682 | return MathUtils.constrain(noneSelected, 0, Integer.MAX_VALUE); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 683 | } |
| 684 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 685 | |
| 686 | private final SegmentedButtons.Callback mZenButtonsCallback = new SegmentedButtons.Callback() { |
| 687 | @Override |
| 688 | public void onSelected(Object value) { |
| 689 | if (value != null && mZenButtons.isShown()) { |
| 690 | if (DEBUG) Log.d(mTag, "mZenButtonsCallback selected=" + value); |
| 691 | mController.setZen((Integer) value); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | }; |
John Spurlock | 8600534 | 2014-05-23 11:58:00 -0400 | [diff] [blame] | 695 | } |