• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.server.notification;
18 
19 import android.companion.ICompanionDeviceManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.service.notification.StatusBarNotification;
23 
24 import androidx.annotation.Nullable;
25 
26 import com.android.internal.logging.InstanceIdSequence;
27 import com.android.server.notification.ManagedServices.ManagedServiceInfo;
28 
29 import java.util.HashSet;
30 import java.util.Set;
31 
32 public class TestableNotificationManagerService extends NotificationManagerService {
33     int countSystemChecks = 0;
34     boolean isSystemUid = true;
35     boolean isSystemAppId = true;
36     int countLogSmartSuggestionsVisible = 0;
37     Set<Integer> mChannelToastsSent = new HashSet<>();
38 
39     String stringArrayResourceValue;
40     @Nullable
41     NotificationAssistantAccessGrantedCallback mNotificationAssistantAccessGrantedCallback;
42 
43     @Nullable
44     Boolean mIsVisibleToListenerReturnValue = null;
45 
TestableNotificationManagerService(Context context, NotificationRecordLogger logger, InstanceIdSequence notificationInstanceIdSequence)46     TestableNotificationManagerService(Context context, NotificationRecordLogger logger,
47             InstanceIdSequence notificationInstanceIdSequence) {
48         super(context, logger, notificationInstanceIdSequence);
49     }
50 
getRankingHelper()51     RankingHelper getRankingHelper() {
52         return mRankingHelper;
53     }
54 
55     @Override
isCallingUidSystem()56     protected boolean isCallingUidSystem() {
57         countSystemChecks++;
58         return isSystemUid;
59     }
60 
61     @Override
isCallingAppIdSystem()62     protected boolean isCallingAppIdSystem() {
63         countSystemChecks++;
64         return isSystemUid || isSystemAppId;
65     }
66 
67     @Override
isCallerSystemOrPhone()68     protected boolean isCallerSystemOrPhone() {
69         countSystemChecks++;
70         return isSystemUid;
71     }
72 
73     @Override
getCompanionManager()74     protected ICompanionDeviceManager getCompanionManager() {
75         return null;
76     }
77 
78     @Override
reportUserInteraction(NotificationRecord r)79     protected void reportUserInteraction(NotificationRecord r) {
80         return;
81     }
82 
83     @Override
handleSavePolicyFile()84     protected void handleSavePolicyFile() {
85         return;
86     }
87 
88     @Override
logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation)89     void logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation) {
90         super.logSmartSuggestionsVisible(r, notificationLocation);
91         countLogSmartSuggestionsVisible++;
92     }
93 
94     @Override
setNotificationAssistantAccessGrantedForUserInternal( ComponentName assistant, int userId, boolean granted, boolean userSet)95     protected void setNotificationAssistantAccessGrantedForUserInternal(
96             ComponentName assistant, int userId, boolean granted, boolean userSet) {
97         if (mNotificationAssistantAccessGrantedCallback != null) {
98             mNotificationAssistantAccessGrantedCallback.onGranted(assistant, userId, granted,
99                     userSet);
100             return;
101         }
102         super.setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted,
103                 userSet);
104     }
105 
106     @Override
getStringArrayResource(int key)107     protected String[] getStringArrayResource(int key) {
108         return new String[] {stringArrayResourceValue};
109     }
110 
setStringArrayResourceValue(String value)111     protected void setStringArrayResourceValue(String value) {
112         stringArrayResourceValue = value;
113     }
114 
setNotificationAssistantAccessGrantedCallback( @ullable NotificationAssistantAccessGrantedCallback callback)115     void setNotificationAssistantAccessGrantedCallback(
116             @Nullable NotificationAssistantAccessGrantedCallback callback) {
117         this.mNotificationAssistantAccessGrantedCallback = callback;
118     }
119 
120     interface NotificationAssistantAccessGrantedCallback {
onGranted(ComponentName assistant, int userId, boolean granted, boolean userSet)121         void onGranted(ComponentName assistant, int userId, boolean granted, boolean userSet);
122     }
123 
124     @Override
doChannelWarningToast(int uid, CharSequence toastText)125     protected void doChannelWarningToast(int uid, CharSequence toastText) {
126         mChannelToastsSent.add(uid);
127     }
128 
129     // Helper method for testing behavior when turning on/off the review permissions notification.
setShowReviewPermissionsNotification(boolean setting)130     protected void setShowReviewPermissionsNotification(boolean setting) {
131         mShowReviewPermissionsNotification = setting;
132     }
133 
setIsVisibleToListenerReturnValue(boolean value)134     protected void setIsVisibleToListenerReturnValue(boolean value) {
135         mIsVisibleToListenerReturnValue = value;
136     }
137 
138     @Override
isVisibleToListener(StatusBarNotification sbn, int notificationType, ManagedServiceInfo listener)139     boolean isVisibleToListener(StatusBarNotification sbn, int notificationType,
140             ManagedServiceInfo listener) {
141         if (mIsVisibleToListenerReturnValue != null) {
142             return mIsVisibleToListenerReturnValue;
143         }
144         return super.isVisibleToListener(sbn, notificationType, listener);
145     }
146 
147     public class StrongAuthTrackerFake extends NotificationManagerService.StrongAuthTracker {
148         private int mGetStrongAuthForUserReturnValue = 0;
StrongAuthTrackerFake(Context context)149         StrongAuthTrackerFake(Context context) {
150             super(context);
151         }
152 
setGetStrongAuthForUserReturnValue(int val)153         public void setGetStrongAuthForUserReturnValue(int val) {
154             mGetStrongAuthForUserReturnValue = val;
155         }
156 
157         @Override
getStrongAuthForUser(int userId)158         public int getStrongAuthForUser(int userId) {
159             return mGetStrongAuthForUserReturnValue;
160         }
161     }
162 }
163