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