• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.notification;
18 
19 import static android.app.NotificationManager.IMPORTANCE_NONE;
20 
21 import android.annotation.Nullable;
22 import android.app.NotificationChannel;
23 import android.app.NotificationChannelGroup;
24 import android.app.NotificationManager;
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.os.UserManager;
28 import android.util.Log;
29 
30 import androidx.preference.Preference;
31 
32 import com.android.settingslib.RestrictedLockUtils;
33 import com.android.settingslib.core.AbstractPreferenceController;
34 
35 import java.util.Objects;
36 
37 /**
38  * Parent class for preferences appearing on notification setting pages at the app,
39  * notification channel group, or notification channel level.
40  */
41 public abstract class NotificationPreferenceController extends AbstractPreferenceController {
42     private static final String TAG = "ChannelPrefContr";
43     @Nullable
44     protected NotificationChannel mChannel;
45     @Nullable
46     protected NotificationChannelGroup mChannelGroup;
47     protected RestrictedLockUtils.EnforcedAdmin mAdmin;
48     protected NotificationBackend.AppRow mAppRow;
49     protected final NotificationManager mNm;
50     protected final NotificationBackend mBackend;
51     protected final Context mContext;
52     protected final UserManager mUm;
53     protected final PackageManager mPm;
54     protected Preference mPreference;
55 
NotificationPreferenceController(Context context, NotificationBackend backend)56     public NotificationPreferenceController(Context context, NotificationBackend backend) {
57         super(context);
58         mContext = context;
59         mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
60         mBackend = backend;
61         mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
62         mPm = mContext.getPackageManager();
63     }
64 
65     /**
66      * Returns true if field's parent object is not blocked.
67      */
68     @Override
isAvailable()69     public boolean isAvailable() {
70         if (mAppRow == null) {
71             return false;
72         }
73         if (mAppRow.banned) {
74             return false;
75         }
76         if (mChannelGroup != null) {
77             if (mChannelGroup.isBlocked()) {
78                 return false;
79             }
80         }
81         if (mChannel != null) {
82             return mChannel.getImportance() != IMPORTANCE_NONE;
83         }
84         return true;
85     }
86 
onResume(NotificationBackend.AppRow appRow, @Nullable NotificationChannel channel, @Nullable NotificationChannelGroup group, RestrictedLockUtils.EnforcedAdmin admin)87     protected void onResume(NotificationBackend.AppRow appRow,
88             @Nullable NotificationChannel channel, @Nullable NotificationChannelGroup group,
89             RestrictedLockUtils.EnforcedAdmin admin) {
90         mAppRow = appRow;
91         mChannel = channel;
92         mChannelGroup = group;
93         mAdmin = admin;
94     }
95 
checkCanBeVisible(int minImportanceVisible)96     protected boolean checkCanBeVisible(int minImportanceVisible) {
97         if (mChannel == null) {
98             Log.w(TAG, "No channel");
99             return false;
100         }
101 
102         int importance = mChannel.getImportance();
103         if (importance == NotificationManager.IMPORTANCE_UNSPECIFIED) {
104             return true;
105         }
106         return importance >= minImportanceVisible;
107     }
108 
saveChannel()109     protected void saveChannel() {
110         if (mChannel != null && mAppRow != null) {
111             mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, mChannel);
112         }
113     }
114 
isChannelBlockable()115     protected boolean isChannelBlockable() {
116         if (mChannel != null && mAppRow != null) {
117             if (mChannel.isImportanceLockedByCriticalDeviceFunction()
118                     || mChannel.isImportanceLockedByOEM()) {
119                 return mChannel.getImportance() == IMPORTANCE_NONE;
120             }
121 
122             return mChannel.isBlockableSystem() || !mAppRow.systemApp
123                     || mChannel.getImportance() == IMPORTANCE_NONE;
124         }
125         return false;
126     }
127 
isChannelGroupBlockable()128     protected boolean isChannelGroupBlockable() {
129         if (mChannelGroup != null && mAppRow != null) {
130             if (!mAppRow.systemApp) {
131                 return true;
132             }
133 
134             return mChannelGroup.isBlocked();
135         }
136         return false;
137     }
138 
hasValidGroup()139     protected boolean hasValidGroup() {
140         return mChannelGroup != null;
141     }
142 
isDefaultChannel()143     protected final boolean isDefaultChannel() {
144         if (mChannel == null) {
145             return false;
146         }
147         return Objects.equals(NotificationChannel.DEFAULT_CHANNEL_ID, mChannel.getId());
148     }
149 }
150