blob: 9504f34984c41c6bb2c4cdb13a246081bb22e497 [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;
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;
Ta-wei Yen41fb43e2016-08-17 15:16:01 -070052import com.android.phone.settings.VoicemailNotificationSettingsUtil;
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;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080055import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070056import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080057import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070058
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059/**
60 * NotificationManager-related utility code for the Phone app.
61 *
62 * This is a singleton object which acts as the interface to the
63 * framework's NotificationManager, and is used to display status bar
64 * icons and control other status bar-related behavior.
65 *
66 * @see PhoneGlobals.notificationMgr
67 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070068public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080069 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070 private static final boolean DBG =
71 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
72 // Do not check in with VDBG = true, since that may write PII to the system log.
73 private static final boolean VDBG = false;
74
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070076 static final int MMI_NOTIFICATION = 1;
77 static final int NETWORK_SELECTION_NOTIFICATION = 2;
78 static final int VOICEMAIL_NOTIFICATION = 3;
79 static final int CALL_FORWARD_NOTIFICATION = 4;
80 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
81 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082
83 /** The singleton NotificationMgr instance. */
84 private static NotificationMgr sInstance;
85
86 private PhoneGlobals mApp;
87 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
89 private Context mContext;
90 private NotificationManager mNotificationManager;
Bryce Lee5dc90842015-08-11 07:57:14 -070091 private final ComponentName mNotificationComponent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070093 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080095 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080096 private TelecomManager mTelecomManager;
97 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099 // used to track the notification of selected network unavailable
100 private boolean mSelectedUnavailableNotify = false;
101
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800102 // used to track whether the message waiting indicator is visible, per subscription id.
103 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
104
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 /**
106 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700107 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 */
109 private NotificationMgr(PhoneGlobals app) {
110 mApp = app;
111 mContext = app;
112 mNotificationManager =
113 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
114 mStatusBarManager =
115 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700116 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800117 mPhone = app.mCM.getDefaultPhone();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800118 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800119 mTelecomManager = TelecomManager.from(mContext);
120 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700121
122 final String notificationComponent = mContext.getString(
123 R.string.config_customVoicemailComponent);
124
125 mNotificationComponent = notificationComponent != null
126 ? ComponentName.unflattenFromString(notificationComponent) : null;
Ta-wei Yenfb4f0502016-05-27 12:15:43 -0700127
128 mSubscriptionManager.addOnSubscriptionsChangedListener(
129 new OnSubscriptionsChangedListener() {
130 @Override
131 public void onSubscriptionsChanged() {
132 updateActivePhonesMwi();
133 }
134 });
135 }
136
137 public void updateActivePhonesMwi() {
138 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
139
140 if (subInfos == null) {
141 return;
142 }
143
144 for (int i = 0; i < subInfos.size(); i++) {
145 int subId = subInfos.get(i).getSubscriptionId();
146 refreshMwi(subId);
147 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149
150 /**
151 * Initialize the singleton NotificationMgr instance.
152 *
153 * This is only done once, at startup, from PhoneApp.onCreate().
154 * From then on, the NotificationMgr instance is available via the
155 * PhoneApp's public "notificationMgr" field, which is why there's no
156 * getInstance() method here.
157 */
158 /* package */ static NotificationMgr init(PhoneGlobals app) {
159 synchronized (NotificationMgr.class) {
160 if (sInstance == null) {
161 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 } else {
163 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
164 }
165 return sInstance;
166 }
167 }
168
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 /** The projection to use when querying the phones table */
170 static final String[] PHONES_PROJECTION = new String[] {
171 PhoneLookup.NUMBER,
172 PhoneLookup.DISPLAY_NAME,
173 PhoneLookup._ID
174 };
175
176 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800177 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
178 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
179 * settings screen. The voicemail notification sound is suppressed.
180 *
181 * @param subId The subscription Id.
182 */
183 /* package */ void refreshMwi(int subId) {
184 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
185 // reference the single subid stored in the mMwiVisible map.
186 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
187 if (mMwiVisible.keySet().size() == 1) {
188 Set<Integer> keySet = mMwiVisible.keySet();
189 Iterator<Integer> keyIt = keySet.iterator();
190 if (!keyIt.hasNext()) {
191 return;
192 }
193 subId = keyIt.next();
194 }
195 }
196 if (mMwiVisible.containsKey(subId)) {
197 boolean mwiVisible = mMwiVisible.get(subId);
198 if (mwiVisible) {
199 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
200 }
201 }
202 }
203
204 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 * Updates the message waiting indicator (voicemail) notification.
206 *
207 * @param visible true if there are messages waiting
208 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800209 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800210 updateMwi(subId, visible, true /* enableNotificationSound */);
211 }
212
213 /**
214 * Updates the message waiting indicator (voicemail) notification.
215 *
216 * @param subId the subId to update.
217 * @param visible true if there are messages waiting
218 * @param enableNotificationSound {@code true} if the notification sound should be played.
219 */
220 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800221 if (!PhoneGlobals.sVoiceCapable) {
222 // Do not show the message waiting indicator on devices which are not voice capable.
223 // These events *should* be blocked at the telephony layer for such devices.
224 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
225 return;
226 }
227
Nancy Chen2cf7f292015-05-15 11:00:10 -0700228 Phone phone = PhoneGlobals.getPhone(subId);
229 if (visible && phone != null) {
230 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
231 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
Ta-wei Yen41fb43e2016-08-17 15:16:01 -0700232 if (queryHelper.isVoicemailSourceConfigured(phoneAccount)) {
233 Log.v(LOG_TAG, "Source configured for visual voicemail, hiding mwi.");
234 // MWI may not be suppressed if the PIN is not set on VVM3 because it is also a
235 // "Not OK" configuration state. But VVM3 never send a MWI after the service is
236 // activated so this should be fine.
237 // TODO(twyen): once unbundled the client should be able to set a flag to suppress
238 // MWI, instead of letting the NotificationMgr try to interpret the states.
Nancy Chen2cf7f292015-05-15 11:00:10 -0700239 visible = false;
240 }
241 }
242
Yorke Lee67a62a22014-12-15 18:46:17 -0800243 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800244 mMwiVisible.put(subId, visible);
245
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800247 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800248 Log.w(LOG_TAG, "Found null phone for: " + subId);
249 return;
250 }
251
252 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
253 if (subInfo == null) {
254 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800255 return;
256 }
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 int resId = android.R.drawable.stat_notify_voicemail;
259
260 // This Notification can get a lot fancier once we have more
261 // information about the current voicemail messages.
262 // (For example, the current voicemail system can't tell
263 // us the caller-id or timestamp of a message, or tell us the
264 // message count.)
265
266 // But for now, the UI is ultra-simple: if the MWI indication
267 // is supposed to be visible, just show a single generic
268 // notification.
269
270 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800271 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 if (DBG) log("- got vm number: '" + vmNumber + "'");
273
Andrew Leea82b8202014-11-21 16:18:28 -0800274 // The voicemail number may be null because:
275 // (1) This phone has no voicemail number.
276 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
277 // happen when the device first boots if we get a MWI notification when we
278 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800279 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
280 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800282 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 }
284
Bryce Lee5dc90842015-08-11 07:57:14 -0700285 Integer vmCount = null;
286
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700288 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
290 notificationTitle = String.format(titleFormat, vmCount);
291 }
292
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800293 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
294 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
295
296 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700298 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
299
300 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800301 notificationText = mContext.getString(
302 R.string.notification_voicemail_no_vm_number);
303
304 // If the voicemail number if unknown, instead of calling voicemail, take the user
305 // to the voicemail settings.
306 notificationText = mContext.getString(
307 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700308 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800309 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700310 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800312 if (mTelephonyManager.getPhoneCount() > 1) {
313 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800314 } else {
315 notificationText = String.format(
316 mContext.getString(R.string.notification_voicemail_text_format),
317 PhoneNumberUtils.formatNumber(vmNumber));
318 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800319 intent = new Intent(
320 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700321 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800322 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 }
324
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800325 PendingIntent pendingIntent =
326 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800327 Uri ringtoneUri = null;
328
329 if (enableNotificationSound) {
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700330 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800331 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332
Nancy Chenb4a92702014-12-04 15:57:29 -0800333 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700334 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700335 subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 Notification.Builder builder = new Notification.Builder(mContext);
337 builder.setSmallIcon(resId)
338 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800339 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 .setContentTitle(notificationTitle)
341 .setContentText(notificationText)
342 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700343 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800344 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700345 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700346 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800348 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700349 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700351
352 final Notification notification = builder.build();
353 List<UserInfo> users = mUserManager.getUsers(true);
354 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700355 final UserInfo user = users.get(i);
356 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700357 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700358 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700359 && !user.isManagedProfile()) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700360 if (!sendNotificationCustomComponent(vmCount, vmNumber, pendingIntent,
361 isSettingsIntent)) {
362 mNotificationManager.notifyAsUser(
363 Integer.toString(subId) /* tag */,
364 VOICEMAIL_NOTIFICATION,
365 notification,
366 userHandle);
367 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700368 }
369 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 } else {
Bryce Lee5dc90842015-08-11 07:57:14 -0700371 if (!sendNotificationCustomComponent(0, null, null, false)) {
372 mNotificationManager.cancelAsUser(
373 Integer.toString(subId) /* tag */,
374 VOICEMAIL_NOTIFICATION,
375 UserHandle.ALL);
376 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 }
378 }
379
380 /**
Bryce Lee5dc90842015-08-11 07:57:14 -0700381 * Sends a broadcast with the voicemail notification information to a custom component to
382 * handle. This method is also used to indicate to the custom component when to clear the
383 * notification. A pending intent can be passed to the custom component to indicate an action to
384 * be taken as it would by a notification produced in this class.
385 * @param count The number of pending voicemail messages to indicate on the notification. A
386 * Value of 0 is passed here to indicate that the notification should be cleared.
387 * @param number The voicemail phone number if specified.
388 * @param pendingIntent The intent that should be passed as the action to be taken.
389 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
390 * otherwise, {@code false} to indicate the intent launches voicemail.
391 * @return {@code true} if a custom component was notified of the notification.
392 */
393 private boolean sendNotificationCustomComponent(Integer count, String number,
394 PendingIntent pendingIntent, boolean isSettingsIntent) {
395 if (mNotificationComponent != null) {
396 Intent intent = new Intent();
397 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
398 intent.setComponent(mNotificationComponent);
399 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
400
401 if (count != null) {
402 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
403 }
404
405 // Additional information about the voicemail notification beyond the count is only
406 // present when the count not specified or greater than 0. The value of 0 represents
407 // clearing the notification, which does not require additional information.
408 if (count == null || count > 0) {
409 if (!TextUtils.isEmpty(number)) {
410 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
411 }
412
413 if (pendingIntent != null) {
414 intent.putExtra(isSettingsIntent
415 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
416 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
417 pendingIntent);
418 }
419 }
420
421 mContext.sendBroadcast(intent);
422 return true;
423 }
424
425 return false;
426 }
427
428 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 * Updates the message call forwarding indicator notification.
430 *
431 * @param visible true if there are messages waiting
432 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800433 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 if (DBG) log("updateCfi(): " + visible);
435 if (visible) {
436 // If Unconditional Call Forwarding (forward all calls) for VOICE
437 // is enabled, just show a notification. We'll default to expanded
438 // view for now, so the there is less confusion about the icon. If
439 // it is deemed too weird to have CF indications as expanded views,
440 // then we'll flip the flag back.
441
442 // TODO: We may want to take a look to see if the notification can
443 // display the target to forward calls to. This will require some
444 // effort though, since there are multiple layers of messages that
445 // will need to propagate that information.
446
Andrew Leed5165b02014-12-05 15:53:58 -0800447 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
448 if (subInfo == null) {
449 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
450 return;
451 }
452
453 String notificationTitle;
454 if (mTelephonyManager.getPhoneCount() > 1) {
455 notificationTitle = subInfo.getDisplayName().toString();
456 } else {
457 notificationTitle = mContext.getString(R.string.labelCF);
458 }
459
Andrew Lee99d0ac22014-10-10 13:18:04 -0700460 Notification.Builder builder = new Notification.Builder(mContext)
461 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800462 .setColor(subInfo.getIconTint())
463 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700464 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
465 .setShowWhen(false)
466 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467
Andrew Lee99d0ac22014-10-10 13:18:04 -0700468 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800469 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700470 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800471 SubscriptionInfoHelper.addExtrasToIntent(
472 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
473 PendingIntent contentIntent =
474 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700475
476 List<UserInfo> users = mUserManager.getUsers(true);
477 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800478 final UserInfo user = users.get(i);
479 if (user.isManagedProfile()) {
480 continue;
481 }
482 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700483 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800484 mNotificationManager.notifyAsUser(
485 Integer.toString(subId) /* tag */,
486 CALL_FORWARD_NOTIFICATION,
487 builder.build(),
488 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700490 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700491 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800492 Integer.toString(subId) /* tag */,
493 CALL_FORWARD_NOTIFICATION,
494 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700495 }
496 }
497
498 /**
499 * Shows the "data disconnected due to roaming" notification, which
500 * appears when you lose data connectivity because you're roaming and
501 * you have the "data roaming" feature turned off.
502 */
503 /* package */ void showDataDisconnectedRoaming() {
504 if (DBG) log("showDataDisconnectedRoaming()...");
505
506 // "Mobile network settings" screen / dialog
507 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700508 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700509
510 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
511
Andrew Lee99d0ac22014-10-10 13:18:04 -0700512 final Notification.Builder builder = new Notification.Builder(mContext)
513 .setSmallIcon(android.R.drawable.stat_sys_warning)
514 .setContentTitle(mContext.getText(R.string.roaming))
515 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
516 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517
Andrew Lee99d0ac22014-10-10 13:18:04 -0700518 List<UserInfo> users = mUserManager.getUsers(true);
519 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800520 final UserInfo user = users.get(i);
521 if (user.isManagedProfile()) {
522 continue;
523 }
524 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700525 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700526 final Notification notif =
527 new Notification.BigTextStyle(builder).bigText(contentText).build();
528 mNotificationManager.notifyAsUser(
529 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
530 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 }
532
533 /**
534 * Turns off the "data disconnected due to roaming" notification.
535 */
536 /* package */ void hideDataDisconnectedRoaming() {
537 if (DBG) log("hideDataDisconnectedRoaming()...");
538 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
539 }
540
541 /**
542 * Display the network selection "no service" notification
543 * @param operator is the numeric operator number
544 */
545 private void showNetworkSelection(String operator) {
546 if (DBG) log("showNetworkSelection(" + operator + ")...");
547
Andrew Lee99d0ac22014-10-10 13:18:04 -0700548 Notification.Builder builder = new Notification.Builder(mContext)
549 .setSmallIcon(android.R.drawable.stat_sys_warning)
550 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
551 .setContentText(
552 mContext.getString(R.string.notification_network_selection_text, operator))
553 .setShowWhen(false)
554 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555
556 // create the target network operators settings intent
557 Intent intent = new Intent(Intent.ACTION_MAIN);
558 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
559 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
560 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700561 intent.setComponent(new ComponentName(
562 mContext.getString(R.string.network_operator_settings_package),
563 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700564 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700565 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566
Andrew Lee99d0ac22014-10-10 13:18:04 -0700567 List<UserInfo> users = mUserManager.getUsers(true);
568 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800569 final UserInfo user = users.get(i);
570 if (user.isManagedProfile()) {
571 continue;
572 }
573 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700574 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700575 mNotificationManager.notifyAsUser(
576 null /* tag */,
577 SELECTED_OPERATOR_FAIL_NOTIFICATION,
578 builder.build(),
579 userHandle);
580 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 }
582
583 /**
584 * Turn off the network selection "no service" notification
585 */
586 private void cancelNetworkSelection() {
587 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700588 mNotificationManager.cancelAsUser(
589 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 }
591
592 /**
593 * Update notification about no service of user selected operator
594 *
595 * @param serviceState Phone service state
596 */
597 void updateNetworkSelection(int serviceState) {
598 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800599 int subId = mPhone.getSubId();
600 if (SubscriptionManager.isValidSubscriptionId(subId)) {
601 // get the shared preference of network_selection.
602 // empty is auto mode, otherwise it is the operator alpha name
603 // in case there is no operator name, check the operator numeric
604 SharedPreferences sp =
605 PreferenceManager.getDefaultSharedPreferences(mContext);
606 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800607 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800608 if (TextUtils.isEmpty(networkSelection)) {
609 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800610 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800611 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612
Amit Mahajana60be872015-01-15 16:05:08 -0800613 if (DBG) log("updateNetworkSelection()..." + "state = " +
614 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700615
Amit Mahajana60be872015-01-15 16:05:08 -0800616 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
617 && !TextUtils.isEmpty(networkSelection)) {
Hall Liu2b846c72016-02-09 18:21:24 -0800618 showNetworkSelection(networkSelection);
619 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800620 } else {
621 if (mSelectedUnavailableNotify) {
622 cancelNetworkSelection();
623 mSelectedUnavailableNotify = false;
624 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 }
626 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800627 if (DBG) log("updateNetworkSelection()..." + "state = " +
628 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629 }
630 }
631 }
632
633 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
634 if (mToast != null) {
635 mToast.cancel();
636 }
637
638 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
639 mToast.show();
640 }
641
642 private void log(String msg) {
643 Log.d(LOG_TAG, msg);
644 }
645}