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