blob: 0a583a00e85ddfd4a861c7e02687a3a99e35a35c [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 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
17package com.android.phone;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
26import android.content.SharedPreferences;
Andrew Lee99d0ac22014-10-10 13:18:04 -070027import android.content.pm.UserInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080028import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070030import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070032import android.os.UserHandle;
33import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.provider.ContactsContract.PhoneLookup;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070036import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080037import android.telecom.PhoneAccountHandle;
38import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070039import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.PhoneNumberUtils;
41import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080042import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080043import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080046import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.widget.Toast;
49
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.Phone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.TelephonyCapabilities;
Andrew Leebf07f762015-04-07 19:05:50 -070052import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070053import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
Andrew Lee8d66d812014-11-24 14:54:02 -080054import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Tyler Gunn9c1071f2014-12-09 10:07:54 -080056import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070057import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080058import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070059
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060/**
61 * NotificationManager-related utility code for the Phone app.
62 *
63 * This is a singleton object which acts as the interface to the
64 * framework's NotificationManager, and is used to display status bar
65 * icons and control other status bar-related behavior.
66 *
67 * @see PhoneGlobals.notificationMgr
68 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070069public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080070 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071 private static final boolean DBG =
72 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
73 // Do not check in with VDBG = true, since that may write PII to the system log.
74 private static final boolean VDBG = false;
75
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070077 static final int MMI_NOTIFICATION = 1;
78 static final int NETWORK_SELECTION_NOTIFICATION = 2;
79 static final int VOICEMAIL_NOTIFICATION = 3;
80 static final int CALL_FORWARD_NOTIFICATION = 4;
81 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
82 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083
84 /** The singleton NotificationMgr instance. */
85 private static NotificationMgr sInstance;
86
87 private PhoneGlobals mApp;
88 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90 private Context mContext;
91 private NotificationManager mNotificationManager;
Bryce Lee5dc90842015-08-11 07:57:14 -070092 private final ComponentName mNotificationComponent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070094 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080096 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080097 private TelecomManager mTelecomManager;
98 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 // used to track the notification of selected network unavailable
101 private boolean mSelectedUnavailableNotify = false;
102
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800103 // used to track whether the message waiting indicator is visible, per subscription id.
104 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
105
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106 /**
107 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700108 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 */
110 private NotificationMgr(PhoneGlobals app) {
111 mApp = app;
112 mContext = app;
113 mNotificationManager =
114 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
115 mStatusBarManager =
116 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700117 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800118 mPhone = app.mCM.getDefaultPhone();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800119 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800120 mTelecomManager = TelecomManager.from(mContext);
121 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700122
123 final String notificationComponent = mContext.getString(
124 R.string.config_customVoicemailComponent);
125
126 mNotificationComponent = notificationComponent != null
127 ? ComponentName.unflattenFromString(notificationComponent) : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 }
129
130 /**
131 * Initialize the singleton NotificationMgr instance.
132 *
133 * This is only done once, at startup, from PhoneApp.onCreate().
134 * From then on, the NotificationMgr instance is available via the
135 * PhoneApp's public "notificationMgr" field, which is why there's no
136 * getInstance() method here.
137 */
138 /* package */ static NotificationMgr init(PhoneGlobals app) {
139 synchronized (NotificationMgr.class) {
140 if (sInstance == null) {
141 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 } else {
143 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
144 }
145 return sInstance;
146 }
147 }
148
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 /** The projection to use when querying the phones table */
150 static final String[] PHONES_PROJECTION = new String[] {
151 PhoneLookup.NUMBER,
152 PhoneLookup.DISPLAY_NAME,
153 PhoneLookup._ID
154 };
155
156 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800157 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
158 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
159 * settings screen. The voicemail notification sound is suppressed.
160 *
161 * @param subId The subscription Id.
162 */
163 /* package */ void refreshMwi(int subId) {
164 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
165 // reference the single subid stored in the mMwiVisible map.
166 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
167 if (mMwiVisible.keySet().size() == 1) {
168 Set<Integer> keySet = mMwiVisible.keySet();
169 Iterator<Integer> keyIt = keySet.iterator();
170 if (!keyIt.hasNext()) {
171 return;
172 }
173 subId = keyIt.next();
174 }
175 }
176 if (mMwiVisible.containsKey(subId)) {
177 boolean mwiVisible = mMwiVisible.get(subId);
178 if (mwiVisible) {
179 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
180 }
181 }
182 }
183
184 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 * Updates the message waiting indicator (voicemail) notification.
186 *
187 * @param visible true if there are messages waiting
188 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800189 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800190 updateMwi(subId, visible, true /* enableNotificationSound */);
191 }
192
193 /**
194 * Updates the message waiting indicator (voicemail) notification.
195 *
196 * @param subId the subId to update.
197 * @param visible true if there are messages waiting
198 * @param enableNotificationSound {@code true} if the notification sound should be played.
199 */
200 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800201 if (!PhoneGlobals.sVoiceCapable) {
202 // Do not show the message waiting indicator on devices which are not voice capable.
203 // These events *should* be blocked at the telephony layer for such devices.
204 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
205 return;
206 }
207
Nancy Chen2cf7f292015-05-15 11:00:10 -0700208 Phone phone = PhoneGlobals.getPhone(subId);
209 if (visible && phone != null) {
210 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
211 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
212 if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
213 Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
214 visible = false;
215 }
216 }
217
Yorke Lee67a62a22014-12-15 18:46:17 -0800218 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800219 mMwiVisible.put(subId, visible);
220
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800222 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800223 Log.w(LOG_TAG, "Found null phone for: " + subId);
224 return;
225 }
226
227 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
228 if (subInfo == null) {
229 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800230 return;
231 }
232
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 int resId = android.R.drawable.stat_notify_voicemail;
234
235 // This Notification can get a lot fancier once we have more
236 // information about the current voicemail messages.
237 // (For example, the current voicemail system can't tell
238 // us the caller-id or timestamp of a message, or tell us the
239 // message count.)
240
241 // But for now, the UI is ultra-simple: if the MWI indication
242 // is supposed to be visible, just show a single generic
243 // notification.
244
245 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800246 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 if (DBG) log("- got vm number: '" + vmNumber + "'");
248
Andrew Leea82b8202014-11-21 16:18:28 -0800249 // The voicemail number may be null because:
250 // (1) This phone has no voicemail number.
251 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
252 // happen when the device first boots if we get a MWI notification when we
253 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800254 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
255 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800257 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 }
259
Bryce Lee5dc90842015-08-11 07:57:14 -0700260 Integer vmCount = null;
261
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800262 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700263 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
265 notificationTitle = String.format(titleFormat, vmCount);
266 }
267
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800268 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
269 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
270
271 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700273 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
274
275 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800276 notificationText = mContext.getString(
277 R.string.notification_voicemail_no_vm_number);
278
279 // If the voicemail number if unknown, instead of calling voicemail, take the user
280 // to the voicemail settings.
281 notificationText = mContext.getString(
282 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700283 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800284 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700285 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800287 if (mTelephonyManager.getPhoneCount() > 1) {
288 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800289 } else {
290 notificationText = String.format(
291 mContext.getString(R.string.notification_voicemail_text_format),
292 PhoneNumberUtils.formatNumber(vmNumber));
293 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800294 intent = new Intent(
295 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700296 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800297 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 }
299
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800300 PendingIntent pendingIntent =
301 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800302 Uri ringtoneUri = null;
303
304 if (enableNotificationSound) {
305 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
306 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700307
Nancy Chenb4a92702014-12-04 15:57:29 -0800308 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700309 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700310 mPhone.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 Notification.Builder builder = new Notification.Builder(mContext);
312 builder.setSmallIcon(resId)
313 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800314 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 .setContentTitle(notificationTitle)
316 .setContentText(notificationText)
317 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700318 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800319 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700320 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700321 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800323 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700324 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700326
327 final Notification notification = builder.build();
328 List<UserInfo> users = mUserManager.getUsers(true);
329 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700330 final UserInfo user = users.get(i);
331 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700332 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700333 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700334 && !user.isManagedProfile()) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700335 if (!sendNotificationCustomComponent(vmCount, vmNumber, pendingIntent,
336 isSettingsIntent)) {
337 mNotificationManager.notifyAsUser(
338 Integer.toString(subId) /* tag */,
339 VOICEMAIL_NOTIFICATION,
340 notification,
341 userHandle);
342 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700343 }
344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 } else {
Bryce Lee5dc90842015-08-11 07:57:14 -0700346 if (!sendNotificationCustomComponent(0, null, null, false)) {
347 mNotificationManager.cancelAsUser(
348 Integer.toString(subId) /* tag */,
349 VOICEMAIL_NOTIFICATION,
350 UserHandle.ALL);
351 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 }
353 }
354
355 /**
Bryce Lee5dc90842015-08-11 07:57:14 -0700356 * Sends a broadcast with the voicemail notification information to a custom component to
357 * handle. This method is also used to indicate to the custom component when to clear the
358 * notification. A pending intent can be passed to the custom component to indicate an action to
359 * be taken as it would by a notification produced in this class.
360 * @param count The number of pending voicemail messages to indicate on the notification. A
361 * Value of 0 is passed here to indicate that the notification should be cleared.
362 * @param number The voicemail phone number if specified.
363 * @param pendingIntent The intent that should be passed as the action to be taken.
364 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
365 * otherwise, {@code false} to indicate the intent launches voicemail.
366 * @return {@code true} if a custom component was notified of the notification.
367 */
368 private boolean sendNotificationCustomComponent(Integer count, String number,
369 PendingIntent pendingIntent, boolean isSettingsIntent) {
370 if (mNotificationComponent != null) {
371 Intent intent = new Intent();
372 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
373 intent.setComponent(mNotificationComponent);
374 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
375
376 if (count != null) {
377 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
378 }
379
380 // Additional information about the voicemail notification beyond the count is only
381 // present when the count not specified or greater than 0. The value of 0 represents
382 // clearing the notification, which does not require additional information.
383 if (count == null || count > 0) {
384 if (!TextUtils.isEmpty(number)) {
385 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
386 }
387
388 if (pendingIntent != null) {
389 intent.putExtra(isSettingsIntent
390 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
391 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
392 pendingIntent);
393 }
394 }
395
396 mContext.sendBroadcast(intent);
397 return true;
398 }
399
400 return false;
401 }
402
403 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 * Updates the message call forwarding indicator notification.
405 *
406 * @param visible true if there are messages waiting
407 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800408 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 if (DBG) log("updateCfi(): " + visible);
410 if (visible) {
411 // If Unconditional Call Forwarding (forward all calls) for VOICE
412 // is enabled, just show a notification. We'll default to expanded
413 // view for now, so the there is less confusion about the icon. If
414 // it is deemed too weird to have CF indications as expanded views,
415 // then we'll flip the flag back.
416
417 // TODO: We may want to take a look to see if the notification can
418 // display the target to forward calls to. This will require some
419 // effort though, since there are multiple layers of messages that
420 // will need to propagate that information.
421
Andrew Leed5165b02014-12-05 15:53:58 -0800422 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
423 if (subInfo == null) {
424 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
425 return;
426 }
427
428 String notificationTitle;
429 if (mTelephonyManager.getPhoneCount() > 1) {
430 notificationTitle = subInfo.getDisplayName().toString();
431 } else {
432 notificationTitle = mContext.getString(R.string.labelCF);
433 }
434
Andrew Lee99d0ac22014-10-10 13:18:04 -0700435 Notification.Builder builder = new Notification.Builder(mContext)
436 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800437 .setColor(subInfo.getIconTint())
438 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700439 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
440 .setShowWhen(false)
441 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700442
Andrew Lee99d0ac22014-10-10 13:18:04 -0700443 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800444 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700445 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800446 SubscriptionInfoHelper.addExtrasToIntent(
447 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
448 PendingIntent contentIntent =
449 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700450
451 List<UserInfo> users = mUserManager.getUsers(true);
452 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800453 final UserInfo user = users.get(i);
454 if (user.isManagedProfile()) {
455 continue;
456 }
457 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700458 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800459 mNotificationManager.notifyAsUser(
460 Integer.toString(subId) /* tag */,
461 CALL_FORWARD_NOTIFICATION,
462 builder.build(),
463 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700466 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800467 Integer.toString(subId) /* tag */,
468 CALL_FORWARD_NOTIFICATION,
469 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 }
471 }
472
473 /**
474 * Shows the "data disconnected due to roaming" notification, which
475 * appears when you lose data connectivity because you're roaming and
476 * you have the "data roaming" feature turned off.
477 */
478 /* package */ void showDataDisconnectedRoaming() {
479 if (DBG) log("showDataDisconnectedRoaming()...");
480
481 // "Mobile network settings" screen / dialog
482 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700483 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484
485 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
486
Andrew Lee99d0ac22014-10-10 13:18:04 -0700487 final Notification.Builder builder = new Notification.Builder(mContext)
488 .setSmallIcon(android.R.drawable.stat_sys_warning)
489 .setContentTitle(mContext.getText(R.string.roaming))
490 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
491 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492
Andrew Lee99d0ac22014-10-10 13:18:04 -0700493 List<UserInfo> users = mUserManager.getUsers(true);
494 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800495 final UserInfo user = users.get(i);
496 if (user.isManagedProfile()) {
497 continue;
498 }
499 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700500 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700501 final Notification notif =
502 new Notification.BigTextStyle(builder).bigText(contentText).build();
503 mNotificationManager.notifyAsUser(
504 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
505 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 }
507
508 /**
509 * Turns off the "data disconnected due to roaming" notification.
510 */
511 /* package */ void hideDataDisconnectedRoaming() {
512 if (DBG) log("hideDataDisconnectedRoaming()...");
513 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
514 }
515
516 /**
517 * Display the network selection "no service" notification
518 * @param operator is the numeric operator number
519 */
520 private void showNetworkSelection(String operator) {
521 if (DBG) log("showNetworkSelection(" + operator + ")...");
522
Andrew Lee99d0ac22014-10-10 13:18:04 -0700523 Notification.Builder builder = new Notification.Builder(mContext)
524 .setSmallIcon(android.R.drawable.stat_sys_warning)
525 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
526 .setContentText(
527 mContext.getString(R.string.notification_network_selection_text, operator))
528 .setShowWhen(false)
529 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530
531 // create the target network operators settings intent
532 Intent intent = new Intent(Intent.ACTION_MAIN);
533 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
534 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
535 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700536 intent.setComponent(new ComponentName(
537 mContext.getString(R.string.network_operator_settings_package),
538 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700539 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700540 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700541
Andrew Lee99d0ac22014-10-10 13:18:04 -0700542 List<UserInfo> users = mUserManager.getUsers(true);
543 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800544 final UserInfo user = users.get(i);
545 if (user.isManagedProfile()) {
546 continue;
547 }
548 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700549 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700550 mNotificationManager.notifyAsUser(
551 null /* tag */,
552 SELECTED_OPERATOR_FAIL_NOTIFICATION,
553 builder.build(),
554 userHandle);
555 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 }
557
558 /**
559 * Turn off the network selection "no service" notification
560 */
561 private void cancelNetworkSelection() {
562 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700563 mNotificationManager.cancelAsUser(
564 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 }
566
567 /**
568 * Update notification about no service of user selected operator
569 *
570 * @param serviceState Phone service state
571 */
572 void updateNetworkSelection(int serviceState) {
573 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800574 int subId = mPhone.getSubId();
575 if (SubscriptionManager.isValidSubscriptionId(subId)) {
576 // get the shared preference of network_selection.
577 // empty is auto mode, otherwise it is the operator alpha name
578 // in case there is no operator name, check the operator numeric
579 SharedPreferences sp =
580 PreferenceManager.getDefaultSharedPreferences(mContext);
581 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800582 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800583 if (TextUtils.isEmpty(networkSelection)) {
584 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800585 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800586 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587
Amit Mahajana60be872015-01-15 16:05:08 -0800588 if (DBG) log("updateNetworkSelection()..." + "state = " +
589 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590
Amit Mahajana60be872015-01-15 16:05:08 -0800591 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
592 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800593 showNetworkSelection(networkSelection);
594 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800595 } else {
596 if (mSelectedUnavailableNotify) {
597 cancelNetworkSelection();
598 mSelectedUnavailableNotify = false;
599 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 }
601 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800602 if (DBG) log("updateNetworkSelection()..." + "state = " +
603 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700604 }
605 }
606 }
607
608 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
609 if (mToast != null) {
610 mToast.cancel();
611 }
612
613 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
614 mToast.show();
615 }
616
617 private void log(String msg) {
618 Log.d(LOG_TAG, msg);
619 }
620}