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.policy;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.util.Log;
22 import android.view.View;
23 
24 import com.android.systemui.res.R;
25 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
26 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
27 import com.android.systemui.util.Compile;
28 
29 /**
30  * A class of utility static methods for heads up notifications.
31  */
32 public final class HeadsUpUtil {
33     private static final int TAG_CLICKED_NOTIFICATION = R.id.is_clicked_heads_up_tag;
34 
35     private static final String LOG_TAG = "HeadsUpUtil";
36     private static final boolean LOG_DEBUG = Compile.IS_DEBUG && Log.isLoggable(LOG_TAG, Log.DEBUG);
37 
38     /**
39      * Set the given view as clicked or not-clicked.
40      * @param view The view to be set the flag to.
41      * @param clicked True to set as clicked. False to not-clicked.
42      */
setNeedsHeadsUpDisappearAnimationAfterClick(View view, boolean clicked)43     public static void setNeedsHeadsUpDisappearAnimationAfterClick(View view, boolean clicked) {
44         if (LOG_DEBUG) {
45             logTagClickedNotificationChanged(view, clicked);
46         }
47         view.setTag(TAG_CLICKED_NOTIFICATION, clicked ? true : null);
48     }
49 
50     /**
51      * Check if the given view has the flag of "clicked notification"
52      * @param view The view to be checked.
53      * @return True if the view has clicked. False othrewise.
54      */
isClickedHeadsUpNotification(View view)55     public static boolean isClickedHeadsUpNotification(View view) {
56         Boolean clicked = (Boolean) view.getTag(TAG_CLICKED_NOTIFICATION);
57         return clicked != null && clicked;
58     }
59 
logTagClickedNotificationChanged(@ullable View view, boolean isClicked)60     private static void logTagClickedNotificationChanged(@Nullable View view, boolean isClicked) {
61         if (view == null) {
62             return;
63         }
64 
65         final boolean wasClicked = isClickedHeadsUpNotification(view);
66         if (isClicked == wasClicked) {
67             return;
68         }
69 
70         Log.d(LOG_TAG, getViewKey(view) + ": TAG_CLICKED_NOTIFICATION set to " + isClicked);
71     }
72 
getViewKey(@onNull View view)73     private static @NonNull String getViewKey(@NonNull View view) {
74         if (!(view instanceof ExpandableNotificationRow)) {
75             return "(not a row)";
76         }
77 
78         final ExpandableNotificationRow row = (ExpandableNotificationRow) view;
79         final NotificationEntry entry = row.getEntry();
80         if (entry == null) {
81             return "(null entry)";
82         }
83 
84         final String key = entry.getKey();
85         if (key == null) {
86             return "(null key)";
87         }
88 
89         return key;
90     }
91 }
92