1 /*
2  * Copyright (C) 2016 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 android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.settings.accounts.AccountRestrictionHelper;
28 import com.android.settings.core.PreferenceControllerMixin;
29 import com.android.settingslib.RestrictedPreference;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 
32 /**
33  * Base class for preference controller that handles preference that enforce adjust volume
34  * restriction
35  */
36 public class EmergencyBroadcastPreferenceController extends AbstractPreferenceController
37         implements PreferenceControllerMixin {
38 
39     private final String mPrefKey;
40 
41     private AccountRestrictionHelper mHelper;
42     private UserManager mUserManager;
43     private PackageManager mPm;
44 
EmergencyBroadcastPreferenceController(Context context, String prefKey)45     public EmergencyBroadcastPreferenceController(Context context, String prefKey) {
46         this(context, new AccountRestrictionHelper(context), prefKey);
47     }
48 
49     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
EmergencyBroadcastPreferenceController(Context context, AccountRestrictionHelper helper, String prefKey)50     EmergencyBroadcastPreferenceController(Context context, AccountRestrictionHelper helper,
51             String prefKey) {
52         super(context);
53         mPrefKey = prefKey;
54         mHelper = helper;
55         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
56         mPm = mContext.getPackageManager();
57     }
58 
59     @Override
updateState(Preference preference)60     public void updateState(Preference preference) {
61         if (!(preference instanceof RestrictedPreference)) {
62             return;
63         }
64         ((RestrictedPreference) preference).checkRestrictionAndSetDisabled(
65                 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
66     }
67 
68     @Override
handlePreferenceTreeClick(Preference preference)69     public boolean handlePreferenceTreeClick(Preference preference) {
70         return false;
71     }
72 
73     @Override
getPreferenceKey()74     public String getPreferenceKey() {
75         return mPrefKey;
76     }
77 
78     @Override
isAvailable()79     public boolean isAvailable() {
80         return mUserManager.isAdminUser() && isCellBroadcastAppLinkEnabled()
81                 && !mHelper.hasBaseUserRestriction(
82                 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, UserHandle.myUserId());
83     }
84 
isCellBroadcastAppLinkEnabled()85     private boolean isCellBroadcastAppLinkEnabled() {
86         // Enable link to CMAS app settings depending on the value in config.xml.
87         boolean enabled = mContext.getResources().getBoolean(
88                 com.android.internal.R.bool.config_cellBroadcastAppLinks);
89         if (enabled) {
90             try {
91                 if (mPm.getApplicationEnabledSetting("com.android.cellbroadcastreceiver")
92                         == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
93                     enabled = false;  // CMAS app disabled
94                 }
95             } catch (IllegalArgumentException ignored) {
96                 enabled = false;  // CMAS app not installed
97             }
98         }
99         return enabled;
100     }
101 
102 }