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.ComponentName; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.text.TextUtils; 23 24 import com.android.internal.logging.nano.MetricsProto; 25 import com.android.internal.telephony.SmsApplication; 26 import com.android.settings.R; 27 import com.android.settings.Utils; 28 29 import java.util.ArrayList; 30 import java.util.Collection; 31 import java.util.List; 32 33 public class DefaultSmsPicker extends DefaultAppPickerFragment { 34 35 private DefaultKeyUpdater mDefaultKeyUpdater = new DefaultKeyUpdater(); 36 37 @Override getMetricsCategory()38 public int getMetricsCategory() { 39 return MetricsProto.MetricsEvent.DEFAULT_SMS_PICKER; 40 } 41 42 @Override getCandidates()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(mPm, 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 getDefaultKey()61 protected String getDefaultKey() { 62 return mDefaultKeyUpdater.getDefaultApplication(getContext()); 63 } 64 65 @Override setDefaultKey(String key)66 protected boolean setDefaultKey(String key) { 67 if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, getDefaultKey())) { 68 mDefaultKeyUpdater.setDefaultApplication(getContext(), key); 69 return true; 70 } 71 return false; 72 } 73 74 @Override getConfirmationMessage(CandidateInfo info)75 protected String getConfirmationMessage(CandidateInfo 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 getDefaultApplication(Context context)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 setDefaultApplication(Context context, String key)93 public void setDefaultApplication(Context context, String key) { 94 SmsApplication.setDefaultApplication(key, context); 95 } 96 } 97 } 98