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 
     17 package com.android.settings.applications;
     18 
     19 import android.content.Context;
     20 import android.os.UserHandle;
     21 import android.os.UserManager;
     22 import android.telecom.DefaultDialerManager;
     23 import android.telecom.TelecomManager;
     24 import android.telephony.TelephonyManager;
     25 import android.text.TextUtils;
     26 import android.util.AttributeSet;
     27 
     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.List;
     34 import java.util.Objects;
     35 
     36 public class DefaultPhonePreference extends AppListPreference implements SelfAvailablePreference {
     37     public DefaultPhonePreference(Context context, AttributeSet attrs) {
     38         super(context, attrs);
     39         loadDialerApps();
     40     }
     41 
     42     @Override
     43     protected CharSequence getConfirmationMessage(String value) {
     44         return Utils.isPackageDirectBootAware(getContext(), value) ? null
     45                 : getContext().getText(R.string.direct_boot_unaware_dialog_message);
     46     }
     47 
     48     @Override
     49     protected boolean persistString(String value) {
     50         if (!TextUtils.isEmpty(value) && !Objects.equals(value, getDefaultPackage())) {
     51             DefaultDialerManager.setDefaultDialerApplication(getContext(), value, mUserId);
     52         }
     53         setSummary(getEntry());
     54         return true;
     55     }
     56 
     57     private void loadDialerApps() {
     58         List<String> dialerPackages =
     59                 DefaultDialerManager.getInstalledDialerApplications(getContext(), mUserId);
     60 
     61         final String[] dialers = new String[dialerPackages.size()];
     62         for (int i = 0; i < dialerPackages.size(); i++) {
     63             dialers[i] = dialerPackages.get(i);
     64         }
     65         setPackageNames(dialers, getDefaultPackage(), getSystemPackage());
     66     }
     67 
     68     private String getDefaultPackage() {
     69         return DefaultDialerManager.getDefaultDialerApplication(getContext(), mUserId);
     70     }
     71 
     72     private String getSystemPackage() {
     73         TelecomManager tm = TelecomManager.from(getContext());
     74         return tm.getSystemDialerPackage();
     75     }
     76 
     77     @Override
     78     public boolean isAvailable(Context context) {
     79         final TelephonyManager tm =
     80                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
     81         if (!tm.isVoiceCapable()) {
     82             return false;
     83         }
     84 
     85         final UserManager um =
     86                 (UserManager) context.getSystemService(Context.USER_SERVICE);
     87         return !um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS);
     88     }
     89 
     90     public static boolean hasPhonePreference(String pkg, Context context) {
     91         List<String> dialerPackages =
     92                 DefaultDialerManager.getInstalledDialerApplications(context, UserHandle.myUserId());
     93         return dialerPackages.contains(pkg);
     94     }
     95 
     96     public static boolean isPhoneDefault(String pkg, Context context) {
     97         String def = DefaultDialerManager.getDefaultDialerApplication(context,
     98                 UserHandle.myUserId());
     99         return def != null && def.equals(pkg);
    100     }
    101 }
    102