blob: 3b27a4d41485bb0f10897a272304bdd3b96b608d [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;
36import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070037import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080038import android.telecom.PhoneAccountHandle;
39import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070040import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.telephony.PhoneNumberUtils;
42import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080043import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080044import android.telephony.SubscriptionManager;
yifan.baie3e5d562016-04-20 16:06:58 +080045import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Andrew Leed5165b02014-12-05 15:53:58 -080046import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080048import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.widget.Toast;
51
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import com.android.internal.telephony.Phone;
53import com.android.internal.telephony.PhoneBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import com.android.internal.telephony.TelephonyCapabilities;
Andrew Leebf07f762015-04-07 19:05:50 -070055import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070056import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
Andrew Lee8d66d812014-11-24 14:54:02 -080057import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080058import com.android.phone.settings.VoicemailProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059
Tyler Gunn9c1071f2014-12-09 10:07:54 -080060import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070061import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080062import java.util.Map;
63import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070064
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065/**
66 * NotificationManager-related utility code for the Phone app.
67 *
68 * This is a singleton object which acts as the interface to the
69 * framework's NotificationManager, and is used to display status bar
70 * icons and control other status bar-related behavior.
71 *
72 * @see PhoneGlobals.notificationMgr
73 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070074public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080075 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 private static final boolean DBG =
77 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
78 // Do not check in with VDBG = true, since that may write PII to the system log.
79 private static final boolean VDBG = false;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070082 static final int MMI_NOTIFICATION = 1;
83 static final int NETWORK_SELECTION_NOTIFICATION = 2;
84 static final int VOICEMAIL_NOTIFICATION = 3;
85 static final int CALL_FORWARD_NOTIFICATION = 4;
86 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
87 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
89 /** The singleton NotificationMgr instance. */
90 private static NotificationMgr sInstance;
91
92 private PhoneGlobals mApp;
93 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094
95 private Context mContext;
96 private NotificationManager mNotificationManager;
Bryce Lee5dc90842015-08-11 07:57:14 -070097 private final ComponentName mNotificationComponent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070099 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800101 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800102 private TelecomManager mTelecomManager;
103 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104
105 public StatusBarHelper statusBarHelper;
106
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // used to track the notification of selected network unavailable
108 private boolean mSelectedUnavailableNotify = false;
109
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800110 // used to track whether the message waiting indicator is visible, per subscription id.
111 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
112
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113 /**
114 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700115 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 */
117 private NotificationMgr(PhoneGlobals app) {
118 mApp = app;
119 mContext = app;
120 mNotificationManager =
121 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
122 mStatusBarManager =
123 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700124 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800125 mPhone = app.mCM.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 statusBarHelper = new StatusBarHelper();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800127 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800128 mTelecomManager = TelecomManager.from(mContext);
129 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700130
131 final String notificationComponent = mContext.getString(
132 R.string.config_customVoicemailComponent);
133
134 mNotificationComponent = notificationComponent != null
135 ? ComponentName.unflattenFromString(notificationComponent) : null;
yifan.baie3e5d562016-04-20 16:06:58 +0800136
137 mSubscriptionManager.addOnSubscriptionsChangedListener(
138 new OnSubscriptionsChangedListener() {
139 @Override
140 public void onSubscriptionsChanged() {
141 updateActivePhonesMwi();
142 }
143 });
144 }
145
146 public void updateActivePhonesMwi() {
147 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
148
149 if (subInfos == null) {
150 return;
151 }
152
153 for (int i = 0; i < subInfos.size(); i++) {
154 int subId = subInfos.get(i).getSubscriptionId();
155 refreshMwi(subId);
156 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 }
158
159 /**
160 * Initialize the singleton NotificationMgr instance.
161 *
162 * This is only done once, at startup, from PhoneApp.onCreate().
163 * From then on, the NotificationMgr instance is available via the
164 * PhoneApp's public "notificationMgr" field, which is why there's no
165 * getInstance() method here.
166 */
167 /* package */ static NotificationMgr init(PhoneGlobals app) {
168 synchronized (NotificationMgr.class) {
169 if (sInstance == null) {
170 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 } else {
172 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
173 }
174 return sInstance;
175 }
176 }
177
178 /**
179 * Helper class that's a wrapper around the framework's
180 * StatusBarManager.disable() API.
181 *
182 * This class is used to control features like:
183 *
184 * - Disabling the status bar "notification windowshade"
185 * while the in-call UI is up
186 *
187 * - Disabling notification alerts (audible or vibrating)
188 * while a phone call is active
189 *
190 * - Disabling navigation via the system bar (the "soft buttons" at
191 * the bottom of the screen on devices with no hard buttons)
192 *
193 * We control these features through a single point of control to make
194 * sure that the various StatusBarManager.disable() calls don't
195 * interfere with each other.
196 */
197 public class StatusBarHelper {
198 // Current desired state of status bar / system bar behavior
199 private boolean mIsNotificationEnabled = true;
200 private boolean mIsExpandedViewEnabled = true;
201 private boolean mIsSystemBarNavigationEnabled = true;
202
Andrew Leed5165b02014-12-05 15:53:58 -0800203 private StatusBarHelper() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 }
205
206 /**
207 * Enables or disables auditory / vibrational alerts.
208 *
209 * (We disable these any time a voice call is active, regardless
210 * of whether or not the in-call UI is visible.)
211 */
212 public void enableNotificationAlerts(boolean enable) {
213 if (mIsNotificationEnabled != enable) {
214 mIsNotificationEnabled = enable;
215 updateStatusBar();
216 }
217 }
218
219 /**
220 * Enables or disables the expanded view of the status bar
221 * (i.e. the ability to pull down the "notification windowshade").
222 *
223 * (This feature is disabled by the InCallScreen while the in-call
224 * UI is active.)
225 */
226 public void enableExpandedView(boolean enable) {
227 if (mIsExpandedViewEnabled != enable) {
228 mIsExpandedViewEnabled = enable;
229 updateStatusBar();
230 }
231 }
232
233 /**
234 * Enables or disables the navigation via the system bar (the
235 * "soft buttons" at the bottom of the screen)
236 *
237 * (This feature is disabled while an incoming call is ringing,
238 * because it's easy to accidentally touch the system bar while
239 * pulling the phone out of your pocket.)
240 */
241 public void enableSystemBarNavigation(boolean enable) {
242 if (mIsSystemBarNavigationEnabled != enable) {
243 mIsSystemBarNavigationEnabled = enable;
244 updateStatusBar();
245 }
246 }
247
248 /**
249 * Updates the status bar to reflect the current desired state.
250 */
251 private void updateStatusBar() {
252 int state = StatusBarManager.DISABLE_NONE;
253
254 if (!mIsExpandedViewEnabled) {
255 state |= StatusBarManager.DISABLE_EXPAND;
256 }
257 if (!mIsNotificationEnabled) {
258 state |= StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
259 }
260 if (!mIsSystemBarNavigationEnabled) {
261 // Disable *all* possible navigation via the system bar.
262 state |= StatusBarManager.DISABLE_HOME;
263 state |= StatusBarManager.DISABLE_RECENT;
264 state |= StatusBarManager.DISABLE_BACK;
Christine Chenb685f172013-09-25 18:32:59 -0700265 state |= StatusBarManager.DISABLE_SEARCH;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 }
267
268 if (DBG) log("updateStatusBar: state = 0x" + Integer.toHexString(state));
269 mStatusBarManager.disable(state);
270 }
271 }
272
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 /** The projection to use when querying the phones table */
274 static final String[] PHONES_PROJECTION = new String[] {
275 PhoneLookup.NUMBER,
276 PhoneLookup.DISPLAY_NAME,
277 PhoneLookup._ID
278 };
279
280 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800281 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
282 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
283 * settings screen. The voicemail notification sound is suppressed.
284 *
285 * @param subId The subscription Id.
286 */
287 /* package */ void refreshMwi(int subId) {
288 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
289 // reference the single subid stored in the mMwiVisible map.
290 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
291 if (mMwiVisible.keySet().size() == 1) {
292 Set<Integer> keySet = mMwiVisible.keySet();
293 Iterator<Integer> keyIt = keySet.iterator();
294 if (!keyIt.hasNext()) {
295 return;
296 }
297 subId = keyIt.next();
298 }
299 }
300 if (mMwiVisible.containsKey(subId)) {
301 boolean mwiVisible = mMwiVisible.get(subId);
302 if (mwiVisible) {
303 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
304 }
305 }
306 }
307
308 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 * Updates the message waiting indicator (voicemail) notification.
310 *
311 * @param visible true if there are messages waiting
312 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800313 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800314 updateMwi(subId, visible, true /* enableNotificationSound */);
315 }
316
317 /**
318 * Updates the message waiting indicator (voicemail) notification.
319 *
320 * @param subId the subId to update.
321 * @param visible true if there are messages waiting
322 * @param enableNotificationSound {@code true} if the notification sound should be played.
323 */
324 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800325 if (!PhoneGlobals.sVoiceCapable) {
326 // Do not show the message waiting indicator on devices which are not voice capable.
327 // These events *should* be blocked at the telephony layer for such devices.
328 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
329 return;
330 }
331
Nancy Chen2cf7f292015-05-15 11:00:10 -0700332 Phone phone = PhoneGlobals.getPhone(subId);
333 if (visible && phone != null) {
334 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
335 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
336 if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
337 Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
338 visible = false;
339 }
340 }
341
Yorke Lee67a62a22014-12-15 18:46:17 -0800342 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800343 mMwiVisible.put(subId, visible);
344
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800346 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800347 Log.w(LOG_TAG, "Found null phone for: " + subId);
348 return;
349 }
350
351 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
352 if (subInfo == null) {
353 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800354 return;
355 }
356
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 int resId = android.R.drawable.stat_notify_voicemail;
358
359 // This Notification can get a lot fancier once we have more
360 // information about the current voicemail messages.
361 // (For example, the current voicemail system can't tell
362 // us the caller-id or timestamp of a message, or tell us the
363 // message count.)
364
365 // But for now, the UI is ultra-simple: if the MWI indication
366 // is supposed to be visible, just show a single generic
367 // notification.
368
369 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800370 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700371 if (DBG) log("- got vm number: '" + vmNumber + "'");
372
Andrew Leea82b8202014-11-21 16:18:28 -0800373 // The voicemail number may be null because:
374 // (1) This phone has no voicemail number.
375 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
376 // happen when the device first boots if we get a MWI notification when we
377 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800378 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
379 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800381 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 }
383
Bryce Lee5dc90842015-08-11 07:57:14 -0700384 Integer vmCount = null;
385
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800386 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700387 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
389 notificationTitle = String.format(titleFormat, vmCount);
390 }
391
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800392 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
393 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
394
395 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700396 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700397 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
398
399 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800400 notificationText = mContext.getString(
401 R.string.notification_voicemail_no_vm_number);
402
403 // If the voicemail number if unknown, instead of calling voicemail, take the user
404 // to the voicemail settings.
405 notificationText = mContext.getString(
406 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700407 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800408 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700409 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800411 if (mTelephonyManager.getPhoneCount() > 1) {
412 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800413 } else {
414 notificationText = String.format(
415 mContext.getString(R.string.notification_voicemail_text_format),
416 PhoneNumberUtils.formatNumber(vmNumber));
417 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800418 intent = new Intent(
419 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700420 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800421 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700422 }
423
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800424 PendingIntent pendingIntent =
425 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800426 Uri ringtoneUri = null;
427
428 if (enableNotificationSound) {
yifan.bai2f36d002016-04-20 10:45:22 +0800429 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800430 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700431
Nancy Chenb4a92702014-12-04 15:57:29 -0800432 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700433 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
yifan.bai2f36d002016-04-20 10:45:22 +0800434 subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700435 Notification.Builder builder = new Notification.Builder(mContext);
436 builder.setSmallIcon(resId)
437 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800438 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 .setContentTitle(notificationTitle)
440 .setContentText(notificationText)
441 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700442 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800443 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700444 .setOngoing(carrierConfig.getBoolean(
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700445 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800447 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700448 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700449 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700450
451 final Notification notification = builder.build();
452 List<UserInfo> users = mUserManager.getUsers(true);
453 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700454 final UserInfo user = users.get(i);
455 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700456 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700457 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700458 && !user.isManagedProfile()) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700459 if (!sendNotificationCustomComponent(vmCount, vmNumber, pendingIntent,
460 isSettingsIntent)) {
461 mNotificationManager.notifyAsUser(
462 Integer.toString(subId) /* tag */,
463 VOICEMAIL_NOTIFICATION,
464 notification,
465 userHandle);
466 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700467 }
468 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700469 } else {
Bryce Lee5dc90842015-08-11 07:57:14 -0700470 if (!sendNotificationCustomComponent(0, null, null, false)) {
471 mNotificationManager.cancelAsUser(
472 Integer.toString(subId) /* tag */,
473 VOICEMAIL_NOTIFICATION,
474 UserHandle.ALL);
475 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700476 }
477 }
478
479 /**
Bryce Lee5dc90842015-08-11 07:57:14 -0700480 * Sends a broadcast with the voicemail notification information to a custom component to
481 * handle. This method is also used to indicate to the custom component when to clear the
482 * notification. A pending intent can be passed to the custom component to indicate an action to
483 * be taken as it would by a notification produced in this class.
484 * @param count The number of pending voicemail messages to indicate on the notification. A
485 * Value of 0 is passed here to indicate that the notification should be cleared.
486 * @param number The voicemail phone number if specified.
487 * @param pendingIntent The intent that should be passed as the action to be taken.
488 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
489 * otherwise, {@code false} to indicate the intent launches voicemail.
490 * @return {@code true} if a custom component was notified of the notification.
491 */
492 private boolean sendNotificationCustomComponent(Integer count, String number,
493 PendingIntent pendingIntent, boolean isSettingsIntent) {
494 if (mNotificationComponent != null) {
495 Intent intent = new Intent();
496 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
497 intent.setComponent(mNotificationComponent);
498 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
499
500 if (count != null) {
501 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
502 }
503
504 // Additional information about the voicemail notification beyond the count is only
505 // present when the count not specified or greater than 0. The value of 0 represents
506 // clearing the notification, which does not require additional information.
507 if (count == null || count > 0) {
508 if (!TextUtils.isEmpty(number)) {
509 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
510 }
511
512 if (pendingIntent != null) {
513 intent.putExtra(isSettingsIntent
514 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
515 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
516 pendingIntent);
517 }
518 }
519
520 mContext.sendBroadcast(intent);
521 return true;
522 }
523
524 return false;
525 }
526
527 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700528 * Updates the message call forwarding indicator notification.
529 *
530 * @param visible true if there are messages waiting
531 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800532 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 if (DBG) log("updateCfi(): " + visible);
534 if (visible) {
535 // If Unconditional Call Forwarding (forward all calls) for VOICE
536 // is enabled, just show a notification. We'll default to expanded
537 // view for now, so the there is less confusion about the icon. If
538 // it is deemed too weird to have CF indications as expanded views,
539 // then we'll flip the flag back.
540
541 // TODO: We may want to take a look to see if the notification can
542 // display the target to forward calls to. This will require some
543 // effort though, since there are multiple layers of messages that
544 // will need to propagate that information.
545
Andrew Leed5165b02014-12-05 15:53:58 -0800546 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
547 if (subInfo == null) {
548 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
549 return;
550 }
551
552 String notificationTitle;
553 if (mTelephonyManager.getPhoneCount() > 1) {
554 notificationTitle = subInfo.getDisplayName().toString();
555 } else {
556 notificationTitle = mContext.getString(R.string.labelCF);
557 }
558
Andrew Lee99d0ac22014-10-10 13:18:04 -0700559 Notification.Builder builder = new Notification.Builder(mContext)
560 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800561 .setColor(subInfo.getIconTint())
562 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700563 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
564 .setShowWhen(false)
565 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566
Andrew Lee99d0ac22014-10-10 13:18:04 -0700567 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800568 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700569 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800570 SubscriptionInfoHelper.addExtrasToIntent(
571 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
572 PendingIntent contentIntent =
573 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700574
575 List<UserInfo> users = mUserManager.getUsers(true);
576 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800577 final UserInfo user = users.get(i);
578 if (user.isManagedProfile()) {
579 continue;
580 }
581 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700582 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800583 mNotificationManager.notifyAsUser(
584 Integer.toString(subId) /* tag */,
585 CALL_FORWARD_NOTIFICATION,
586 builder.build(),
587 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700590 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800591 Integer.toString(subId) /* tag */,
592 CALL_FORWARD_NOTIFICATION,
593 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 }
595 }
596
597 /**
598 * Shows the "data disconnected due to roaming" notification, which
599 * appears when you lose data connectivity because you're roaming and
600 * you have the "data roaming" feature turned off.
601 */
602 /* package */ void showDataDisconnectedRoaming() {
603 if (DBG) log("showDataDisconnectedRoaming()...");
604
605 // "Mobile network settings" screen / dialog
606 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700607 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608
609 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
610
Andrew Lee99d0ac22014-10-10 13:18:04 -0700611 final Notification.Builder builder = new Notification.Builder(mContext)
612 .setSmallIcon(android.R.drawable.stat_sys_warning)
613 .setContentTitle(mContext.getText(R.string.roaming))
614 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
615 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616
Andrew Lee99d0ac22014-10-10 13:18:04 -0700617 List<UserInfo> users = mUserManager.getUsers(true);
618 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800619 final UserInfo user = users.get(i);
620 if (user.isManagedProfile()) {
621 continue;
622 }
623 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700624 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
625 final Notification notif =
626 new Notification.BigTextStyle(builder).bigText(contentText).build();
627 mNotificationManager.notifyAsUser(
628 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
629 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 }
631
632 /**
633 * Turns off the "data disconnected due to roaming" notification.
634 */
635 /* package */ void hideDataDisconnectedRoaming() {
636 if (DBG) log("hideDataDisconnectedRoaming()...");
637 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
638 }
639
640 /**
641 * Display the network selection "no service" notification
642 * @param operator is the numeric operator number
643 */
644 private void showNetworkSelection(String operator) {
645 if (DBG) log("showNetworkSelection(" + operator + ")...");
646
Andrew Lee99d0ac22014-10-10 13:18:04 -0700647 Notification.Builder builder = new Notification.Builder(mContext)
648 .setSmallIcon(android.R.drawable.stat_sys_warning)
649 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
650 .setContentText(
651 mContext.getString(R.string.notification_network_selection_text, operator))
652 .setShowWhen(false)
653 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654
655 // create the target network operators settings intent
656 Intent intent = new Intent(Intent.ACTION_MAIN);
657 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
658 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
659 // Use NetworkSetting to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700660 intent.setComponent(new ComponentName(
661 mContext.getString(R.string.network_operator_settings_package),
662 mContext.getString(R.string.network_operator_settings_class)));
Sanket Padawe3e1073d2015-07-15 18:28:12 -0700663 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, mPhone.getSubId());
Andrew Lee99d0ac22014-10-10 13:18:04 -0700664 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665
Andrew Lee99d0ac22014-10-10 13:18:04 -0700666 List<UserInfo> users = mUserManager.getUsers(true);
667 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800668 final UserInfo user = users.get(i);
669 if (user.isManagedProfile()) {
670 continue;
671 }
672 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700673 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
674 mNotificationManager.notifyAsUser(
675 null /* tag */,
676 SELECTED_OPERATOR_FAIL_NOTIFICATION,
677 builder.build(),
678 userHandle);
679 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 }
681
682 /**
683 * Turn off the network selection "no service" notification
684 */
685 private void cancelNetworkSelection() {
686 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700687 mNotificationManager.cancelAsUser(
688 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700689 }
690
691 /**
692 * Update notification about no service of user selected operator
693 *
694 * @param serviceState Phone service state
695 */
696 void updateNetworkSelection(int serviceState) {
697 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800698 int subId = mPhone.getSubId();
699 if (SubscriptionManager.isValidSubscriptionId(subId)) {
700 // get the shared preference of network_selection.
701 // empty is auto mode, otherwise it is the operator alpha name
702 // in case there is no operator name, check the operator numeric
703 SharedPreferences sp =
704 PreferenceManager.getDefaultSharedPreferences(mContext);
705 String networkSelection =
706 sp.getString(PhoneBase.NETWORK_SELECTION_NAME_KEY + subId, "");
707 if (TextUtils.isEmpty(networkSelection)) {
708 networkSelection =
709 sp.getString(PhoneBase.NETWORK_SELECTION_KEY + subId, "");
710 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711
Amit Mahajana60be872015-01-15 16:05:08 -0800712 if (DBG) log("updateNetworkSelection()..." + "state = " +
713 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700714
Amit Mahajana60be872015-01-15 16:05:08 -0800715 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
716 && !TextUtils.isEmpty(networkSelection)) {
717 if (!mSelectedUnavailableNotify) {
718 showNetworkSelection(networkSelection);
719 mSelectedUnavailableNotify = true;
720 }
721 } else {
722 if (mSelectedUnavailableNotify) {
723 cancelNetworkSelection();
724 mSelectedUnavailableNotify = false;
725 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 }
727 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800728 if (DBG) log("updateNetworkSelection()..." + "state = " +
729 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 }
731 }
732 }
733
734 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
735 if (mToast != null) {
736 mToast.cancel();
737 }
738
739 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
740 mToast.show();
741 }
742
743 private void log(String msg) {
744 Log.d(LOG_TAG, msg);
745 }
746}