blob: 1b2d49e4b071ffbbabbeee6fa63f7f162ce5c670 [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;
Jeff Davidson6d9bf522017-11-03 14:51:13 -070039import android.provider.Settings;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080040import android.telecom.DefaultDialerManager;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070041import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080042import android.telecom.PhoneAccountHandle;
43import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070044import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.PhoneNumberUtils;
46import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080047import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080048import android.telephony.SubscriptionManager;
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);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 }
128
129 /**
130 * Initialize the singleton NotificationMgr instance.
131 *
132 * This is only done once, at startup, from PhoneApp.onCreate().
133 * From then on, the NotificationMgr instance is available via the
134 * PhoneApp's public "notificationMgr" field, which is why there's no
135 * getInstance() method here.
136 */
137 /* package */ static NotificationMgr init(PhoneGlobals app) {
138 synchronized (NotificationMgr.class) {
139 if (sInstance == null) {
140 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 } else {
142 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
143 }
144 return sInstance;
145 }
146 }
147
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 /** The projection to use when querying the phones table */
149 static final String[] PHONES_PROJECTION = new String[] {
150 PhoneLookup.NUMBER,
151 PhoneLookup.DISPLAY_NAME,
152 PhoneLookup._ID
153 };
154
155 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800156 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
157 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
158 * settings screen. The voicemail notification sound is suppressed.
159 *
160 * @param subId The subscription Id.
161 */
162 /* package */ void refreshMwi(int subId) {
163 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
164 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800165 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800166 if (mMwiVisible.keySet().size() == 1) {
167 Set<Integer> keySet = mMwiVisible.keySet();
168 Iterator<Integer> keyIt = keySet.iterator();
169 if (!keyIt.hasNext()) {
170 return;
171 }
172 subId = keyIt.next();
173 }
174 }
175 if (mMwiVisible.containsKey(subId)) {
176 boolean mwiVisible = mMwiVisible.get(subId);
177 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900178 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800179 }
180 }
181 }
182
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700183 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
184 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
185 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
186 + subId);
187 return;
188 }
189
190 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
191 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
192 .apply();
193 }
194
195 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
196 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
197 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
198 return true;
199 }
200 return PreferenceManager
201 .getDefaultSharedPreferences(mContext)
202 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
203 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800204 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205 * Updates the message waiting indicator (voicemail) notification.
206 *
207 * @param visible true if there are messages waiting
208 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800209 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700210 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800211 }
212
213 /**
214 * Updates the message waiting indicator (voicemail) notification.
215 *
216 * @param subId the subId to update.
217 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700218 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
219 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800220 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700221 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800222 if (!PhoneGlobals.sVoiceCapable) {
223 // Do not show the message waiting indicator on devices which are not voice capable.
224 // These events *should* be blocked at the telephony layer for such devices.
225 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
226 return;
227 }
228
Nancy Chen2cf7f292015-05-15 11:00:10 -0700229 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800230 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800231 mMwiVisible.put(subId, visible);
232
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800234 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800235 Log.w(LOG_TAG, "Found null phone for: " + subId);
236 return;
237 }
238
239 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
240 if (subInfo == null) {
241 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800242 return;
243 }
244
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900246 if (mTelephonyManager.getPhoneCount() > 1) {
247 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
248 : R.drawable.stat_notify_voicemail_sub2;
249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250
251 // This Notification can get a lot fancier once we have more
252 // information about the current voicemail messages.
253 // (For example, the current voicemail system can't tell
254 // us the caller-id or timestamp of a message, or tell us the
255 // message count.)
256
257 // But for now, the UI is ultra-simple: if the MWI indication
258 // is supposed to be visible, just show a single generic
259 // notification.
260
261 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800262 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 if (DBG) log("- got vm number: '" + vmNumber + "'");
264
Andrew Leea82b8202014-11-21 16:18:28 -0800265 // The voicemail number may be null because:
266 // (1) This phone has no voicemail number.
267 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
268 // happen when the device first boots if we get a MWI notification when we
269 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800270 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
271 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800273 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 }
275
Bryce Lee5dc90842015-08-11 07:57:14 -0700276 Integer vmCount = null;
277
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800278 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700279 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
281 notificationTitle = String.format(titleFormat, vmCount);
282 }
283
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800284 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
285 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
286
287 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700289 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
290
291 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800292 notificationText = mContext.getString(
293 R.string.notification_voicemail_no_vm_number);
294
295 // If the voicemail number if unknown, instead of calling voicemail, take the user
296 // to the voicemail settings.
297 notificationText = mContext.getString(
298 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700299 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800300 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700301 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800303 if (mTelephonyManager.getPhoneCount() > 1) {
304 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800305 } else {
306 notificationText = String.format(
307 mContext.getString(R.string.notification_voicemail_text_format),
308 PhoneNumberUtils.formatNumber(vmNumber));
309 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800310 intent = new Intent(
311 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700312 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800313 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 }
315
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800316 PendingIntent pendingIntent =
317 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318
Nancy Chenb4a92702014-12-04 15:57:29 -0800319 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700320 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700321 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700322 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 builder.setSmallIcon(resId)
324 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800325 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 .setContentTitle(notificationTitle)
327 .setContentText(notificationText)
328 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800329 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700330 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700331 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Ta-wei Yen282a9702017-05-30 17:32:29 -0700332 .setChannel(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
333 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334
Andrew Lee99d0ac22014-10-10 13:18:04 -0700335 final Notification notification = builder.build();
336 List<UserInfo> users = mUserManager.getUsers(true);
337 for (int i = 0; i < users.size(); i++) {
Yorke Lee047b1f92014-10-24 10:22:41 -0700338 final UserInfo user = users.get(i);
339 final UserHandle userHandle = user.getUserHandle();
Andrew Lee99d0ac22014-10-10 13:18:04 -0700340 if (!mUserManager.hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700341 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700342 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800343 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700344 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700345 mNotificationManager.notifyAsUser(
346 Integer.toString(subId) /* tag */,
347 VOICEMAIL_NOTIFICATION,
348 notification,
349 userHandle);
350 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700351 }
352 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 } else {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800354 List<UserInfo> users = mUserManager.getUsers(true /* excludeDying */);
355 for (int i = 0; i < users.size(); i++) {
356 final UserInfo user = users.get(i);
357 final UserHandle userHandle = user.getUserHandle();
358 if (!mUserManager.hasUserRestriction(
359 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
360 && !user.isManagedProfile()) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800361 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700362 false, userHandle, isRefresh)) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800363 mNotificationManager.cancelAsUser(
364 Integer.toString(subId) /* tag */,
365 VOICEMAIL_NOTIFICATION,
366 userHandle);
367 }
368 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700369 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 }
371 }
372
373 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800374 * Sends a broadcast with the voicemail notification information to the default dialer. This
375 * method is also used to indicate to the default dialer when to clear the
376 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700377 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800378 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700379 * @param count The number of pending voicemail messages to indicate on the notification. A
380 * Value of 0 is passed here to indicate that the notification should be cleared.
381 * @param number The voicemail phone number if specified.
382 * @param pendingIntent The intent that should be passed as the action to be taken.
383 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
384 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800385 * @param userHandle The user to receive the notification. Each user can have their own default
386 * dialer.
387 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700388 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800389 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
390 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700391 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800392
393 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
394 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700395 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700396 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800397 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
398 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700399 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700400 if (count != null) {
401 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
402 }
403
404 // Additional information about the voicemail notification beyond the count is only
405 // present when the count not specified or greater than 0. The value of 0 represents
406 // clearing the notification, which does not require additional information.
407 if (count == null || count > 0) {
408 if (!TextUtils.isEmpty(number)) {
409 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
410 }
411
412 if (pendingIntent != null) {
413 intent.putExtra(isSettingsIntent
414 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
415 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
416 pendingIntent);
417 }
418 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800419 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700420 return true;
421 }
422
423 return false;
424 }
425
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800426 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
427 String dialerPackage = DefaultDialerManager
428 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
429 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
430 .setPackage(dialerPackage);
431 }
432
433 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
434 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
435 if (intent == null) {
436 return false;
437 }
438
439 List<ResolveInfo> receivers = mContext.getPackageManager()
440 .queryBroadcastReceivers(intent, 0);
441 return receivers.size() > 0;
442 }
443
Bryce Lee5dc90842015-08-11 07:57:14 -0700444 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445 * Updates the message call forwarding indicator notification.
446 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530447 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700448 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530449
450 /* package */ void updateCfi(int subId, boolean visible) {
451 updateCfi(subId, visible, false /* isRefresh */);
452 }
453
454 /**
455 * Updates the message call forwarding indicator notification.
456 *
457 * @param visible true if call forwarding enabled
458 */
459 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunna584e2c2017-09-19 11:40:12 -0700460 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700461 if (visible) {
462 // If Unconditional Call Forwarding (forward all calls) for VOICE
463 // is enabled, just show a notification. We'll default to expanded
464 // view for now, so the there is less confusion about the icon. If
465 // it is deemed too weird to have CF indications as expanded views,
466 // then we'll flip the flag back.
467
468 // TODO: We may want to take a look to see if the notification can
469 // display the target to forward calls to. This will require some
470 // effort though, since there are multiple layers of messages that
471 // will need to propagate that information.
472
Andrew Leed5165b02014-12-05 15:53:58 -0800473 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
474 if (subInfo == null) {
475 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
476 return;
477 }
478
479 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900480 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800481 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900482 int slotId = SubscriptionManager.getSlotIndex(subId);
483 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
484 : R.drawable.stat_sys_phone_call_forward_sub2;
Andrew Leed5165b02014-12-05 15:53:58 -0800485 notificationTitle = subInfo.getDisplayName().toString();
486 } else {
487 notificationTitle = mContext.getString(R.string.labelCF);
488 }
489
fionaxu8b7620d2017-05-01 16:22:17 -0700490 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900491 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800492 .setColor(subInfo.getIconTint())
493 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700494 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
495 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700496 .setOngoing(true)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530497 .setChannel(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
498 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499
Andrew Lee99d0ac22014-10-10 13:18:04 -0700500 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800501 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700502 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800503 SubscriptionInfoHelper.addExtrasToIntent(
504 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700505 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
506 intent, 0));
507 mNotificationManager.notifyAsUser(
508 Integer.toString(subId) /* tag */,
509 CALL_FORWARD_NOTIFICATION,
510 builder.build(),
511 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 } else {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900513 List<UserInfo> users = mUserManager.getUsers(true);
514 for (UserInfo user : users) {
515 if (user.isManagedProfile()) {
516 continue;
517 }
518 UserHandle userHandle = user.getUserHandle();
519 mNotificationManager.cancelAsUser(
520 Integer.toString(subId) /* tag */,
521 CALL_FORWARD_NOTIFICATION,
522 userHandle);
523 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700524 }
525 }
526
527 /**
528 * Shows the "data disconnected due to roaming" notification, which
529 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700530 * you have the "data roaming" feature turned off for the given {@code subId}.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 */
Pengquan Meng8783d932017-10-16 14:57:40 -0700532 /* package */ void showDataDisconnectedRoaming(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 if (DBG) log("showDataDisconnectedRoaming()...");
534
535 // "Mobile network settings" screen / dialog
536 Intent intent = new Intent(mContext, com.android.phone.MobileNetworkSettings.class);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700537 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Pengquan Meng8783d932017-10-16 14:57:40 -0700538 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
539 PendingIntent contentIntent = PendingIntent.getActivity(mContext, subId, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540
541 final CharSequence contentText = mContext.getText(R.string.roaming_reenable_message);
542
fionaxu8b7620d2017-05-01 16:22:17 -0700543 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700544 .setSmallIcon(android.R.drawable.stat_sys_warning)
fionaxuba6bdef2018-03-13 14:18:34 -0700545 .setContentTitle(mContext.getText(R.string.roaming_notification_title))
Andrew Lee99d0ac22014-10-10 13:18:04 -0700546 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700547 .setContentText(contentText)
fionaxu96ceebd2017-08-24 12:12:32 -0700548 .setChannel(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
549 .setContentIntent(contentIntent);
550 final Notification notif =
551 new Notification.BigTextStyle(builder).bigText(contentText).build();
552 mNotificationManager.notifyAsUser(
553 null /* tag */, DATA_DISCONNECTED_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554 }
555
556 /**
557 * Turns off the "data disconnected due to roaming" notification.
558 */
559 /* package */ void hideDataDisconnectedRoaming() {
560 if (DBG) log("hideDataDisconnectedRoaming()...");
561 mNotificationManager.cancel(DATA_DISCONNECTED_ROAMING_NOTIFICATION);
562 }
563
564 /**
565 * Display the network selection "no service" notification
566 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700567 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700569 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 if (DBG) log("showNetworkSelection(" + operator + ")...");
571
fionaxu8b7620d2017-05-01 16:22:17 -0700572 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700573 .setSmallIcon(android.R.drawable.stat_sys_warning)
574 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
575 .setContentText(
576 mContext.getString(R.string.notification_network_selection_text, operator))
577 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700578 .setOngoing(true)
fionaxu8b7620d2017-05-01 16:22:17 -0700579 .setChannel(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580
581 // create the target network operators settings intent
582 Intent intent = new Intent(Intent.ACTION_MAIN);
583 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
584 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700585 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700586 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700587 mContext.getString(R.string.mobile_network_settings_package),
588 mContext.getString(R.string.mobile_network_settings_class)));
Jayachandran C2ef9a482017-05-12 22:07:47 -0700589 intent.putExtra(GsmUmtsOptions.EXTRA_SUB_ID, subId);
fionaxu96ceebd2017-08-24 12:12:32 -0700590 builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
591 mNotificationManager.notifyAsUser(
592 null /* tag */,
593 SELECTED_OPERATOR_FAIL_NOTIFICATION,
594 builder.build(),
595 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 }
597
598 /**
599 * Turn off the network selection "no service" notification
600 */
601 private void cancelNetworkSelection() {
602 if (DBG) log("cancelNetworkSelection()...");
Andrew Lee99d0ac22014-10-10 13:18:04 -0700603 mNotificationManager.cancelAsUser(
604 null /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 }
606
607 /**
608 * Update notification about no service of user selected operator
609 *
610 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700611 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700613 void updateNetworkSelection(int serviceState, int subId) {
614 int phoneId = SubscriptionManager.getPhoneId(subId);
615 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
616 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
617 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800618 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700619 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
620 String selectedNetworkOperatorName =
621 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
622 // get the shared preference of network_selection.
623 // empty is auto mode, otherwise it is the operator alpha name
624 // in case there is no operator name, check the operator numeric
625 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
626 selectedNetworkOperatorName =
627 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
628 }
629 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700630 // if restoring manual selection is controlled by framework, then get network
631 // selection from shared preference, otherwise get from real network indicators.
632 boolean restoreSelection = !mContext.getResources().getBoolean(
633 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700634 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700635 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
636 } else {
fionaxud6aac662018-03-14 16:44:29 -0700637 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800638 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700639
fionaxud6aac662018-03-14 16:44:29 -0700640 if (DBG) {
641 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
642 + (isManualSelection ? selectedNetworkOperatorName : ""));
643 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644
fionaxud6aac662018-03-14 16:44:29 -0700645 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE && isManualSelection) {
646 showNetworkSelection(selectedNetworkOperatorName, subId);
Hall Liu2b846c72016-02-09 18:21:24 -0800647 mSelectedUnavailableNotify = true;
Amit Mahajana60be872015-01-15 16:05:08 -0800648 } else {
649 if (mSelectedUnavailableNotify) {
650 cancelNetworkSelection();
651 mSelectedUnavailableNotify = false;
652 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700653 }
654 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800655 if (DBG) log("updateNetworkSelection()..." + "state = " +
656 serviceState + " not updating network due to invalid subId " + subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 }
658 }
659 }
660
661 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
662 if (mToast != null) {
663 mToast.cancel();
664 }
665
666 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
667 mToast.show();
668 }
669
670 private void log(String msg) {
671 Log.d(LOG_TAG, msg);
672 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700673
674 private void logi(String msg) {
675 Log.i(LOG_TAG, msg);
676 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700677}