blob: d223d830cc396406ca18ecec64d452956cac2767 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
26import android.content.SharedPreferences;
Andrew Lee99d0ac22014-10-10 13:18:04 -070027import android.content.pm.UserInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080028import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070030import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070032import android.os.UserHandle;
33import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.provider.ContactsContract.PhoneLookup;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070036import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080037import android.telecom.PhoneAccountHandle;
38import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070039import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.PhoneNumberUtils;
41import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080042import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080043import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080044import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080046import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.widget.Toast;
49
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.Phone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.TelephonyCapabilities;
Andrew Leebf07f762015-04-07 19:05:50 -070052import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070053import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
Andrew Lee8d66d812014-11-24 14:54:02 -080054import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Tyler Gunn9c1071f2014-12-09 10:07:54 -080056import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070057import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080058import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070059
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060/**
61 * NotificationManager-related utility code for the Phone app.
62 *
63 * This is a singleton object which acts as the interface to the
64 * framework's NotificationManager, and is used to display status bar
65 * icons and control other status bar-related behavior.
66 *
67 * @see PhoneGlobals.notificationMgr
68 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070069public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080070 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071 private static final boolean DBG =
72 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
73 // Do not check in with VDBG = true, since that may write PII to the system log.
74 private static final boolean VDBG = false;
75
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070077 static final int MMI_NOTIFICATION = 1;
78 static final int NETWORK_SELECTION_NOTIFICATION = 2;
79 static final int VOICEMAIL_NOTIFICATION = 3;
80 static final int CALL_FORWARD_NOTIFICATION = 4;
81 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
82 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083
84 /** The singleton NotificationMgr instance. */
85 private static NotificationMgr sInstance;
86
87 private PhoneGlobals mApp;
88 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90 private Context mContext;
91 private NotificationManager mNotificationManager;
Bryce Lee5dc90842015-08-11 07:57:14 -070092 private final ComponentName mNotificationComponent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070094 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080096 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080097 private TelecomManager mTelecomManager;
98 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
100 public StatusBarHelper statusBarHelper;
101
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 // used to track the notification of selected network unavailable
103 private boolean mSelectedUnavailableNotify = false;
104
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800105 // used to track whether the message waiting indicator is visible, per subscription id.
106 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
107
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 /**
109 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700110 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 */
112 private NotificationMgr(PhoneGlobals app) {
113 mApp = app;
114 mContext = app;
115 mNotificationManager =
116 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
117 mStatusBarManager =
118 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700119 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800120 mPhone = app.mCM.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 statusBarHelper = new StatusBarHelper();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800122 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800123 mTelecomManager = TelecomManager.from(mContext);
124 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700125
126 final String notificationComponent = mContext.getString(
127 R.string.config_customVoicemailComponent);
128
129 mNotificationComponent = notificationComponent != null
130 ? ComponentName.unflattenFromString(notificationComponent) : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131 }
132
133 /**
134 * Initialize the singleton NotificationMgr instance.
135 *
136 * This is only done once, at startup, from PhoneApp.onCreate().
137 * From then on, the NotificationMgr instance is available via the
138 * PhoneApp's public "notificationMgr" field, which is why there's no
139 * getInstance() method here.
140 */
141 /* package */ static NotificationMgr init(PhoneGlobals app) {
142 synchronized (NotificationMgr.class) {
143 if (sInstance == null) {
144 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145 } else {
146 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
147 }
148 return sInstance;
149 }
150 }
151
152 /**
153 * Helper class that's a wrapper around the framework's
154 * StatusBarManager.disable() API.
155 *
156 * This class is used to control features like:
157 *
158 * - Disabling the status bar "notification windowshade"
159 * while the in-call UI is up
160 *
161 * - Disabling notification alerts (audible or vibrating)
162 * while a phone call is active
163 *
164 * - Disabling navigation via the system bar (the "soft buttons" at
165 * the bottom of the screen on devices with no hard buttons)
166 *
167 * We control these features through a single point of control to make
168 * sure that the various StatusBarManager.disable() calls don't
169 * interfere with each other.
170 */
171 public class StatusBarHelper {
172 // Current desired state of status bar / system bar behavior
173 private boolean mIsNotificationEnabled = true;
174 private boolean mIsExpandedViewEnabled = true;
175 private boolean mIsSystemBarNavigationEnabled = true;
176
Andrew Leed5165b02014-12-05 15:53:58 -0800177 private StatusBarHelper() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178 }
179
180 /**
181 * Enables or disables auditory / vibrational alerts.
182 *
183 * (We disable these any time a voice call is active, regardless
184 * of whether or not the in-call UI is visible.)
185 */
186 public void enableNotificationAlerts(boolean enable) {
187 if (mIsNotificationEnabled != enable) {
188 mIsNotificationEnabled = enable;
189 updateStatusBar();
190 }
191 }
192
193 /**
194 * Enables or disables the expanded view of the status bar
195 * (i.e. the ability to pull down the "notification windowshade").
196 *
197 * (This feature is disabled by the InCallScreen while the in-call
198 * UI is active.)
199 */
200 public void enableExpandedView(boolean enable) {
201 if (mIsExpandedViewEnabled != enable) {
202 mIsExpandedViewEnabled = enable;
203 updateStatusBar();
204 }
205 }
206
207 /**
208 * Enables or disables the navigation via the system bar (the
209 * "soft buttons" at the bottom of the screen)
210 *
211 * (This feature is disabled while an incoming call is ringing,
212 * because it's easy to accidentally touch the system bar while
213 * pulling the phone out of your pocket.)
214 */
215 public void enableSystemBarNavigation(boolean enable) {
216 if (mIsSystemBarNavigationEnabled != enable) {
217 mIsSystemBarNavigationEnabled = enable;
218 updateStatusBar();
219 }
220 }
221
222 /**
223 * Updates the status bar to reflect the current desired state.
224 */
225 private void updateStatusBar() {
226 int state = StatusBarManager.DISABLE_NONE;
227
228 if (!mIsExpandedViewEnabled) {
229 state |= StatusBarManager.DISABLE_EXPAND;
230 }
231 if (!mIsNotificationEnabled) {
232 state |= StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
233 }
234 if (!mIsSystemBarNavigationEnabled) {
235 // Disable *all* possible navigation via the system bar.
236 state |= StatusBarManager.DISABLE_HOME;
237 state |= StatusBarManager.DISABLE_RECENT;
238 state |= StatusBarManager.DISABLE_BACK;
Christine Chenb685f172013-09-25 18:32:59 -0700239 state |= StatusBarManager.DISABLE_SEARCH;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 }
241
242 if (DBG) log("updateStatusBar: state = 0x" + Integer.toHexString(state));
243 mStatusBarManager.disable(state);
244 }
245 }
246
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 /** The projection to use when querying the phones table */
248 static final String[] PHONES_PROJECTION = new String[] {
249 PhoneLookup.NUMBER,
250 PhoneLookup.DISPLAY_NAME,
251 PhoneLookup._ID
252 };
253
254 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800255 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
256 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
257 * settings screen. The voicemail notification sound is suppressed.
258 *
259 * @param subId The subscription Id.
260 */
261 /* package */ void refreshMwi(int subId) {
262 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
263 // reference the single subid stored in the mMwiVisible map.
264 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
265 if (mMwiVisible.keySet().size() == 1) {
266 Set<Integer> keySet = mMwiVisible.keySet();
267 Iterator<Integer> keyIt = keySet.iterator();
268 if (!keyIt.hasNext()) {
269 return;
270 }
271 subId = keyIt.next();
272 }
273 }
274 if (mMwiVisible.containsKey(subId)) {
275 boolean mwiVisible = mMwiVisible.get(subId);
276 if (mwiVisible) {
277 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
278 }
279 }
280 }
281
282 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700283 * Updates the message waiting indicator (voicemail) notification.
284 *
285 * @param visible true if there are messages waiting
286 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800288 updateMwi(subId, visible, true /* enableNotificationSound */);
289 }
290
291 /**
292 * Updates the message waiting indicator (voicemail) notification.
293 *
294 * @param subId the subId to update.
295 * @param visible true if there are messages waiting
296 * @param enableNotificationSound {@code true} if the notification sound should be played.
297 */
298 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800299 if (!PhoneGlobals.sVoiceCapable) {
300 // Do not show the message waiting indicator on devices which are not voice capable.
301 // These events *should* be blocked at the telephony layer for such devices.
302 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
303 return;
304 }
305
Nancy Chen2cf7f292015-05-15 11:00:10 -0700306 Phone phone = PhoneGlobals.getPhone(subId);
307 if (visible && phone != null) {
308 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
309 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
310 if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
311 Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
312 visible = false;
313 }
314 }
315
Yorke Lee67a62a22014-12-15 18:46:17 -0800316 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800317 mMwiVisible.put(subId, visible);
318
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800320 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800321 Log.w(LOG_TAG, "Found null phone for: " + subId);
322 return;
323 }
324
325 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
326 if (subInfo == null) {
327 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800328 return;
329 }
330
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 int resId = android.R.drawable.stat_notify_voicemail;
332
333 // This Notification can get a lot fancier once we have more
334 // information about the current voicemail messages.
335 // (For example, the current voicemail system can't tell
336 // us the caller-id or timestamp of a message, or tell us the
337 // message count.)
338
339 // But for now, the UI is ultra-simple: if the MWI indication
340 // is supposed to be visible, just show a single generic
341 // notification.
342
343 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800344 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 if (DBG) log("- got vm number: '" + vmNumber + "'");
346
Andrew Leea82b8202014-11-21 16:18:28 -0800347 // The voicemail number may be null because:
348 // (1) This phone has no voicemail number.
349 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
350 // happen when the device first boots if we get a MWI notification when we
351 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800352 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
353 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700354 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800355 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 }
357
Bryce Lee5dc90842015-08-11 07:57:14 -0700358 Integer vmCount = null;
359
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800360 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700361 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
363 notificationTitle = String.format(titleFormat, vmCount);
364 }
365
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800366 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
367 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
368
369 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700371 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
372
373 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800374 notificationText = mContext.getString(
375 R.string.notification_voicemail_no_vm_number);
376
377 // If the voicemail number if unknown, instead of calling voicemail, take the user
378 // to the voicemail settings.
379 notificationText = mContext.getString(
380 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700381 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800382 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700383 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800385 if (mTelephonyManager.getPhoneCount() > 1) {
386 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800387 } else {
388 notificationText = String.format(
389 mContext.getString(R.string.notification_voicemail_text_format),
390 PhoneNumberUtils.formatNumber(vmNumber));
391 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800392 intent = new Intent(
393 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700394 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800395 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 }
397
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800398 PendingIntent pendingIntent =
399 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800400 Uri ringtoneUri = null;
401
402 if (enableNotificationSound) {
403 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
404 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405
Nancy Chenb4a92702014-12-04 15:57:29 -0800406 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700407 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700408 mPhone.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 Notification.Builder builder = new Notification.Builder(mContext);
410 builder.setSmallIcon(resId)
411 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800412 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413 .setContentTitle(notificationTitle)
414 .setContentText(notificationText)
415 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700416 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800417 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700418 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700419 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800421 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700422 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700423 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700424
425 final Notification notification = builder.build();
426 List<UserInfo> users = mUserManager.getUsers(true);
427 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700428 final UserInfo user = users.get(i);
429 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700430 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700431 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700432 && !user.isManagedProfile()) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700433 if (!sendNotificationCustomComponent(vmCount, vmNumber, pendingIntent,
434 isSettingsIntent)) {
435 mNotificationManager.notifyAsUser(
436 Integer.toString(subId) /* tag */,
437 VOICEMAIL_NOTIFICATION,
438 notification,
439 userHandle);
440 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700441 }
442 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700443 } else {
Bryce Lee5dc90842015-08-11 07:57:14 -0700444 if (!sendNotificationCustomComponent(0, null, null, false)) {
445 mNotificationManager.cancelAsUser(
446 Integer.toString(subId) /* tag */,
447 VOICEMAIL_NOTIFICATION,
448 UserHandle.ALL);
449 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700450 }
451 }
452
453 /**
Bryce Lee5dc90842015-08-11 07:57:14 -0700454 * Sends a broadcast with the voicemail notification information to a custom component to
455 * handle. This method is also used to indicate to the custom component when to clear the
456 * notification. A pending intent can be passed to the custom component to indicate an action to
457 * be taken as it would by a notification produced in this class.
458 * @param count The number of pending voicemail messages to indicate on the notification. A
459 * Value of 0 is passed here to indicate that the notification should be cleared.
460 * @param number The voicemail phone number if specified.
461 * @param pendingIntent The intent that should be passed as the action to be taken.
462 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
463 * otherwise, {@code false} to indicate the intent launches voicemail.
464 * @return {@code true} if a custom component was notified of the notification.
465 */
466 private boolean sendNotificationCustomComponent(Integer count, String number,
467 PendingIntent pendingIntent, boolean isSettingsIntent) {
468 if (mNotificationComponent != null) {
469 Intent intent = new Intent();
470 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
471 intent.setComponent(mNotificationComponent);
472 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
473
474 if (count != null) {
475 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
476 }
477
478 // Additional information about the voicemail notification beyond the count is only
479 // present when the count not specified or greater than 0. The value of 0 represents
480 // clearing the notification, which does not require additional information.
481 if (count == null || count > 0) {
482 if (!TextUtils.isEmpty(number)) {
483 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
484 }
485
486 if (pendingIntent != null) {
487 intent.putExtra(isSettingsIntent
488 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
489 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
490 pendingIntent);
491 }
492 }
493
494 mContext.sendBroadcast(intent);
495 return true;
496 }
497
498 return false;
499 }
500
501 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 * Updates the message call forwarding indicator notification.
503 *
504 * @param visible true if there are messages waiting
505 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800506 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507 if (DBG) log("updateCfi(): " + visible);
508 if (visible) {
509 // If Unconditional Call Forwarding (forward all calls) for VOICE
510 // is enabled, just show a notification. We'll default to expanded
511 // view for now, so the there is less confusion about the icon. If
512 // it is deemed too weird to have CF indications as expanded views,
513 // then we'll flip the flag back.
514
515 // TODO: We may want to take a look to see if the notification can
516 // display the target to forward calls to. This will require some
517 // effort though, since there are multiple layers of messages that
518 // will need to propagate that information.
519
Andrew Leed5165b02014-12-05 15:53:58 -0800520 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
521 if (subInfo == null) {
522 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
523 return;
524 }
525
526 String notificationTitle;
527 if (mTelephonyManager.getPhoneCount() > 1) {
528 notificationTitle = subInfo.getDisplayName().toString();
529 } else {
530 notificationTitle = mContext.getString(R.string.labelCF);
531 }
532
Andrew Lee99d0ac22014-10-10 13:18:04 -0700533 Notification.Builder builder = new Notification.Builder(mContext)
534 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800535 .setColor(subInfo.getIconTint())
536 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700537 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
538 .setShowWhen(false)
539 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540
Andrew Lee99d0ac22014-10-10 13:18:04 -0700541 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800542 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700543 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800544 SubscriptionInfoHelper.addExtrasToIntent(
545 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
546 PendingIntent contentIntent =
547 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700548
549 List<UserInfo> users = mUserManager.getUsers(true);
550 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800551 final UserInfo user = users.get(i);
552 if (user.isManagedProfile()) {
553 continue;
554 }
555 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700556 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800557 mNotificationManager.notifyAsUser(
558 Integer.toString(subId) /* tag */,
559 CALL_FORWARD_NOTIFICATION,
560 builder.build(),
561 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700564 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800565 Integer.toString(subId) /* tag */,
566 CALL_FORWARD_NOTIFICATION,
567 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 }
569 }
570
571 /**
572 * Shows the "data disconnected due to roaming" notification, which
573 * appears when you lose data connectivity because you're roaming and
574 * you have the "data roaming" feature turned off.
575 */
576 /* package */ void showDataDisconnectedRoaming() {
577 if (DBG) log("showDataDisconnectedRoaming()...");
578
579 // "Mobile network settings" screen / dialog
580 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700581 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582
583 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
584
Andrew Lee99d0ac22014-10-10 13:18:04 -0700585 final Notification.Builder builder = new Notification.Builder(mContext)
586 .setSmallIcon(android.R.drawable.stat_sys_warning)
587 .setContentTitle(mContext.getText(R.string.roaming))
588 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
589 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590
Andrew Lee99d0ac22014-10-10 13:18:04 -0700591 List<UserInfo> users = mUserManager.getUsers(true);
592 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800593 final UserInfo user = users.get(i);
594 if (user.isManagedProfile()) {
595 continue;
596 }
597 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700598 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700599 final Notification notif =
600 new Notification.BigTextStyle(builder).bigText(contentText).build();
601 mNotificationManager.notifyAsUser(
602 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
603 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700604 }
605
606 /**
607 * Turns off the "data disconnected due to roaming" notification.
608 */
609 /* package */ void hideDataDisconnectedRoaming() {
610 if (DBG) log("hideDataDisconnectedRoaming()...");
611 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
612 }
613
614 /**
615 * Display the network selection "no service" notification
616 * @param operator is the numeric operator number
617 */
618 private void showNetworkSelection(String operator) {
619 if (DBG) log("showNetworkSelection(" + operator + ")...");
620
Andrew Lee99d0ac22014-10-10 13:18:04 -0700621 Notification.Builder builder = new Notification.Builder(mContext)
622 .setSmallIcon(android.R.drawable.stat_sys_warning)
623 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
624 .setContentText(
625 mContext.getString(R.string.notification_network_selection_text, operator))
626 .setShowWhen(false)
627 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700628
629 // create the target network operators settings intent
630 Intent intent = new Intent(Intent.ACTION_MAIN);
631 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
632 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
633 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700634 intent.setComponent(new ComponentName(
635 mContext.getString(R.string.network_operator_settings_package),
636 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700637 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700638 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639
Andrew Lee99d0ac22014-10-10 13:18:04 -0700640 List<UserInfo> users = mUserManager.getUsers(true);
641 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800642 final UserInfo user = users.get(i);
643 if (user.isManagedProfile()) {
644 continue;
645 }
646 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700647 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700648 mNotificationManager.notifyAsUser(
649 null /* tag */,
650 SELECTED_OPERATOR_FAIL_NOTIFICATION,
651 builder.build(),
652 userHandle);
653 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 }
655
656 /**
657 * Turn off the network selection "no service" notification
658 */
659 private void cancelNetworkSelection() {
660 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700661 mNotificationManager.cancelAsUser(
662 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664
665 /**
666 * Update notification about no service of user selected operator
667 *
668 * @param serviceState Phone service state
669 */
670 void updateNetworkSelection(int serviceState) {
671 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800672 int subId = mPhone.getSubId();
673 if (SubscriptionManager.isValidSubscriptionId(subId)) {
674 // get the shared preference of network_selection.
675 // empty is auto mode, otherwise it is the operator alpha name
676 // in case there is no operator name, check the operator numeric
677 SharedPreferences sp =
678 PreferenceManager.getDefaultSharedPreferences(mContext);
679 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800680 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800681 if (TextUtils.isEmpty(networkSelection)) {
682 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800683 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800684 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685
Amit Mahajana60be872015-01-15 16:05:08 -0800686 if (DBG) log("updateNetworkSelection()..." + "state = " +
687 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700688
Amit Mahajana60be872015-01-15 16:05:08 -0800689 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
690 && !TextUtils.isEmpty(networkSelection)) {
691 if (!mSelectedUnavailableNotify) {
692 showNetworkSelection(networkSelection);
693 mSelectedUnavailableNotify = true;
694 }
695 } else {
696 if (mSelectedUnavailableNotify) {
697 cancelNetworkSelection();
698 mSelectedUnavailableNotify = false;
699 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 }
701 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800702 if (DBG) log("updateNetworkSelection()..." + "state = " +
703 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700704 }
705 }
706 }
707
708 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
709 if (mToast != null) {
710 mToast.cancel();
711 }
712
713 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
714 mToast.show();
715 }
716
717 private void log(String msg) {
718 Log.d(LOG_TAG, msg);
719 }
720}