blob: d308a289639ef749dc7eda6af0c90dd767f7aee8 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070030import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070031import android.os.UserHandle;
32import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.provider.ContactsContract.PhoneLookup;
35import android.provider.Settings;
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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.telephony.PhoneNumberUtils;
40import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080041import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080042import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080043import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080045import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.widget.Toast;
48
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import com.android.internal.telephony.Phone;
50import com.android.internal.telephony.PhoneBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.TelephonyCapabilities;
Andrew Lee8d66d812014-11-24 14:54:02 -080052import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080053import com.android.phone.settings.VoicemailProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054
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.Map;
58import 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;
92 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
99 public StatusBarHelper statusBarHelper;
100
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();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 statusBarHelper = new StatusBarHelper();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800121 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800122 mTelecomManager = TelecomManager.from(mContext);
123 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 }
125
126 /**
127 * Initialize the singleton NotificationMgr instance.
128 *
129 * This is only done once, at startup, from PhoneApp.onCreate().
130 * From then on, the NotificationMgr instance is available via the
131 * PhoneApp's public "notificationMgr" field, which is why there's no
132 * getInstance() method here.
133 */
134 /* package */ static NotificationMgr init(PhoneGlobals app) {
135 synchronized (NotificationMgr.class) {
136 if (sInstance == null) {
137 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138 } else {
139 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
140 }
141 return sInstance;
142 }
143 }
144
145 /**
146 * Helper class that's a wrapper around the framework's
147 * StatusBarManager.disable() API.
148 *
149 * This class is used to control features like:
150 *
151 * - Disabling the status bar "notification windowshade"
152 * while the in-call UI is up
153 *
154 * - Disabling notification alerts (audible or vibrating)
155 * while a phone call is active
156 *
157 * - Disabling navigation via the system bar (the "soft buttons" at
158 * the bottom of the screen on devices with no hard buttons)
159 *
160 * We control these features through a single point of control to make
161 * sure that the various StatusBarManager.disable() calls don't
162 * interfere with each other.
163 */
164 public class StatusBarHelper {
165 // Current desired state of status bar / system bar behavior
166 private boolean mIsNotificationEnabled = true;
167 private boolean mIsExpandedViewEnabled = true;
168 private boolean mIsSystemBarNavigationEnabled = true;
169
Andrew Leed5165b02014-12-05 15:53:58 -0800170 private StatusBarHelper() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 }
172
173 /**
174 * Enables or disables auditory / vibrational alerts.
175 *
176 * (We disable these any time a voice call is active, regardless
177 * of whether or not the in-call UI is visible.)
178 */
179 public void enableNotificationAlerts(boolean enable) {
180 if (mIsNotificationEnabled != enable) {
181 mIsNotificationEnabled = enable;
182 updateStatusBar();
183 }
184 }
185
186 /**
187 * Enables or disables the expanded view of the status bar
188 * (i.e. the ability to pull down the "notification windowshade").
189 *
190 * (This feature is disabled by the InCallScreen while the in-call
191 * UI is active.)
192 */
193 public void enableExpandedView(boolean enable) {
194 if (mIsExpandedViewEnabled != enable) {
195 mIsExpandedViewEnabled = enable;
196 updateStatusBar();
197 }
198 }
199
200 /**
201 * Enables or disables the navigation via the system bar (the
202 * "soft buttons" at the bottom of the screen)
203 *
204 * (This feature is disabled while an incoming call is ringing,
205 * because it's easy to accidentally touch the system bar while
206 * pulling the phone out of your pocket.)
207 */
208 public void enableSystemBarNavigation(boolean enable) {
209 if (mIsSystemBarNavigationEnabled != enable) {
210 mIsSystemBarNavigationEnabled = enable;
211 updateStatusBar();
212 }
213 }
214
215 /**
216 * Updates the status bar to reflect the current desired state.
217 */
218 private void updateStatusBar() {
219 int state = StatusBarManager.DISABLE_NONE;
220
221 if (!mIsExpandedViewEnabled) {
222 state |= StatusBarManager.DISABLE_EXPAND;
223 }
224 if (!mIsNotificationEnabled) {
225 state |= StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
226 }
227 if (!mIsSystemBarNavigationEnabled) {
228 // Disable *all* possible navigation via the system bar.
229 state |= StatusBarManager.DISABLE_HOME;
230 state |= StatusBarManager.DISABLE_RECENT;
231 state |= StatusBarManager.DISABLE_BACK;
Christine Chenb685f172013-09-25 18:32:59 -0700232 state |= StatusBarManager.DISABLE_SEARCH;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 }
234
235 if (DBG) log("updateStatusBar: state = 0x" + Integer.toHexString(state));
236 mStatusBarManager.disable(state);
237 }
238 }
239
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 /** The projection to use when querying the phones table */
241 static final String[] PHONES_PROJECTION = new String[] {
242 PhoneLookup.NUMBER,
243 PhoneLookup.DISPLAY_NAME,
244 PhoneLookup._ID
245 };
246
247 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800248 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
249 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
250 * settings screen. The voicemail notification sound is suppressed.
251 *
252 * @param subId The subscription Id.
253 */
254 /* package */ void refreshMwi(int subId) {
255 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
256 // reference the single subid stored in the mMwiVisible map.
257 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
258 if (mMwiVisible.keySet().size() == 1) {
259 Set<Integer> keySet = mMwiVisible.keySet();
260 Iterator<Integer> keyIt = keySet.iterator();
261 if (!keyIt.hasNext()) {
262 return;
263 }
264 subId = keyIt.next();
265 }
266 }
267 if (mMwiVisible.containsKey(subId)) {
268 boolean mwiVisible = mMwiVisible.get(subId);
269 if (mwiVisible) {
270 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
271 }
272 }
273 }
274
275 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 * Updates the message waiting indicator (voicemail) notification.
277 *
278 * @param visible true if there are messages waiting
279 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800280 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800281 updateMwi(subId, visible, true /* enableNotificationSound */);
282 }
283
284 /**
285 * Updates the message waiting indicator (voicemail) notification.
286 *
287 * @param subId the subId to update.
288 * @param visible true if there are messages waiting
289 * @param enableNotificationSound {@code true} if the notification sound should be played.
290 */
291 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800292 if (!PhoneGlobals.sVoiceCapable) {
293 // Do not show the message waiting indicator on devices which are not voice capable.
294 // These events *should* be blocked at the telephony layer for such devices.
295 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
296 return;
297 }
298
Yorke Lee67a62a22014-12-15 18:46:17 -0800299 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800300 mMwiVisible.put(subId, visible);
301
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800303 Phone phone = PhoneGlobals.getPhone(subId);
304 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800305 Log.w(LOG_TAG, "Found null phone for: " + subId);
306 return;
307 }
308
309 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
310 if (subInfo == null) {
311 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800312 return;
313 }
314
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 int resId = android.R.drawable.stat_notify_voicemail;
316
317 // This Notification can get a lot fancier once we have more
318 // information about the current voicemail messages.
319 // (For example, the current voicemail system can't tell
320 // us the caller-id or timestamp of a message, or tell us the
321 // message count.)
322
323 // But for now, the UI is ultra-simple: if the MWI indication
324 // is supposed to be visible, just show a single generic
325 // notification.
326
327 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800328 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 if (DBG) log("- got vm number: '" + vmNumber + "'");
330
Andrew Leea82b8202014-11-21 16:18:28 -0800331 // The voicemail number may be null because:
332 // (1) This phone has no voicemail number.
333 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
334 // happen when the device first boots if we get a MWI notification when we
335 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800336 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
337 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700338 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800339 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 }
341
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800342 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
343 int vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
345 notificationTitle = String.format(titleFormat, vmCount);
346 }
347
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800348 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
349 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
350
351 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 String notificationText;
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800353 if (TextUtils.isEmpty(vmNumber)) {
354 notificationText = mContext.getString(
355 R.string.notification_voicemail_no_vm_number);
356
357 // If the voicemail number if unknown, instead of calling voicemail, take the user
358 // to the voicemail settings.
359 notificationText = mContext.getString(
360 R.string.notification_voicemail_no_vm_number);
361 intent = new Intent(CallFeaturesSetting.ACTION_ADD_VOICEMAIL);
362 intent.putExtra(CallFeaturesSetting.SETUP_VOICEMAIL_EXTRA, true);
363 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
364 intent.setClass(mContext, CallFeaturesSetting.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800366 if (mTelephonyManager.getPhoneCount() > 1) {
367 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800368 } else {
369 notificationText = String.format(
370 mContext.getString(R.string.notification_voicemail_text_format),
371 PhoneNumberUtils.formatNumber(vmNumber));
372 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800373 intent = new Intent(
374 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
375 null));
376 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 }
378
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800379 PendingIntent pendingIntent =
380 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800381 Uri ringtoneUri = null;
382
383 if (enableNotificationSound) {
384 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
385 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386
Nancy Chenb4a92702014-12-04 15:57:29 -0800387 Resources res = mContext.getResources();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 Notification.Builder builder = new Notification.Builder(mContext);
389 builder.setSmallIcon(resId)
390 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800391 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 .setContentTitle(notificationTitle)
393 .setContentText(notificationText)
394 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700395 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800396 .setColor(res.getColor(R.color.dialer_theme_color))
397 .setOngoing(res.getBoolean(R.bool.voicemail_notification_persistent));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800399 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700400 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700402
403 final Notification notification = builder.build();
404 List<UserInfo> users = mUserManager.getUsers(true);
405 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700406 final UserInfo user = users.get(i);
407 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700408 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700409 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
410 && !user.isManagedProfile()) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700411 mNotificationManager.notifyAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800412 Integer.toString(subId) /* tag */,
413 VOICEMAIL_NOTIFICATION,
414 notification,
415 userHandle);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700416 }
417 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700419 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800420 Integer.toString(subId) /* tag */,
421 VOICEMAIL_NOTIFICATION,
422 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 }
424 }
425
426 /**
427 * Updates the message call forwarding indicator notification.
428 *
429 * @param visible true if there are messages waiting
430 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800431 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700432 if (DBG) log("updateCfi(): " + visible);
433 if (visible) {
434 // If Unconditional Call Forwarding (forward all calls) for VOICE
435 // is enabled, just show a notification. We'll default to expanded
436 // view for now, so the there is less confusion about the icon. If
437 // it is deemed too weird to have CF indications as expanded views,
438 // then we'll flip the flag back.
439
440 // TODO: We may want to take a look to see if the notification can
441 // display the target to forward calls to. This will require some
442 // effort though, since there are multiple layers of messages that
443 // will need to propagate that information.
444
Andrew Leed5165b02014-12-05 15:53:58 -0800445 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
446 if (subInfo == null) {
447 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
448 return;
449 }
450
451 String notificationTitle;
452 if (mTelephonyManager.getPhoneCount() > 1) {
453 notificationTitle = subInfo.getDisplayName().toString();
454 } else {
455 notificationTitle = mContext.getString(R.string.labelCF);
456 }
457
Andrew Lee99d0ac22014-10-10 13:18:04 -0700458 Notification.Builder builder = new Notification.Builder(mContext)
459 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800460 .setColor(subInfo.getIconTint())
461 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700462 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
463 .setShowWhen(false)
464 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700465
Andrew Lee99d0ac22014-10-10 13:18:04 -0700466 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800467 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700468 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800469 SubscriptionInfoHelper.addExtrasToIntent(
470 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
471 PendingIntent contentIntent =
472 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700473
474 List<UserInfo> users = mUserManager.getUsers(true);
475 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800476 final UserInfo user = users.get(i);
477 if (user.isManagedProfile()) {
478 continue;
479 }
480 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700481 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800482 mNotificationManager.notifyAsUser(
483 Integer.toString(subId) /* tag */,
484 CALL_FORWARD_NOTIFICATION,
485 builder.build(),
486 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700487 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700489 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800490 Integer.toString(subId) /* tag */,
491 CALL_FORWARD_NOTIFICATION,
492 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700493 }
494 }
495
496 /**
497 * Shows the "data disconnected due to roaming" notification, which
498 * appears when you lose data connectivity because you're roaming and
499 * you have the "data roaming" feature turned off.
500 */
501 /* package */ void showDataDisconnectedRoaming() {
502 if (DBG) log("showDataDisconnectedRoaming()...");
503
504 // "Mobile network settings" screen / dialog
505 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700506 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507
508 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
509
Andrew Lee99d0ac22014-10-10 13:18:04 -0700510 final Notification.Builder builder = new Notification.Builder(mContext)
511 .setSmallIcon(android.R.drawable.stat_sys_warning)
512 .setContentTitle(mContext.getText(R.string.roaming))
513 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
514 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515
Andrew Lee99d0ac22014-10-10 13:18:04 -0700516 List<UserInfo> users = mUserManager.getUsers(true);
517 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800518 final UserInfo user = users.get(i);
519 if (user.isManagedProfile()) {
520 continue;
521 }
522 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700523 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
524 final Notification notif =
525 new Notification.BigTextStyle(builder).bigText(contentText).build();
526 mNotificationManager.notifyAsUser(
527 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
528 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 }
530
531 /**
532 * Turns off the "data disconnected due to roaming" notification.
533 */
534 /* package */ void hideDataDisconnectedRoaming() {
535 if (DBG) log("hideDataDisconnectedRoaming()...");
536 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
537 }
538
539 /**
540 * Display the network selection "no service" notification
541 * @param operator is the numeric operator number
542 */
543 private void showNetworkSelection(String operator) {
544 if (DBG) log("showNetworkSelection(" + operator + ")...");
545
Andrew Lee99d0ac22014-10-10 13:18:04 -0700546 Notification.Builder builder = new Notification.Builder(mContext)
547 .setSmallIcon(android.R.drawable.stat_sys_warning)
548 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
549 .setContentText(
550 mContext.getString(R.string.notification_network_selection_text, operator))
551 .setShowWhen(false)
552 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553
554 // create the target network operators settings intent
555 Intent intent = new Intent(Intent.ACTION_MAIN);
556 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
557 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
558 // Use NetworkSetting to handle the selection intent
559 intent.setComponent(new ComponentName("com.android.phone",
560 "com.android.phone.NetworkSetting"));
Andrew Lee99d0ac22014-10-10 13:18:04 -0700561 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562
Andrew Lee99d0ac22014-10-10 13:18:04 -0700563 List<UserInfo> users = mUserManager.getUsers(true);
564 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800565 final UserInfo user = users.get(i);
566 if (user.isManagedProfile()) {
567 continue;
568 }
569 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700570 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
571 mNotificationManager.notifyAsUser(
572 null /* tag */,
573 SELECTED_OPERATOR_FAIL_NOTIFICATION,
574 builder.build(),
575 userHandle);
576 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 }
578
579 /**
580 * Turn off the network selection "no service" notification
581 */
582 private void cancelNetworkSelection() {
583 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700584 mNotificationManager.cancelAsUser(
585 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700586 }
587
588 /**
589 * Update notification about no service of user selected operator
590 *
591 * @param serviceState Phone service state
592 */
593 void updateNetworkSelection(int serviceState) {
594 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800595 int subId = mPhone.getSubId();
596 if (SubscriptionManager.isValidSubscriptionId(subId)) {
597 // get the shared preference of network_selection.
598 // empty is auto mode, otherwise it is the operator alpha name
599 // in case there is no operator name, check the operator numeric
600 SharedPreferences sp =
601 PreferenceManager.getDefaultSharedPreferences(mContext);
602 String networkSelection =
603 sp.getString(PhoneBase.NETWORK_SELECTION_NAME_KEY + subId, "");
604 if (TextUtils.isEmpty(networkSelection)) {
605 networkSelection =
606 sp.getString(PhoneBase.NETWORK_SELECTION_KEY + subId, "");
607 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608
Amit Mahajana60be872015-01-15 16:05:08 -0800609 if (DBG) log("updateNetworkSelection()..." + "state = " +
610 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611
Amit Mahajana60be872015-01-15 16:05:08 -0800612 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
613 && !TextUtils.isEmpty(networkSelection)) {
614 if (!mSelectedUnavailableNotify) {
615 showNetworkSelection(networkSelection);
616 mSelectedUnavailableNotify = true;
617 }
618 } else {
619 if (mSelectedUnavailableNotify) {
620 cancelNetworkSelection();
621 mSelectedUnavailableNotify = false;
622 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623 }
624 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800625 if (DBG) log("updateNetworkSelection()..." + "state = " +
626 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 }
628 }
629 }
630
631 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
632 if (mToast != null) {
633 mToast.cancel();
634 }
635
636 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
637 mToast.show();
638 }
639
640 private void log(String msg) {
641 Log.d(LOG_TAG, msg);
642 }
643}