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