• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.app;
18 
19 import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL;
20 import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
21 import static android.provider.Settings.Secure.NOTIFICATION_BUBBLES;
22 
23 import android.app.ActivityManager;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.res.Resources;
27 import android.provider.Settings;
28 
29 import androidx.annotation.VisibleForTesting;
30 import androidx.preference.Preference;
31 
32 import com.android.settings.R;
33 import com.android.settings.notification.NotificationBackend;
34 
35 /**
36  * Summary of the app setting for bubbles, available through app notification settings.
37  */
38 public class BubbleSummaryPreferenceController extends NotificationPreferenceController {
39     private static final String KEY = "bubble_pref_link";
40 
41     @VisibleForTesting
42     static final int ON = 1;
43 
BubbleSummaryPreferenceController(Context context, NotificationBackend backend)44     public BubbleSummaryPreferenceController(Context context, NotificationBackend backend) {
45         super(context, backend);
46     }
47 
48     @Override
isAvailable()49     public boolean isAvailable() {
50         if (!super.isAvailable()) {
51             return false;
52         }
53         if (mAppRow == null) {
54             return false;
55         }
56         if (mChannel != null) {
57             if (!isGloballyEnabled()) {
58                 return false;
59             }
60             if (isDefaultChannel()) {
61                 return true;
62             } else {
63                 return mAppRow != null;
64             }
65         }
66         return isGloballyEnabled() && mBackend.hasSentValidMsg(mAppRow.pkg, mAppRow.uid);
67     }
68 
69     @Override
isIncludedInFilter()70     boolean isIncludedInFilter() {
71         return false;
72     }
73 
74     @Override
getPreferenceKey()75     public String getPreferenceKey() {
76         return KEY;
77     }
78 
79     @Override
updateState(Preference preference)80     public void updateState(Preference preference) {
81         super.updateState(preference);
82 
83         if (mAppRow != null) {
84             final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_BUBBLE_SETTINGS);
85             intent.putExtra(Settings.EXTRA_APP_PACKAGE, mAppRow.pkg);
86             intent.putExtra(Settings.EXTRA_APP_UID, mAppRow.uid);
87             preference.setIntent(intent);
88         }
89     }
90 
91     @Override
getSummary()92     public CharSequence getSummary() {
93         if (mAppRow == null) {
94             return null;
95         }
96         int backEndPref = mAppRow.bubblePreference;
97         Resources res = mContext.getResources();
98         if (backEndPref == BUBBLE_PREFERENCE_NONE || !isGloballyEnabled()) {
99             return res.getString(R.string.bubble_app_setting_none);
100         } else if (backEndPref == BUBBLE_PREFERENCE_ALL) {
101             return res.getString(R.string.bubble_app_setting_all);
102         } else {
103             return res.getString(R.string.bubble_app_setting_selected);
104         }
105     }
106 
isGloballyEnabled()107     private boolean isGloballyEnabled() {
108         ActivityManager am = mContext.getSystemService(ActivityManager.class);
109         return !am.isLowRamDevice() && Settings.Secure.getInt(mContext.getContentResolver(),
110                 NOTIFICATION_BUBBLES, ON) == ON;
111     }
112 }
113