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.settings.notification;
18 
19 import static android.provider.Settings.Global.NOTIFICATION_BUBBLES;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.provider.Settings;
25 
26 import com.android.settings.R;
27 import com.android.settings.applications.AppInfoBase;
28 import com.android.settings.core.SubSettingLauncher;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.preference.Preference;
32 
33 public class BubbleSummaryPreferenceController extends NotificationPreferenceController {
34 
35     private static final String KEY = "bubble_link_pref";
36     @VisibleForTesting
37     static final int SYSTEM_WIDE_ON = 1;
38     @VisibleForTesting
39     static final int SYSTEM_WIDE_OFF = 0;
40 
BubbleSummaryPreferenceController(Context context, NotificationBackend backend)41     public BubbleSummaryPreferenceController(Context context, NotificationBackend backend) {
42         super(context, backend);
43     }
44 
45     @Override
getPreferenceKey()46     public String getPreferenceKey() {
47         return KEY;
48     }
49 
50     @Override
isAvailable()51     public boolean isAvailable() {
52         if (!super.isAvailable()) {
53             return false;
54         }
55         if (mAppRow == null && mChannel == null) {
56             return false;
57         }
58         if (mChannel != null) {
59             if (!isGloballyEnabled()) {
60                 return false;
61             }
62             if (isDefaultChannel()) {
63                 return true;
64             } else {
65                 return mAppRow != null && mAppRow.allowBubbles;
66             }
67         }
68         return isGloballyEnabled();
69     }
70 
71     @Override
updateState(Preference preference)72     public void updateState(Preference preference) {
73         super.updateState(preference);
74 
75         if (mAppRow != null) {
76             Bundle args = new Bundle();
77             args.putString(AppInfoBase.ARG_PACKAGE_NAME, mAppRow.pkg);
78             args.putInt(AppInfoBase.ARG_PACKAGE_UID, mAppRow.uid);
79 
80             preference.setIntent(new SubSettingLauncher(mContext)
81                     .setDestination(AppBubbleNotificationSettings.class.getName())
82                     .setArguments(args)
83                     .setSourceMetricsCategory(
84                             SettingsEnums.NOTIFICATION_APP_NOTIFICATION)
85                     .toIntent());
86         }
87     }
88 
89     @Override
getSummary()90     public CharSequence getSummary() {
91         boolean canBubble = false;
92         if (mAppRow != null) {
93             if (mChannel != null) {
94                canBubble |= mChannel.canBubble() && isGloballyEnabled();
95             } else {
96                canBubble |= mAppRow.allowBubbles && isGloballyEnabled();
97             }
98         }
99         return mContext.getString(canBubble ? R.string.switch_on_text : R.string.switch_off_text);
100     }
101 
isGloballyEnabled()102     private boolean isGloballyEnabled() {
103         return Settings.Global.getInt(mContext.getContentResolver(),
104                 NOTIFICATION_BUBBLES, SYSTEM_WIDE_OFF) == SYSTEM_WIDE_ON;
105     }
106 }
107