ZenModePanel.java revision 530052a2fe3b6a6a4246ce28ab0ced647fe7f470
18600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock/* 28600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * Copyright (C) 2014 The Android Open Source Project 38600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * 48600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License"); 58600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * you may not use this file except in compliance with the License. 68600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * You may obtain a copy of the License at 78600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * 88600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * http://www.apache.org/licenses/LICENSE-2.0 98600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * 108600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * Unless required by applicable law or agreed to in writing, software 118600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS, 128600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * See the License for the specific language governing permissions and 148600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock * limitations under the License. 158600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock */ 168600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 178600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockpackage com.android.systemui.volume; 188600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 193e0770163686c1be83959d681976dafaf25272e1John Spurlockimport android.animation.LayoutTransition; 20530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport android.animation.LayoutTransition.TransitionListener; 219c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinekimport android.app.ActivityManager; 22530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport android.app.NotificationManager; 238600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.content.Context; 248600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.content.Intent; 25856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.content.SharedPreferences; 26856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.content.SharedPreferences.OnSharedPreferenceChangeListener; 27b71e68f9aa44b220cabaace70493751538d94dc0John Spurlockimport android.content.res.Resources; 288600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.net.Uri; 29530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport android.os.AsyncTask; 308600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.os.Handler; 318600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.os.Looper; 328600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.os.Message; 338600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.provider.Settings; 34ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlockimport android.provider.Settings.Global; 358600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.service.notification.Condition; 36856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.service.notification.ZenModeConfig; 37c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlockimport android.text.TextUtils; 38530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport android.util.ArraySet; 398600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.util.AttributeSet; 40856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport android.util.Log; 418f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlockimport android.util.MathUtils; 428600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.view.LayoutInflater; 438600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.view.View; 443e0770163686c1be83959d681976dafaf25272e1John Spurlockimport android.view.ViewGroup; 453e0770163686c1be83959d681976dafaf25272e1John Spurlockimport android.view.animation.AnimationUtils; 463e0770163686c1be83959d681976dafaf25272e1John Spurlockimport android.view.animation.Interpolator; 478600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.CompoundButton; 488600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.CompoundButton.OnCheckedChangeListener; 498600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.ImageView; 508600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.LinearLayout; 518600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.RadioButton; 528600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport android.widget.TextView; 538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport com.android.systemui.R; 558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport com.android.systemui.statusbar.policy.ZenModeController; 568600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 57530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport java.io.FileDescriptor; 58530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlockimport java.io.PrintWriter; 598600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockimport java.util.Arrays; 60856edebad73560e9b1cce021a7de9a0470d07176John Spurlockimport java.util.Objects; 618600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 628600534df66c2ff5846ed230b50c56229322d48aJohn Spurlockpublic class ZenModePanel extends LinearLayout { 63ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final String TAG = "ZenModePanel"; 64ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 65856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 6650806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock private static final int SECONDS_MS = 1000; 6750806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock private static final int MINUTES_MS = 60 * SECONDS_MS; 6850806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock 69ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final int[] MINUTE_BUCKETS = DEBUG 7050806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock ? new int[] { 0, 1, 2, 5, 15, 30, 45, 60, 120, 180, 240, 480 } 714dd81467e33a694138da6916fc68ca79501a9429Jason Monk : ZenModeConfig.MINUTE_BUCKETS; 72856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final int MIN_BUCKET_MINUTES = MINUTE_BUCKETS[0]; 73856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final int MAX_BUCKET_MINUTES = MINUTE_BUCKETS[MINUTE_BUCKETS.length - 1]; 74856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final int DEFAULT_BUCKET_INDEX = Arrays.binarySearch(MINUTE_BUCKETS, 60); 75ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final int FOREVER_CONDITION_INDEX = 0; 76530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private static final int COUNTDOWN_CONDITION_INDEX = 1; 77856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 788600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public static final Intent ZEN_SETTINGS = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS); 798600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 807f1df5e98578f8532a5e009009e7c1f82ed5885cJohn Spurlock private final Context mContext; 818600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final LayoutInflater mInflater; 828600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final H mHandler = new H(); 838f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final Prefs mPrefs; 84661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock private final IconPulser mIconPulser; 858f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final int mSubheadWarningColor; 868f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final int mSubheadColor; 873e0770163686c1be83959d681976dafaf25272e1John Spurlock private final Interpolator mInterpolator; 883e0770163686c1be83959d681976dafaf25272e1John Spurlock private final int mMaxConditions; 89530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final int mMaxOptionalConditions; 90530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final boolean mCountdownConditionSupported; 91530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final int mFirstConditionIndex; 92530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final TransitionHelper mTransitionHelper = new TransitionHelper(); 93856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 94eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock private String mTag = TAG + "/" + Integer.toHexString(System.identityHashCode(this)); 95ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 96ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private SegmentedButtons mZenButtons; 97ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private View mZenSubhead; 98ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private TextView mZenSubheadCollapsed; 99ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private TextView mZenSubheadExpanded; 1007f8f22a53005f776c8ee4d299e748e74b077ffd9John Spurlock private View mMoreSettings; 101ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private LinearLayout mZenConditions; 102ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 1038600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private Callback mCallback; 1048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private ZenModeController mController; 1058600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private boolean mRequestingConditions; 1064db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mExitCondition; 1074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private String mExitConditionText; 108856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private int mBucketIndex = -1; 109ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private boolean mExpanded; 110530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mHidden; 111fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock private int mSessionZen; 1128f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int mAttachedZen; 1133e0770163686c1be83959d681976dafaf25272e1John Spurlock private boolean mAttached; 1144db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mSessionExitCondition; 1154db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition[] mConditions; 1164db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mTimeCondition; 1178600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 1188600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public ZenModePanel(Context context, AttributeSet attrs) { 1198600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super(context, attrs); 1207f1df5e98578f8532a5e009009e7c1f82ed5885cJohn Spurlock mContext = context; 1218f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs = new Prefs(); 122f7d22132c2c9b83134b05aaed63a89f61c639fd9John Spurlock mInflater = LayoutInflater.from(mContext.getApplicationContext()); 123661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock mIconPulser = new IconPulser(mContext); 124b71e68f9aa44b220cabaace70493751538d94dc0John Spurlock final Resources res = mContext.getResources(); 1258f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSubheadWarningColor = res.getColor(R.color.system_warning_color); 1268f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSubheadColor = res.getColor(R.color.qs_subhead); 1273e0770163686c1be83959d681976dafaf25272e1John Spurlock mInterpolator = AnimationUtils.loadInterpolator(mContext, 1283e0770163686c1be83959d681976dafaf25272e1John Spurlock com.android.internal.R.interpolator.fast_out_slow_in); 129530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mCountdownConditionSupported = NotificationManager.from(mContext) 130530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock .isSystemConditionProviderEnabled(ZenModeConfig.COUNTDOWN_PATH); 131530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int countdownDelta = mCountdownConditionSupported ? 1 : 0; 132530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mFirstConditionIndex = COUNTDOWN_CONDITION_INDEX + countdownDelta; 133530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int minConditions = 1 /*forever*/ + countdownDelta; 1343e0770163686c1be83959d681976dafaf25272e1John Spurlock mMaxConditions = MathUtils.constrain(res.getInteger(R.integer.zen_mode_max_conditions), 135530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock minConditions, 100); 136530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mMaxOptionalConditions = mMaxConditions - minConditions; 137856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "new ZenModePanel"); 138856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 139856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 140530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 141530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.println("ZenModePanel state:"); 142530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mCountdownConditionSupported="); pw.println(mCountdownConditionSupported); 143530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mMaxConditions="); pw.println(mMaxConditions); 144530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mRequestingConditions="); pw.println(mRequestingConditions); 145530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mAttached="); pw.println(mAttached); 146530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mHidden="); pw.println(mHidden); 147530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mExpanded="); pw.println(mExpanded); 148530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mSessionZen="); pw.println(mSessionZen); 149530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mAttachedZen="); pw.println(mAttachedZen); 150530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.dump(fd, pw, args); 151530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 152530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 1538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 1548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock protected void onFinishInflate() { 1558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super.onFinishInflate(); 156ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 157ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons = (SegmentedButtons) findViewById(R.id.zen_buttons); 158bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_none, R.drawable.ic_zen_none, 159bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock Global.ZEN_MODE_NO_INTERRUPTIONS); 160bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_priority, R.drawable.ic_zen_important, 1614291fb79c70ba77af4357d53dac2e5626eabba45John Spurlock Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); 162bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_all, R.drawable.ic_zen_all, 163bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock Global.ZEN_MODE_OFF); 164ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons.setCallback(mZenButtonsCallback); 165ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 166530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ViewGroup zenButtonsContainer = (ViewGroup) findViewById(R.id.zen_buttons_container); 167530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock zenButtonsContainer.setLayoutTransition(newLayoutTransition(null)); 168530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 169ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubhead = findViewById(R.id.zen_subhead); 170ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 171ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed = (TextView) findViewById(R.id.zen_subhead_collapsed); 172ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setOnClickListener(new View.OnClickListener() { 173ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 174ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void onClick(View v) { 175ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(true); 176ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 177ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock }); 178a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mZenSubheadCollapsed, mInteractionCallback); 179ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 180ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadExpanded = (TextView) findViewById(R.id.zen_subhead_expanded); 181a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mZenSubheadExpanded, mInteractionCallback); 182ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 183ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mMoreSettings = findViewById(R.id.zen_more_settings); 1847f8f22a53005f776c8ee4d299e748e74b077ffd9John Spurlock mMoreSettings.setOnClickListener(new View.OnClickListener() { 1858600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 1868600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 1878600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock fireMoreSettings(); 1888600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 1898600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 190a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mMoreSettings, mInteractionCallback); 191ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 192ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenConditions = (LinearLayout) findViewById(R.id.zen_conditions); 193530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < mMaxConditions; i++) { 194530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.addView(mInflater.inflate(R.layout.zen_mode_condition, this, false)); 195530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 196530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 197530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setLayoutTransition(newLayoutTransition(mTransitionHelper)); 1983e0770163686c1be83959d681976dafaf25272e1John Spurlock } 1993e0770163686c1be83959d681976dafaf25272e1John Spurlock 200530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private LayoutTransition newLayoutTransition(TransitionListener listener) { 2013e0770163686c1be83959d681976dafaf25272e1John Spurlock final LayoutTransition transition = new LayoutTransition(); 2023e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.disableTransitionType(LayoutTransition.DISAPPEARING); 2033e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); 204530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock transition.disableTransitionType(LayoutTransition.APPEARING); 2053e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.setInterpolator(LayoutTransition.CHANGE_APPEARING, mInterpolator); 206530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (listener != null) { 207530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock transition.addTransitionListener(listener); 208530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 2093e0770163686c1be83959d681976dafaf25272e1John Spurlock return transition; 2108600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 2118600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 2128600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 213856edebad73560e9b1cce021a7de9a0470d07176John Spurlock protected void onAttachedToWindow() { 214856edebad73560e9b1cce021a7de9a0470d07176John Spurlock super.onAttachedToWindow(); 215856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "onAttachedToWindow"); 2163e0770163686c1be83959d681976dafaf25272e1John Spurlock mAttached = true; 2178f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mAttachedZen = getSelectedZen(-1); 2188f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSessionZen = mAttachedZen; 219530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.clear(); 220530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(copy(mExitCondition)); 221ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 22250806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock updateWidgets(); 2233e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(!mHidden); 224856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 225856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 226856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 227856edebad73560e9b1cce021a7de9a0470d07176John Spurlock protected void onDetachedFromWindow() { 228856edebad73560e9b1cce021a7de9a0470d07176John Spurlock super.onDetachedFromWindow(); 229856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "onDetachedFromWindow"); 2308f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock checkForAttachedZenChange(); 2313e0770163686c1be83959d681976dafaf25272e1John Spurlock mAttached = false; 2328f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mAttachedZen = -1; 233fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = -1; 234530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(null); 235ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(false); 2363e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(false); 237530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.clear(); 238530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 239530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 240530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void setSessionExitCondition(Condition condition) { 241530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (Objects.equals(condition, mSessionExitCondition)) return; 242530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "mSessionExitCondition=" + getConditionId(condition)); 243530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mSessionExitCondition = condition; 244856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 245856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 246eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock public void setHidden(boolean hidden) { 247eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock if (mHidden == hidden) return; 2483e0770163686c1be83959d681976dafaf25272e1John Spurlock if (DEBUG) Log.d(mTag, "hidden=" + hidden); 249eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mHidden = hidden; 2503e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(mAttached && !mHidden); 251eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock updateWidgets(); 252eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock } 253eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock 2548f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private void checkForAttachedZenChange() { 2558f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock final int selectedZen = getSelectedZen(-1); 2568f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "selectedZen=" + selectedZen); 2578f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (selectedZen != mAttachedZen) { 2588f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "attachedZen: " + mAttachedZen + " -> " + selectedZen); 2598f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (selectedZen == Global.ZEN_MODE_NO_INTERRUPTIONS) { 2608f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.trackNoneSelected(); 2618f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2628f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2638f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2648f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 265ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void setExpanded(boolean expanded) { 266ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (expanded == mExpanded) return; 267ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mExpanded = expanded; 2683e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mExpanded) { 2693e0770163686c1be83959d681976dafaf25272e1John Spurlock ensureSelection(); 2703e0770163686c1be83959d681976dafaf25272e1John Spurlock } 271ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock updateWidgets(); 272ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock fireExpanded(); 2738600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 2748600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 2758600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock /** Start or stop requesting relevant zen mode exit conditions */ 276530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void setRequestingConditions(final boolean requesting) { 2778600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mRequestingConditions == requesting) return; 278856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "setRequestingConditions " + requesting); 2798600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mRequestingConditions = requesting; 280856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mController != null) { 281530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 282530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 283530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 284530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mController.requestConditions(requesting); 285530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 286530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 287856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 2888600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mRequestingConditions) { 2894db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mTimeCondition = parseExistingTimeCondition(mExitCondition); 2904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (mTimeCondition != null) { 291856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = -1; 292856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 293856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = DEFAULT_BUCKET_INDEX; 294c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock mTimeCondition = ZenModeConfig.toTimeCondition(mContext, 2959c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 296856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 297856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Initial bucket index: " + mBucketIndex); 2984db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mConditions = null; // reset conditions 2994db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleUpdateConditions(); 3008600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 301530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock hideAllConditions(); 3028600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3038600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 305eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock public void init(ZenModeController controller) { 3068600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mController = controller; 3074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(mController.getExitCondition()); 308ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 309fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = getSelectedZen(-1); 310ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock handleUpdateZen(mController.getZen()); 3114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "init mExitCondition=" + mExitCondition); 312530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock hideAllConditions(); 313856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mController.addCallback(mZenCallback); 314ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 315ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 316e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk public void updateLocale() { 3174291fb79c70ba77af4357d53dac2e5626eabba45John Spurlock mZenButtons.updateLocale(); 318e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk } 319e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk 3204db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void setExitCondition(Condition exitCondition) { 32125c3421c5e65ddc7f2b2bf1b1208f3634e6f5256John Spurlock if (Objects.equals(mExitCondition, exitCondition)) return; 3224db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mExitCondition = exitCondition; 323530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "mExitCondition=" + getConditionId(mExitCondition)); 324ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 32589f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock updateWidgets(); 326ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 327ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 3284db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static Uri getConditionId(Condition condition) { 3294db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return condition != null ? condition.id : null; 3304db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3314db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 3324db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static boolean sameConditionId(Condition lhs, Condition rhs) { 3334db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return lhs == null ? rhs == null : rhs != null && lhs.id.equals(rhs.id); 3344db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3354db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 3364db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static Condition copy(Condition condition) { 3374db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return condition == null ? null : condition.copy(); 3384db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3394db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 340ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void refreshExitConditionText() { 3414dd81467e33a694138da6916fc68ca79501a9429Jason Monk final String forever = mContext.getString(com.android.internal.R.string.zen_mode_forever); 3424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (mExitCondition == null) { 343bc5858f4893558ae6fee24c14489be56e6c3f528John Spurlock mExitConditionText = forever; 344530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else if (isCountdown(mExitCondition)) { 3454db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final Condition condition = parseExistingTimeCondition(mExitCondition); 346bc5858f4893558ae6fee24c14489be56e6c3f528John Spurlock mExitConditionText = condition != null ? condition.summary : forever; 347ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else { 3484db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mExitConditionText = mExitCondition.summary; 349856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 3508600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3518600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 3528600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void setCallback(Callback callback) { 3538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback = callback; 3548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 356ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void showSilentHint() { 357ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (DEBUG) Log.d(mTag, "showSilentHint"); 358ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mZenButtons == null || mZenButtons.getChildCount() == 0) return; 359ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final View noneButton = mZenButtons.getChildAt(0); 360661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock mIconPulser.start(noneButton); 361ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 362ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 363ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void handleUpdateZen(int zen) { 364fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock if (mSessionZen != -1 && mSessionZen != zen) { 365ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(zen != Global.ZEN_MODE_OFF); 366fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = zen; 367ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 368ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons.setSelectedValue(zen); 369ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock updateWidgets(); 370530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock handleUpdateConditions(); 371530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mExpanded) { 372530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Condition selected = getSelectedCondition(); 373530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (!Objects.equals(mExitCondition, selected)) { 374530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock select(selected); 375530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 376530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 377530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 378530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 379530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private Condition getSelectedCondition() { 380530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 381530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 382530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag tag = getConditionTagAt(i); 383530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null && tag.rb.isChecked()) { 384530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return tag.condition; 385530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 386530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 387530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return null; 388ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 389ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 390ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private int getSelectedZen(int defValue) { 391ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final Object zen = mZenButtons.getSelectedValue(); 392ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock return zen != null ? (Integer) zen : defValue; 393ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 394ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 395ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void updateWidgets() { 396530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitionHelper.isTransitioning()) { 397530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.pendingUpdateWidgets(); 398530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return; 399530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 400ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final int zen = getSelectedZen(Global.ZEN_MODE_OFF); 401ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenOff = zen == Global.ZEN_MODE_OFF; 402ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenImportant = zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; 403ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenNone = zen == Global.ZEN_MODE_NO_INTERRUPTIONS; 404eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock final boolean expanded = !mHidden && mExpanded; 405eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock 406eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenButtons.setVisibility(mHidden ? GONE : VISIBLE); 40789fe1cb9debbfd629f1467b2a3d957e6dc1a8507John Spurlock mZenSubhead.setVisibility(!mHidden && !zenOff ? VISIBLE : GONE); 408eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenSubheadExpanded.setVisibility(expanded ? VISIBLE : GONE); 409eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenSubheadCollapsed.setVisibility(!expanded ? VISIBLE : GONE); 410eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mMoreSettings.setVisibility(zenImportant && expanded ? VISIBLE : GONE); 411eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenConditions.setVisibility(!zenOff && expanded ? VISIBLE : GONE); 412ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 413ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (zenNone) { 41486d04fdaa1853cf7ff8ec9aba1dd925dd098167eJohn Spurlock mZenSubheadExpanded.setText(R.string.zen_no_interruptions_with_warning); 415ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setText(mExitConditionText); 416ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else if (zenImportant) { 417ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadExpanded.setText(R.string.zen_important_interruptions); 418ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setText(mExitConditionText); 419ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 4208f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mZenSubheadExpanded.setTextColor(zenNone && mPrefs.isNoneDangerous() 4218f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock ? mSubheadWarningColor : mSubheadColor); 42289f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock } 42389f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock 4244db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition parseExistingTimeCondition(Condition condition) { 4254db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (condition == null) return null; 4264db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(condition.id); 427856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (time == 0) return null; 428c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final long now = System.currentTimeMillis(); 429c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final long span = time - now; 430856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null; 431c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock return ZenModeConfig.toTimeCondition(mContext, 4329c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek time, Math.round(span / (float) MINUTES_MS), now, ActivityManager.getCurrentUser()); 4338600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 4348600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 4358600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void handleUpdateConditions(Condition[] conditions) { 4363e0770163686c1be83959d681976dafaf25272e1John Spurlock conditions = trimConditions(conditions); 4373e0770163686c1be83959d681976dafaf25272e1John Spurlock if (Arrays.equals(conditions, mConditions)) { 4383e0770163686c1be83959d681976dafaf25272e1John Spurlock final int count = mConditions == null ? 0 : mConditions.length; 4393e0770163686c1be83959d681976dafaf25272e1John Spurlock if (DEBUG) Log.d(mTag, "handleUpdateConditions unchanged conditionCount=" + count); 4403e0770163686c1be83959d681976dafaf25272e1John Spurlock return; 4413e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mConditions = conditions; 4434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleUpdateConditions(); 4444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4454db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 4463e0770163686c1be83959d681976dafaf25272e1John Spurlock private Condition[] trimConditions(Condition[] conditions) { 447530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (conditions == null || conditions.length <= mMaxOptionalConditions) { 4483e0770163686c1be83959d681976dafaf25272e1John Spurlock // no need to trim 4493e0770163686c1be83959d681976dafaf25272e1John Spurlock return conditions; 4503e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4513e0770163686c1be83959d681976dafaf25272e1John Spurlock // look for current exit condition, ensure it is included if found 4523e0770163686c1be83959d681976dafaf25272e1John Spurlock int found = -1; 4533e0770163686c1be83959d681976dafaf25272e1John Spurlock for (int i = 0; i < conditions.length; i++) { 4543e0770163686c1be83959d681976dafaf25272e1John Spurlock final Condition c = conditions[i]; 4553e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mSessionExitCondition != null && sameConditionId(mSessionExitCondition, c)) { 4563e0770163686c1be83959d681976dafaf25272e1John Spurlock found = i; 4573e0770163686c1be83959d681976dafaf25272e1John Spurlock break; 4583e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4593e0770163686c1be83959d681976dafaf25272e1John Spurlock } 460530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Condition[] rt = Arrays.copyOf(conditions, mMaxOptionalConditions); 461530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (found >= mMaxOptionalConditions) { 4623e0770163686c1be83959d681976dafaf25272e1John Spurlock // found after the first N, promote to the end of the first N 463530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock rt[mMaxOptionalConditions - 1] = conditions[found]; 4643e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4653e0770163686c1be83959d681976dafaf25272e1John Spurlock return rt; 4663e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4673e0770163686c1be83959d681976dafaf25272e1John Spurlock 4684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void handleUpdateConditions() { 469530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitionHelper.isTransitioning()) { 470530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.pendingUpdateConditions(); 471530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return; 472530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 4734db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final int conditionCount = mConditions == null ? 0 : mConditions.length; 4744db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "handleUpdateConditions conditionCount=" + conditionCount); 4754db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // forever 476ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock bind(null, mZenConditions.getChildAt(FOREVER_CONDITION_INDEX)); 4774db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // countdown 478530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mCountdownConditionSupported) { 479530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mTimeCondition, mZenConditions.getChildAt(COUNTDOWN_CONDITION_INDEX)); 480530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 4814db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // provider conditions 4824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock for (int i = 0; i < conditionCount; i++) { 483530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mConditions[i], mZenConditions.getChildAt(mFirstConditionIndex + i)); 4848600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 485530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock // hide the rest 486530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = mZenConditions.getChildCount() - 1; i > mFirstConditionIndex + conditionCount; i--) { 487530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.getChildAt(i).setVisibility(GONE); 4884db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4893e0770163686c1be83959d681976dafaf25272e1John Spurlock // ensure something is selected 4903e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mExpanded) { 4913e0770163686c1be83959d681976dafaf25272e1John Spurlock ensureSelection(); 4923e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4934db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4944db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 495856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private ConditionTag getConditionTagAt(int index) { 496ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock return (ConditionTag) mZenConditions.getChildAt(index).getTag(); 497856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 498856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 499530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private int getVisibleConditions() { 500530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock int rt = 0; 501530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = mZenConditions.getChildCount(); 502530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 503530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock rt += mZenConditions.getChildAt(i).getVisibility() == VISIBLE ? 1 : 0; 504530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 505530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return rt; 506530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 507530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 508530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void hideAllConditions() { 509530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = mZenConditions.getChildCount(); 510530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 511530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.getChildAt(i).setVisibility(GONE); 512530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 513530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 514530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 5153e0770163686c1be83959d681976dafaf25272e1John Spurlock private void ensureSelection() { 516856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // are we left without anything selected? if so, set a default 517530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int visibleConditions = getVisibleConditions(); 518530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (visibleConditions == 0) return; 519530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < visibleConditions; i++) { 520530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag tag = getConditionTagAt(i); 521530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null && tag.rb.isChecked()) { 522530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "Not selecting a default, checked=" + tag.condition); 523856edebad73560e9b1cce021a7de9a0470d07176John Spurlock return; 524856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 525856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 526530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag foreverTag = getConditionTagAt(FOREVER_CONDITION_INDEX); 527530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (foreverTag == null) return; 528856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Selecting a default"); 5298f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock final int favoriteIndex = mPrefs.getMinuteIndex(); 530530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (favoriteIndex == -1 || !mCountdownConditionSupported) { 531530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock foreverTag.rb.setChecked(true); 532856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 5339c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek mTimeCondition = ZenModeConfig.toTimeCondition(mContext, 5349c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[favoriteIndex], ActivityManager.getCurrentUser()); 535856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = favoriteIndex; 536530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mTimeCondition, mZenConditions.getChildAt(COUNTDOWN_CONDITION_INDEX)); 537530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true); 538856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5398600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5408600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 5414db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void handleExitConditionChanged(Condition exitCondition) { 5424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(exitCondition); 5434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "handleExitConditionChanged " + mExitCondition); 544530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 545856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 546856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final ConditionTag tag = getConditionTagAt(i); 547530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null) { 548530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (sameConditionId(tag.condition, mExitCondition)) { 549530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(exitCondition, mZenConditions.getChildAt(i)); 550530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 551530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 552856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 555530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean isCountdown(Condition c) { 556530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return c != null && ZenModeConfig.isValidCountdownConditionId(c.id); 557530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 558530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 559530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void bind(final Condition condition, final View row) { 5608600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock final boolean enabled = condition == null || condition.state == Condition.STATE_TRUE; 561856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final ConditionTag tag = 562856edebad73560e9b1cce021a7de9a0470d07176John Spurlock row.getTag() != null ? (ConditionTag) row.getTag() : new ConditionTag(); 563856edebad73560e9b1cce021a7de9a0470d07176John Spurlock row.setTag(tag); 564530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean first = tag.rb == null; 565856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (tag.rb == null) { 566856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb = (RadioButton) row.findViewById(android.R.id.checkbox); 567856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5684db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock tag.condition = condition; 569530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Uri conditionId = getConditionId(tag.condition); 570530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "bind i=" + mZenConditions.indexOfChild(row) + " first=" + first 571530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " condition=" + conditionId); 572856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setEnabled(enabled); 573530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean checked = (mSessionExitCondition != null 574530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock || mAttachedZen != Global.ZEN_MODE_OFF) 575530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock && (sameConditionId(mSessionExitCondition, tag.condition) 576530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock || isCountdown(mSessionExitCondition) && isCountdown(tag.condition)); 577530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (checked != tag.rb.isChecked()) { 578530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "bind checked=" + checked + " condition=" + conditionId); 579530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock tag.rb.setChecked(checked); 58050806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock } 581856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 5828600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 5838600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 584ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mExpanded && isChecked) { 585530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "onCheckedChanged " + conditionId); 586530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 587856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 588530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag childTag = getConditionTagAt(i); 589530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (childTag == null || childTag == tag) continue; 590856edebad73560e9b1cce021a7de9a0470d07176John Spurlock childTag.rb.setChecked(false); 5918600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock select(tag.condition); 593a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceConditionSelection(tag); 5948600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5958600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5968600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 597a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 598c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.lines == null) { 599c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines = row.findViewById(android.R.id.content); 600a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 601c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.line1 == null) { 602c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1 = (TextView) row.findViewById(android.R.id.text1); 603c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } 604c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.line2 == null) { 605c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2 = (TextView) row.findViewById(android.R.id.text2); 606c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } 607c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final String line1, line2; 6088600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (condition == null) { 609c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock line1 = mContext.getString(com.android.internal.R.string.zen_mode_forever); 610c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock line2 = null; 611c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } else { 612c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock line1 = !TextUtils.isEmpty(condition.line1) ? condition.line1 : condition.summary; 613c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock line2 = condition.line2; 614c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } 615c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1.setText(line1); 616c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (TextUtils.isEmpty(line2)) { 617c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setVisibility(GONE); 6188600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 619c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setVisibility(VISIBLE); 620c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setText(line2); 6218600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 622c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setEnabled(enabled); 623c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setAlpha(enabled ? 1 : .4f); 624a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 6258600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock final ImageView button1 = (ImageView) row.findViewById(android.R.id.button1); 6268600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock button1.setOnClickListener(new OnClickListener() { 6278600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6288600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 629856edebad73560e9b1cce021a7de9a0470d07176John Spurlock onClickTimeButton(row, tag, false /*down*/); 6308600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6318600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 6328600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 6338600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock final ImageView button2 = (ImageView) row.findViewById(android.R.id.button2); 6348600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock button2.setOnClickListener(new OnClickListener() { 6358600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6368600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 637856edebad73560e9b1cce021a7de9a0470d07176John Spurlock onClickTimeButton(row, tag, true /*up*/); 6388600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6398600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 640c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setOnClickListener(new OnClickListener() { 6418600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6428600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 643856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setChecked(true); 6448600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6458600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 646856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 647530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); 648856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (time > 0) { 649530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button1.setVisibility(VISIBLE); 650530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button2.setVisibility(VISIBLE); 651856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mBucketIndex > -1) { 652856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setEnabled(mBucketIndex > 0); 653856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setEnabled(mBucketIndex < MINUTE_BUCKETS.length - 1); 654856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 655856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long span = time - System.currentTimeMillis(); 656856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS); 657c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final Condition maxCondition = ZenModeConfig.toTimeCondition(mContext, 6589c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MAX_BUCKET_MINUTES, ActivityManager.getCurrentUser()); 659856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary)); 660856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 661856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 662856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setAlpha(button1.isEnabled() ? 1f : .5f); 663856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setAlpha(button2.isEnabled() ? 1f : .5f); 6648600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 665530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button1.setVisibility(GONE); 666530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button2.setVisibility(GONE); 6678600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 668a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock // wire up interaction callbacks for newly-added condition rows 669530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (first) { 670a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(tag.rb, mInteractionCallback); 671c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock Interaction.register(tag.lines, mInteractionCallback); 672a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(button1, mInteractionCallback); 673a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(button2, mInteractionCallback); 674a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 675530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock row.setVisibility(VISIBLE); 676a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 677a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 678a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock private void announceConditionSelection(ConditionTag tag) { 679a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock final int zen = getSelectedZen(Global.ZEN_MODE_OFF); 680a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock String modeText; 681a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock switch(zen) { 682a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 683a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock modeText = mContext.getString(R.string.zen_important_interruptions); 684a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock break; 685a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock case Global.ZEN_MODE_NO_INTERRUPTIONS: 686a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock modeText = mContext.getString(R.string.zen_no_interruptions); 687a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock break; 688a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock default: 689a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock return; 690a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 691a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceForAccessibility(mContext.getString(R.string.zen_mode_and_condition, modeText, 692c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1.getText())); 693856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 694856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 695856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private void onClickTimeButton(View row, ConditionTag tag, boolean up) { 696856edebad73560e9b1cce021a7de9a0470d07176John Spurlock Condition newCondition = null; 697856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final int N = MINUTE_BUCKETS.length; 698856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mBucketIndex == -1) { 699856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // not on a known index, search for the next or prev bucket by time 7004db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final Uri conditionId = getConditionId(tag.condition); 7014db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); 702856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long now = System.currentTimeMillis(); 703856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 704856edebad73560e9b1cce021a7de9a0470d07176John Spurlock int j = up ? i : N - 1 - i; 705856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final int bucketMinutes = MINUTE_BUCKETS[j]; 706856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long bucketTime = now + bucketMinutes * MINUTES_MS; 707856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (up && bucketTime > time || !up && bucketTime < time) { 708856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = j; 709c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7109c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek bucketTime, bucketMinutes, now, ActivityManager.getCurrentUser()); 711856edebad73560e9b1cce021a7de9a0470d07176John Spurlock break; 712856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 713856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 714856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (newCondition == null) { 715856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = DEFAULT_BUCKET_INDEX; 716c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7179c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 718856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 719856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 720856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // on a known index, simply increment or decrement 721856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1))); 722c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7239c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 724856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7254db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mTimeCondition = newCondition; 7264db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock bind(mTimeCondition, row); 727856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setChecked(true); 7284db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock select(mTimeCondition); 729a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceConditionSelection(tag); 730856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 731856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 732530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void select(final Condition condition) { 7334db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "select " + condition); 734856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mController != null) { 735530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 736530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 737530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 738530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mController.setExitCondition(condition); 739530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 740530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 741856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7424db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(condition); 7434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (condition == null) { 7448f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.setMinuteIndex(-1); 745530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else if (isCountdown(condition) && mBucketIndex != -1) { 7468f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.setMinuteIndex(mBucketIndex); 7478600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 748530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(copy(condition)); 7498600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7508600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7518600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void fireMoreSettings() { 7528600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mCallback != null) { 7538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback.onMoreSettings(); 7548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7568600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7578600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void fireInteraction() { 7588600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mCallback != null) { 7598600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback.onInteraction(); 7608600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7618600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7628600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 763ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void fireExpanded() { 764ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mCallback != null) { 765ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mCallback.onExpanded(mExpanded); 766ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 767ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 768ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 7698600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { 7708600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 771ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void onZenChanged(int zen) { 772ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mHandler.obtainMessage(H.UPDATE_ZEN, zen, 0).sendToTarget(); 773ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 774ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 7758600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onConditionsChanged(Condition[] conditions) { 7768600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mHandler.obtainMessage(H.UPDATE_CONDITIONS, conditions).sendToTarget(); 7778600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 778856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 779856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 7804db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock public void onExitConditionChanged(Condition exitCondition) { 7814db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mHandler.obtainMessage(H.EXIT_CONDITION_CHANGED, exitCondition).sendToTarget(); 782856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7838600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }; 7848600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7858600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final class H extends Handler { 7868600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private static final int UPDATE_CONDITIONS = 1; 787856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final int EXIT_CONDITION_CHANGED = 2; 788ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final int UPDATE_ZEN = 3; 7898600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7908600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private H() { 7918600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super(Looper.getMainLooper()); 7928600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7938600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7948600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 7958600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void handleMessage(Message msg) { 7968600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (msg.what == UPDATE_CONDITIONS) { 79789f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock handleUpdateConditions((Condition[]) msg.obj); 798856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else if (msg.what == EXIT_CONDITION_CHANGED) { 7994db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleExitConditionChanged((Condition) msg.obj); 800ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else if (msg.what == UPDATE_ZEN) { 801ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock handleUpdateZen(msg.arg1); 8028600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8038600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8058600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 8068600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public interface Callback { 8078600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock void onMoreSettings(); 8088600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock void onInteraction(); 809ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock void onExpanded(boolean expanded); 8108600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 811856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 812856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // used as the view tag on condition rows 813856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static class ConditionTag { 814856edebad73560e9b1cce021a7de9a0470d07176John Spurlock RadioButton rb; 815c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock View lines; 816c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock TextView line1; 817c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock TextView line2; 8184db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock Condition condition; 819856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 820856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8218f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final class Prefs implements OnSharedPreferenceChangeListener { 822856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final String KEY_MINUTE_INDEX = "minuteIndex"; 8238f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private static final String KEY_NONE_SELECTED = "noneSelected"; 8248f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8258f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final int mNoneDangerousThreshold; 826856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 827856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private int mMinuteIndex; 8288f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int mNoneSelected; 829856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8308f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private Prefs() { 8318f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneDangerousThreshold = mContext.getResources() 8328f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock .getInteger(R.integer.zen_mode_alarm_warning_threshold); 833856edebad73560e9b1cce021a7de9a0470d07176John Spurlock prefs().registerOnSharedPreferenceChangeListener(this); 834856edebad73560e9b1cce021a7de9a0470d07176John Spurlock updateMinuteIndex(); 8358f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock updateNoneSelected(); 8368f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8378f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8388f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock public boolean isNoneDangerous() { 8398f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return mNoneSelected < mNoneDangerousThreshold; 8408f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8418f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8428f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock public void trackNoneSelected() { 8438f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneSelected = clampNoneSelected(mNoneSelected + 1); 8448f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "Setting none selected: " + mNoneSelected + " threshold=" 8458f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock + mNoneDangerousThreshold); 8468f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock prefs().edit().putInt(KEY_NONE_SELECTED, mNoneSelected).apply(); 847856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 848856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 849856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public int getMinuteIndex() { 850856edebad73560e9b1cce021a7de9a0470d07176John Spurlock return mMinuteIndex; 851856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 852856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 853856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public void setMinuteIndex(int minuteIndex) { 8548f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock minuteIndex = clampIndex(minuteIndex); 855856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (minuteIndex == mMinuteIndex) return; 8568f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mMinuteIndex = clampIndex(minuteIndex); 857856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Setting favorite minute index: " + mMinuteIndex); 858856edebad73560e9b1cce021a7de9a0470d07176John Spurlock prefs().edit().putInt(KEY_MINUTE_INDEX, mMinuteIndex).apply(); 859856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 860856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 861856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 862856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 863856edebad73560e9b1cce021a7de9a0470d07176John Spurlock updateMinuteIndex(); 8648f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock updateNoneSelected(); 865856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 866856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 867856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private SharedPreferences prefs() { 8687bbb9f64103c297d1b9738339f3bfeed0f41030eJohn Spurlock return mContext.getSharedPreferences(mContext.getPackageName(), 0); 869856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 870856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 871856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private void updateMinuteIndex() { 8728f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mMinuteIndex = clampIndex(prefs().getInt(KEY_MINUTE_INDEX, DEFAULT_BUCKET_INDEX)); 873856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Favorite minute index: " + mMinuteIndex); 874856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 875856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8768f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int clampIndex(int index) { 8778f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return MathUtils.constrain(index, -1, MINUTE_BUCKETS.length - 1); 8788f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8798f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8808f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private void updateNoneSelected() { 8818f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneSelected = clampNoneSelected(prefs().getInt(KEY_NONE_SELECTED, 0)); 8828f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "None selected: " + mNoneSelected); 8838f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8848f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8858f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int clampNoneSelected(int noneSelected) { 8868f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return MathUtils.constrain(noneSelected, 0, Integer.MAX_VALUE); 887856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 888856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 889ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 890ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private final SegmentedButtons.Callback mZenButtonsCallback = new SegmentedButtons.Callback() { 891ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 892530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void onSelected(final Object value) { 893ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (value != null && mZenButtons.isShown()) { 894ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (DEBUG) Log.d(mTag, "mZenButtonsCallback selected=" + value); 895530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 896530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 897530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 898530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mController.setZen((Integer) value); 899530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 900530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 901ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 902ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 903a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 904a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock @Override 905a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock public void onInteraction() { 906a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock fireInteraction(); 907a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 908a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock }; 909a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 910a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock private final Interaction.Callback mInteractionCallback = new Interaction.Callback() { 911a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock @Override 912a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock public void onInteraction() { 913a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock fireInteraction(); 914a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 915ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock }; 916530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 917530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final class TransitionHelper implements TransitionListener, Runnable { 918530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final ArraySet<View> mTransitioningViews = new ArraySet<View>(); 919530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 920530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mTransitioning; 921530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mPendingUpdateConditions; 922530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mPendingUpdateWidgets; 923530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 924530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void clear() { 925530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.clear(); 926530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = mPendingUpdateWidgets = false; 927530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 928530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 929530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 930530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.println(" TransitionHelper state:"); 931530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mPendingUpdateConditions="); pw.println(mPendingUpdateConditions); 932530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mPendingUpdateWidgets="); pw.println(mPendingUpdateWidgets); 933530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mTransitioning="); pw.println(mTransitioning); 934530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mTransitioningViews="); pw.println(mTransitioningViews); 935530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 936530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 937530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void pendingUpdateConditions() { 938530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = true; 939530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 940530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 941530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void pendingUpdateWidgets() { 942530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateWidgets = true; 943530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 944530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 945530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public boolean isTransitioning() { 946530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return !mTransitioningViews.isEmpty(); 947530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 948530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 949530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 950530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void startTransition(LayoutTransition transition, 951530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock ViewGroup container, View view, int transitionType) { 952530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.add(view); 953530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateTransitioning(); 954530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 955530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 956530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 957530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void endTransition(LayoutTransition transition, 958530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock ViewGroup container, View view, int transitionType) { 959530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.remove(view); 960530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateTransitioning(); 961530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 962530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 963530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 964530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 965530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "TransitionHelper run" 966530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " mPendingUpdateWidgets=" + mPendingUpdateWidgets 967530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " mPendingUpdateConditions=" + mPendingUpdateConditions); 968530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateWidgets) { 969530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateWidgets(); 970530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 971530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateConditions) { 972530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock handleUpdateConditions(); 973530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 974530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateWidgets = mPendingUpdateConditions = false; 975530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 976530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 977530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void updateTransitioning() { 978530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean transitioning = isTransitioning(); 979530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitioning == transitioning) return; 980530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioning = transitioning; 981530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "TransitionHelper mTransitioning=" + mTransitioning); 982530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (!mTransitioning) { 983530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateConditions || mPendingUpdateWidgets) { 984530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mHandler.post(this); 985530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else { 986530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = mPendingUpdateWidgets = false; 987530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 988530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 989530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 990530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 9918600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock} 992