1 /* 2 * Copyright (C) 2019 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.packageinstaller.role.model; 18 19 import android.content.Context; 20 import android.content.pm.PackageInfo; 21 import android.os.Process; 22 import android.os.UserHandle; 23 import android.telephony.TelephonyManager; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.packageinstaller.role.utils.PackageUtils; 29 30 import java.util.List; 31 32 /** 33 * Class for behavior of the emergency role. 34 * 35 * @see com.android.settings.applications.DefaultAppSettings 36 * @see com.android.settings.applications.defaultapps.DefaultEmergencyPreferenceController 37 * @see com.android.settings.applications.defaultapps.DefaultEmergencyPicker 38 */ 39 public class EmergencyRoleBehavior implements RoleBehavior { 40 41 @Override isAvailableAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)42 public boolean isAvailableAsUser(@NonNull Role role, @NonNull UserHandle user, 43 @NonNull Context context) { 44 TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class); 45 return telephonyManager.isEmergencyAssistanceEnabled() && telephonyManager.isVoiceCapable(); 46 } 47 48 @Nullable 49 @Override getFallbackHolder(@onNull Role role, @NonNull Context context)50 public String getFallbackHolder(@NonNull Role role, @NonNull Context context) { 51 List<String> packageNames = role.getQualifyingPackagesAsUser(Process.myUserHandle(), 52 context); 53 PackageInfo fallbackPackageInfo = null; 54 int packageNamesSize = packageNames.size(); 55 for (int i = 0; i < packageNamesSize; i++) { 56 String packageName = packageNames.get(i); 57 58 PackageInfo packageInfo = PackageUtils.getPackageInfo(packageName, 0, context); 59 if (packageInfo == null) { 60 continue; 61 } 62 if (fallbackPackageInfo == null || packageInfo.firstInstallTime 63 < fallbackPackageInfo.firstInstallTime) { 64 fallbackPackageInfo = packageInfo; 65 } 66 } 67 return fallbackPackageInfo != null ? fallbackPackageInfo.packageName : null; 68 } 69 70 @Override isVisibleAsUser(@onNull Role role, @NonNull UserHandle user, @NonNull Context context)71 public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user, 72 @NonNull Context context) { 73 return VisibilityMixin.isVisible("config_showDefaultEmergency", context); 74 } 75 76 @Nullable 77 @Override getConfirmationMessage(@onNull Role role, @NonNull String packageName, @NonNull Context context)78 public CharSequence getConfirmationMessage(@NonNull Role role, @NonNull String packageName, 79 @NonNull Context context) { 80 return EncryptionUnawareConfirmationMixin.getConfirmationMessage(role, packageName, 81 context); 82 } 83 } 84