blob: b2c763281664732f26a770043ca52d7635f24b34 [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
Ta-wei Yenafca2d62017-07-18 17:20:59 -070019import static android.Manifest.permission.READ_PHONE_STATE;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.Notification;
22import android.app.NotificationManager;
23import android.app.PendingIntent;
24import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
28import android.content.SharedPreferences;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080029import android.content.pm.ResolveInfo;
Andrew Lee99d0ac22014-10-10 13:18:04 -070030import android.content.pm.UserInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080031import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070035import android.os.UserHandle;
36import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.provider.ContactsContract.PhoneLookup;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080039import android.telecom.DefaultDialerManager;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070040import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080041import android.telecom.PhoneAccountHandle;
42import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.PhoneNumberUtils;
45import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080046import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080047import android.telephony.SubscriptionManager;
Ta-wei Yenfb4f0502016-05-27 12:15:43 -070048import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
Andrew Leed5165b02014-12-05 15:53:58 -080049import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080051import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.util.Log;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070054
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070056import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070058import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070059import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070060
Tyler Gunn9c1071f2014-12-09 10:07:54 -080061import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070062import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080063import 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
Ta-wei Yenb29425b2016-09-21 17:28:14 -070081 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
82 "mwi_should_check_vvm_configuration_state_";
83
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070085 static final int MMI_NOTIFICATION = 1;
86 static final int NETWORK_SELECTION_NOTIFICATION = 2;
87 static final int VOICEMAIL_NOTIFICATION = 3;
88 static final int CALL_FORWARD_NOTIFICATION = 4;
89 static final int DATA_DISCONNECTED_ROAMING_NOTIFICATION = 5;
90 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091
92 /** The singleton NotificationMgr instance. */
93 private static NotificationMgr sInstance;
94
95 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97 private Context mContext;
98 private NotificationManager mNotificationManager;
99 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700100 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800102 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800103 private TelecomManager mTelecomManager;
104 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106 // used to track the notification of selected network unavailable
107 private boolean mSelectedUnavailableNotify = false;
108
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800109 // used to track whether the message waiting indicator is visible, per subscription id.
110 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
111
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 /**
113 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700114 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 */
116 private NotificationMgr(PhoneGlobals app) {
117 mApp = app;
118 mContext = app;
119 mNotificationManager =
120 (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
121 mStatusBarManager =
122 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700123 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800124 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800125 mTelecomManager = TelecomManager.from(mContext);
126 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700127
Ta-wei Yenfb4f0502016-05-27 12:15:43 -0700128 mSubscriptionManager.addOnSubscriptionsChangedListener(
129 new OnSubscriptionsChangedListener() {
130 @Override
131 public void onSubscriptionsChanged() {
132 updateActivePhonesMwi();
133 }
134 });
135 }
136
137 public void updateActivePhonesMwi() {
138 List<SubscriptionInfo> subInfos = mSubscriptionManager.getActiveSubscriptionInfoList();
139
140 if (subInfos == null) {
141 return;
142 }
143
144 for (int i = 0; i < subInfos.size(); i++) {
145 int subId = subInfos.get(i).getSubscriptionId();
146 refreshMwi(subId);
147 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 }
149
150 /**
151 * Initialize the singleton NotificationMgr instance.
152 *
153 * This is only done once, at startup, from PhoneApp.onCreate().
154 * From then on, the NotificationMgr instance is available via the
155 * PhoneApp's public "notificationMgr" field, which is why there's no
156 * getInstance() method here.
157 */
158 /* package */ static NotificationMgr init(PhoneGlobals app) {
159 synchronized (NotificationMgr.class) {
160 if (sInstance == null) {
161 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 } else {
163 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
164 }
165 return sInstance;
166 }
167 }
168
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 /** The projection to use when querying the phones table */
170 static final String[] PHONES_PROJECTION = new String[] {
171 PhoneLookup.NUMBER,
172 PhoneLookup.DISPLAY_NAME,
173 PhoneLookup._ID
174 };
175
176 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800177 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
178 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
179 * settings screen. The voicemail notification sound is suppressed.
180 *
181 * @param subId The subscription Id.
182 */
183 /* package */ void refreshMwi(int subId) {
184 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
185 // reference the single subid stored in the mMwiVisible map.
186 if (subId == SubscriptionInfoHelper.NO_SUB_ID) {
187 if (mMwiVisible.keySet().size() == 1) {
188 Set<Integer> keySet = mMwiVisible.keySet();
189 Iterator<Integer> keyIt = keySet.iterator();
190 if (!keyIt.hasNext()) {
191 return;
192 }
193 subId = keyIt.next();
194 }
195 }
196 if (mMwiVisible.containsKey(subId)) {
197 boolean mwiVisible = mMwiVisible.get(subId);
198 if (mwiVisible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700199 updateMwi(subId, mwiVisible, true /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800200 }
201 }
202 }
203
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700204 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
205 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
206 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
207 + subId);
208 return;
209 }
210
211 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
212 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
213 .apply();
214 }
215
216 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
217 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
218 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
219 return true;
220 }
221 return PreferenceManager
222 .getDefaultSharedPreferences(mContext)
223 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
224 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800225 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 * Updates the message waiting indicator (voicemail) notification.
227 *
228 * @param visible true if there are messages waiting
229 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800230 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700231 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800232 }
233
234 /**
235 * Updates the message waiting indicator (voicemail) notification.
236 *
237 * @param subId the subId to update.
238 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700239 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
240 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800241 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700242 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800243 if (!PhoneGlobals.sVoiceCapable) {
244 // Do not show the message waiting indicator on devices which are not voice capable.
245 // These events *should* be blocked at the telephony layer for such devices.
246 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
247 return;
248 }
249
Nancy Chen2cf7f292015-05-15 11:00:10 -0700250 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800251 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800252 mMwiVisible.put(subId, visible);
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800255 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800256 Log.w(LOG_TAG, "Found null phone for: " + subId);
257 return;
258 }
259
260 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
261 if (subInfo == null) {
262 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800263 return;
264 }
265
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 int resId = android.R.drawable.stat_notify_voicemail;
267
268 // This Notification can get a lot fancier once we have more
269 // information about the current voicemail messages.
270 // (For example, the current voicemail system can't tell
271 // us the caller-id or timestamp of a message, or tell us the
272 // message count.)
273
274 // But for now, the UI is ultra-simple: if the MWI indication
275 // is supposed to be visible, just show a single generic
276 // notification.
277
278 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800279 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 if (DBG) log("- got vm number: '" + vmNumber + "'");
281
Andrew Leea82b8202014-11-21 16:18:28 -0800282 // The voicemail number may be null because:
283 // (1) This phone has no voicemail number.
284 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
285 // happen when the device first boots if we get a MWI notification when we
286 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
288 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800290 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 }
292
Bryce Lee5dc90842015-08-11 07:57:14 -0700293 Integer vmCount = null;
294
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800295 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700296 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
298 notificationTitle = String.format(titleFormat, vmCount);
299 }
300
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800301 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
302 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
303
304 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700306 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
307
308 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800309 notificationText = mContext.getString(
310 R.string.notification_voicemail_no_vm_number);
311
312 // If the voicemail number if unknown, instead of calling voicemail, take the user
313 // to the voicemail settings.
314 notificationText = mContext.getString(
315 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700316 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800317 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700318 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800320 if (mTelephonyManager.getPhoneCount() > 1) {
321 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800322 } else {
323 notificationText = String.format(
324 mContext.getString(R.string.notification_voicemail_text_format),
325 PhoneNumberUtils.formatNumber(vmNumber));
326 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800327 intent = new Intent(
328 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700329 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800330 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 }
332
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800333 PendingIntent pendingIntent =
334 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335
Nancy Chenb4a92702014-12-04 15:57:29 -0800336 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700337 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700338 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700339 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 builder.setSmallIcon(resId)
341 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800342 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 .setContentTitle(notificationTitle)
344 .setContentText(notificationText)
345 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800346 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700347 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700348 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Ta-wei Yen282a9702017-05-30 17:32:29 -0700349 .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
350 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351
Andrew Lee99d0ac22014-10-10 13:18:04 -0700352 final Notification notification = builder.build();
353 List<UserInfo> users = mUserManager.getUsers(true);
354 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700355 final UserInfo user = users.get(i);
356 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700357 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700358 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700359 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800360 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700361 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700362 mNotificationManager.notifyAsUser(
363 Integer.toString(subId) /* tag */,
364 VOICEMAIL_NOTIFICATION,
365 notification,
366 userHandle);
367 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700368 }
369 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800371 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
372 for (int i = 0; i < users.size(); i++) {
373 final UserInfo user = users.get(i);
374 final UserHandle userHandle = user.getUserHandle();
375 if (!mUserManager.hasUserRestriction(
376 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
377 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800378 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700379 false, userHandle, isRefresh)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800380 mNotificationManager.cancelAsUser(
381 Integer.toString(subId) /* tag */,
382 VOICEMAIL_NOTIFICATION,
383 userHandle);
384 }
385 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700386 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 }
388 }
389
390 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800391 * Sends a broadcast with the voicemail notification information to the default dialer. This
392 * method is also used to indicate to the default dialer when to clear the
393 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700394 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800395 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700396 * @param count The number of pending voicemail messages to indicate on the notification. A
397 * Value of 0 is passed here to indicate that the notification should be cleared.
398 * @param number The voicemail phone number if specified.
399 * @param pendingIntent The intent that should be passed as the action to be taken.
400 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
401 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800402 * @param userHandle The user to receive the notification. Each user can have their own default
403 * dialer.
404 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700405 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800406 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
407 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700408 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800409
410 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
411 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700412 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700413 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800414 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
415 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700416 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700417 if (count != null) {
418 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
419 }
420
421 // Additional information about the voicemail notification beyond the count is only
422 // present when the count not specified or greater than 0. The value of 0 represents
423 // clearing the notification, which does not require additional information.
424 if (count == null || count > 0) {
425 if (!TextUtils.isEmpty(number)) {
426 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
427 }
428
429 if (pendingIntent != null) {
430 intent.putExtra(isSettingsIntent
431 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
432 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
433 pendingIntent);
434 }
435 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800436 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700437 return true;
438 }
439
440 return false;
441 }
442
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800443 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
444 String dialerPackage = DefaultDialerManager
445 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
446 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
447 .setPackage(dialerPackage);
448 }
449
450 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
451 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
452 if (intent == null) {
453 return false;
454 }
455
456 List<ResolveInfo> receivers = mContext.getPackageManager()
457 .queryBroadcastReceivers(intent, 0);
458 return receivers.size() > 0;
459 }
460
Bryce Lee5dc90842015-08-11 07:57:14 -0700461 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700462 * Updates the message call forwarding indicator notification.
463 *
464 * @param visible true if there are messages waiting
465 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800466 /* package */ void updateCfi(int subId, boolean visible) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 if (DBG) log("updateCfi(): " + visible);
468 if (visible) {
469 // If Unconditional Call Forwarding (forward all calls) for VOICE
470 // is enabled, just show a notification. We'll default to expanded
471 // view for now, so the there is less confusion about the icon. If
472 // it is deemed too weird to have CF indications as expanded views,
473 // then we'll flip the flag back.
474
475 // TODO: We may want to take a look to see if the notification can
476 // display the target to forward calls to. This will require some
477 // effort though, since there are multiple layers of messages that
478 // will need to propagate that information.
479
Andrew Leed5165b02014-12-05 15:53:58 -0800480 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
481 if (subInfo == null) {
482 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
483 return;
484 }
485
486 String notificationTitle;
487 if (mTelephonyManager.getPhoneCount() > 1) {
488 notificationTitle = subInfo.getDisplayName().toString();
489 } else {
490 notificationTitle = mContext.getString(R.string.labelCF);
491 }
492
fionaxu8b7620d2017-05-01 16:22:17 -0700493 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700494 .setSmallIcon(R.drawable.stat_sys_phone_call_forward)
Andrew Leed5165b02014-12-05 15:53:58 -0800495 .setColor(subInfo.getIconTint())
496 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700497 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
498 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700499 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700500 .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700501
Andrew Lee99d0ac22014-10-10 13:18:04 -0700502 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800503 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700504 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800505 SubscriptionInfoHelper.addExtrasToIntent(
506 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
507 PendingIntent contentIntent =
508 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700509
510 List<UserInfo> users = mUserManager.getUsers(true);
511 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800512 final UserInfo user = users.get(i);
513 if (user.isManagedProfile()) {
514 continue;
515 }
516 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700517 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800518 mNotificationManager.notifyAsUser(
519 Integer.toString(subId) /* tag */,
520 CALL_FORWARD_NOTIFICATION,
521 builder.build(),
522 userHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 } else {
Andrew Lee99d0ac22014-10-10 13:18:04 -0700525 mNotificationManager.cancelAsUser(
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800526 Integer.toString(subId) /* tag */,
527 CALL_FORWARD_NOTIFICATION,
528 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700529 }
530 }
531
532 /**
533 * Shows the "data disconnected due to roaming" notification, which
534 * appears when you lose data connectivity because you're roaming and
535 * you have the "data roaming" feature turned off.
536 */
Sanket Padawe336ef362017-07-12 12:12:37 -0700537 void showDataDisconnectedRoaming() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 if (DBG) log("showDataDisconnectedRoaming()...");
539
540 // "Mobile network settings" screen / dialog
541 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700542 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543
544 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
545
fionaxu8b7620d2017-05-01 16:22:17 -0700546 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700547 .setSmallIcon(android.R.drawable.stat_sys_warning)
548 .setContentTitle(mContext.getText(R.string.roaming))
549 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700550 .setContentText(contentText)
fionaxu8b7620d2017-05-01 16:22:17 -0700551 .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552
Andrew Lee99d0ac22014-10-10 13:18:04 -0700553 List<UserInfo> users = mUserManager.getUsers(true);
554 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800555 final UserInfo user = users.get(i);
556 if (user.isManagedProfile()) {
557 continue;
558 }
559 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700560 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700561 final Notification notif =
562 new Notification.BigTextStyle(builder).bigText(contentText).build();
563 mNotificationManager.notifyAsUser(
564 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, userHandle);
565 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 }
567
568 /**
569 * Turns off the "data disconnected due to roaming" notification.
570 */
571 /* package */ void hideDataDisconnectedRoaming() {
572 if (DBG) log("hideDataDisconnectedRoaming()...");
573 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
574 }
575
576 /**
577 * Display the network selection "no service" notification
578 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700579 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700581 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 if (DBG) log("showNetworkSelection(" + operator + ")...");
583
fionaxu8b7620d2017-05-01 16:22:17 -0700584 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700585 .setSmallIcon(android.R.drawable.stat_sys_warning)
586 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
587 .setContentText(
588 mContext.getString(R.string.notification_network_selection_text, operator))
589 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700590 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700591 .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592
593 // create the target network operators settings intent
594 Intent intent = new Intent(Intent.ACTION_MAIN);
595 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
596 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700597 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700598 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700599 mContext.getString(R.string.mobile_network_settings_package),
600 mContext.getString(R.string.mobile_network_settings_class)));
Jayachandran C2ef9a482017-05-12 22:07:47 -0700601 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700602 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603
Andrew Lee99d0ac22014-10-10 13:18:04 -0700604 List<UserInfo> users = mUserManager.getUsers(true);
605 for (int i = 0; i < users.size(); i++) {
Yorke Lee3faa5942014-11-05 16:50:04 -0800606 final UserInfo user = users.get(i);
607 if (user.isManagedProfile()) {
608 continue;
609 }
610 UserHandle userHandle = user.getUserHandle();
Xiaohui Chen3105e9a2015-10-21 12:27:17 -0700611 builder.setContentIntent(user.isAdmin() ? contentIntent : null);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700612 mNotificationManager.notifyAsUser(
613 null /* tag */,
614 SELECTED_OPERATOR_FAIL_NOTIFICATION,
615 builder.build(),
616 userHandle);
617 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 }
619
620 /**
621 * Turn off the network selection "no service" notification
622 */
623 private void cancelNetworkSelection() {
624 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700625 mNotificationManager.cancelAsUser(
626 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 }
628
629 /**
630 * Update notification about no service of user selected operator
631 *
632 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700633 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700635 void updateNetworkSelection(int serviceState, int subId) {
636 int phoneId = SubscriptionManager.getPhoneId(subId);
637 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
638 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
639 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800640 if (SubscriptionManager.isValidSubscriptionId(subId)) {
641 // get the shared preference of network_selection.
642 // empty is auto mode, otherwise it is the operator alpha name
643 // in case there is no operator name, check the operator numeric
644 SharedPreferences sp =
645 PreferenceManager.getDefaultSharedPreferences(mContext);
646 String networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800647 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800648 if (TextUtils.isEmpty(networkSelection)) {
649 networkSelection =
Amit Mahajanc5201f42015-11-24 15:38:12 -0800650 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
Amit Mahajana60be872015-01-15 16:05:08 -0800651 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652
Amit Mahajana60be872015-01-15 16:05:08 -0800653 if (DBG) log("updateNetworkSelection()..." + "state = " +
654 serviceState + " new network " + networkSelection);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700655
Amit Mahajana60be872015-01-15 16:05:08 -0800656 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
657 && !TextUtils.isEmpty(networkSelection)) {
Jayachandran C2ef9a482017-05-12 22:07:47 -0700658 showNetworkSelection(networkSelection, subId);
Hall Liu2b846c72016-02-09 18:21:24 -0800659 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800660 } else {
661 if (mSelectedUnavailableNotify) {
662 cancelNetworkSelection();
663 mSelectedUnavailableNotify = false;
664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 }
666 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800667 if (DBG) log("updateNetworkSelection()..." + "state = " +
668 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 }
670 }
671 }
672
673 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
674 if (mToast != null) {
675 mToast.cancel();
676 }
677
678 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
679 mToast.show();
680 }
681
682 private void log(String msg) {
683 Log.d(LOG_TAG, msg);
684 }
685}