• 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.car.settings.notifications;
18 
19 import static android.app.NotificationManager.IMPORTANCE_NONE;
20 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
21 
22 import android.app.INotificationManager;
23 import android.app.NotificationChannel;
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.os.ServiceManager;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 
31 import com.android.car.settings.common.FragmentController;
32 import com.android.car.settings.common.Logger;
33 import com.android.car.settings.common.PreferenceController;
34 
35 /**
36  * Base notifications class that handles checking and changing notification availability
37  *
38  * @param <T> The upper bound on the type of {@link Preference} on which the controller
39  *            expects to operate.
40  */
41 public abstract class BaseNotificationsPreferenceController<T extends Preference> extends
42         PreferenceController<T> {
43 
44     private static final Logger LOG = new Logger(BaseNotificationsPreferenceController.class);
45 
46     @VisibleForTesting
47     public INotificationManager mNotificationManager = INotificationManager.Stub.asInterface(
48             ServiceManager.getService(Context.NOTIFICATION_SERVICE));
49 
BaseNotificationsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)50     public BaseNotificationsPreferenceController(Context context, String preferenceKey,
51             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
52         super(context, preferenceKey, fragmentController, uxRestrictions);
53     }
54 
55     /**
56      * Changes the notifications availability of the specified app
57      *
58      * @param packageName Package name of app
59      * @param uid Uid of app
60      * @param enabled Whether to enable or disable the notification
61      * @return Whether changing the notification availability succeeded or not
62      */
toggleNotificationsSetting(String packageName, int uid, boolean enabled)63     public boolean toggleNotificationsSetting(String packageName, int uid, boolean enabled) {
64         try {
65             if (mNotificationManager.onlyHasDefaultChannel(packageName, uid)) {
66                 NotificationChannel defaultChannel =
67                         mNotificationManager.getNotificationChannelForPackage(
68                                 packageName,
69                                 uid,
70                                 NotificationChannel.DEFAULT_CHANNEL_ID,
71                                 /* conversationId= */ null,
72                                 /* includeDeleted= */ true);
73                 defaultChannel.setImportance(enabled ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE);
74                 mNotificationManager
75                         .updateNotificationChannelForPackage(packageName, uid, defaultChannel);
76             }
77             mNotificationManager.setNotificationsEnabledForPackage(packageName, uid, enabled);
78         } catch (Exception e) {
79             LOG.w("Error querying notification setting for package");
80             return false;
81         }
82         return true;
83     }
84 
85     /**
86      * Checks whether notifications are enabled for specified app
87      *
88      * @param packageName Package name of the app
89      * @param uid Uid of the app
90      * @return Whether notifications are enabled for the specified app
91      */
areNotificationsEnabled(String packageName, int uid)92     public boolean areNotificationsEnabled(String packageName, int uid) {
93         try {
94             return mNotificationManager.areNotificationsEnabledForPackage(packageName, uid);
95         } catch (Exception e) {
96             LOG.w("Error querying notification setting for package");
97             return false;
98         }
99     }
100 }
101