DefaultSmsPicker.java revision 03a3b518debfb3870d780a6e29c8f115013cd866
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
17package com.android.settings.applications.defaultapps;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.text.TextUtils;
23
24import com.android.internal.logging.nano.MetricsProto;
25import com.android.internal.telephony.SmsApplication;
26import com.android.settings.R;
27import com.android.settings.Utils;
28
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.List;
32
33public class DefaultSmsPicker extends DefaultAppPickerFragment {
34
35    private DefaultKeyUpdater mDefaultKeyUpdater = new DefaultKeyUpdater();
36
37    @Override
38    public int getMetricsCategory() {
39        return MetricsProto.MetricsEvent.DEFAULT_SMS_PICKER;
40    }
41
42    @Override
43    protected int getPreferenceScreenResId() {
44        return R.xml.default_sms_settings;
45    }
46
47    @Override
48    protected List<DefaultAppInfo> getCandidates() {
49        final Context context = getContext();
50        final Collection<SmsApplication.SmsApplicationData> smsApplications =
51                SmsApplication.getApplicationCollection(context);
52        final List<DefaultAppInfo> candidates = new ArrayList<>(smsApplications.size());
53
54        for (SmsApplication.SmsApplicationData smsApplicationData : smsApplications) {
55            try {
56                candidates.add(new DefaultAppInfo(context, mPm,
57                        mPm.getApplicationInfoAsUser(smsApplicationData.mPackageName, 0, mUserId)));
58            } catch (PackageManager.NameNotFoundException e) {
59                // Skip unknown packages.
60            }
61        }
62
63        return candidates;
64    }
65
66    @Override
67    protected String getDefaultKey() {
68        return mDefaultKeyUpdater.getDefaultApplication(getContext());
69    }
70
71    @Override
72    protected boolean setDefaultKey(String key) {
73        if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultKey())) {
74            mDefaultKeyUpdater.setDefaultApplication(getContext(), key);
75            return true;
76        }
77        return false;
78    }
79
80    @Override
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    /**
87     * Wrapper class to handle default phone app update.
88     */
89    static class DefaultKeyUpdater {
90
91        public String getDefaultApplication(Context context) {
92            final ComponentName appName = SmsApplication.getDefaultSmsApplication(context, true);
93            if (appName != null) {
94                return appName.getPackageName();
95            }
96            return null;
97        }
98
99        public void setDefaultApplication(Context context, String key) {
100            SmsApplication.setDefaultApplication(key, context);
101        }
102    }
103}
104