• 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.pm.ApplicationInfo;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.provider.Settings;
25 import android.text.TextUtils;
26 
27 import com.android.internal.logging.nano.MetricsProto;
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
35 
36     @Override
getMetricsCategory()37     public int getMetricsCategory() {
38         return MetricsProto.MetricsEvent.DEFAULT_EMERGENCY_APP_PICKER;
39     }
40 
41     @Override
getCandidates()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
getConfirmationMessage(CandidateInfo info)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
getDefaultKey()78     protected String getDefaultKey() {
79         return Settings.Secure.getString(getContext().getContentResolver(),
80                 Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
81     }
82 
83     @Override
setDefaultKey(String key)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 
isSystemApp(ApplicationInfo info)98     private boolean isSystemApp(ApplicationInfo info) {
99         return info != null && (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
100     }
101 }
102