1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.systemui.statusbar.notification.row; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorListenerAdapter; 21 import android.animation.AnimatorSet; 22 import android.animation.ObjectAnimator; 23 import android.content.Context; 24 import android.content.res.Resources; 25 import android.graphics.Typeface; 26 import android.metrics.LogMaker; 27 import android.os.Bundle; 28 import android.provider.Settings; 29 import android.service.notification.SnoozeCriterion; 30 import android.service.notification.StatusBarNotification; 31 import android.text.SpannableString; 32 import android.text.style.StyleSpan; 33 import android.util.AttributeSet; 34 import android.util.KeyValueListParser; 35 import android.util.Log; 36 import android.view.LayoutInflater; 37 import android.view.View; 38 import android.view.ViewGroup; 39 import android.view.accessibility.AccessibilityEvent; 40 import android.view.accessibility.AccessibilityNodeInfo; 41 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; 42 import android.widget.ImageView; 43 import android.widget.LinearLayout; 44 import android.widget.TextView; 45 46 import com.android.internal.annotations.VisibleForTesting; 47 import com.android.internal.logging.MetricsLogger; 48 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 49 import com.android.systemui.Interpolators; 50 import com.android.systemui.R; 51 import com.android.systemui.plugins.statusbar.NotificationSwipeActionHelper; 52 import com.android.systemui.plugins.statusbar.NotificationSwipeActionHelper.SnoozeOption; 53 54 import java.util.ArrayList; 55 import java.util.List; 56 import java.util.concurrent.TimeUnit; 57 58 public class NotificationSnooze extends LinearLayout 59 implements NotificationGuts.GutsContent, View.OnClickListener { 60 61 private static final String TAG = "NotificationSnooze"; 62 /** 63 * If this changes more number increases, more assistant action resId's should be defined for 64 * accessibility purposes, see {@link #setSnoozeOptions(List)} 65 */ 66 private static final int MAX_ASSISTANT_SUGGESTIONS = 1; 67 private static final String KEY_DEFAULT_SNOOZE = "default"; 68 private static final String KEY_OPTIONS = "options_array"; 69 private static final LogMaker OPTIONS_OPEN_LOG = 70 new LogMaker(MetricsEvent.NOTIFICATION_SNOOZE_OPTIONS) 71 .setType(MetricsEvent.TYPE_OPEN); 72 private static final LogMaker OPTIONS_CLOSE_LOG = 73 new LogMaker(MetricsEvent.NOTIFICATION_SNOOZE_OPTIONS) 74 .setType(MetricsEvent.TYPE_CLOSE); 75 private static final LogMaker UNDO_LOG = 76 new LogMaker(MetricsEvent.NOTIFICATION_UNDO_SNOOZE) 77 .setType(MetricsEvent.TYPE_ACTION); 78 private NotificationGuts mGutsContainer; 79 private NotificationSwipeActionHelper mSnoozeListener; 80 private StatusBarNotification mSbn; 81 82 private TextView mSelectedOptionText; 83 private TextView mUndoButton; 84 private ImageView mExpandButton; 85 private View mDivider; 86 private ViewGroup mSnoozeOptionContainer; 87 private List<SnoozeOption> mSnoozeOptions; 88 private int mCollapsedHeight; 89 private SnoozeOption mDefaultOption; 90 private SnoozeOption mSelectedOption; 91 private boolean mSnoozing; 92 private boolean mExpanded; 93 private AnimatorSet mExpandAnimation; 94 private KeyValueListParser mParser; 95 96 private final static int[] sAccessibilityActions = { 97 R.id.action_snooze_shorter, 98 R.id.action_snooze_short, 99 R.id.action_snooze_long, 100 R.id.action_snooze_longer, 101 }; 102 103 private MetricsLogger mMetricsLogger = new MetricsLogger(); 104 NotificationSnooze(Context context, AttributeSet attrs)105 public NotificationSnooze(Context context, AttributeSet attrs) { 106 super(context, attrs); 107 mParser = new KeyValueListParser(','); 108 } 109 110 @VisibleForTesting getDefaultOption()111 SnoozeOption getDefaultOption() 112 { 113 return mDefaultOption; 114 } 115 116 @VisibleForTesting setKeyValueListParser(KeyValueListParser parser)117 void setKeyValueListParser(KeyValueListParser parser) { 118 mParser = parser; 119 } 120 121 @Override onFinishInflate()122 protected void onFinishInflate() { 123 super.onFinishInflate(); 124 mCollapsedHeight = getResources().getDimensionPixelSize(R.dimen.snooze_snackbar_min_height); 125 findViewById(R.id.notification_snooze).setOnClickListener(this); 126 mSelectedOptionText = (TextView) findViewById(R.id.snooze_option_default); 127 mUndoButton = (TextView) findViewById(R.id.undo); 128 mUndoButton.setOnClickListener(this); 129 mExpandButton = (ImageView) findViewById(R.id.expand_button); 130 mDivider = findViewById(R.id.divider); 131 mDivider.setAlpha(0f); 132 mSnoozeOptionContainer = (ViewGroup) findViewById(R.id.snooze_options); 133 mSnoozeOptionContainer.setVisibility(View.INVISIBLE); 134 mSnoozeOptionContainer.setAlpha(0f); 135 136 // Create the different options based on list 137 mSnoozeOptions = getDefaultSnoozeOptions(); 138 createOptionViews(); 139 140 setSelected(mDefaultOption, false); 141 } 142 143 @Override onAttachedToWindow()144 protected void onAttachedToWindow() { 145 super.onAttachedToWindow(); 146 logOptionSelection(MetricsEvent.NOTIFICATION_SNOOZE_CLICKED, mDefaultOption); 147 } 148 149 @Override onInitializeAccessibilityEvent(AccessibilityEvent event)150 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 151 super.onInitializeAccessibilityEvent(event); 152 if (mGutsContainer != null && mGutsContainer.isExposed()) { 153 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { 154 event.getText().add(mSelectedOptionText.getText()); 155 } 156 } 157 } 158 159 @Override onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)160 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 161 super.onInitializeAccessibilityNodeInfo(info); 162 info.addAction(new AccessibilityAction(R.id.action_snooze_undo, 163 getResources().getString(R.string.snooze_undo))); 164 int count = mSnoozeOptions.size(); 165 for (int i = 0; i < count; i++) { 166 AccessibilityAction action = mSnoozeOptions.get(i).getAccessibilityAction(); 167 if (action != null) { 168 info.addAction(action); 169 } 170 } 171 } 172 173 @Override performAccessibilityActionInternal(int action, Bundle arguments)174 public boolean performAccessibilityActionInternal(int action, Bundle arguments) { 175 if (super.performAccessibilityActionInternal(action, arguments)) { 176 return true; 177 } 178 if (action == R.id.action_snooze_undo) { 179 undoSnooze(mUndoButton); 180 return true; 181 } 182 for (int i = 0; i < mSnoozeOptions.size(); i++) { 183 SnoozeOption so = mSnoozeOptions.get(i); 184 if (so.getAccessibilityAction() != null 185 && so.getAccessibilityAction().getId() == action) { 186 setSelected(so, true); 187 return true; 188 } 189 } 190 return false; 191 } 192 setSnoozeOptions(final List<SnoozeCriterion> snoozeList)193 public void setSnoozeOptions(final List<SnoozeCriterion> snoozeList) { 194 if (snoozeList == null) { 195 return; 196 } 197 mSnoozeOptions.clear(); 198 mSnoozeOptions = getDefaultSnoozeOptions(); 199 final int count = Math.min(MAX_ASSISTANT_SUGGESTIONS, snoozeList.size()); 200 for (int i = 0; i < count; i++) { 201 SnoozeCriterion sc = snoozeList.get(i); 202 AccessibilityAction action = new AccessibilityAction( 203 R.id.action_snooze_assistant_suggestion_1, sc.getExplanation()); 204 mSnoozeOptions.add(new NotificationSnoozeOption(sc, 0, sc.getExplanation(), 205 sc.getConfirmation(), action)); 206 } 207 createOptionViews(); 208 } 209 isExpanded()210 public boolean isExpanded() { 211 return mExpanded; 212 } 213 setSnoozeListener(NotificationSwipeActionHelper listener)214 public void setSnoozeListener(NotificationSwipeActionHelper listener) { 215 mSnoozeListener = listener; 216 } 217 setStatusBarNotification(StatusBarNotification sbn)218 public void setStatusBarNotification(StatusBarNotification sbn) { 219 mSbn = sbn; 220 } 221 222 @VisibleForTesting getDefaultSnoozeOptions()223 ArrayList<SnoozeOption> getDefaultSnoozeOptions() { 224 final Resources resources = getContext().getResources(); 225 ArrayList<SnoozeOption> options = new ArrayList<>(); 226 try { 227 final String config = Settings.Global.getString(getContext().getContentResolver(), 228 Settings.Global.NOTIFICATION_SNOOZE_OPTIONS); 229 mParser.setString(config); 230 } catch (IllegalArgumentException e) { 231 Log.e(TAG, "Bad snooze constants"); 232 } 233 234 final int defaultSnooze = mParser.getInt(KEY_DEFAULT_SNOOZE, 235 resources.getInteger(R.integer.config_notification_snooze_time_default)); 236 final int[] snoozeTimes = mParser.getIntArray(KEY_OPTIONS, 237 resources.getIntArray(R.array.config_notification_snooze_times)); 238 239 for (int i = 0; i < snoozeTimes.length && i < sAccessibilityActions.length; i++) { 240 int snoozeTime = snoozeTimes[i]; 241 SnoozeOption option = createOption(snoozeTime, sAccessibilityActions[i]); 242 if (i == 0 || snoozeTime == defaultSnooze) { 243 mDefaultOption = option; 244 } 245 options.add(option); 246 } 247 return options; 248 } 249 createOption(int minutes, int accessibilityActionId)250 private SnoozeOption createOption(int minutes, int accessibilityActionId) { 251 Resources res = getResources(); 252 boolean showInHours = minutes >= 60; 253 int pluralResId = showInHours 254 ? R.plurals.snoozeHourOptions 255 : R.plurals.snoozeMinuteOptions; 256 int count = showInHours ? (minutes / 60) : minutes; 257 String description = res.getQuantityString(pluralResId, count, count); 258 String resultText = String.format(res.getString(R.string.snoozed_for_time), description); 259 AccessibilityAction action = new AccessibilityAction(accessibilityActionId, description); 260 final int index = resultText.indexOf(description); 261 if (index == -1) { 262 return new NotificationSnoozeOption(null, minutes, description, resultText, action); 263 } 264 SpannableString string = new SpannableString(resultText); 265 string.setSpan(new StyleSpan(Typeface.BOLD), 266 index, index + description.length(), 0 /* flags */); 267 return new NotificationSnoozeOption(null, minutes, description, string, 268 action); 269 } 270 createOptionViews()271 private void createOptionViews() { 272 mSnoozeOptionContainer.removeAllViews(); 273 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( 274 Context.LAYOUT_INFLATER_SERVICE); 275 for (int i = 0; i < mSnoozeOptions.size(); i++) { 276 SnoozeOption option = mSnoozeOptions.get(i); 277 TextView tv = (TextView) inflater.inflate(R.layout.notification_snooze_option, 278 mSnoozeOptionContainer, false); 279 mSnoozeOptionContainer.addView(tv); 280 tv.setText(option.getDescription()); 281 tv.setTag(option); 282 tv.setOnClickListener(this); 283 } 284 } 285 hideSelectedOption()286 private void hideSelectedOption() { 287 final int childCount = mSnoozeOptionContainer.getChildCount(); 288 for (int i = 0; i < childCount; i++) { 289 final View child = mSnoozeOptionContainer.getChildAt(i); 290 child.setVisibility(child.getTag() == mSelectedOption ? View.GONE : View.VISIBLE); 291 } 292 } 293 showSnoozeOptions(boolean show)294 private void showSnoozeOptions(boolean show) { 295 int drawableId = show ? com.android.internal.R.drawable.ic_collapse_notification 296 : com.android.internal.R.drawable.ic_expand_notification; 297 mExpandButton.setImageResource(drawableId); 298 if (mExpanded != show) { 299 mExpanded = show; 300 animateSnoozeOptions(show); 301 if (mGutsContainer != null) { 302 mGutsContainer.onHeightChanged(); 303 } 304 } 305 } 306 animateSnoozeOptions(boolean show)307 private void animateSnoozeOptions(boolean show) { 308 if (mExpandAnimation != null) { 309 mExpandAnimation.cancel(); 310 } 311 ObjectAnimator dividerAnim = ObjectAnimator.ofFloat(mDivider, View.ALPHA, 312 mDivider.getAlpha(), show ? 1f : 0f); 313 ObjectAnimator optionAnim = ObjectAnimator.ofFloat(mSnoozeOptionContainer, View.ALPHA, 314 mSnoozeOptionContainer.getAlpha(), show ? 1f : 0f); 315 mSnoozeOptionContainer.setVisibility(View.VISIBLE); 316 mExpandAnimation = new AnimatorSet(); 317 mExpandAnimation.playTogether(dividerAnim, optionAnim); 318 mExpandAnimation.setDuration(150); 319 mExpandAnimation.setInterpolator(show ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT); 320 mExpandAnimation.addListener(new AnimatorListenerAdapter() { 321 boolean cancelled = false; 322 323 @Override 324 public void onAnimationCancel(Animator animation) { 325 cancelled = true; 326 } 327 328 @Override 329 public void onAnimationEnd(Animator animation) { 330 if (!show && !cancelled) { 331 mSnoozeOptionContainer.setVisibility(View.INVISIBLE); 332 mSnoozeOptionContainer.setAlpha(0f); 333 } 334 } 335 }); 336 mExpandAnimation.start(); 337 } 338 setSelected(SnoozeOption option, boolean userAction)339 private void setSelected(SnoozeOption option, boolean userAction) { 340 mSelectedOption = option; 341 mSelectedOptionText.setText(option.getConfirmation()); 342 showSnoozeOptions(false); 343 hideSelectedOption(); 344 sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 345 if (userAction) { 346 logOptionSelection(MetricsEvent.NOTIFICATION_SELECT_SNOOZE, option); 347 } 348 } 349 logOptionSelection(int category, SnoozeOption option)350 private void logOptionSelection(int category, SnoozeOption option) { 351 int index = mSnoozeOptions.indexOf(option); 352 long duration = TimeUnit.MINUTES.toMillis(option.getMinutesToSnoozeFor()); 353 mMetricsLogger.write(new LogMaker(category) 354 .setType(MetricsEvent.TYPE_ACTION) 355 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_INDEX, index) 356 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, duration)); 357 } 358 359 @Override onClick(View v)360 public void onClick(View v) { 361 if (mGutsContainer != null) { 362 mGutsContainer.resetFalsingCheck(); 363 } 364 final int id = v.getId(); 365 final SnoozeOption tag = (SnoozeOption) v.getTag(); 366 if (tag != null) { 367 setSelected(tag, true); 368 } else if (id == R.id.notification_snooze) { 369 // Toggle snooze options 370 showSnoozeOptions(!mExpanded); 371 mMetricsLogger.write(!mExpanded ? OPTIONS_OPEN_LOG : OPTIONS_CLOSE_LOG); 372 } else { 373 // Undo snooze was selected 374 undoSnooze(v); 375 mMetricsLogger.write(UNDO_LOG); 376 } 377 } 378 undoSnooze(View v)379 private void undoSnooze(View v) { 380 mSelectedOption = null; 381 int[] parentLoc = new int[2]; 382 int[] targetLoc = new int[2]; 383 mGutsContainer.getLocationOnScreen(parentLoc); 384 v.getLocationOnScreen(targetLoc); 385 final int centerX = v.getWidth() / 2; 386 final int centerY = v.getHeight() / 2; 387 final int x = targetLoc[0] - parentLoc[0] + centerX; 388 final int y = targetLoc[1] - parentLoc[1] + centerY; 389 showSnoozeOptions(false); 390 mGutsContainer.closeControls(x, y, false /* save */, false /* force */); 391 } 392 393 @Override getActualHeight()394 public int getActualHeight() { 395 return mExpanded ? getHeight() : mCollapsedHeight; 396 } 397 398 @Override willBeRemoved()399 public boolean willBeRemoved() { 400 return mSnoozing; 401 } 402 403 @Override getContentView()404 public View getContentView() { 405 // Reset the view before use 406 setSelected(mDefaultOption, false); 407 return this; 408 } 409 410 @Override setGutsParent(NotificationGuts guts)411 public void setGutsParent(NotificationGuts guts) { 412 mGutsContainer = guts; 413 } 414 415 @Override handleCloseControls(boolean save, boolean force)416 public boolean handleCloseControls(boolean save, boolean force) { 417 if (mExpanded && !force) { 418 // Collapse expanded state on outside touch 419 showSnoozeOptions(false); 420 return true; 421 } else if (mSnoozeListener != null && mSelectedOption != null) { 422 // Snooze option selected so commit it 423 mSnoozing = true; 424 mSnoozeListener.snooze(mSbn, mSelectedOption); 425 return true; 426 } else { 427 // The view should actually be closed 428 setSelected(mSnoozeOptions.get(0), false); 429 return false; // Return false here so that guts handles closing the view 430 } 431 } 432 433 @Override isLeavebehind()434 public boolean isLeavebehind() { 435 return true; 436 } 437 438 @Override shouldBeSaved()439 public boolean shouldBeSaved() { 440 return true; 441 } 442 443 public class NotificationSnoozeOption implements SnoozeOption { 444 private SnoozeCriterion mCriterion; 445 private int mMinutesToSnoozeFor; 446 private CharSequence mDescription; 447 private CharSequence mConfirmation; 448 private AccessibilityAction mAction; 449 NotificationSnoozeOption(SnoozeCriterion sc, int minToSnoozeFor, CharSequence description, CharSequence confirmation, AccessibilityAction action)450 public NotificationSnoozeOption(SnoozeCriterion sc, int minToSnoozeFor, 451 CharSequence description, 452 CharSequence confirmation, AccessibilityAction action) { 453 mCriterion = sc; 454 mMinutesToSnoozeFor = minToSnoozeFor; 455 mDescription = description; 456 mConfirmation = confirmation; 457 mAction = action; 458 } 459 460 @Override getSnoozeCriterion()461 public SnoozeCriterion getSnoozeCriterion() { 462 return mCriterion; 463 } 464 465 @Override getDescription()466 public CharSequence getDescription() { 467 return mDescription; 468 } 469 470 @Override getConfirmation()471 public CharSequence getConfirmation() { 472 return mConfirmation; 473 } 474 475 @Override getMinutesToSnoozeFor()476 public int getMinutesToSnoozeFor() { 477 return mMinutesToSnoozeFor; 478 } 479 480 @Override getAccessibilityAction()481 public AccessibilityAction getAccessibilityAction() { 482 return mAction; 483 } 484 485 } 486 } 487