• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.applications;
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.content.pm.PackageInfo;
27 import android.os.ServiceManager;
28 
29 import androidx.preference.TwoStatePreference;
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 import com.android.internal.annotations.VisibleForTesting;
35 
36 /**
37  * Controller for preference which enables / disables showing notifications for an application.
38  */
39 public class NotificationsPreferenceController extends PreferenceController<TwoStatePreference> {
40 
41     private static final Logger LOG = new Logger(NotificationsPreferenceController.class);
42 
43     private String mPackageName;
44     private int mUid;
45 
46     @VisibleForTesting
47     INotificationManager mNotificationManager =
48             INotificationManager.Stub.asInterface(
49                     ServiceManager.getService(Context.NOTIFICATION_SERVICE));
50 
NotificationsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51     public NotificationsPreferenceController(Context context, String preferenceKey,
52             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
53         super(context, preferenceKey, fragmentController, uxRestrictions);
54     }
55 
56     /**
57      * Set the package info of the application.
58      */
setPackageInfo(PackageInfo packageInfo)59     public void setPackageInfo(PackageInfo packageInfo) {
60         mPackageName = packageInfo.packageName;
61         mUid = packageInfo.applicationInfo.uid;
62     }
63 
64     @Override
getPreferenceType()65     protected Class<TwoStatePreference> getPreferenceType() {
66         return TwoStatePreference.class;
67     }
68 
69     @Override
updateState(TwoStatePreference preference)70     protected void updateState(TwoStatePreference preference) {
71         preference.setChecked(isNotificationsEnabled());
72     }
73 
74     @Override
handlePreferenceChanged(TwoStatePreference preference, Object newValue)75     protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) {
76         boolean enabled = (boolean) newValue;
77 
78         try {
79             if (mNotificationManager.onlyHasDefaultChannel(mPackageName, mUid)) {
80                 NotificationChannel defaultChannel =
81                         mNotificationManager.getNotificationChannelForPackage(
82                                 mPackageName,
83                                 mUid,
84                                 NotificationChannel.DEFAULT_CHANNEL_ID,
85                                 /* conversationId= */ null,
86                                 /* includeDeleted= */ true);
87                 defaultChannel.setImportance(enabled ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE);
88                 mNotificationManager
89                         .updateNotificationChannelForPackage(mPackageName, mUid, defaultChannel);
90             }
91             mNotificationManager.setNotificationsEnabledForPackage(mPackageName, mUid, enabled);
92         } catch (Exception e) {
93             LOG.w("Error querying notification setting for package");
94             return false;
95         }
96         return true;
97     }
98 
isNotificationsEnabled()99     private boolean isNotificationsEnabled() {
100         try {
101             return mNotificationManager.areNotificationsEnabledForPackage(mPackageName, mUid);
102         } catch (Exception e) {
103             LOG.w("Error querying notification setting for package");
104             return false;
105         }
106     }
107 }
108