blob: c5a9f127bb57f4e0b03cedac9b4ebcca2cdeb1f9 [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;
Andrew Leed5165b02014-12-05 15:53:58 -080045import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080047import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.widget.Toast;
50
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.Phone;
52import com.android.internal.telephony.PhoneBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.TelephonyCapabilities;
Andrew Leebf07f762015-04-07 19:05:50 -070054import com.android.phone.settings.VoicemailSettingsActivity;
Nancy Chen2cf7f292015-05-15 11:00:10 -070055import com.android.phone.vvm.omtp.sync.VoicemailStatusQueryHelper;
Andrew Lee8d66d812014-11-24 14:54:02 -080056import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080057import com.android.phone.settings.VoicemailProviderSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058
Tyler Gunn9c1071f2014-12-09 10:07:54 -080059import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070060import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080061import java.util.Map;
62import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070063
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064/**
65 * NotificationManager-related utility code for the Phone app.
66 *
67 * This is a singleton object which acts as the interface to the
68 * framework's NotificationManager, and is used to display status bar
69 * icons and control other status bar-related behavior.
70 *
71 * @see PhoneGlobals.notificationMgr
72 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070073public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080074 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075 private static final boolean DBG =
76 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
77 // Do not check in with VDBG = true, since that may write PII to the system log.
78 private static final boolean VDBG = false;
79
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070081 static final int MMI_NOTIFICATION = 1;
82 static final int NETWORK_SELECTION_NOTIFICATION = 2;
83 static final int VOICEMAIL_NOTIFICATION = 3;
84 static final int CALL_FORWARD_NOTIFICATION = 4;
85 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
86 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
88 /** The singleton NotificationMgr instance. */
89 private static NotificationMgr sInstance;
90
91 private PhoneGlobals mApp;
92 private Phone mPhone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093
94 private Context mContext;
95 private NotificationManager mNotificationManager;
96 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -070097 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080099 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800100 private TelecomManager mTelecomManager;
101 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102
103 public StatusBarHelper statusBarHelper;
104
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 // used to track the notification of selected network unavailable
106 private boolean mSelectedUnavailableNotify = false;
107
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800108 // used to track whether the message waiting indicator is visible, per subscription id.
109 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
110
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 /**
112 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700113 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 */
115 private NotificationMgr(PhoneGlobals app) {
116 mApp = app;
117 mContext = app;
118 mNotificationManager =
119 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
120 mStatusBarManager =
121 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700122 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Stuart Scottdcf40a92014-12-09 10:45:01 -0800123 mPhone = app.mCM.getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 statusBarHelper = new StatusBarHelper();
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800126 mTelecomManager = TelecomManager.from(mContext);
127 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 }
129
130 /**
131 * Initialize the singleton NotificationMgr instance.
132 *
133 * This is only done once, at startup, from PhoneApp.onCreate().
134 * From then on, the NotificationMgr instance is available via the
135 * PhoneApp's public "notificationMgr" field, which is why there's no
136 * getInstance() method here.
137 */
138 /* package */ static NotificationMgr init(PhoneGlobals app) {
139 synchronized (NotificationMgr.class) {
140 if (sInstance == null) {
141 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 } else {
143 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
144 }
145 return sInstance;
146 }
147 }
148
149 /**
150 * Helper class that's a wrapper around the framework's
151 * StatusBarManager.disable() API.
152 *
153 * This class is used to control features like:
154 *
155 * - Disabling the status bar "notification windowshade"
156 * while the in-call UI is up
157 *
158 * - Disabling notification alerts (audible or vibrating)
159 * while a phone call is active
160 *
161 * - Disabling navigation via the system bar (the "soft buttons" at
162 * the bottom of the screen on devices with no hard buttons)
163 *
164 * We control these features through a single point of control to make
165 * sure that the various StatusBarManager.disable() calls don't
166 * interfere with each other.
167 */
168 public class StatusBarHelper {
169 // Current desired state of status bar / system bar behavior
170 private boolean mIsNotificationEnabled = true;
171 private boolean mIsExpandedViewEnabled = true;
172 private boolean mIsSystemBarNavigationEnabled = true;
173
Andrew Leed5165b02014-12-05 15:53:58 -0800174 private StatusBarHelper() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 }
176
177 /**
178 * Enables or disables auditory / vibrational alerts.
179 *
180 * (We disable these any time a voice call is active, regardless
181 * of whether or not the in-call UI is visible.)
182 */
183 public void enableNotificationAlerts(boolean enable) {
184 if (mIsNotificationEnabled != enable) {
185 mIsNotificationEnabled = enable;
186 updateStatusBar();
187 }
188 }
189
190 /**
191 * Enables or disables the expanded view of the status bar
192 * (i.e. the ability to pull down the "notification windowshade").
193 *
194 * (This feature is disabled by the InCallScreen while the in-call
195 * UI is active.)
196 */
197 public void enableExpandedView(boolean enable) {
198 if (mIsExpandedViewEnabled != enable) {
199 mIsExpandedViewEnabled = enable;
200 updateStatusBar();
201 }
202 }
203
204 /**
205 * Enables or disables the navigation via the system bar (the
206 * "soft buttons" at the bottom of the screen)
207 *
208 * (This feature is disabled while an incoming call is ringing,
209 * because it's easy to accidentally touch the system bar while
210 * pulling the phone out of your pocket.)
211 */
212 public void enableSystemBarNavigation(boolean enable) {
213 if (mIsSystemBarNavigationEnabled != enable) {
214 mIsSystemBarNavigationEnabled = enable;
215 updateStatusBar();
216 }
217 }
218
219 /**
220 * Updates the status bar to reflect the current desired state.
221 */
222 private void updateStatusBar() {
223 int state = StatusBarManager.DISABLE_NONE;
224
225 if (!mIsExpandedViewEnabled) {
226 state |= StatusBarManager.DISABLE_EXPAND;
227 }
228 if (!mIsNotificationEnabled) {
229 state |= StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
230 }
231 if (!mIsSystemBarNavigationEnabled) {
232 // Disable *all* possible navigation via the system bar.
233 state |= StatusBarManager.DISABLE_HOME;
234 state |= StatusBarManager.DISABLE_RECENT;
235 state |= StatusBarManager.DISABLE_BACK;
Christine Chenb685f172013-09-25 18:32:59 -0700236 state |= StatusBarManager.DISABLE_SEARCH;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 }
238
239 if (DBG) log("updateStatusBar: state = 0x" + Integer.toHexString(state));
240 mStatusBarManager.disable(state);
241 }
242 }
243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 /** The projection to use when querying the phones table */
245 static final String[] PHONES_PROJECTION = new String[] {
246 PhoneLookup.NUMBER,
247 PhoneLookup.DISPLAY_NAME,
248 PhoneLookup._ID
249 };
250
251 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800252 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
253 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
254 * settings screen. The voicemail notification sound is suppressed.
255 *
256 * @param subId The subscription Id.
257 */
258 /* package */ void refreshMwi(int subId) {
259 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
260 // reference the single subid stored in the mMwiVisible map.
261 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
262 if (mMwiVisible.keySet().size() == 1) {
263 Set<Integer> keySet = mMwiVisible.keySet();
264 Iterator<Integer> keyIt = keySet.iterator();
265 if (!keyIt.hasNext()) {
266 return;
267 }
268 subId = keyIt.next();
269 }
270 }
271 if (mMwiVisible.containsKey(subId)) {
272 boolean mwiVisible = mMwiVisible.get(subId);
273 if (mwiVisible) {
274 updateMwi(subId, mwiVisible, false /* enableNotificationSound */);
275 }
276 }
277 }
278
279 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 * Updates the message waiting indicator (voicemail) notification.
281 *
282 * @param visible true if there are messages waiting
283 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800284 /* package */ void updateMwi(int subId, boolean visible) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800285 updateMwi(subId, visible, true /* enableNotificationSound */);
286 }
287
288 /**
289 * Updates the message waiting indicator (voicemail) notification.
290 *
291 * @param subId the subId to update.
292 * @param visible true if there are messages waiting
293 * @param enableNotificationSound {@code true} if the notification sound should be played.
294 */
295 void updateMwi(int subId, boolean visible, boolean enableNotificationSound) {
Andrew Leea82b8202014-11-21 16:18:28 -0800296 if (!PhoneGlobals.sVoiceCapable) {
297 // Do not show the message waiting indicator on devices which are not voice capable.
298 // These events *should* be blocked at the telephony layer for such devices.
299 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
300 return;
301 }
302
Nancy Chen2cf7f292015-05-15 11:00:10 -0700303 Phone phone = PhoneGlobals.getPhone(subId);
304 if (visible && phone != null) {
305 VoicemailStatusQueryHelper queryHelper = new VoicemailStatusQueryHelper(mContext);
306 PhoneAccountHandle phoneAccount = PhoneUtils.makePstnPhoneAccountHandle(phone);
307 if (queryHelper.isNotificationsChannelActive(phoneAccount)) {
308 Log.v(LOG_TAG, "Notifications channel active for visual voicemail, hiding mwi.");
309 visible = false;
310 }
311 }
312
Yorke Lee67a62a22014-12-15 18:46:17 -0800313 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800314 mMwiVisible.put(subId, visible);
315
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800317 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800318 Log.w(LOG_TAG, "Found null phone for: " + subId);
319 return;
320 }
321
322 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
323 if (subInfo == null) {
324 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800325 return;
326 }
327
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 int resId = android.R.drawable.stat_notify_voicemail;
329
330 // This Notification can get a lot fancier once we have more
331 // information about the current voicemail messages.
332 // (For example, the current voicemail system can't tell
333 // us the caller-id or timestamp of a message, or tell us the
334 // message count.)
335
336 // But for now, the UI is ultra-simple: if the MWI indication
337 // is supposed to be visible, just show a single generic
338 // notification.
339
340 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800341 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 if (DBG) log("- got vm number: '" + vmNumber + "'");
343
Andrew Leea82b8202014-11-21 16:18:28 -0800344 // The voicemail number may be null because:
345 // (1) This phone has no voicemail number.
346 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
347 // happen when the device first boots if we get a MWI notification when we
348 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800349 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
350 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800352 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 }
354
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800355 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
356 int vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
358 notificationTitle = String.format(titleFormat, vmCount);
359 }
360
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800361 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
362 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
363
364 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 String notificationText;
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800366 if (TextUtils.isEmpty(vmNumber)) {
367 notificationText = mContext.getString(
368 R.string.notification_voicemail_no_vm_number);
369
370 // If the voicemail number if unknown, instead of calling voicemail, take the user
371 // to the voicemail settings.
372 notificationText = mContext.getString(
373 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700374 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800375 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700376 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800378 if (mTelephonyManager.getPhoneCount() > 1) {
379 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800380 } else {
381 notificationText = String.format(
382 mContext.getString(R.string.notification_voicemail_text_format),
383 PhoneNumberUtils.formatNumber(vmNumber));
384 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800385 intent = new Intent(
386 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700387 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800388 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 }
390
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800391 PendingIntent pendingIntent =
392 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800393 Uri ringtoneUri = null;
394
395 if (enableNotificationSound) {
396 ringtoneUri = VoicemailNotificationSettingsUtil.getRingtoneUri(mPhone);
397 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700398
Nancy Chenb4a92702014-12-04 15:57:29 -0800399 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700400 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700401 mPhone.getSubId());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 Notification.Builder builder = new Notification.Builder(mContext);
403 builder.setSmallIcon(resId)
404 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800405 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 .setContentTitle(notificationTitle)
407 .setContentText(notificationText)
408 .setContentIntent(pendingIntent)
Yorke Leeacb5f742014-08-19 09:08:42 -0700409 .setSound(ringtoneUri)
Nancy Chenb4a92702014-12-04 15:57:29 -0800410 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700411 .setOngoing(carrierConfig.getBoolean(
412 CarrierConfigManager.BOOL_VOICEMAIL_NOTIFICATION_PERSISTENT));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800414 if (VoicemailNotificationSettingsUtil.isVibrationEnabled(phone)) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700415 builder.setDefaults(Notification.DEFAULT_VIBRATE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700417
418 final Notification notification = builder.build();
419 List<UserInfo> users = mUserManager.getUsers(true);
420 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700421 final UserInfo user = users.get(i);
422 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700423 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700424 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700425 && !user.isManagedProfile()) {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700426 mNotificationManager.notifyAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800427 Integer.toString(subId) /* tag */,
428 VOICEMAIL_NOTIFICATION,
429 notification,
430 userHandle);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700431 }
432 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700434 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800435 Integer.toString(subId) /* tag */,
436 VOICEMAIL_NOTIFICATION,
437 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700438 }
439 }
440
441 /**
442 * Updates the message call forwarding indicator notification.
443 *
444 * @param visible true if there are messages waiting
445 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800446 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447 if (DBG) log("updateCfi(): " + visible);
448 if (visible) {
449 // If Unconditional Call Forwarding (forward all calls) for VOICE
450 // is enabled, just show a notification. We'll default to expanded
451 // view for now, so the there is less confusion about the icon. If
452 // it is deemed too weird to have CF indications as expanded views,
453 // then we'll flip the flag back.
454
455 // TODO: We may want to take a look to see if the notification can
456 // display the target to forward calls to. This will require some
457 // effort though, since there are multiple layers of messages that
458 // will need to propagate that information.
459
Andrew Leed5165b02014-12-05 15:53:58 -0800460 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
461 if (subInfo == null) {
462 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
463 return;
464 }
465
466 String notificationTitle;
467 if (mTelephonyManager.getPhoneCount() > 1) {
468 notificationTitle = subInfo.getDisplayName().toString();
469 } else {
470 notificationTitle = mContext.getString(R.string.labelCF);
471 }
472
Andrew Lee99d0ac22014-10-10 13:18:04 -0700473 Notification.Builder builder = new Notification.Builder(mContext)
474 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800475 .setColor(subInfo.getIconTint())
476 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700477 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
478 .setShowWhen(false)
479 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480
Andrew Lee99d0ac22014-10-10 13:18:04 -0700481 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800482 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700483 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800484 SubscriptionInfoHelper.addExtrasToIntent(
485 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
486 PendingIntent contentIntent =
487 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700488
489 List<UserInfo> users = mUserManager.getUsers(true);
490 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800491 final UserInfo user = users.get(i);
492 if (user.isManagedProfile()) {
493 continue;
494 }
495 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700496 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800497 mNotificationManager.notifyAsUser(
498 Integer.toString(subId) /* tag */,
499 CALL_FORWARD_NOTIFICATION,
500 builder.build(),
501 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700504 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800505 Integer.toString(subId) /* tag */,
506 CALL_FORWARD_NOTIFICATION,
507 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 }
509 }
510
511 /**
512 * Shows the "data disconnected due to roaming" notification, which
513 * appears when you lose data connectivity because you're roaming and
514 * you have the "data roaming" feature turned off.
515 */
516 /* package */ void showDataDisconnectedRoaming() {
517 if (DBG) log("showDataDisconnectedRoaming()...");
518
519 // "Mobile network settings" screen / dialog
520 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700521 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700522
523 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
524
Andrew Lee99d0ac22014-10-10 13:18:04 -0700525 final Notification.Builder builder = new Notification.Builder(mContext)
526 .setSmallIcon(android.R.drawable.stat_sys_warning)
527 .setContentTitle(mContext.getText(R.string.roaming))
528 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
529 .setContentText(contentText);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530
Andrew Lee99d0ac22014-10-10 13:18:04 -0700531 List<UserInfo> users = mUserManager.getUsers(true);
532 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800533 final UserInfo user = users.get(i);
534 if (user.isManagedProfile()) {
535 continue;
536 }
537 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700538 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
539 final Notification notif =
540 new Notification.BigTextStyle(builder).bigText(contentText).build();
541 mNotificationManager.notifyAsUser(
542 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
543 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700544 }
545
546 /**
547 * Turns off the "data disconnected due to roaming" notification.
548 */
549 /* package */ void hideDataDisconnectedRoaming() {
550 if (DBG) log("hideDataDisconnectedRoaming()...");
551 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
552 }
553
554 /**
555 * Display the network selection "no service" notification
556 * @param operator is the numeric operator number
557 */
558 private void showNetworkSelection(String operator) {
559 if (DBG) log("showNetworkSelection(" + operator + ")...");
560
Andrew Lee99d0ac22014-10-10 13:18:04 -0700561 Notification.Builder builder = new Notification.Builder(mContext)
562 .setSmallIcon(android.R.drawable.stat_sys_warning)
563 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
564 .setContentText(
565 mContext.getString(R.string.notification_network_selection_text, operator))
566 .setShowWhen(false)
567 .setOngoing(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568
569 // create the target network operators settings intent
570 Intent intent = new Intent(Intent.ACTION_MAIN);
571 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
572 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
573 // Use NetworkSetting to handle the selection intent
574 intent.setComponent(new ComponentName("com.android.phone",
575 "com.android.phone.NetworkSetting"));
Andrew Lee99d0ac22014-10-10 13:18:04 -0700576 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577
Andrew Lee99d0ac22014-10-10 13:18:04 -0700578 List<UserInfo> users = mUserManager.getUsers(true);
579 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800580 final UserInfo user = users.get(i);
581 if (user.isManagedProfile()) {
582 continue;
583 }
584 UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700585 builder.setContentIntent(userHandle.isOwner() ? contentIntent : null);
586 mNotificationManager.notifyAsUser(
587 null /* tag */,
588 SELECTED_OPERATOR_FAIL_NOTIFICATION,
589 builder.build(),
590 userHandle);
591 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592 }
593
594 /**
595 * Turn off the network selection "no service" notification
596 */
597 private void cancelNetworkSelection() {
598 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700599 mNotificationManager.cancelAsUser(
600 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700601 }
602
603 /**
604 * Update notification about no service of user selected operator
605 *
606 * @param serviceState Phone service state
607 */
608 void updateNetworkSelection(int serviceState) {
609 if (TelephonyCapabilities.supportsNetworkSelection(mPhone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800610 int subId = mPhone.getSubId();
611 if (SubscriptionManager.isValidSubscriptionId(subId)) {
612 // get the shared preference of network_selection.
613 // empty is auto mode, otherwise it is the operator alpha name
614 // in case there is no operator name, check the operator numeric
615 SharedPreferences sp =
616 PreferenceManager.getDefaultSharedPreferences(mContext);
617 String networkSelection =
618 sp.getString(PhoneBase.NETWORK_SELECTION_NAME_KEY + subId, "");
619 if (TextUtils.isEmpty(networkSelection)) {
620 networkSelection =
621 sp.getString(PhoneBase.NETWORK_SELECTION_KEY + subId, "");
622 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700623
Amit Mahajana60be872015-01-15 16:05:08 -0800624 if (DBG) log("updateNetworkSelection()..." + "state = " +
625 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626
Amit Mahajana60be872015-01-15 16:05:08 -0800627 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
628 && !TextUtils.isEmpty(networkSelection)) {
629 if (!mSelectedUnavailableNotify) {
630 showNetworkSelection(networkSelection);
631 mSelectedUnavailableNotify = true;
632 }
633 } else {
634 if (mSelectedUnavailableNotify) {
635 cancelNetworkSelection();
636 mSelectedUnavailableNotify = false;
637 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 }
639 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800640 if (DBG) log("updateNetworkSelection()..." + "state = " +
641 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 }
643 }
644 }
645
646 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
647 if (mToast != null) {
648 mToast.cancel();
649 }
650
651 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
652 mToast.show();
653 }
654
655 private void log(String msg) {
656 Log.d(LOG_TAG, msg);
657 }
658}