DefaultEmergencyPreferenceController.java revision fef14cac00b7e4f1ef0481845421618bc3810643
1/*
2 * Copyright (C) 2017 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
17package com.android.settings.applications.defaultapps;
18
19import android.content.Context;
20import android.content.Intent;
21import android.content.pm.ResolveInfo;
22import android.provider.Settings;
23import android.telephony.TelephonyManager;
24
25import com.android.settingslib.applications.DefaultAppInfo;
26
27import java.util.List;
28
29public class DefaultEmergencyPreferenceController extends DefaultAppPreferenceController {
30
31    private static final boolean DEFAULT_EMERGENCY_APP_IS_CONFIGURABLE = false;
32
33    public static final Intent QUERY_INTENT = new Intent(
34            TelephonyManager.ACTION_EMERGENCY_ASSISTANCE);
35
36    public DefaultEmergencyPreferenceController(Context context) {
37        super(context);
38    }
39
40    @Override
41    public boolean isAvailable() {
42        return DEFAULT_EMERGENCY_APP_IS_CONFIGURABLE
43                && isCapable()
44                && mPackageManager.getPackageManager().resolveActivity(QUERY_INTENT, 0) != null;
45    }
46
47    @Override
48    public String getPreferenceKey() {
49        return "default_emergency_app";
50    }
51
52    @Override
53    protected DefaultAppInfo getDefaultAppInfo() {
54        return null;
55    }
56
57    private boolean isCapable() {
58        return TelephonyManager.EMERGENCY_ASSISTANCE_ENABLED
59                && mContext.getResources().getBoolean(
60                com.android.internal.R.bool.config_voice_capable);
61    }
62
63    public static boolean hasEmergencyPreference(String pkg, Context context) {
64        Intent i = new Intent(QUERY_INTENT);
65        i.setPackage(pkg);
66        final List<ResolveInfo> resolveInfos =
67                context.getPackageManager().queryIntentActivities(i, 0);
68        return resolveInfos != null && resolveInfos.size() != 0;
69    }
70
71    public static boolean isEmergencyDefault(String pkg, Context context) {
72        String defaultPackage = Settings.Secure.getString(context.getContentResolver(),
73                Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
74        return defaultPackage != null && defaultPackage.equals(pkg);
75    }
76}
77