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.ContentResolver;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.PackageInfo;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.provider.Settings;
25import android.text.TextUtils;
26
27import com.android.internal.logging.nano.MetricsProto;
28import com.android.settings.R;
29import com.android.settings.Utils;
30
31import java.util.ArrayList;
32import java.util.List;
33
34public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
35
36    @Override
37    public int getMetricsCategory() {
38        return MetricsProto.MetricsEvent.DEFAULT_EMERGENCY_APP_PICKER;
39    }
40
41    @Override
42    protected List<DefaultAppInfo> getCandidates() {
43        final List<DefaultAppInfo> candidates = new ArrayList<>();
44        final List<ResolveInfo> infos = mPm.getPackageManager().queryIntentActivities(
45                DefaultEmergencyPreferenceController.QUERY_INTENT, 0);
46        PackageInfo bestMatch = null;
47        for (ResolveInfo info : infos) {
48            try {
49                final PackageInfo packageInfo =
50                        mPm.getPackageManager().getPackageInfo(info.activityInfo.packageName, 0);
51                final ApplicationInfo appInfo = packageInfo.applicationInfo;
52                candidates.add(new DefaultAppInfo(mPm, appInfo));
53                // Get earliest installed system app.
54                if (isSystemApp(appInfo) && (bestMatch == null ||
55                        bestMatch.firstInstallTime > packageInfo.firstInstallTime)) {
56                    bestMatch = packageInfo;
57                }
58            } catch (PackageManager.NameNotFoundException e) {
59                // Skip unknown packages.
60            }
61            if (bestMatch != null) {
62                final String defaultKey = getDefaultKey();
63                if (TextUtils.isEmpty(defaultKey)) {
64                    setDefaultKey(bestMatch.packageName);
65                }
66            }
67        }
68        return candidates;
69    }
70
71    @Override
72    protected String getConfirmationMessage(CandidateInfo info) {
73        return Utils.isPackageDirectBootAware(getContext(), info.getKey()) ? null
74                : getContext().getString(R.string.direct_boot_unaware_dialog_message);
75    }
76
77    @Override
78    protected String getDefaultKey() {
79        return Settings.Secure.getString(getContext().getContentResolver(),
80                Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
81    }
82
83    @Override
84    protected boolean setDefaultKey(String key) {
85        final ContentResolver contentResolver = getContext().getContentResolver();
86        final String previousValue = Settings.Secure.getString(contentResolver,
87                Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
88
89        if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, previousValue)) {
90            Settings.Secure.putString(contentResolver,
91                    Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
92                    key);
93            return true;
94        }
95        return false;
96    }
97
98    private boolean isSystemApp(ApplicationInfo info) {
99        return info != null && (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
100    }
101}
102