blob: 54dfb6001b72c25304a0152080d2da9fc7eb78a4 [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;
Ta-wei Yenfb4f0502016-05-27 12:15:43 -070044import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Andrew Leed5165b02014-12-05 15:53:58 -080045import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080047import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.widget.Toast;
50
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.Phone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.TelephonyCapabilities;
Andrew Leebf07f762015-04-07 19:05:50 -070053import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070054import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
Andrew Lee8d66d812014-11-24 14:54:02 -080055import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056
Tyler Gunn9c1071f2014-12-09 10:07:54 -080057import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070058import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080059import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070060
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061/**
62 * NotificationManager-related utility code for the Phone app.
63 *
64 * This is a singleton object which acts as the interface to the
65 * framework's NotificationManager, and is used to display status bar
66 * icons and control other status bar-related behavior.
67 *
68 * @see PhoneGlobals.notificationMgr
69 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070070public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080071 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072 private static final boolean DBG =
73 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
74 // Do not check in with VDBG = true, since that may write PII to the system log.
75 private static final boolean VDBG = false;
76
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070078 static final int MMI_NOTIFICATION = 1;
79 static final int NETWORK_SELECTION_NOTIFICATION = 2;
80 static final int VOICEMAIL_NOTIFICATION = 3;
81 static final int CALL_FORWARD_NOTIFICATION = 4;
82 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
83 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084
85 /** The singleton NotificationMgr instance. */
86 private static NotificationMgr sInstance;
87
88 private PhoneGlobals mApp;
89 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090
91 private Context mContext;
92 private NotificationManager mNotificationManager;
Bryce Lee5dc90842015-08-11 07:57:14 -070093 private final ComponentName mNotificationComponent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070095 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080097 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080098 private TelecomManager mTelecomManager;
99 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 // used to track the notification of selected network unavailable
102 private boolean mSelectedUnavailableNotify = false;
103
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800104 // used to track whether the message waiting indicator is visible, per subscription id.
105 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
106
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 /**
108 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700109 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 */
111 private NotificationMgr(PhoneGlobals app) {
112 mApp = app;
113 mContext = app;
114 mNotificationManager =
115 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
116 mStatusBarManager =
117 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700118 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800119 mPhone = app.mCM.getDefaultPhone();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800120 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800121 mTelecomManager = TelecomManager.from(mContext);
122 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700123
124 final String notificationComponent = mContext.getString(
125 R.string.config_customVoicemailComponent);
126
127 mNotificationComponent = notificationComponent != null
128 ? ComponentName.unflattenFromString(notificationComponent) : null;
Ta-wei Yenfb4f0502016-05-27 12:15:43 -0700129
130 mSubscriptionManager.addOnSubscriptionsChangedListener(
131 new OnSubscriptionsChangedListener() {
132 @Override
133 public void onSubscriptionsChanged() {
134 updateActivePhonesMwi();
135 }
136 });
137 }
138
139 public void updateActivePhonesMwi() {
140 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
141
142 if (subInfos == null) {
143 return;
144 }
145
146 for (int i = 0; i < subInfos.size(); i++) {
147 int subId = subInfos.get(i).getSubscriptionId();
148 refreshMwi(subId);
149 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 }
151
152 /**
153 * Initialize the singleton NotificationMgr instance.
154 *
155 * This is only done once, at startup, from PhoneApp.onCreate().
156 * From then on, the NotificationMgr instance is available via the
157 * PhoneApp's public "notificationMgr" field, which is why there's no
158 * getInstance() method here.
159 */
160 /* package */ static NotificationMgr init(PhoneGlobals app) {
161 synchronized (NotificationMgr.class) {
162 if (sInstance == null) {
163 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 } else {
165 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
166 }
167 return sInstance;
168 }
169 }
170
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 /** The projection to use when querying the phones table */
172 static final String[] PHONES_PROJECTION = new String[] {
173 PhoneLookup.NUMBER,
174 PhoneLookup.DISPLAY_NAME,
175 PhoneLookup._ID
176 };
177
178 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800179 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
180 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
181 * settings screen. The voicemail notification sound is suppressed.
182 *
183 * @param subId The subscription Id.
184 */
185 /* package */ void refreshMwi(int subId) {
186 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
187 // reference the single subid stored in the mMwiVisible map.
188 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
189 if (mMwiVisible.keySet().size() == 1) {
190 Set<Integer> keySet = mMwiVisible.keySet();
191 Iterator<Integer> keyIt = keySet.iterator();
192 if (!keyIt.hasNext()) {
193 return;
194 }
195 subId = keyIt.next();
196 }
197 }
198 if (mMwiVisible.containsKey(subId)) {
199 boolean mwiVisible = mMwiVisible.get(subId);
200 if (mwiVisible) {
201 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
202 }
203 }
204 }
205
206 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 * Updates the message waiting indicator (voicemail) notification.
208 *
209 * @param visible true if there are messages waiting
210 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800211 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800212 updateMwi(subId, visible, true /* enableNotificationSound */);
213 }
214
215 /**
216 * Updates the message waiting indicator (voicemail) notification.
217 *
218 * @param subId the subId to update.
219 * @param visible true if there are messages waiting
220 * @param enableNotificationSound {@code true} if the notification sound should be played.
221 */
222 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800223 if (!PhoneGlobals.sVoiceCapable) {
224 // Do not show the message waiting indicator on devices which are not voice capable.
225 // These events *should* be blocked at the telephony layer for such devices.
226 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
227 return;
228 }
229
Nancy Chen2cf7f292015-05-15 11:00:10 -0700230 Phone phone = PhoneGlobals.getPhone(subId);
231 if (visible && phone != null) {
232 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
233 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
234 if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
235 Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
236 visible = false;
237 }
238 }
239
Yorke Lee67a62a22014-12-15 18:46:17 -0800240 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800241 mMwiVisible.put(subId, visible);
242
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800244 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800245 Log.w(LOG_TAG, "Found null phone for: " + subId);
246 return;
247 }
248
249 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
250 if (subInfo == null) {
251 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800252 return;
253 }
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 int resId = android.R.drawable.stat_notify_voicemail;
256
257 // This Notification can get a lot fancier once we have more
258 // information about the current voicemail messages.
259 // (For example, the current voicemail system can't tell
260 // us the caller-id or timestamp of a message, or tell us the
261 // message count.)
262
263 // But for now, the UI is ultra-simple: if the MWI indication
264 // is supposed to be visible, just show a single generic
265 // notification.
266
267 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800268 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 if (DBG) log("- got vm number: '" + vmNumber + "'");
270
Andrew Leea82b8202014-11-21 16:18:28 -0800271 // The voicemail number may be null because:
272 // (1) This phone has no voicemail number.
273 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
274 // happen when the device first boots if we get a MWI notification when we
275 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800276 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
277 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800279 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 }
281
Bryce Lee5dc90842015-08-11 07:57:14 -0700282 Integer vmCount = null;
283
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800284 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700285 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
287 notificationTitle = String.format(titleFormat, vmCount);
288 }
289
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800290 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
291 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
292
293 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700295 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
296
297 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800298 notificationText = mContext.getString(
299 R.string.notification_voicemail_no_vm_number);
300
301 // If the voicemail number if unknown, instead of calling voicemail, take the user
302 // to the voicemail settings.
303 notificationText = mContext.getString(
304 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700305 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800306 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700307 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800309 if (mTelephonyManager.getPhoneCount() > 1) {
310 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800311 } else {
312 notificationText = String.format(
313 mContext.getString(R.string.notification_voicemail_text_format),
314 PhoneNumberUtils.formatNumber(vmNumber));
315 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800316 intent = new Intent(
317 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700318 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800319 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 }
321
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800322 PendingIntent pendingIntent =
323 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800324 Uri ringtoneUri = null;
325
326 if (enableNotificationSound) {
327 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
328 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329
Nancy Chenb4a92702014-12-04 15:57:29 -0800330 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700331 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700332 mPhone.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 Notification.Builder builder = new Notification.Builder(mContext);
334 builder.setSmallIcon(resId)
335 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800336 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 .setContentTitle(notificationTitle)
338 .setContentText(notificationText)
339 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700340 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800341 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700342 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700343 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800345 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700346 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700348
349 final Notification notification = builder.build();
350 List<UserInfo> users = mUserManager.getUsers(true);
351 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700352 final UserInfo user = users.get(i);
353 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700354 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700355 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700356 && !user.isManagedProfile()) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700357 if (!sendNotificationCustomComponent(vmCount, vmNumber, pendingIntent,
358 isSettingsIntent)) {
359 mNotificationManager.notifyAsUser(
360 Integer.toString(subId) /* tag */,
361 VOICEMAIL_NOTIFICATION,
362 notification,
363 userHandle);
364 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700365 }
366 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 } else {
Bryce Lee5dc90842015-08-11 07:57:14 -0700368 if (!sendNotificationCustomComponent(0, null, null, false)) {
369 mNotificationManager.cancelAsUser(
370 Integer.toString(subId) /* tag */,
371 VOICEMAIL_NOTIFICATION,
372 UserHandle.ALL);
373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 }
375 }
376
377 /**
Bryce Lee5dc90842015-08-11 07:57:14 -0700378 * Sends a broadcast with the voicemail notification information to a custom component to
379 * handle. This method is also used to indicate to the custom component when to clear the
380 * notification. A pending intent can be passed to the custom component to indicate an action to
381 * be taken as it would by a notification produced in this class.
382 * @param count The number of pending voicemail messages to indicate on the notification. A
383 * Value of 0 is passed here to indicate that the notification should be cleared.
384 * @param number The voicemail phone number if specified.
385 * @param pendingIntent The intent that should be passed as the action to be taken.
386 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
387 * otherwise, {@code false} to indicate the intent launches voicemail.
388 * @return {@code true} if a custom component was notified of the notification.
389 */
390 private boolean sendNotificationCustomComponent(Integer count, String number,
391 PendingIntent pendingIntent, boolean isSettingsIntent) {
392 if (mNotificationComponent != null) {
393 Intent intent = new Intent();
394 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
395 intent.setComponent(mNotificationComponent);
396 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
397
398 if (count != null) {
399 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
400 }
401
402 // Additional information about the voicemail notification beyond the count is only
403 // present when the count not specified or greater than 0. The value of 0 represents
404 // clearing the notification, which does not require additional information.
405 if (count == null || count > 0) {
406 if (!TextUtils.isEmpty(number)) {
407 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
408 }
409
410 if (pendingIntent != null) {
411 intent.putExtra(isSettingsIntent
412 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
413 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
414 pendingIntent);
415 }
416 }
417
418 mContext.sendBroadcast(intent);
419 return true;
420 }
421
422 return false;
423 }
424
425 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700426 * Updates the message call forwarding indicator notification.
427 *
428 * @param visible true if there are messages waiting
429 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800430 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700431 if (DBG) log("updateCfi(): " + visible);
432 if (visible) {
433 // If Unconditional Call Forwarding (forward all calls) for VOICE
434 // is enabled, just show a notification. We'll default to expanded
435 // view for now, so the there is less confusion about the icon. If
436 // it is deemed too weird to have CF indications as expanded views,
437 // then we'll flip the flag back.
438
439 // TODO: We may want to take a look to see if the notification can
440 // display the target to forward calls to. This will require some
441 // effort though, since there are multiple layers of messages that
442 // will need to propagate that information.
443
Andrew Leed5165b02014-12-05 15:53:58 -0800444 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
445 if (subInfo == null) {
446 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
447 return;
448 }
449
450 String notificationTitle;
451 if (mTelephonyManager.getPhoneCount() > 1) {
452 notificationTitle = subInfo.getDisplayName().toString();
453 } else {
454 notificationTitle = mContext.getString(R.string.labelCF);
455 }
456
Andrew Lee99d0ac22014-10-10 13:18:04 -0700457 Notification.Builder builder = new Notification.Builder(mContext)
458 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800459 .setColor(subInfo.getIconTint())
460 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700461 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
462 .setShowWhen(false)
463 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700464
Andrew Lee99d0ac22014-10-10 13:18:04 -0700465 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800466 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700467 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800468 SubscriptionInfoHelper.addExtrasToIntent(
469 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
470 PendingIntent contentIntent =
471 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700472
473 List<UserInfo> users = mUserManager.getUsers(true);
474 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800475 final UserInfo user = users.get(i);
476 if (user.isManagedProfile()) {
477 continue;
478 }
479 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700480 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800481 mNotificationManager.notifyAsUser(
482 Integer.toString(subId) /* tag */,
483 CALL_FORWARD_NOTIFICATION,
484 builder.build(),
485 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700488 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800489 Integer.toString(subId) /* tag */,
490 CALL_FORWARD_NOTIFICATION,
491 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700492 }
493 }
494
495 /**
496 * Shows the "data disconnected due to roaming" notification, which
497 * appears when you lose data connectivity because you're roaming and
498 * you have the "data roaming" feature turned off.
499 */
500 /* package */ void showDataDisconnectedRoaming() {
501 if (DBG) log("showDataDisconnectedRoaming()...");
502
503 // "Mobile network settings" screen / dialog
504 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700505 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506
507 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
508
Andrew Lee99d0ac22014-10-10 13:18:04 -0700509 final Notification.Builder builder = new Notification.Builder(mContext)
510 .setSmallIcon(android.R.drawable.stat_sys_warning)
511 .setContentTitle(mContext.getText(R.string.roaming))
512 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
513 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514
Andrew Lee99d0ac22014-10-10 13:18:04 -0700515 List<UserInfo> users = mUserManager.getUsers(true);
516 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800517 final UserInfo user = users.get(i);
518 if (user.isManagedProfile()) {
519 continue;
520 }
521 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700522 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700523 final Notification notif =
524 new Notification.BigTextStyle(builder).bigText(contentText).build();
525 mNotificationManager.notifyAsUser(
526 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
527 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 }
529
530 /**
531 * Turns off the "data disconnected due to roaming" notification.
532 */
533 /* package */ void hideDataDisconnectedRoaming() {
534 if (DBG) log("hideDataDisconnectedRoaming()...");
535 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
536 }
537
538 /**
539 * Display the network selection "no service" notification
540 * @param operator is the numeric operator number
541 */
542 private void showNetworkSelection(String operator) {
543 if (DBG) log("showNetworkSelection(" + operator + ")...");
544
Andrew Lee99d0ac22014-10-10 13:18:04 -0700545 Notification.Builder builder = new Notification.Builder(mContext)
546 .setSmallIcon(android.R.drawable.stat_sys_warning)
547 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
548 .setContentText(
549 mContext.getString(R.string.notification_network_selection_text, operator))
550 .setShowWhen(false)
551 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552
553 // create the target network operators settings intent
554 Intent intent = new Intent(Intent.ACTION_MAIN);
555 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
556 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
557 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700558 intent.setComponent(new ComponentName(
559 mContext.getString(R.string.network_operator_settings_package),
560 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700561 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700562 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563
Andrew Lee99d0ac22014-10-10 13:18:04 -0700564 List<UserInfo> users = mUserManager.getUsers(true);
565 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800566 final UserInfo user = users.get(i);
567 if (user.isManagedProfile()) {
568 continue;
569 }
570 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700571 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700572 mNotificationManager.notifyAsUser(
573 null /* tag */,
574 SELECTED_OPERATOR_FAIL_NOTIFICATION,
575 builder.build(),
576 userHandle);
577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 }
579
580 /**
581 * Turn off the network selection "no service" notification
582 */
583 private void cancelNetworkSelection() {
584 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700585 mNotificationManager.cancelAsUser(
586 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 }
588
589 /**
590 * Update notification about no service of user selected operator
591 *
592 * @param serviceState Phone service state
593 */
594 void updateNetworkSelection(int serviceState) {
595 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800596 int subId = mPhone.getSubId();
597 if (SubscriptionManager.isValidSubscriptionId(subId)) {
598 // get the shared preference of network_selection.
599 // empty is auto mode, otherwise it is the operator alpha name
600 // in case there is no operator name, check the operator numeric
601 SharedPreferences sp =
602 PreferenceManager.getDefaultSharedPreferences(mContext);
603 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800604 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800605 if (TextUtils.isEmpty(networkSelection)) {
606 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800607 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800608 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700609
Amit Mahajana60be872015-01-15 16:05:08 -0800610 if (DBG) log("updateNetworkSelection()..." + "state = " +
611 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612
Amit Mahajana60be872015-01-15 16:05:08 -0800613 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
614 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800615 showNetworkSelection(networkSelection);
616 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800617 } else {
618 if (mSelectedUnavailableNotify) {
619 cancelNetworkSelection();
620 mSelectedUnavailableNotify = false;
621 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 }
623 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800624 if (DBG) log("updateNetworkSelection()..." + "state = " +
625 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 }
627 }
628 }
629
630 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
631 if (mToast != null) {
632 mToast.cancel();
633 }
634
635 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
636 mToast.show();
637 }
638
639 private void log(String msg) {
640 Log.d(LOG_TAG, msg);
641 }
642}