DefaultSmsPicker.java revision 35692cf439fe1067ea742174033f5748a08a5d66
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 List<DefaultAppInfo> getCandidates() {
44        final Collection<SmsApplication.SmsApplicationData> smsApplications =
45                SmsApplication.getApplicationCollection(getContext());
46        final List<DefaultAppInfo> candidates = new ArrayList<>(smsApplications.size());
47
48        for (SmsApplication.SmsApplicationData smsApplicationData : smsApplications) {
49            try {
50                candidates.add(new DefaultAppInfo(
51                        mPm.getApplicationInfoAsUser(smsApplicationData.mPackageName, 0, mUserId)));
52            } catch (PackageManager.NameNotFoundException e) {
53                // Skip unknown packages.
54            }
55        }
56
57        return candidates;
58    }
59
60    @Override
61    protected String getDefaultAppKey() {
62        return mDefaultKeyUpdater.getDefaultApplication(getContext());
63    }
64
65    @Override
66    protected boolean setDefaultAppKey(String key) {
67        if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultAppKey())) {
68            mDefaultKeyUpdater.setDefaultApplication(getContext(), key);
69            return true;
70        }
71        return false;
72    }
73
74    @Override
75    protected String getConfirmationMessage(DefaultAppInfo info) {
76        return Utils.isPackageDirectBootAware(getContext(), info.getKey()) ? null
77                : getContext().getString(R.string.direct_boot_unaware_dialog_message);
78    }
79
80    /**
81     * Wrapper class to handle default phone app update.
82     */
83    static class DefaultKeyUpdater {
84
85        public String getDefaultApplication(Context context) {
86            final ComponentName appName = SmsApplication.getDefaultSmsApplication(context, true);
87            if (appName != null) {
88                return appName.getPackageName();
89            }
90            return null;
91        }
92
93        public void setDefaultApplication(Context context, String key) {
94            SmsApplication.setDefaultApplication(key, context);
95        }
96    }
97}
98