summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/accounts/EmergencyInfoPreferenceController.java
blob: 790ce7f7a2c6849434d0ce1fa6c41029c97516f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.accounts;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.search.SearchIndexableRaw;

import java.util.List;

public class EmergencyInfoPreferenceController extends BasePreferenceController {

    @VisibleForTesting
    Intent mIntent;

    public EmergencyInfoPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    @Override
    public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
        if (isAvailable()) {
            SearchIndexableRaw data = new SearchIndexableRaw(mContext);
            final Resources res = mContext.getResources();
            data.title = res.getString(com.android.settings.R.string.emergency_info_title);
            data.screenTitle = res.getString(com.android.settings.R.string.emergency_info_title);
            rawData.add(data);
        }
    }

    @Override
    public void updateState(Preference preference) {
        UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
                UserHandle.myUserId());
        preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name));
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mIntent != null) {
            mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(mIntent);
            return true;
        }
        return false;
    }

    @Override
    public int getAvailabilityStatus() {
        if (!mContext.getResources().getBoolean(R.bool.config_show_emergency_info_in_device_info)) {
            return UNSUPPORTED_ON_DEVICE;
        }

        // If the variant of emergency info can not work, we should fallback to AOSP version.
        if (isEmergencyInfoSupported()) {
            return AVAILABLE;
        } else if (isAOSPVersionSupported()) {
            return AVAILABLE;
        }
        return UNSUPPORTED_ON_DEVICE;
    }

    private boolean isEmergencyInfoSupported() {
        final String packageName = mContext.getResources().getString(
                R.string.config_emergency_package_name);
        final String intentName = mContext.getResources().getString(
                R.string.config_emergency_intent_action);
        mIntent = new Intent(intentName).setPackage(packageName);
        final List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(mIntent,
                0);

        return infos != null && !infos.isEmpty();
    }

    private boolean isAOSPVersionSupported() {
        final String aospPackageName = mContext.getResources().getString(
                R.string.config_aosp_emergency_package_name);
        final String aospIntentName = mContext.getResources().getString(
                R.string.config_aosp_emergency_intent_action);

        mIntent = new Intent(aospIntentName).setPackage(aospPackageName);
        final List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(mIntent,
                0);

        return infos != null && !infos.isEmpty();
    }
}