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.Context; 20 import android.content.pm.PackageManager; 21 import android.telecom.DefaultDialerManager; 22 import android.telecom.TelecomManager; 23 import android.text.TextUtils; 24 25 import com.android.internal.logging.nano.MetricsProto; 26 import com.android.settings.R; 27 import com.android.settingslib.applications.DefaultAppInfo; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 32 public class DefaultPhonePicker extends DefaultAppPickerFragment { 33 34 private DefaultKeyUpdater mDefaultKeyUpdater; 35 36 @Override 37 public int getMetricsCategory() { 38 return MetricsProto.MetricsEvent.DEFAULT_PHONE_PICKER; 39 } 40 41 @Override 42 public void onAttach(Context context) { 43 super.onAttach(context); 44 mDefaultKeyUpdater = new DefaultKeyUpdater( 45 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE)); 46 } 47 48 @Override 49 protected int getPreferenceScreenResId() { 50 return R.xml.default_phone_settings; 51 } 52 53 @Override 54 protected List<DefaultAppInfo> getCandidates() { 55 final List<DefaultAppInfo> candidates = new ArrayList<>(); 56 final List<String> dialerPackages = 57 DefaultDialerManager.getInstalledDialerApplications(getContext(), mUserId); 58 final Context context = getContext(); 59 for (String packageName : dialerPackages) { 60 try { 61 candidates.add(new DefaultAppInfo(context, mPm, 62 mPm.getApplicationInfoAsUser(packageName, 0, mUserId))); 63 } catch (PackageManager.NameNotFoundException e) { 64 // Skip unknown packages. 65 } 66 } 67 return candidates; 68 } 69 70 @Override 71 protected String getDefaultKey() { 72 return mDefaultKeyUpdater.getDefaultDialerApplication(getContext(), mUserId); 73 } 74 75 @Override 76 protected String getSystemDefaultKey() { 77 return mDefaultKeyUpdater.getSystemDialerPackage(); 78 } 79 80 @Override 81 protected boolean setDefaultKey(String key) { 82 if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultKey())) { 83 return mDefaultKeyUpdater.setDefaultDialerApplication(getContext(), key, mUserId); 84 } 85 return false; 86 } 87 88 /** 89 * Wrapper class to handle default phone app update. 90 */ 91 static class DefaultKeyUpdater { 92 private final TelecomManager mTelecomManager; 93 94 public DefaultKeyUpdater(TelecomManager telecomManager) { 95 mTelecomManager = telecomManager; 96 } 97 98 public String getSystemDialerPackage() { 99 return mTelecomManager.getSystemDialerPackage(); 100 } 101 102 public String getDefaultDialerApplication(Context context, int uid) { 103 return DefaultDialerManager.getDefaultDialerApplication(context, uid); 104 } 105 106 public boolean setDefaultDialerApplication(Context context, String key, int uid) { 107 return DefaultDialerManager.setDefaultDialerApplication(context, key, uid); 108 } 109 } 110 } 111