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