• 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.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceGroup;
30 import android.support.v7.preference.PreferenceScreen;
31 import android.util.Log;
32 
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 
36 import java.util.Objects;
37 
38 /**
39  * Parent class for preferences appearing on notification setting pages at the app,
40  * notification channel group, or notification channel level.
41  */
42 public abstract class NotificationPreferenceController extends AbstractPreferenceController {
43     private static final String TAG = "ChannelPrefContr";
44     @Nullable
45     protected NotificationChannel mChannel;
46     @Nullable
47     protected NotificationChannelGroup mChannelGroup;
48     protected RestrictedLockUtils.EnforcedAdmin mAdmin;
49     protected NotificationBackend.AppRow mAppRow;
50     protected final NotificationManager mNm;
51     protected final NotificationBackend mBackend;
52     protected final Context mContext;
53     protected final UserManager mUm;
54     protected final PackageManager mPm;
55     protected Preference mPreference;
56 
NotificationPreferenceController(Context context, NotificationBackend backend)57     public NotificationPreferenceController(Context context, NotificationBackend backend) {
58         super(context);
59         mContext = context;
60         mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
61         mBackend = backend;
62         mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
63         mPm = mContext.getPackageManager();
64     }
65 
66     /**
67      * Returns true if field's parent object is not blocked.
68      */
69     @Override
isAvailable()70     public boolean isAvailable() {
71         if (mAppRow == null) {
72             return false;
73         }
74         if (mAppRow.banned) {
75             return false;
76         }
77         if (mChannel != null) {
78             return mChannel.getImportance() != IMPORTANCE_NONE;
79         }
80         if (mChannelGroup != null) {
81             return !mChannelGroup.isBlocked();
82         }
83         return true;
84     }
85 
86     // finds the preference recursively and removes it from its parent
findAndRemovePreference(PreferenceGroup prefGroup, String key)87     private void findAndRemovePreference(PreferenceGroup prefGroup, String key) {
88         final int preferenceCount = prefGroup.getPreferenceCount();
89         for (int i = preferenceCount - 1; i >= 0; i--) {
90             final Preference preference = prefGroup.getPreference(i);
91             final String curKey = preference.getKey();
92 
93             if (curKey != null && curKey.equals(key)) {
94                 mPreference = preference;
95                 prefGroup.removePreference(preference);
96             }
97 
98             if (preference instanceof PreferenceGroup) {
99                 findAndRemovePreference((PreferenceGroup) preference, key);
100             }
101         }
102     }
103 
onResume(NotificationBackend.AppRow appRow, @Nullable NotificationChannel channel, @Nullable NotificationChannelGroup group, RestrictedLockUtils.EnforcedAdmin admin)104     protected void onResume(NotificationBackend.AppRow appRow,
105             @Nullable NotificationChannel channel, @Nullable NotificationChannelGroup group,
106             RestrictedLockUtils.EnforcedAdmin admin) {
107         mAppRow = appRow;
108         mChannel = channel;
109         mChannelGroup = group;
110         mAdmin = admin;
111     }
112 
checkCanBeVisible(int minImportanceVisible)113     protected boolean checkCanBeVisible(int minImportanceVisible) {
114         if (mChannel == null) {
115             Log.w(TAG, "No channel");
116             return false;
117         }
118 
119         int importance = mChannel.getImportance();
120         if (importance == NotificationManager.IMPORTANCE_UNSPECIFIED) {
121             return true;
122         }
123         return importance >= minImportanceVisible;
124     }
125 
saveChannel()126     protected void saveChannel() {
127         if (mChannel != null && mAppRow != null) {
128             mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, mChannel);
129         }
130     }
131 
isChannelConfigurable()132     protected boolean isChannelConfigurable() {
133         if (mChannel != null && mAppRow != null) {
134             return !Objects.equals(mChannel.getId(), mAppRow.lockedChannelId);
135         }
136         return false;
137     }
138 
isChannelBlockable()139     protected boolean isChannelBlockable() {
140         if (mChannel != null && mAppRow != null) {
141             if (!mAppRow.systemApp) {
142                 return true;
143             }
144 
145             return mChannel.isBlockableSystem()
146                     || mChannel.getImportance() == IMPORTANCE_NONE;
147         }
148         return false;
149     }
150 
isChannelGroupBlockable()151     protected boolean isChannelGroupBlockable() {
152         if (mChannelGroup != null && mAppRow != null) {
153             if (!mAppRow.systemApp) {
154                 return true;
155             }
156 
157             return mChannelGroup.isBlocked();
158         }
159         return false;
160     }
161 
hasValidGroup()162     protected boolean hasValidGroup() {
163         return mChannelGroup != null;
164     }
165 
isDefaultChannel()166     protected final boolean isDefaultChannel() {
167         if (mChannel == null) {
168             return false;
169         }
170         return Objects.equals(NotificationChannel.DEFAULT_CHANNEL_ID, mChannel.getId());
171     }
172 }
173