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