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