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(); 930b688503754737a20165f0cff1833e714a5c4164John Spurlock private final Uri mForeverId; 94856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 95eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock private String mTag = TAG + "/" + Integer.toHexString(System.identityHashCode(this)); 96ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 97ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private SegmentedButtons mZenButtons; 98ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private View mZenSubhead; 99ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private TextView mZenSubheadCollapsed; 100ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private TextView mZenSubheadExpanded; 1017f8f22a53005f776c8ee4d299e748e74b077ffd9John Spurlock private View mMoreSettings; 102ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private LinearLayout mZenConditions; 103ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 1048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private Callback mCallback; 1058600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private ZenModeController mController; 1068600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private boolean mRequestingConditions; 1074db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mExitCondition; 1084db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private String mExitConditionText; 109856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private int mBucketIndex = -1; 110ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private boolean mExpanded; 111530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mHidden; 112fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock private int mSessionZen; 1138f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int mAttachedZen; 1143e0770163686c1be83959d681976dafaf25272e1John Spurlock private boolean mAttached; 1154db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mSessionExitCondition; 1164db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition[] mConditions; 1174db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition mTimeCondition; 1188600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 1198600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public ZenModePanel(Context context, AttributeSet attrs) { 1208600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super(context, attrs); 1217f1df5e98578f8532a5e009009e7c1f82ed5885cJohn Spurlock mContext = context; 1228f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs = new Prefs(); 123f7d22132c2c9b83134b05aaed63a89f61c639fd9John Spurlock mInflater = LayoutInflater.from(mContext.getApplicationContext()); 124661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock mIconPulser = new IconPulser(mContext); 125b71e68f9aa44b220cabaace70493751538d94dc0John Spurlock final Resources res = mContext.getResources(); 1268f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSubheadWarningColor = res.getColor(R.color.system_warning_color); 1278f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSubheadColor = res.getColor(R.color.qs_subhead); 1283e0770163686c1be83959d681976dafaf25272e1John Spurlock mInterpolator = AnimationUtils.loadInterpolator(mContext, 1293e0770163686c1be83959d681976dafaf25272e1John Spurlock com.android.internal.R.interpolator.fast_out_slow_in); 130530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mCountdownConditionSupported = NotificationManager.from(mContext) 131530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock .isSystemConditionProviderEnabled(ZenModeConfig.COUNTDOWN_PATH); 132530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int countdownDelta = mCountdownConditionSupported ? 1 : 0; 133530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mFirstConditionIndex = COUNTDOWN_CONDITION_INDEX + countdownDelta; 134530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int minConditions = 1 /*forever*/ + countdownDelta; 1353e0770163686c1be83959d681976dafaf25272e1John Spurlock mMaxConditions = MathUtils.constrain(res.getInteger(R.integer.zen_mode_max_conditions), 136530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock minConditions, 100); 137530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mMaxOptionalConditions = mMaxConditions - minConditions; 1380b688503754737a20165f0cff1833e714a5c4164John Spurlock mForeverId = Condition.newId(mContext).appendPath("forever").build(); 139856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "new ZenModePanel"); 140856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 141856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 142530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 143530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.println("ZenModePanel state:"); 144530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mCountdownConditionSupported="); pw.println(mCountdownConditionSupported); 145530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mMaxConditions="); pw.println(mMaxConditions); 146530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mRequestingConditions="); pw.println(mRequestingConditions); 147530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mAttached="); pw.println(mAttached); 148530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mHidden="); pw.println(mHidden); 149530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mExpanded="); pw.println(mExpanded); 150530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mSessionZen="); pw.println(mSessionZen); 151530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mAttachedZen="); pw.println(mAttachedZen); 152530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.dump(fd, pw, args); 153530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 154530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 1558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 1568600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock protected void onFinishInflate() { 1578600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super.onFinishInflate(); 158ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 159ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons = (SegmentedButtons) findViewById(R.id.zen_buttons); 160bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_none, R.drawable.ic_zen_none, 161bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock Global.ZEN_MODE_NO_INTERRUPTIONS); 162bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_priority, R.drawable.ic_zen_important, 1634291fb79c70ba77af4357d53dac2e5626eabba45John Spurlock Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS); 164bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock mZenButtons.addButton(R.string.interruption_level_all, R.drawable.ic_zen_all, 165bb4a702e6fe44cb026097db13492f8345b38ee97John Spurlock Global.ZEN_MODE_OFF); 166ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons.setCallback(mZenButtonsCallback); 167ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 168530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ViewGroup zenButtonsContainer = (ViewGroup) findViewById(R.id.zen_buttons_container); 169530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock zenButtonsContainer.setLayoutTransition(newLayoutTransition(null)); 170530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 171ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubhead = findViewById(R.id.zen_subhead); 172ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 173ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed = (TextView) findViewById(R.id.zen_subhead_collapsed); 174ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setOnClickListener(new View.OnClickListener() { 175ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 176ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void onClick(View v) { 177ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(true); 178ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 179ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock }); 180a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mZenSubheadCollapsed, mInteractionCallback); 181ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 182ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadExpanded = (TextView) findViewById(R.id.zen_subhead_expanded); 183a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mZenSubheadExpanded, mInteractionCallback); 184ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 185ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mMoreSettings = findViewById(R.id.zen_more_settings); 1867f8f22a53005f776c8ee4d299e748e74b077ffd9John Spurlock mMoreSettings.setOnClickListener(new View.OnClickListener() { 1878600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 1888600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 1898600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock fireMoreSettings(); 1908600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 1918600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 192a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(mMoreSettings, mInteractionCallback); 193ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 194ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenConditions = (LinearLayout) findViewById(R.id.zen_conditions); 195530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < mMaxConditions; i++) { 196530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.addView(mInflater.inflate(R.layout.zen_mode_condition, this, false)); 197530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 198530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 199530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setLayoutTransition(newLayoutTransition(mTransitionHelper)); 2003e0770163686c1be83959d681976dafaf25272e1John Spurlock } 2013e0770163686c1be83959d681976dafaf25272e1John Spurlock 202530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private LayoutTransition newLayoutTransition(TransitionListener listener) { 2033e0770163686c1be83959d681976dafaf25272e1John Spurlock final LayoutTransition transition = new LayoutTransition(); 2043e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.disableTransitionType(LayoutTransition.DISAPPEARING); 2053e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); 206530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock transition.disableTransitionType(LayoutTransition.APPEARING); 2073e0770163686c1be83959d681976dafaf25272e1John Spurlock transition.setInterpolator(LayoutTransition.CHANGE_APPEARING, mInterpolator); 208530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (listener != null) { 209530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock transition.addTransitionListener(listener); 210530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 2113e0770163686c1be83959d681976dafaf25272e1John Spurlock return transition; 2128600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 2138600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 2148600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 215856edebad73560e9b1cce021a7de9a0470d07176John Spurlock protected void onAttachedToWindow() { 216856edebad73560e9b1cce021a7de9a0470d07176John Spurlock super.onAttachedToWindow(); 217856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "onAttachedToWindow"); 2183e0770163686c1be83959d681976dafaf25272e1John Spurlock mAttached = true; 2198f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mAttachedZen = getSelectedZen(-1); 2208f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mSessionZen = mAttachedZen; 221530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.clear(); 222530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(copy(mExitCondition)); 223ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 22450806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock updateWidgets(); 2253e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(!mHidden); 226856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 227856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 228856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 229856edebad73560e9b1cce021a7de9a0470d07176John Spurlock protected void onDetachedFromWindow() { 230856edebad73560e9b1cce021a7de9a0470d07176John Spurlock super.onDetachedFromWindow(); 231856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "onDetachedFromWindow"); 2328f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock checkForAttachedZenChange(); 2333e0770163686c1be83959d681976dafaf25272e1John Spurlock mAttached = false; 2348f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mAttachedZen = -1; 235fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = -1; 236530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(null); 237ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(false); 2383e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(false); 239530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.clear(); 240530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 241530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 242530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void setSessionExitCondition(Condition condition) { 243530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (Objects.equals(condition, mSessionExitCondition)) return; 244530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "mSessionExitCondition=" + getConditionId(condition)); 245530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mSessionExitCondition = condition; 246856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 247856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 248eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock public void setHidden(boolean hidden) { 249eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock if (mHidden == hidden) return; 2503e0770163686c1be83959d681976dafaf25272e1John Spurlock if (DEBUG) Log.d(mTag, "hidden=" + hidden); 251eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mHidden = hidden; 2523e0770163686c1be83959d681976dafaf25272e1John Spurlock setRequestingConditions(mAttached && !mHidden); 253eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock updateWidgets(); 254eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock } 255eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock 2568f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private void checkForAttachedZenChange() { 2578f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock final int selectedZen = getSelectedZen(-1); 2588f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "selectedZen=" + selectedZen); 2598f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (selectedZen != mAttachedZen) { 2608f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "attachedZen: " + mAttachedZen + " -> " + selectedZen); 2618f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (selectedZen == Global.ZEN_MODE_NO_INTERRUPTIONS) { 2628f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.trackNoneSelected(); 2638f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2648f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2658f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 2668f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 267ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void setExpanded(boolean expanded) { 268ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (expanded == mExpanded) return; 269ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mExpanded = expanded; 2703e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mExpanded) { 2713e0770163686c1be83959d681976dafaf25272e1John Spurlock ensureSelection(); 2723e0770163686c1be83959d681976dafaf25272e1John Spurlock } 273ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock updateWidgets(); 274ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock fireExpanded(); 2758600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 2768600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 2778600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock /** Start or stop requesting relevant zen mode exit conditions */ 278530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void setRequestingConditions(final boolean requesting) { 2798600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mRequestingConditions == requesting) return; 280856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "setRequestingConditions " + requesting); 2818600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mRequestingConditions = requesting; 282856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mController != null) { 283530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 284530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 285530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 286530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mController.requestConditions(requesting); 287530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 288530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 289856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 2908600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mRequestingConditions) { 2914db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mTimeCondition = parseExistingTimeCondition(mExitCondition); 2924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (mTimeCondition != null) { 293856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = -1; 294856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 295856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = DEFAULT_BUCKET_INDEX; 296c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock mTimeCondition = ZenModeConfig.toTimeCondition(mContext, 2979c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 298856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 299856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Initial bucket index: " + mBucketIndex); 3004db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mConditions = null; // reset conditions 3014db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleUpdateConditions(); 3028600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 303530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock hideAllConditions(); 3048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3058600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3068600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 307eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock public void init(ZenModeController controller) { 3088600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mController = controller; 3094db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(mController.getExitCondition()); 310ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 311fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = getSelectedZen(-1); 312ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock handleUpdateZen(mController.getZen()); 3134db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "init mExitCondition=" + mExitCondition); 314530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock hideAllConditions(); 315856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mController.addCallback(mZenCallback); 316ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 317ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 318e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk public void updateLocale() { 3194291fb79c70ba77af4357d53dac2e5626eabba45John Spurlock mZenButtons.updateLocale(); 320e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk } 321e2f4771a0105ad6a94ff759353ca00c3a5fba72aJason Monk 3224db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void setExitCondition(Condition exitCondition) { 32325c3421c5e65ddc7f2b2bf1b1208f3634e6f5256John Spurlock if (Objects.equals(mExitCondition, exitCondition)) return; 3244db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mExitCondition = exitCondition; 325530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "mExitCondition=" + getConditionId(mExitCondition)); 326ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock refreshExitConditionText(); 32789f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock updateWidgets(); 328ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 329ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 3304db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static Uri getConditionId(Condition condition) { 3314db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return condition != null ? condition.id : null; 3324db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3334db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 3344db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static boolean sameConditionId(Condition lhs, Condition rhs) { 3354db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return lhs == null ? rhs == null : rhs != null && lhs.id.equals(rhs.id); 3364db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3374db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 3384db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private static Condition copy(Condition condition) { 3394db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock return condition == null ? null : condition.copy(); 3404db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 3414db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 342ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void refreshExitConditionText() { 3434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (mExitCondition == null) { 3440b688503754737a20165f0cff1833e714a5c4164John Spurlock mExitConditionText = foreverSummary(); 345530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else if (isCountdown(mExitCondition)) { 3464db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final Condition condition = parseExistingTimeCondition(mExitCondition); 3470b688503754737a20165f0cff1833e714a5c4164John Spurlock mExitConditionText = condition != null ? condition.summary : foreverSummary(); 348ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else { 3494db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mExitConditionText = mExitCondition.summary; 350856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 3518600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3528600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 3538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void setCallback(Callback callback) { 3548600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback = callback; 3558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 3568600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 357ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void showSilentHint() { 358ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (DEBUG) Log.d(mTag, "showSilentHint"); 359ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mZenButtons == null || mZenButtons.getChildCount() == 0) return; 360ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final View noneButton = mZenButtons.getChildAt(0); 361661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock mIconPulser.start(noneButton); 362ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 363ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 364ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void handleUpdateZen(int zen) { 365fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock if (mSessionZen != -1 && mSessionZen != zen) { 366ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock setExpanded(zen != Global.ZEN_MODE_OFF); 367fbb3d6ff392e9cd13fea49f85f12efd0a304d448John Spurlock mSessionZen = zen; 368ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 369ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenButtons.setSelectedValue(zen); 370ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock updateWidgets(); 371530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock handleUpdateConditions(); 372530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mExpanded) { 373530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Condition selected = getSelectedCondition(); 374530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (!Objects.equals(mExitCondition, selected)) { 375530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock select(selected); 376530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 377530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 378530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 379530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 380530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private Condition getSelectedCondition() { 381530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 382530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 383530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag tag = getConditionTagAt(i); 384530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null && tag.rb.isChecked()) { 385530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return tag.condition; 386530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 387530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 388530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return null; 389ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 390ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 391ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private int getSelectedZen(int defValue) { 392ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final Object zen = mZenButtons.getSelectedValue(); 393ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock return zen != null ? (Integer) zen : defValue; 394ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 395ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 396ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void updateWidgets() { 397530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitionHelper.isTransitioning()) { 398530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.pendingUpdateWidgets(); 399530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return; 400530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 401ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final int zen = getSelectedZen(Global.ZEN_MODE_OFF); 402ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenOff = zen == Global.ZEN_MODE_OFF; 403ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenImportant = zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; 404ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock final boolean zenNone = zen == Global.ZEN_MODE_NO_INTERRUPTIONS; 405eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock final boolean expanded = !mHidden && mExpanded; 406eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock 407eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenButtons.setVisibility(mHidden ? GONE : VISIBLE); 40889fe1cb9debbfd629f1467b2a3d957e6dc1a8507John Spurlock mZenSubhead.setVisibility(!mHidden && !zenOff ? VISIBLE : GONE); 409eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenSubheadExpanded.setVisibility(expanded ? VISIBLE : GONE); 410eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenSubheadCollapsed.setVisibility(!expanded ? VISIBLE : GONE); 411eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mMoreSettings.setVisibility(zenImportant && expanded ? VISIBLE : GONE); 412eb2727bb2f0f32016ca7c4bdd2c0a79eec8cb80aJohn Spurlock mZenConditions.setVisibility(!zenOff && expanded ? VISIBLE : GONE); 413ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 414ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (zenNone) { 41586d04fdaa1853cf7ff8ec9aba1dd925dd098167eJohn Spurlock mZenSubheadExpanded.setText(R.string.zen_no_interruptions_with_warning); 416ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setText(mExitConditionText); 417ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else if (zenImportant) { 418ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadExpanded.setText(R.string.zen_important_interruptions); 419ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mZenSubheadCollapsed.setText(mExitConditionText); 420ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 4218f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mZenSubheadExpanded.setTextColor(zenNone && mPrefs.isNoneDangerous() 4228f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock ? mSubheadWarningColor : mSubheadColor); 42389f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock } 42489f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock 4254db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private Condition parseExistingTimeCondition(Condition condition) { 4264db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (condition == null) return null; 4274db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(condition.id); 428856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (time == 0) return null; 429c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final long now = System.currentTimeMillis(); 430c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final long span = time - now; 431856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (span <= 0 || span > MAX_BUCKET_MINUTES * MINUTES_MS) return null; 432c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock return ZenModeConfig.toTimeCondition(mContext, 4339c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek time, Math.round(span / (float) MINUTES_MS), now, ActivityManager.getCurrentUser()); 4348600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 4358600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 4368600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void handleUpdateConditions(Condition[] conditions) { 4373e0770163686c1be83959d681976dafaf25272e1John Spurlock conditions = trimConditions(conditions); 4383e0770163686c1be83959d681976dafaf25272e1John Spurlock if (Arrays.equals(conditions, mConditions)) { 4393e0770163686c1be83959d681976dafaf25272e1John Spurlock final int count = mConditions == null ? 0 : mConditions.length; 4403e0770163686c1be83959d681976dafaf25272e1John Spurlock if (DEBUG) Log.d(mTag, "handleUpdateConditions unchanged conditionCount=" + count); 4413e0770163686c1be83959d681976dafaf25272e1John Spurlock return; 4423e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4434db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mConditions = conditions; 4444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleUpdateConditions(); 4454db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4464db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 4473e0770163686c1be83959d681976dafaf25272e1John Spurlock private Condition[] trimConditions(Condition[] conditions) { 448530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (conditions == null || conditions.length <= mMaxOptionalConditions) { 4493e0770163686c1be83959d681976dafaf25272e1John Spurlock // no need to trim 4503e0770163686c1be83959d681976dafaf25272e1John Spurlock return conditions; 4513e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4523e0770163686c1be83959d681976dafaf25272e1John Spurlock // look for current exit condition, ensure it is included if found 4533e0770163686c1be83959d681976dafaf25272e1John Spurlock int found = -1; 4543e0770163686c1be83959d681976dafaf25272e1John Spurlock for (int i = 0; i < conditions.length; i++) { 4553e0770163686c1be83959d681976dafaf25272e1John Spurlock final Condition c = conditions[i]; 4563e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mSessionExitCondition != null && sameConditionId(mSessionExitCondition, c)) { 4573e0770163686c1be83959d681976dafaf25272e1John Spurlock found = i; 4583e0770163686c1be83959d681976dafaf25272e1John Spurlock break; 4593e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4603e0770163686c1be83959d681976dafaf25272e1John Spurlock } 461530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Condition[] rt = Arrays.copyOf(conditions, mMaxOptionalConditions); 462530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (found >= mMaxOptionalConditions) { 4633e0770163686c1be83959d681976dafaf25272e1John Spurlock // found after the first N, promote to the end of the first N 464530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock rt[mMaxOptionalConditions - 1] = conditions[found]; 4653e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4663e0770163686c1be83959d681976dafaf25272e1John Spurlock return rt; 4673e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4683e0770163686c1be83959d681976dafaf25272e1John Spurlock 4694db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void handleUpdateConditions() { 470530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitionHelper.isTransitioning()) { 471530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitionHelper.pendingUpdateConditions(); 472530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return; 473530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 4744db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final int conditionCount = mConditions == null ? 0 : mConditions.length; 4754db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "handleUpdateConditions conditionCount=" + conditionCount); 4764db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // forever 4770b688503754737a20165f0cff1833e714a5c4164John Spurlock bind(forever(), mZenConditions.getChildAt(FOREVER_CONDITION_INDEX)); 4784db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // countdown 4790b688503754737a20165f0cff1833e714a5c4164John Spurlock if (mCountdownConditionSupported && mTimeCondition != null) { 480530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mTimeCondition, mZenConditions.getChildAt(COUNTDOWN_CONDITION_INDEX)); 481530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 4824db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock // provider conditions 4834db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock for (int i = 0; i < conditionCount; i++) { 484530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mConditions[i], mZenConditions.getChildAt(mFirstConditionIndex + i)); 4858600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 486530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock // hide the rest 4870b688503754737a20165f0cff1833e714a5c4164John Spurlock for (int i = mZenConditions.getChildCount() - 1; i > mFirstConditionIndex + conditionCount; 4880b688503754737a20165f0cff1833e714a5c4164John Spurlock i--) { 489530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.getChildAt(i).setVisibility(GONE); 4904db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4913e0770163686c1be83959d681976dafaf25272e1John Spurlock // ensure something is selected 4923e0770163686c1be83959d681976dafaf25272e1John Spurlock if (mExpanded) { 4933e0770163686c1be83959d681976dafaf25272e1John Spurlock ensureSelection(); 4943e0770163686c1be83959d681976dafaf25272e1John Spurlock } 4954db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock } 4964db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock 4970b688503754737a20165f0cff1833e714a5c4164John Spurlock private Condition forever() { 4980b688503754737a20165f0cff1833e714a5c4164John Spurlock return new Condition(mForeverId, foreverSummary(), "", "", 0 /*icon*/, Condition.STATE_TRUE, 4990b688503754737a20165f0cff1833e714a5c4164John Spurlock 0 /*flags*/); 5000b688503754737a20165f0cff1833e714a5c4164John Spurlock } 5010b688503754737a20165f0cff1833e714a5c4164John Spurlock 5020b688503754737a20165f0cff1833e714a5c4164John Spurlock private String foreverSummary() { 5030b688503754737a20165f0cff1833e714a5c4164John Spurlock return mContext.getString(com.android.internal.R.string.zen_mode_forever); 5040b688503754737a20165f0cff1833e714a5c4164John Spurlock } 5050b688503754737a20165f0cff1833e714a5c4164John Spurlock 506856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private ConditionTag getConditionTagAt(int index) { 507ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock return (ConditionTag) mZenConditions.getChildAt(index).getTag(); 508856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 509856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 510530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private int getVisibleConditions() { 511530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock int rt = 0; 512530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = mZenConditions.getChildCount(); 513530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 514530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock rt += mZenConditions.getChildAt(i).getVisibility() == VISIBLE ? 1 : 0; 515530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 516530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return rt; 517530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 518530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 519530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void hideAllConditions() { 520530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = mZenConditions.getChildCount(); 521530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < N; i++) { 522530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mZenConditions.getChildAt(i).setVisibility(GONE); 523530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 524530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 525530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 5263e0770163686c1be83959d681976dafaf25272e1John Spurlock private void ensureSelection() { 527856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // are we left without anything selected? if so, set a default 528530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int visibleConditions = getVisibleConditions(); 529530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (visibleConditions == 0) return; 530530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock for (int i = 0; i < visibleConditions; i++) { 531530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag tag = getConditionTagAt(i); 532530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null && tag.rb.isChecked()) { 533530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "Not selecting a default, checked=" + tag.condition); 534856edebad73560e9b1cce021a7de9a0470d07176John Spurlock return; 535856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 536856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 537530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag foreverTag = getConditionTagAt(FOREVER_CONDITION_INDEX); 538530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (foreverTag == null) return; 539856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Selecting a default"); 5408f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock final int favoriteIndex = mPrefs.getMinuteIndex(); 541530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (favoriteIndex == -1 || !mCountdownConditionSupported) { 542530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock foreverTag.rb.setChecked(true); 543856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 5449c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek mTimeCondition = ZenModeConfig.toTimeCondition(mContext, 5459c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[favoriteIndex], ActivityManager.getCurrentUser()); 546856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = favoriteIndex; 547530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(mTimeCondition, mZenConditions.getChildAt(COUNTDOWN_CONDITION_INDEX)); 548530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock getConditionTagAt(COUNTDOWN_CONDITION_INDEX).rb.setChecked(true); 549856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5508600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5518600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 5524db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock private void handleExitConditionChanged(Condition exitCondition) { 5534db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(exitCondition); 5544db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "handleExitConditionChanged " + mExitCondition); 555530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 556856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 557856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final ConditionTag tag = getConditionTagAt(i); 558530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (tag != null) { 559530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (sameConditionId(tag.condition, mExitCondition)) { 560530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock bind(exitCondition, mZenConditions.getChildAt(i)); 561530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 562530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 563856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5648600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 5658600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 566530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean isCountdown(Condition c) { 567530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return c != null && ZenModeConfig.isValidCountdownConditionId(c.id); 568530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 569530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 5700b688503754737a20165f0cff1833e714a5c4164John Spurlock private boolean isForever(Condition c) { 5710b688503754737a20165f0cff1833e714a5c4164John Spurlock return c != null && mForeverId.equals(c.id); 5720b688503754737a20165f0cff1833e714a5c4164John Spurlock } 5730b688503754737a20165f0cff1833e714a5c4164John Spurlock 574530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void bind(final Condition condition, final View row) { 5750b688503754737a20165f0cff1833e714a5c4164John Spurlock if (condition == null) throw new IllegalArgumentException("condition must not be null"); 5760b688503754737a20165f0cff1833e714a5c4164John Spurlock final boolean enabled = condition.state == Condition.STATE_TRUE; 577856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final ConditionTag tag = 578856edebad73560e9b1cce021a7de9a0470d07176John Spurlock row.getTag() != null ? (ConditionTag) row.getTag() : new ConditionTag(); 579856edebad73560e9b1cce021a7de9a0470d07176John Spurlock row.setTag(tag); 580530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean first = tag.rb == null; 581856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (tag.rb == null) { 582856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb = (RadioButton) row.findViewById(android.R.id.checkbox); 583856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 5844db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock tag.condition = condition; 585530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final Uri conditionId = getConditionId(tag.condition); 586530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "bind i=" + mZenConditions.indexOfChild(row) + " first=" + first 587530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " condition=" + conditionId); 588856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setEnabled(enabled); 589530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean checked = (mSessionExitCondition != null 590530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock || mAttachedZen != Global.ZEN_MODE_OFF) 591530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock && (sameConditionId(mSessionExitCondition, tag.condition) 592530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock || isCountdown(mSessionExitCondition) && isCountdown(tag.condition)); 593530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (checked != tag.rb.isChecked()) { 594530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "bind checked=" + checked + " condition=" + conditionId); 595530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock tag.rb.setChecked(checked); 59650806fc4ceff4bb093a18bdecb506163e68b9cbbJohn Spurlock } 597856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 5988600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 5998600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 600ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mExpanded && isChecked) { 601530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "onCheckedChanged " + conditionId); 602530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final int N = getVisibleConditions(); 603856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 604530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final ConditionTag childTag = getConditionTagAt(i); 605530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (childTag == null || childTag == tag) continue; 606856edebad73560e9b1cce021a7de9a0470d07176John Spurlock childTag.rb.setChecked(false); 6078600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6084db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock select(tag.condition); 609a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceConditionSelection(tag); 6108600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6118600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6128600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 613a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 614c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.lines == null) { 615c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines = row.findViewById(android.R.id.content); 616a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 617c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.line1 == null) { 618c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1 = (TextView) row.findViewById(android.R.id.text1); 619c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } 620c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (tag.line2 == null) { 621c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2 = (TextView) row.findViewById(android.R.id.text2); 622c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock } 6230b688503754737a20165f0cff1833e714a5c4164John Spurlock final String line1 = !TextUtils.isEmpty(condition.line1) ? condition.line1 6240b688503754737a20165f0cff1833e714a5c4164John Spurlock : condition.summary; 6250b688503754737a20165f0cff1833e714a5c4164John Spurlock final String line2 = condition.line2; 626c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1.setText(line1); 627c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock if (TextUtils.isEmpty(line2)) { 628c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setVisibility(GONE); 6298600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 630c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setVisibility(VISIBLE); 631c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line2.setText(line2); 6328600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 633c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setEnabled(enabled); 634c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setAlpha(enabled ? 1 : .4f); 635a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 6368600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock final ImageView button1 = (ImageView) row.findViewById(android.R.id.button1); 6378600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock button1.setOnClickListener(new OnClickListener() { 6388600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6398600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 640856edebad73560e9b1cce021a7de9a0470d07176John Spurlock onClickTimeButton(row, tag, false /*down*/); 6418600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6428600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 6438600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 6448600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock final ImageView button2 = (ImageView) row.findViewById(android.R.id.button2); 6458600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock button2.setOnClickListener(new OnClickListener() { 6468600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6478600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 648856edebad73560e9b1cce021a7de9a0470d07176John Spurlock onClickTimeButton(row, tag, true /*up*/); 6498600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6508600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 651c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.lines.setOnClickListener(new OnClickListener() { 6528600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 6538600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onClick(View v) { 654856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setChecked(true); 6558600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 6568600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }); 657856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 658530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); 659856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (time > 0) { 660530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button1.setVisibility(VISIBLE); 661530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button2.setVisibility(VISIBLE); 662856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mBucketIndex > -1) { 663856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setEnabled(mBucketIndex > 0); 664856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setEnabled(mBucketIndex < MINUTE_BUCKETS.length - 1); 665856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 666856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long span = time - System.currentTimeMillis(); 667856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setEnabled(span > MIN_BUCKET_MINUTES * MINUTES_MS); 668c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock final Condition maxCondition = ZenModeConfig.toTimeCondition(mContext, 6699c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MAX_BUCKET_MINUTES, ActivityManager.getCurrentUser()); 670856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setEnabled(!Objects.equals(condition.summary, maxCondition.summary)); 671856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 672856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 673856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button1.setAlpha(button1.isEnabled() ? 1f : .5f); 674856edebad73560e9b1cce021a7de9a0470d07176John Spurlock button2.setAlpha(button2.isEnabled() ? 1f : .5f); 6758600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } else { 676530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button1.setVisibility(GONE); 677530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock button2.setVisibility(GONE); 6788600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 679a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock // wire up interaction callbacks for newly-added condition rows 680530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (first) { 681a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(tag.rb, mInteractionCallback); 682c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock Interaction.register(tag.lines, mInteractionCallback); 683a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(button1, mInteractionCallback); 684a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock Interaction.register(button2, mInteractionCallback); 685a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 686530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock row.setVisibility(VISIBLE); 687a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 688a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 689a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock private void announceConditionSelection(ConditionTag tag) { 690a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock final int zen = getSelectedZen(Global.ZEN_MODE_OFF); 691a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock String modeText; 692a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock switch(zen) { 693a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 694a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock modeText = mContext.getString(R.string.zen_important_interruptions); 695a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock break; 696a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock case Global.ZEN_MODE_NO_INTERRUPTIONS: 697a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock modeText = mContext.getString(R.string.zen_no_interruptions); 698a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock break; 699a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock default: 700a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock return; 701a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 702a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceForAccessibility(mContext.getString(R.string.zen_mode_and_condition, modeText, 703c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock tag.line1.getText())); 704856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 705856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 706856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private void onClickTimeButton(View row, ConditionTag tag, boolean up) { 707856edebad73560e9b1cce021a7de9a0470d07176John Spurlock Condition newCondition = null; 708856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final int N = MINUTE_BUCKETS.length; 709856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mBucketIndex == -1) { 710856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // not on a known index, search for the next or prev bucket by time 7114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final Uri conditionId = getConditionId(tag.condition); 7124db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock final long time = ZenModeConfig.tryParseCountdownConditionId(conditionId); 713856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long now = System.currentTimeMillis(); 714856edebad73560e9b1cce021a7de9a0470d07176John Spurlock for (int i = 0; i < N; i++) { 715856edebad73560e9b1cce021a7de9a0470d07176John Spurlock int j = up ? i : N - 1 - i; 716856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final int bucketMinutes = MINUTE_BUCKETS[j]; 717856edebad73560e9b1cce021a7de9a0470d07176John Spurlock final long bucketTime = now + bucketMinutes * MINUTES_MS; 718856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (up && bucketTime > time || !up && bucketTime < time) { 719856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = j; 720c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7219c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek bucketTime, bucketMinutes, now, ActivityManager.getCurrentUser()); 722856edebad73560e9b1cce021a7de9a0470d07176John Spurlock break; 723856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 724856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 725856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (newCondition == null) { 726856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = DEFAULT_BUCKET_INDEX; 727c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7289c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 729856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 730856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else { 731856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // on a known index, simply increment or decrement 732856edebad73560e9b1cce021a7de9a0470d07176John Spurlock mBucketIndex = Math.max(0, Math.min(N - 1, mBucketIndex + (up ? 1 : -1))); 733c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock newCondition = ZenModeConfig.toTimeCondition(mContext, 7349c4a707912da2c954b2d3d1311b8a691ded8aa16Selim Cinek MINUTE_BUCKETS[mBucketIndex], ActivityManager.getCurrentUser()); 735856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7364db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mTimeCondition = newCondition; 7374db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock bind(mTimeCondition, row); 738856edebad73560e9b1cce021a7de9a0470d07176John Spurlock tag.rb.setChecked(true); 7394db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock select(mTimeCondition); 740a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock announceConditionSelection(tag); 741856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 742856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 743530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void select(final Condition condition) { 7444db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock if (DEBUG) Log.d(mTag, "select " + condition); 7450b688503754737a20165f0cff1833e714a5c4164John Spurlock final boolean isForever = isForever(condition); 746856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (mController != null) { 747530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 748530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 749530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 7500b688503754737a20165f0cff1833e714a5c4164John Spurlock mController.setExitCondition(isForever ? null : condition); 751530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 752530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 753856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7544db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock setExitCondition(condition); 7550b688503754737a20165f0cff1833e714a5c4164John Spurlock if (isForever) { 7568f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.setMinuteIndex(-1); 757530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else if (isCountdown(condition) && mBucketIndex != -1) { 7588f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mPrefs.setMinuteIndex(mBucketIndex); 7598600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 760530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock setSessionExitCondition(copy(condition)); 7618600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7628600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7638600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void fireMoreSettings() { 7648600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mCallback != null) { 7658600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback.onMoreSettings(); 7668600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7678600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7688600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7698600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private void fireInteraction() { 7708600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (mCallback != null) { 7718600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mCallback.onInteraction(); 7728600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7738600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 7748600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 775ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private void fireExpanded() { 776ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (mCallback != null) { 777ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mCallback.onExpanded(mExpanded); 778ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 779ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 780ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 7818600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() { 7828600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 783ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock public void onZenChanged(int zen) { 784ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock mHandler.obtainMessage(H.UPDATE_ZEN, zen, 0).sendToTarget(); 785ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 786ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 7878600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void onConditionsChanged(Condition[] conditions) { 7888600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock mHandler.obtainMessage(H.UPDATE_CONDITIONS, conditions).sendToTarget(); 7898600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 790856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 791856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 7924db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock public void onExitConditionChanged(Condition exitCondition) { 7934db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock mHandler.obtainMessage(H.EXIT_CONDITION_CHANGED, exitCondition).sendToTarget(); 794856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 7958600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock }; 7968600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 7978600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private final class H extends Handler { 7988600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private static final int UPDATE_CONDITIONS = 1; 799856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final int EXIT_CONDITION_CHANGED = 2; 800ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private static final int UPDATE_ZEN = 3; 8018600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 8028600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock private H() { 8038600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock super(Looper.getMainLooper()); 8048600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8058600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 8068600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock @Override 8078600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public void handleMessage(Message msg) { 8088600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock if (msg.what == UPDATE_CONDITIONS) { 80989f060a1e0fa48c497855d061b5c9ec44bb576c8John Spurlock handleUpdateConditions((Condition[]) msg.obj); 810856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } else if (msg.what == EXIT_CONDITION_CHANGED) { 8114db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock handleExitConditionChanged((Condition) msg.obj); 812ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } else if (msg.what == UPDATE_ZEN) { 813ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock handleUpdateZen(msg.arg1); 8148600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8158600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8168600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 8178600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock 8188600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock public interface Callback { 8198600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock void onMoreSettings(); 8208600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock void onInteraction(); 821ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock void onExpanded(boolean expanded); 8228600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock } 823856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 824856edebad73560e9b1cce021a7de9a0470d07176John Spurlock // used as the view tag on condition rows 825856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static class ConditionTag { 826856edebad73560e9b1cce021a7de9a0470d07176John Spurlock RadioButton rb; 827c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock View lines; 828c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock TextView line1; 829c90e6fe863bb5ac84612bd777d3b352f69265950John Spurlock TextView line2; 8304db0d98b42a723f2e16c6595e85e866fd26c6d98John Spurlock Condition condition; 831856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 832856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8338f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final class Prefs implements OnSharedPreferenceChangeListener { 834856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private static final String KEY_MINUTE_INDEX = "minuteIndex"; 8358f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private static final String KEY_NONE_SELECTED = "noneSelected"; 8368f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8378f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private final int mNoneDangerousThreshold; 838856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 839856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private int mMinuteIndex; 8408f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int mNoneSelected; 841856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8428f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private Prefs() { 8438f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneDangerousThreshold = mContext.getResources() 8448f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock .getInteger(R.integer.zen_mode_alarm_warning_threshold); 845856edebad73560e9b1cce021a7de9a0470d07176John Spurlock prefs().registerOnSharedPreferenceChangeListener(this); 846856edebad73560e9b1cce021a7de9a0470d07176John Spurlock updateMinuteIndex(); 8478f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock updateNoneSelected(); 8488f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8498f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8508f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock public boolean isNoneDangerous() { 8518f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return mNoneSelected < mNoneDangerousThreshold; 8528f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8538f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8548f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock public void trackNoneSelected() { 8558f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneSelected = clampNoneSelected(mNoneSelected + 1); 8568f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "Setting none selected: " + mNoneSelected + " threshold=" 8578f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock + mNoneDangerousThreshold); 8588f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock prefs().edit().putInt(KEY_NONE_SELECTED, mNoneSelected).apply(); 859856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 860856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 861856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public int getMinuteIndex() { 862856edebad73560e9b1cce021a7de9a0470d07176John Spurlock return mMinuteIndex; 863856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 864856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 865856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public void setMinuteIndex(int minuteIndex) { 8668f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock minuteIndex = clampIndex(minuteIndex); 867856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (minuteIndex == mMinuteIndex) return; 8688f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mMinuteIndex = clampIndex(minuteIndex); 869856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Setting favorite minute index: " + mMinuteIndex); 870856edebad73560e9b1cce021a7de9a0470d07176John Spurlock prefs().edit().putInt(KEY_MINUTE_INDEX, mMinuteIndex).apply(); 871856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 872856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 873856edebad73560e9b1cce021a7de9a0470d07176John Spurlock @Override 874856edebad73560e9b1cce021a7de9a0470d07176John Spurlock public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 875856edebad73560e9b1cce021a7de9a0470d07176John Spurlock updateMinuteIndex(); 8768f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock updateNoneSelected(); 877856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 878856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 879856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private SharedPreferences prefs() { 8807bbb9f64103c297d1b9738339f3bfeed0f41030eJohn Spurlock return mContext.getSharedPreferences(mContext.getPackageName(), 0); 881856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 882856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 883856edebad73560e9b1cce021a7de9a0470d07176John Spurlock private void updateMinuteIndex() { 8848f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mMinuteIndex = clampIndex(prefs().getInt(KEY_MINUTE_INDEX, DEFAULT_BUCKET_INDEX)); 885856edebad73560e9b1cce021a7de9a0470d07176John Spurlock if (DEBUG) Log.d(mTag, "Favorite minute index: " + mMinuteIndex); 886856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 887856edebad73560e9b1cce021a7de9a0470d07176John Spurlock 8888f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int clampIndex(int index) { 8898f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return MathUtils.constrain(index, -1, MINUTE_BUCKETS.length - 1); 8908f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8918f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8928f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private void updateNoneSelected() { 8938f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock mNoneSelected = clampNoneSelected(prefs().getInt(KEY_NONE_SELECTED, 0)); 8948f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock if (DEBUG) Log.d(mTag, "None selected: " + mNoneSelected); 8958f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock } 8968f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock 8978f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock private int clampNoneSelected(int noneSelected) { 8988f8ecd620c796834a1048ec98f0e8072f5ad926cJohn Spurlock return MathUtils.constrain(noneSelected, 0, Integer.MAX_VALUE); 899856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 900856edebad73560e9b1cce021a7de9a0470d07176John Spurlock } 901ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock 902ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock private final SegmentedButtons.Callback mZenButtonsCallback = new SegmentedButtons.Callback() { 903ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock @Override 904530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void onSelected(final Object value) { 905ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (value != null && mZenButtons.isShown()) { 906ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock if (DEBUG) Log.d(mTag, "mZenButtonsCallback selected=" + value); 907530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock AsyncTask.execute(new Runnable() { 908530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 909530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 910530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mController.setZen((Integer) value); 911530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 912530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock }); 913ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 914ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock } 915a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 916a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock @Override 917a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock public void onInteraction() { 918a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock fireInteraction(); 919a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 920a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock }; 921a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock 922a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock private final Interaction.Callback mInteractionCallback = new Interaction.Callback() { 923a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock @Override 924a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock public void onInteraction() { 925a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock fireInteraction(); 926a0457c2e92fc85cfba9a52d502d5038ecc7920f4John Spurlock } 927ae641c9ccd3f81214cee54a5f13804f1765187adJohn Spurlock }; 928530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 929530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final class TransitionHelper implements TransitionListener, Runnable { 930530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private final ArraySet<View> mTransitioningViews = new ArraySet<View>(); 931530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 932530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mTransitioning; 933530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mPendingUpdateConditions; 934530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private boolean mPendingUpdateWidgets; 935530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 936530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void clear() { 937530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.clear(); 938530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = mPendingUpdateWidgets = false; 939530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 940530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 941530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 942530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.println(" TransitionHelper state:"); 943530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mPendingUpdateConditions="); pw.println(mPendingUpdateConditions); 944530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mPendingUpdateWidgets="); pw.println(mPendingUpdateWidgets); 945530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mTransitioning="); pw.println(mTransitioning); 946530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock pw.print(" mTransitioningViews="); pw.println(mTransitioningViews); 947530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 948530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 949530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void pendingUpdateConditions() { 950530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = true; 951530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 952530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 953530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void pendingUpdateWidgets() { 954530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateWidgets = true; 955530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 956530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 957530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public boolean isTransitioning() { 958530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock return !mTransitioningViews.isEmpty(); 959530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 960530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 961530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 962530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void startTransition(LayoutTransition transition, 963530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock ViewGroup container, View view, int transitionType) { 964530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.add(view); 965530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateTransitioning(); 966530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 967530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 968530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 969530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void endTransition(LayoutTransition transition, 970530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock ViewGroup container, View view, int transitionType) { 971530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioningViews.remove(view); 972530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateTransitioning(); 973530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 974530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 975530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock @Override 976530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock public void run() { 977530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "TransitionHelper run" 978530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " mPendingUpdateWidgets=" + mPendingUpdateWidgets 979530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock + " mPendingUpdateConditions=" + mPendingUpdateConditions); 980530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateWidgets) { 981530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock updateWidgets(); 982530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 983530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateConditions) { 984530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock handleUpdateConditions(); 985530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 986530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateWidgets = mPendingUpdateConditions = false; 987530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 988530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock 989530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock private void updateTransitioning() { 990530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock final boolean transitioning = isTransitioning(); 991530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mTransitioning == transitioning) return; 992530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mTransitioning = transitioning; 993530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (DEBUG) Log.d(mTag, "TransitionHelper mTransitioning=" + mTransitioning); 994530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (!mTransitioning) { 995530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock if (mPendingUpdateConditions || mPendingUpdateWidgets) { 996530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mHandler.post(this); 997530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } else { 998530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock mPendingUpdateConditions = mPendingUpdateWidgets = false; 999530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 1000530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 1001530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 1002530052a2fe3b6a6a4246ce28ab0ced647fe7f470John Spurlock } 10038600534df66c2ff5846ed230b50c56229322d48aJohn Spurlock} 1004