Home | History | Annotate | Download | only in components
      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.server.telecom.components;
     18 
     19 import android.content.Context;
     20 import android.content.DialogInterface;
     21 import android.content.pm.ApplicationInfo;
     22 import android.content.pm.PackageManager;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.graphics.Color;
     25 import android.graphics.Typeface;
     26 import android.os.Bundle;
     27 import android.telecom.DefaultDialerManager;
     28 import android.telecom.TelecomManager;
     29 import android.telephony.TelephonyManager;
     30 import android.text.Spannable;
     31 import android.text.SpannableString;
     32 import android.text.SpannableStringBuilder;
     33 import android.text.TextUtils;
     34 import android.text.style.ForegroundColorSpan;
     35 import android.text.style.StyleSpan;
     36 import android.util.Log;
     37 
     38 import com.android.internal.app.AlertActivity;
     39 import com.android.internal.app.AlertController;
     40 import com.android.server.telecom.R;
     41 
     42 /**
     43  * Activity that shows a dialog for the user to confirm whether or not the default dialer should
     44  * be changed.
     45  *
     46  * This dialog can be skipped directly for CTS tests using the adb command:
     47  * adb shell am start -a android.telecom.action.CHANGE_DEFAULT_DIALER_PRIVILEGED -e android.telecom.extra.CHANGE_DEFAULT_DIALER_PACKAGE_NAME <packageName>
     48  */
     49 public class ChangeDefaultDialerDialog extends AlertActivity implements
     50         DialogInterface.OnClickListener{
     51     private static final String TAG = ChangeDefaultDialerDialog.class.getSimpleName();
     52     private String mNewPackage;
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57 
     58         final String oldPackage = DefaultDialerManager.getDefaultDialerApplication(this);
     59         mNewPackage = getIntent().getStringExtra(
     60                 TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
     61         if (!canChangeToProvidedPackage(oldPackage, mNewPackage)) {
     62             setResult(RESULT_CANCELED);
     63             finish();
     64         }
     65 
     66         // Show dialog to require user confirmation.
     67          buildDialog(mNewPackage);
     68     }
     69 
     70     @Override
     71     public void onClick(DialogInterface dialog, int which) {
     72         switch (which) {
     73             case BUTTON_POSITIVE:
     74                 TelecomManager.from(this).setDefaultDialer(mNewPackage);
     75                 setResult(RESULT_OK);
     76                 break;
     77             case BUTTON_NEGATIVE:
     78                 setResult(RESULT_CANCELED);
     79                 break;
     80         }
     81     }
     82 
     83     private boolean canChangeToProvidedPackage(String oldPackage, String newPackage) {
     84         final TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     85         if (!tm.isVoiceCapable()) {
     86             Log.w(TAG, "Dialog launched but device is not voice capable.");
     87             return false;
     88         }
     89 
     90         if (!DefaultDialerManager.getInstalledDialerApplications(this).contains(newPackage)) {
     91             Log.w(TAG, "Provided package name does not correspond to an installed Phone "
     92                     + "application.");
     93             return false;
     94         }
     95 
     96         if (!TextUtils.isEmpty(oldPackage) && TextUtils.equals(oldPackage, newPackage)) {
     97             Log.w(TAG, "Provided package name is already the current default Phone application.");
     98             return false;
     99         }
    100         return true;
    101     }
    102 
    103     private boolean buildDialog(String newPackage) {
    104         final PackageManager pm = getPackageManager();
    105         final String newPackageLabel = getApplicationLabelForPackageName(pm, newPackage);
    106         final AlertController.AlertParams p = mAlertParams;
    107         p.mTitle = getString(R.string.change_default_dialer_dialog_title, newPackageLabel);
    108         p.mMessage = getString(R.string.change_default_dialer_warning_message, newPackageLabel);
    109         p.mPositiveButtonText = getString(R.string.change_default_dialer_dialog_affirmative);
    110         p.mNegativeButtonText = getString(R.string.change_default_dialer_dialog_negative);
    111         p.mPositiveButtonListener = this;
    112         p.mNegativeButtonListener = this;
    113         setupAlert();
    114 
    115         return true;
    116     }
    117 
    118     /**
    119      * Returns the application label that corresponds to the given package name
    120      *
    121      * @param pm An instance of a {@link PackageManager}.
    122      * @param packageName A valid package name.
    123      *
    124      * @return Application label for the given package name, or null if not found.
    125      */
    126     private String getApplicationLabelForPackageName(PackageManager pm, String packageName) {
    127         ApplicationInfo info = null;
    128         try {
    129             info = pm.getApplicationInfo(packageName, 0);
    130         } catch (NameNotFoundException e) {
    131             Log.w(TAG, "Application info not found for packageName " + packageName);
    132         }
    133         if (info == null) {
    134             return packageName;
    135         } else {
    136             return info.loadLabel(pm).toString();
    137         }
    138     }
    139 }
    140