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                                 /* includeDeleted= */ true);
86                 defaultChannel.setImportance(enabled ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_NONE);
87                 mNotificationManager
88                         .updateNotificationChannelForPackage(mPackageName, mUid, defaultChannel);
89             }
90             mNotificationManager.setNotificationsEnabledForPackage(mPackageName, mUid, enabled);
91         } catch (Exception e) {
92             LOG.w("Error querying notification setting for package");
93             return false;
94         }
95         return true;
96     }
97 
isNotificationsEnabled()98     private boolean isNotificationsEnabled() {
99         try {
100             return mNotificationManager.areNotificationsEnabledForPackage(mPackageName, mUid);
101         } catch (Exception e) {
102             LOG.w("Error querying notification setting for package");
103             return false;
104         }
105     }
106 }
107