OSDN Git Service

Fix crash when there is no emergencybroadcast app on device
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / EmergencyBroadcastPreferenceController.java
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.PreferenceController;
28 import com.android.settingslib.RestrictedPreference;
29
30 /**
31  * Base class for preference controller that handles preference that enforce adjust volume
32  * restriction
33  */
34 public class EmergencyBroadcastPreferenceController extends PreferenceController {
35
36     private final String mPrefKey;
37
38     private AccountRestrictionHelper mHelper;
39     private UserManager mUserManager;
40     private PackageManager mPm;
41     private boolean mCellBroadcastAppLinkEnabled;
42
43     public EmergencyBroadcastPreferenceController(Context context, String prefKey) {
44         this(context, new AccountRestrictionHelper(context), prefKey);
45     }
46
47     @VisibleForTesting(otherwise = VisibleForTesting.NONE)
48     EmergencyBroadcastPreferenceController(Context context, AccountRestrictionHelper helper,
49             String prefKey) {
50         super(context);
51         mPrefKey = prefKey;
52         mHelper = helper;
53         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
54         mPm = mContext.getPackageManager();
55         // Enable link to CMAS app settings depending on the value in config.xml.
56         mCellBroadcastAppLinkEnabled = isCellBroadcastAppLinkEnabled();
57     }
58
59     @Override
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
69     public boolean handlePreferenceTreeClick(Preference preference) {
70         return false;
71     }
72
73     @Override
74     public String getPreferenceKey() {
75         return mPrefKey;
76     }
77
78     @Override
79     public boolean isAvailable() {
80         return mUserManager.isAdminUser() && mCellBroadcastAppLinkEnabled
81                 && !mHelper.hasBaseUserRestriction(
82                 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, UserHandle.myUserId());
83     }
84
85     private boolean isCellBroadcastAppLinkEnabled() {
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 }