• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.policy;
18 
19 import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.database.ContentObserver;
26 import android.provider.Settings;
27 import android.util.ArrayMap;
28 import android.util.Log;
29 import android.view.accessibility.AccessibilityManager;
30 
31 import com.android.internal.logging.MetricsLogger;
32 import com.android.systemui.Dependency;
33 import com.android.systemui.R;
34 import com.android.systemui.statusbar.AlertingNotificationManager;
35 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
36 import com.android.systemui.statusbar.notification.row.NotificationContentInflater.InflationFlag;
37 
38 import java.io.FileDescriptor;
39 import java.io.PrintWriter;
40 import java.util.HashSet;
41 
42 /**
43  * A manager which handles heads up notifications which is a special mode where
44  * they simply peek from the top of the screen.
45  */
46 public abstract class HeadsUpManager extends AlertingNotificationManager {
47     private static final String TAG = "HeadsUpManager";
48     private static final String SETTING_HEADS_UP_SNOOZE_LENGTH_MS = "heads_up_snooze_length_ms";
49 
50     protected final HashSet<OnHeadsUpChangedListener> mListeners = new HashSet<>();
51 
52     protected final Context mContext;
53 
54     protected int mTouchAcceptanceDelay;
55     protected int mSnoozeLengthMs;
56     protected boolean mHasPinnedNotification;
57     protected int mUser;
58 
59     private final ArrayMap<String, Long> mSnoozedPackages;
60     private final AccessibilityManagerWrapper mAccessibilityMgr;
61 
HeadsUpManager(@onNull final Context context)62     public HeadsUpManager(@NonNull final Context context) {
63         mContext = context;
64         mAccessibilityMgr = Dependency.get(AccessibilityManagerWrapper.class);
65         Resources resources = context.getResources();
66         mMinimumDisplayTime = resources.getInteger(R.integer.heads_up_notification_minimum_time);
67         mAutoDismissNotificationDecay = resources.getInteger(R.integer.heads_up_notification_decay);
68         mTouchAcceptanceDelay = resources.getInteger(R.integer.touch_acceptance_delay);
69         mSnoozedPackages = new ArrayMap<>();
70         int defaultSnoozeLengthMs =
71                 resources.getInteger(R.integer.heads_up_default_snooze_length_ms);
72 
73         mSnoozeLengthMs = Settings.Global.getInt(context.getContentResolver(),
74                 SETTING_HEADS_UP_SNOOZE_LENGTH_MS, defaultSnoozeLengthMs);
75         ContentObserver settingsObserver = new ContentObserver(mHandler) {
76             @Override
77             public void onChange(boolean selfChange) {
78                 final int packageSnoozeLengthMs = Settings.Global.getInt(
79                         context.getContentResolver(), SETTING_HEADS_UP_SNOOZE_LENGTH_MS, -1);
80                 if (packageSnoozeLengthMs > -1 && packageSnoozeLengthMs != mSnoozeLengthMs) {
81                     mSnoozeLengthMs = packageSnoozeLengthMs;
82                     if (Log.isLoggable(TAG, Log.VERBOSE)) {
83                         Log.v(TAG, "mSnoozeLengthMs = " + mSnoozeLengthMs);
84                     }
85                 }
86             }
87         };
88         context.getContentResolver().registerContentObserver(
89                 Settings.Global.getUriFor(SETTING_HEADS_UP_SNOOZE_LENGTH_MS), false,
90                 settingsObserver);
91     }
92 
93     /**
94      * Adds an OnHeadUpChangedListener to observe events.
95      */
addListener(@onNull OnHeadsUpChangedListener listener)96     public void addListener(@NonNull OnHeadsUpChangedListener listener) {
97         mListeners.add(listener);
98     }
99 
100     /**
101      * Removes the OnHeadUpChangedListener from the observer list.
102      */
removeListener(@onNull OnHeadsUpChangedListener listener)103     public void removeListener(@NonNull OnHeadsUpChangedListener listener) {
104         mListeners.remove(listener);
105     }
106 
updateNotification(@onNull String key, boolean alert)107     public void updateNotification(@NonNull String key, boolean alert) {
108         super.updateNotification(key, alert);
109         AlertEntry alertEntry = getHeadsUpEntry(key);
110         if (alert && alertEntry != null) {
111             setEntryPinned((HeadsUpEntry) alertEntry, shouldHeadsUpBecomePinned(alertEntry.mEntry));
112         }
113     }
114 
shouldHeadsUpBecomePinned(@onNull NotificationEntry entry)115     protected boolean shouldHeadsUpBecomePinned(@NonNull NotificationEntry entry) {
116         return hasFullScreenIntent(entry);
117     }
118 
hasFullScreenIntent(@onNull NotificationEntry entry)119     protected boolean hasFullScreenIntent(@NonNull NotificationEntry entry) {
120         return entry.notification.getNotification().fullScreenIntent != null;
121     }
122 
setEntryPinned( @onNull HeadsUpManager.HeadsUpEntry headsUpEntry, boolean isPinned)123     protected void setEntryPinned(
124             @NonNull HeadsUpManager.HeadsUpEntry headsUpEntry, boolean isPinned) {
125         if (Log.isLoggable(TAG, Log.VERBOSE)) {
126             Log.v(TAG, "setEntryPinned: " + isPinned);
127         }
128         NotificationEntry entry = headsUpEntry.mEntry;
129         if (entry.isRowPinned() != isPinned) {
130             entry.setRowPinned(isPinned);
131             updatePinnedMode();
132             for (OnHeadsUpChangedListener listener : mListeners) {
133                 if (isPinned) {
134                     listener.onHeadsUpPinned(entry);
135                 } else {
136                     listener.onHeadsUpUnPinned(entry);
137                 }
138             }
139         }
140     }
141 
getContentFlag()142     public @InflationFlag int getContentFlag() {
143         return FLAG_CONTENT_VIEW_HEADS_UP;
144     }
145 
146     @Override
onAlertEntryAdded(AlertEntry alertEntry)147     protected void onAlertEntryAdded(AlertEntry alertEntry) {
148         NotificationEntry entry = alertEntry.mEntry;
149         entry.setHeadsUp(true);
150         setEntryPinned((HeadsUpEntry) alertEntry, shouldHeadsUpBecomePinned(entry));
151         for (OnHeadsUpChangedListener listener : mListeners) {
152             listener.onHeadsUpStateChanged(entry, true);
153         }
154     }
155 
156     @Override
onAlertEntryRemoved(AlertEntry alertEntry)157     protected void onAlertEntryRemoved(AlertEntry alertEntry) {
158         NotificationEntry entry = alertEntry.mEntry;
159         entry.setHeadsUp(false);
160         setEntryPinned((HeadsUpEntry) alertEntry, false /* isPinned */);
161         for (OnHeadsUpChangedListener listener : mListeners) {
162             listener.onHeadsUpStateChanged(entry, false);
163         }
164         entry.freeContentViewWhenSafe(FLAG_CONTENT_VIEW_HEADS_UP);
165     }
166 
updatePinnedMode()167     protected void updatePinnedMode() {
168         boolean hasPinnedNotification = hasPinnedNotificationInternal();
169         if (hasPinnedNotification == mHasPinnedNotification) {
170             return;
171         }
172         if (Log.isLoggable(TAG, Log.VERBOSE)) {
173             Log.v(TAG, "Pinned mode changed: " + mHasPinnedNotification + " -> " +
174                        hasPinnedNotification);
175         }
176         mHasPinnedNotification = hasPinnedNotification;
177         if (mHasPinnedNotification) {
178             MetricsLogger.count(mContext, "note_peek", 1);
179         }
180         for (OnHeadsUpChangedListener listener : mListeners) {
181             listener.onHeadsUpPinnedModeChanged(hasPinnedNotification);
182         }
183     }
184 
185     /**
186      * Returns if the given notification is snoozed or not.
187      */
isSnoozed(@onNull String packageName)188     public boolean isSnoozed(@NonNull String packageName) {
189         final String key = snoozeKey(packageName, mUser);
190         Long snoozedUntil = mSnoozedPackages.get(key);
191         if (snoozedUntil != null) {
192             if (snoozedUntil > mClock.currentTimeMillis()) {
193                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
194                     Log.v(TAG, key + " snoozed");
195                 }
196                 return true;
197             }
198             mSnoozedPackages.remove(packageName);
199         }
200         return false;
201     }
202 
203     /**
204      * Snoozes all current Heads Up Notifications.
205      */
snooze()206     public void snooze() {
207         for (String key : mAlertEntries.keySet()) {
208             AlertEntry entry = getHeadsUpEntry(key);
209             String packageName = entry.mEntry.notification.getPackageName();
210             mSnoozedPackages.put(snoozeKey(packageName, mUser),
211                     mClock.currentTimeMillis() + mSnoozeLengthMs);
212         }
213     }
214 
215     @NonNull
snoozeKey(@onNull String packageName, int user)216     private static String snoozeKey(@NonNull String packageName, int user) {
217         return user + "," + packageName;
218     }
219 
220     @Nullable
getHeadsUpEntry(@onNull String key)221     protected HeadsUpEntry getHeadsUpEntry(@NonNull String key) {
222         return (HeadsUpEntry) mAlertEntries.get(key);
223     }
224 
225     /**
226      * Returns the top Heads Up Notification, which appears to show at first.
227      */
228     @Nullable
getTopEntry()229     public NotificationEntry getTopEntry() {
230         HeadsUpEntry topEntry = getTopHeadsUpEntry();
231         return (topEntry != null) ? topEntry.mEntry : null;
232     }
233 
234     @Nullable
getTopHeadsUpEntry()235     protected HeadsUpEntry getTopHeadsUpEntry() {
236         if (mAlertEntries.isEmpty()) {
237             return null;
238         }
239         HeadsUpEntry topEntry = null;
240         for (AlertEntry entry: mAlertEntries.values()) {
241             if (topEntry == null || entry.compareTo(topEntry) < 0) {
242                 topEntry = (HeadsUpEntry) entry;
243             }
244         }
245         return topEntry;
246     }
247 
248     /**
249      * Sets the current user.
250      */
setUser(int user)251     public void setUser(int user) {
252         mUser = user;
253     }
254 
dump(@onNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args)255     public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
256         pw.println("HeadsUpManager state:");
257         dumpInternal(fd, pw, args);
258     }
259 
dumpInternal( @onNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args)260     protected void dumpInternal(
261             @NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
262         pw.print("  mTouchAcceptanceDelay="); pw.println(mTouchAcceptanceDelay);
263         pw.print("  mSnoozeLengthMs="); pw.println(mSnoozeLengthMs);
264         pw.print("  now="); pw.println(mClock.currentTimeMillis());
265         pw.print("  mUser="); pw.println(mUser);
266         for (AlertEntry entry: mAlertEntries.values()) {
267             pw.print("  HeadsUpEntry="); pw.println(entry.mEntry);
268         }
269         int N = mSnoozedPackages.size();
270         pw.println("  snoozed packages: " + N);
271         for (int i = 0; i < N; i++) {
272             pw.print("    "); pw.print(mSnoozedPackages.valueAt(i));
273             pw.print(", "); pw.println(mSnoozedPackages.keyAt(i));
274         }
275     }
276 
277     /**
278      * Returns if there are any pinned Heads Up Notifications or not.
279      */
hasPinnedHeadsUp()280     public boolean hasPinnedHeadsUp() {
281         return mHasPinnedNotification;
282     }
283 
hasPinnedNotificationInternal()284     private boolean hasPinnedNotificationInternal() {
285         for (String key : mAlertEntries.keySet()) {
286             AlertEntry entry = getHeadsUpEntry(key);
287             if (entry.mEntry.isRowPinned()) {
288                 return true;
289             }
290         }
291         return false;
292     }
293 
294     /**
295      * Unpins all pinned Heads Up Notifications.
296      * @param userUnPinned The unpinned action is trigger by user real operation.
297      */
unpinAll(boolean userUnPinned)298     public void unpinAll(boolean userUnPinned) {
299         for (String key : mAlertEntries.keySet()) {
300             HeadsUpEntry entry = getHeadsUpEntry(key);
301             setEntryPinned(entry, false /* isPinned */);
302             // maybe it got un sticky
303             entry.updateEntry(false /* updatePostTime */);
304 
305             // when the user unpinned all of HUNs by moving one HUN, all of HUNs should not stay
306             // on the screen.
307             if (userUnPinned && entry.mEntry != null) {
308                 if (entry.mEntry.mustStayOnScreen()) {
309                     entry.mEntry.setHeadsUpIsVisible();
310                 }
311             }
312         }
313     }
314 
315     /**
316      * Returns the value of the tracking-heads-up flag. See the doc of {@code setTrackingHeadsUp} as
317      * well.
318      */
isTrackingHeadsUp()319     public boolean isTrackingHeadsUp() {
320         // Might be implemented in subclass.
321         return false;
322     }
323 
324     /**
325      * Compare two entries and decide how they should be ranked.
326      *
327      * @return -1 if the first argument should be ranked higher than the second, 1 if the second
328      * one should be ranked higher and 0 if they are equal.
329      */
compare(@onNull NotificationEntry a, @NonNull NotificationEntry b)330     public int compare(@NonNull NotificationEntry a, @NonNull NotificationEntry b) {
331         AlertEntry aEntry = getHeadsUpEntry(a.key);
332         AlertEntry bEntry = getHeadsUpEntry(b.key);
333         if (aEntry == null || bEntry == null) {
334             return aEntry == null ? 1 : -1;
335         }
336         return aEntry.compareTo(bEntry);
337     }
338 
339     /**
340      * Set an entry to be expanded and therefore stick in the heads up area if it's pinned
341      * until it's collapsed again.
342      */
setExpanded(@onNull NotificationEntry entry, boolean expanded)343     public void setExpanded(@NonNull NotificationEntry entry, boolean expanded) {
344         HeadsUpEntry headsUpEntry = getHeadsUpEntry(entry.key);
345         if (headsUpEntry != null && entry.isRowPinned()) {
346             headsUpEntry.setExpanded(expanded);
347         }
348     }
349 
350     @NonNull
351     @Override
createAlertEntry()352     protected HeadsUpEntry createAlertEntry() {
353         return new HeadsUpEntry();
354     }
355 
onDensityOrFontScaleChanged()356     public void onDensityOrFontScaleChanged() {
357     }
358 
359     /**
360      * This represents a notification and how long it is in a heads up mode. It also manages its
361      * lifecycle automatically when created.
362      */
363     protected class HeadsUpEntry extends AlertEntry {
364         public boolean remoteInputActive;
365         protected boolean expanded;
366 
367         @Override
isSticky()368         protected boolean isSticky() {
369             return (mEntry.isRowPinned() && expanded)
370                     || remoteInputActive || hasFullScreenIntent(mEntry);
371         }
372 
373         @Override
compareTo(@onNull AlertEntry alertEntry)374         public int compareTo(@NonNull AlertEntry alertEntry) {
375             HeadsUpEntry headsUpEntry = (HeadsUpEntry) alertEntry;
376             boolean isPinned = mEntry.isRowPinned();
377             boolean otherPinned = headsUpEntry.mEntry.isRowPinned();
378             if (isPinned && !otherPinned) {
379                 return -1;
380             } else if (!isPinned && otherPinned) {
381                 return 1;
382             }
383             boolean selfFullscreen = hasFullScreenIntent(mEntry);
384             boolean otherFullscreen = hasFullScreenIntent(headsUpEntry.mEntry);
385             if (selfFullscreen && !otherFullscreen) {
386                 return -1;
387             } else if (!selfFullscreen && otherFullscreen) {
388                 return 1;
389             }
390 
391             if (remoteInputActive && !headsUpEntry.remoteInputActive) {
392                 return -1;
393             } else if (!remoteInputActive && headsUpEntry.remoteInputActive) {
394                 return 1;
395             }
396 
397             return super.compareTo(headsUpEntry);
398         }
399 
setExpanded(boolean expanded)400         public void setExpanded(boolean expanded) {
401             this.expanded = expanded;
402         }
403 
404         @Override
reset()405         public void reset() {
406             super.reset();
407             expanded = false;
408             remoteInputActive = false;
409         }
410 
411         @Override
calculatePostTime()412         protected long calculatePostTime() {
413             // The actual post time will be just after the heads-up really slided in
414             return super.calculatePostTime() + mTouchAcceptanceDelay;
415         }
416 
417         @Override
calculateFinishTime()418         protected long calculateFinishTime() {
419             return mPostTime + getRecommendedTimeoutMillis();
420         }
421 
422         /**
423          * Get user-preferred or default timeout duration. The larger one will be returned.
424          * @return milliseconds before auto-dismiss
425          */
getRecommendedTimeoutMillis()426         private int getRecommendedTimeoutMillis() {
427             return mAccessibilityMgr.getRecommendedTimeoutMillis(
428                     mAutoDismissNotificationDecay,
429                     AccessibilityManager.FLAG_CONTENT_CONTROLS
430                             | AccessibilityManager.FLAG_CONTENT_ICONS
431                             | AccessibilityManager.FLAG_CONTENT_TEXT);
432         }
433     }
434 }
435