1 /* 2 * Copyright (C) 2015 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 package com.android.settings.applications; 17 18 import android.content.ComponentName; 19 import android.content.Context; 20 import android.os.UserHandle; 21 import android.os.UserManager; 22 import android.telephony.TelephonyManager; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import com.android.internal.telephony.SmsApplication; 26 import com.android.internal.telephony.SmsApplication.SmsApplicationData; 27 import com.android.settings.AppListPreference; 28 import com.android.settings.SelfAvailablePreference; 29 30 import java.util.Collection; 31 import java.util.Objects; 32 33 public class DefaultSmsPreference extends AppListPreference implements SelfAvailablePreference { 34 DefaultSmsPreference(Context context, AttributeSet attrs)35 public DefaultSmsPreference(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 38 loadSmsApps(); 39 } 40 loadSmsApps()41 private void loadSmsApps() { 42 Collection<SmsApplicationData> smsApplications = 43 SmsApplication.getApplicationCollection(getContext()); 44 45 int count = smsApplications.size(); 46 String[] packageNames = new String[count]; 47 int i = 0; 48 for (SmsApplicationData smsApplicationData : smsApplications) { 49 packageNames[i++] = smsApplicationData.mPackageName; 50 } 51 setPackageNames(packageNames, getDefaultPackage()); 52 } 53 getDefaultPackage()54 private String getDefaultPackage() { 55 ComponentName appName = SmsApplication.getDefaultSmsApplication(getContext(), true); 56 if (appName != null) { 57 return appName.getPackageName(); 58 } 59 return null; 60 } 61 62 @Override persistString(String value)63 protected boolean persistString(String value) { 64 if (!TextUtils.isEmpty(value) && !Objects.equals(value, getDefaultPackage())) { 65 SmsApplication.setDefaultApplication(value, getContext()); 66 } 67 setSummary(getEntry()); 68 return true; 69 } 70 71 @Override isAvailable(Context context)72 public boolean isAvailable(Context context) { 73 boolean isRestrictedUser = 74 UserManager.get(context) 75 .getUserInfo(UserHandle.myUserId()).isRestricted(); 76 TelephonyManager tm = 77 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 78 return !isRestrictedUser && tm.isSmsCapable(); 79 } 80 hasSmsPreference(String pkg, Context context)81 public static boolean hasSmsPreference(String pkg, Context context) { 82 Collection<SmsApplicationData> smsApplications = 83 SmsApplication.getApplicationCollection(context); 84 for (SmsApplicationData data : smsApplications) { 85 if (data.mPackageName.equals(pkg)) { 86 return true; 87 } 88 } 89 return false; 90 } 91 isSmsDefault(String pkg, Context context)92 public static boolean isSmsDefault(String pkg, Context context) { 93 ComponentName appName = SmsApplication.getDefaultSmsApplication(context, true); 94 return appName != null && appName.getPackageName().equals(pkg); 95 } 96 } 97