blob: d2c0e6b22df1869b96b85b5fa0ed40653bfbe987 [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
Rambo Wangc30938c2022-08-10 22:54:23 +000021import android.annotation.NonNull;
chen xubaf9fe52019-07-02 17:28:24 -070022import android.annotation.Nullable;
Tyler Gunn99ae2692020-12-09 11:35:21 -080023import android.app.BroadcastOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.app.Notification;
25import android.app.NotificationManager;
26import android.app.PendingIntent;
27import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.Context;
30import android.content.Intent;
31import android.content.SharedPreferences;
Jordan Liu9ddde022019-08-05 13:49:08 -070032import android.content.pm.PackageManager;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080033import android.content.pm.ResolveInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080034import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.net.Uri;
irisykyang25202462018-11-13 14:49:54 +080036import android.os.Handler;
37import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070038import android.os.PersistableBundle;
irisykyang25202462018-11-13 14:49:54 +080039import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070041import android.os.UserHandle;
42import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.provider.ContactsContract.PhoneLookup;
Jeff Davidson6d9bf522017-11-03 14:51:13 -070045import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070046import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080047import android.telecom.PhoneAccountHandle;
48import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070049import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.telephony.PhoneNumberUtils;
Rambo Wangc30938c2022-08-10 22:54:23 +000051import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080053import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080054import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080055import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080057import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.util.Log;
irisykyang25202462018-11-13 14:49:54 +080059import android.util.SparseArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070061
rambowang7d6f4142022-08-13 20:45:20 -050062import com.android.internal.annotations.VisibleForTesting;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070064import com.android.internal.telephony.PhoneFactory;
Rambo Wangc30938c2022-08-10 22:54:23 +000065import com.android.internal.telephony.RILConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070067import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070068import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070069
Meng Wang1a19e442019-10-11 10:09:00 -070070import java.util.ArrayList;
chen xubaf9fe52019-07-02 17:28:24 -070071import java.util.HashSet;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080072import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070073import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080074import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070075
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076/**
77 * NotificationManager-related utility code for the Phone app.
78 *
79 * This is a singleton object which acts as the interface to the
80 * framework's NotificationManager, and is used to display status bar
81 * icons and control other status bar-related behavior.
82 *
83 * @see PhoneGlobals.notificationMgr
84 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070085public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080086 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087 private static final boolean DBG =
88 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
89 // Do not check in with VDBG = true, since that may write PII to the system log.
90 private static final boolean VDBG = false;
91
Ta-wei Yenb29425b2016-09-21 17:28:14 -070092 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
93 "mwi_should_check_vvm_configuration_state_";
94
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070096 static final int MMI_NOTIFICATION = 1;
97 static final int NETWORK_SELECTION_NOTIFICATION = 2;
98 static final int VOICEMAIL_NOTIFICATION = 3;
99 static final int CALL_FORWARD_NOTIFICATION = 4;
Jordan Liuc353b162019-07-23 15:54:41 -0700100 static final int DATA_ROAMING_NOTIFICATION = 5;
Santos Cordonf68db2e2014-07-02 14:40:44 -0700101 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
chen xubaf9fe52019-07-02 17:28:24 -0700102 static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103
irisykyang25202462018-11-13 14:49:54 +0800104 // Event for network selection notification.
105 private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1;
106
107 private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L;
108 private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10;
109
110 private static final int STATE_UNKNOWN_SERVICE = -1;
111
chen xubaf9fe52019-07-02 17:28:24 -0700112 private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST";
113
Tyler Gunn99ae2692020-12-09 11:35:21 -0800114 /**
115 * Grant recipients of new voicemail broadcasts a 10sec allowlist so they can start a background
116 * service to do VVM processing.
117 */
118 private final long VOICEMAIL_ALLOW_LIST_DURATION_MILLIS = 10000L;
119
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 /** The singleton NotificationMgr instance. */
121 private static NotificationMgr sInstance;
122
123 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
125 private Context mContext;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700127 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800129 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800130 private TelecomManager mTelecomManager;
131 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132
irisykyang25202462018-11-13 14:49:54 +0800133 // used to track the notification of selected network unavailable, per subscription id.
134 private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135
chen xubaf9fe52019-07-02 17:28:24 -0700136 // used to track the notification of limited sim function under dual sim, per subscription id.
137 private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>();
138
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800139 // used to track whether the message waiting indicator is visible, per subscription id.
140 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
141
irisykyang25202462018-11-13 14:49:54 +0800142 // those flags are used to track whether to show network selection notification or not.
143 private SparseArray<Integer> mPreviousServiceState = new SparseArray<>();
144 private SparseArray<Long> mOOSTimestamp = new SparseArray<>();
145 private SparseArray<Integer> mPendingEventCounter = new SparseArray<>();
146 // maps each subId to selected network operator name.
147 private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>();
148
149 private final Handler mHandler = new Handler() {
150 @Override
151 public void handleMessage(Message msg) {
152 switch (msg.what) {
153 case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION:
154 int subId = (int) msg.obj;
155 TelephonyManager telephonyManager =
156 mTelephonyManager.createForSubscriptionId(subId);
157 if (telephonyManager.getServiceState() != null) {
158 shouldShowNotification(telephonyManager.getServiceState().getState(),
159 subId);
160 }
161 break;
162 }
163 }
164 };
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 /**
167 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700168 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 */
rambowang7d6f4142022-08-13 20:45:20 -0500170 @VisibleForTesting
171 /* package */ NotificationMgr(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 mApp = app;
173 mContext = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 mStatusBarManager =
175 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700176 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800177 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700178 mTelecomManager = app.getSystemService(TelecomManager.class);
Andrew Leed5165b02014-12-05 15:53:58 -0800179 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180 }
181
182 /**
183 * Initialize the singleton NotificationMgr instance.
184 *
185 * This is only done once, at startup, from PhoneApp.onCreate().
186 * From then on, the NotificationMgr instance is available via the
187 * PhoneApp's public "notificationMgr" field, which is why there's no
188 * getInstance() method here.
189 */
190 /* package */ static NotificationMgr init(PhoneGlobals app) {
191 synchronized (NotificationMgr.class) {
192 if (sInstance == null) {
193 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700194 } else {
195 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
196 }
197 return sInstance;
198 }
199 }
200
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 /** The projection to use when querying the phones table */
202 static final String[] PHONES_PROJECTION = new String[] {
203 PhoneLookup.NUMBER,
204 PhoneLookup.DISPLAY_NAME,
205 PhoneLookup._ID
206 };
207
208 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800209 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
210 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
211 * settings screen. The voicemail notification sound is suppressed.
212 *
213 * @param subId The subscription Id.
214 */
215 /* package */ void refreshMwi(int subId) {
216 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
217 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800218 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800219 if (mMwiVisible.keySet().size() == 1) {
220 Set<Integer> keySet = mMwiVisible.keySet();
221 Iterator<Integer> keyIt = keySet.iterator();
222 if (!keyIt.hasNext()) {
223 return;
224 }
225 subId = keyIt.next();
226 }
227 }
228 if (mMwiVisible.containsKey(subId)) {
229 boolean mwiVisible = mMwiVisible.get(subId);
230 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900231 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800232 }
233 }
234 }
235
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700236 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
237 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
238 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
239 + subId);
240 return;
241 }
242
243 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
244 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
245 .apply();
246 }
247
248 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
249 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
250 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
251 return true;
252 }
253 return PreferenceManager
254 .getDefaultSharedPreferences(mContext)
255 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
256 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800257 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 * Updates the message waiting indicator (voicemail) notification.
259 *
260 * @param visible true if there are messages waiting
261 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800262 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700263 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800264 }
265
266 /**
267 * Updates the message waiting indicator (voicemail) notification.
268 *
269 * @param subId the subId to update.
270 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700271 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
272 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800273 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700274 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800275 if (!PhoneGlobals.sVoiceCapable) {
276 // Do not show the message waiting indicator on devices which are not voice capable.
277 // These events *should* be blocked at the telephony layer for such devices.
278 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
279 return;
280 }
281
Nancy Chen2cf7f292015-05-15 11:00:10 -0700282 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800283 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800284 mMwiVisible.put(subId, visible);
285
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800287 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800288 Log.w(LOG_TAG, "Found null phone for: " + subId);
289 return;
290 }
291
292 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
293 if (subInfo == null) {
294 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800295 return;
296 }
297
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900299 if (mTelephonyManager.getPhoneCount() > 1) {
300 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
301 : R.drawable.stat_notify_voicemail_sub2;
302 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303
304 // This Notification can get a lot fancier once we have more
305 // information about the current voicemail messages.
306 // (For example, the current voicemail system can't tell
307 // us the caller-id or timestamp of a message, or tell us the
308 // message count.)
309
310 // But for now, the UI is ultra-simple: if the MWI indication
311 // is supposed to be visible, just show a single generic
312 // notification.
313
314 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800315 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 if (DBG) log("- got vm number: '" + vmNumber + "'");
317
Andrew Leea82b8202014-11-21 16:18:28 -0800318 // The voicemail number may be null because:
319 // (1) This phone has no voicemail number.
320 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
321 // happen when the device first boots if we get a MWI notification when we
322 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800323 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
324 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800326 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327 }
328
Bryce Lee5dc90842015-08-11 07:57:14 -0700329 Integer vmCount = null;
330
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800331 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700332 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
334 notificationTitle = String.format(titleFormat, vmCount);
335 }
336
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800337 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
338 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
339
340 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700342 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
343
344 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800345 notificationText = mContext.getString(
346 R.string.notification_voicemail_no_vm_number);
347
348 // If the voicemail number if unknown, instead of calling voicemail, take the user
349 // to the voicemail settings.
350 notificationText = mContext.getString(
351 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700352 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800353 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700354 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800356 if (mTelephonyManager.getPhoneCount() > 1) {
357 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800358 } else {
359 notificationText = String.format(
360 mContext.getString(R.string.notification_voicemail_text_format),
361 PhoneNumberUtils.formatNumber(vmNumber));
362 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800363 intent = new Intent(
364 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700365 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800366 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367 }
368
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800369 PendingIntent pendingIntent =
Shuo Qian9a41a172020-04-24 19:19:31 -0700370 PendingIntent.getActivity(mContext, subId /* requestCode */, intent,
371 PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372
Nancy Chenb4a92702014-12-04 15:57:29 -0800373 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700374 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700375 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700376 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 builder.setSmallIcon(resId)
378 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800379 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 .setContentTitle(notificationTitle)
381 .setContentText(notificationText)
382 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800383 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700384 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700385 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Jordan Liu575c0d02019-07-09 16:29:48 -0700386 .setChannelId(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
Ta-wei Yen282a9702017-05-30 17:32:29 -0700387 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700388
Andrew Lee99d0ac22014-10-10 13:18:04 -0700389 final Notification notification = builder.build();
Meng Wang1a19e442019-10-11 10:09:00 -0700390 List<UserHandle> users = getUsersExcludeDying();
391 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700392 if (!hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700393 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700394 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800395 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700396 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700397 notifyAsUser(
Bryce Lee5dc90842015-08-11 07:57:14 -0700398 Integer.toString(subId) /* tag */,
399 VOICEMAIL_NOTIFICATION,
400 notification,
401 userHandle);
402 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700403 }
404 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700405 } else {
Meng Wang1a19e442019-10-11 10:09:00 -0700406 List<UserHandle> users = getUsersExcludeDying();
407 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700408 if (!hasUserRestriction(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800409 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700410 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800411 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700412 false, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700413 cancelAsUser(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800414 Integer.toString(subId) /* tag */,
415 VOICEMAIL_NOTIFICATION,
416 userHandle);
417 }
418 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700419 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700420 }
421 }
422
Meng Wang1a19e442019-10-11 10:09:00 -0700423 private List<UserHandle> getUsersExcludeDying() {
424 long[] serialNumbersOfUsers =
425 mUserManager.getSerialNumbersOfUsers(/* excludeDying= */ true);
426 List<UserHandle> users = new ArrayList<>(serialNumbersOfUsers.length);
427 for (long serialNumber : serialNumbersOfUsers) {
428 users.add(mUserManager.getUserForSerialNumber(serialNumber));
429 }
430 return users;
431 }
432
Amit Mahajan2f489b72019-10-08 11:21:52 -0700433 private boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
434 final List<UserManager.EnforcingUser> sources = mUserManager
435 .getUserRestrictionSources(restrictionKey, userHandle);
436 return (sources != null && !sources.isEmpty());
437 }
438
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700439 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800440 * Sends a broadcast with the voicemail notification information to the default dialer. This
441 * method is also used to indicate to the default dialer when to clear the
442 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700443 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800444 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700445 * @param count The number of pending voicemail messages to indicate on the notification. A
446 * Value of 0 is passed here to indicate that the notification should be cleared.
447 * @param number The voicemail phone number if specified.
448 * @param pendingIntent The intent that should be passed as the action to be taken.
449 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
450 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800451 * @param userHandle The user to receive the notification. Each user can have their own default
452 * dialer.
453 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700454 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800455 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
456 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700457 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800458
459 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
460 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700461 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700462 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800463 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
464 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700465 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700466 if (count != null) {
467 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
468 }
469
470 // Additional information about the voicemail notification beyond the count is only
471 // present when the count not specified or greater than 0. The value of 0 represents
472 // clearing the notification, which does not require additional information.
473 if (count == null || count > 0) {
474 if (!TextUtils.isEmpty(number)) {
475 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
476 }
477
478 if (pendingIntent != null) {
479 intent.putExtra(isSettingsIntent
480 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
481 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
482 pendingIntent);
483 }
484 }
Tyler Gunn99ae2692020-12-09 11:35:21 -0800485
486 BroadcastOptions bopts = BroadcastOptions.makeBasic();
487 bopts.setTemporaryAppWhitelistDuration(VOICEMAIL_ALLOW_LIST_DURATION_MILLIS);
488 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE, bopts.toBundle());
Bryce Lee5dc90842015-08-11 07:57:14 -0700489 return true;
490 }
491
492 return false;
493 }
494
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800495 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700496 String dialerPackage = mContext.getSystemService(TelecomManager.class)
Tyler Gunn83adc052020-01-28 09:12:21 -0800497 .getDefaultDialerPackage(userHandle);
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800498 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
499 .setPackage(dialerPackage);
500 }
501
502 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
503 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
504 if (intent == null) {
505 return false;
506 }
507
508 List<ResolveInfo> receivers = mContext.getPackageManager()
509 .queryBroadcastReceivers(intent, 0);
510 return receivers.size() > 0;
511 }
512
Bryce Lee5dc90842015-08-11 07:57:14 -0700513 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700514 * Updates the message call forwarding indicator notification.
515 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530516 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530518
519 /* package */ void updateCfi(int subId, boolean visible) {
520 updateCfi(subId, visible, false /* isRefresh */);
521 }
522
523 /**
524 * Updates the message call forwarding indicator notification.
525 *
526 * @param visible true if call forwarding enabled
527 */
528 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunna584e2c2017-09-19 11:40:12 -0700529 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 if (visible) {
531 // If Unconditional Call Forwarding (forward all calls) for VOICE
532 // is enabled, just show a notification. We'll default to expanded
533 // view for now, so the there is less confusion about the icon. If
534 // it is deemed too weird to have CF indications as expanded views,
535 // then we'll flip the flag back.
536
537 // TODO: We may want to take a look to see if the notification can
538 // display the target to forward calls to. This will require some
539 // effort though, since there are multiple layers of messages that
540 // will need to propagate that information.
541
Andrew Leed5165b02014-12-05 15:53:58 -0800542 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
543 if (subInfo == null) {
544 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
545 return;
546 }
547
548 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900549 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800550 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900551 int slotId = SubscriptionManager.getSlotIndex(subId);
552 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
553 : R.drawable.stat_sys_phone_call_forward_sub2;
Sarah Chin2c5f23d2020-11-02 21:52:11 -0800554 if (subInfo.getDisplayName() != null) {
555 notificationTitle = subInfo.getDisplayName().toString();
556 } else {
557 notificationTitle = mContext.getString(R.string.labelCF);
558 }
Andrew Leed5165b02014-12-05 15:53:58 -0800559 } else {
560 notificationTitle = mContext.getString(R.string.labelCF);
561 }
562
fionaxu8b7620d2017-05-01 16:22:17 -0700563 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900564 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800565 .setColor(subInfo.getIconTint())
566 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700567 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
568 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700569 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700570 .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530571 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572
Tyler Gunn5bc21712021-04-30 15:07:59 -0700573 Intent intent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800574 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800575 SubscriptionInfoHelper.addExtrasToIntent(
576 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700577 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
Shuo Qian9a41a172020-04-24 19:19:31 -0700578 intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700579 notifyAsUser(
fionaxu96ceebd2017-08-24 12:12:32 -0700580 Integer.toString(subId) /* tag */,
581 CALL_FORWARD_NOTIFICATION,
582 builder.build(),
583 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 } else {
Meng Wang21487ab2019-11-21 10:44:42 -0800585 List<UserHandle> users = getUsersExcludeDying();
586 for (UserHandle user : users) {
587 if (mUserManager.isManagedProfile(user.getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900588 continue;
589 }
Jordan Liu9ddde022019-08-05 13:49:08 -0700590 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900591 Integer.toString(subId) /* tag */,
592 CALL_FORWARD_NOTIFICATION,
Meng Wang21487ab2019-11-21 10:44:42 -0800593 user);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900594 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 }
596 }
597
598 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700599 * Shows either:
600 * 1) the "Data roaming is on" notification, which
601 * appears when you're roaming and you have the "data roaming" feature turned on for the
602 * given {@code subId}.
603 * or
604 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700606 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700607 * @param subId which subscription it's notifying about.
608 * @param roamingOn whether currently roaming is on or off. If true, we show notification
609 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610 */
Jordan Liuc353b162019-07-23 15:54:41 -0700611 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
612 if (DBG) {
613 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
614 + " on subId " + subId);
615 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700616
617 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700618 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700619 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700620 PendingIntent contentIntent = PendingIntent.getActivity(
621 mContext, subId, intent, PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622
Jordan Liuc353b162019-07-23 15:54:41 -0700623 CharSequence contentTitle = mContext.getText(roamingOn
624 ? R.string.roaming_on_notification_title
625 : R.string.roaming_notification_title);
626 CharSequence contentText = mContext.getText(roamingOn
627 ? R.string.roaming_enabled_message
628 : R.string.roaming_reenable_message);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700629
fionaxu8b7620d2017-05-01 16:22:17 -0700630 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700631 .setSmallIcon(android.R.drawable.stat_sys_warning)
Jordan Liuc353b162019-07-23 15:54:41 -0700632 .setContentTitle(contentTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700633 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700634 .setContentText(contentText)
Jordan Liu575c0d02019-07-09 16:29:48 -0700635 .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
fionaxu96ceebd2017-08-24 12:12:32 -0700636 .setContentIntent(contentIntent);
637 final Notification notif =
638 new Notification.BigTextStyle(builder).bigText(contentText).build();
Jordan Liu9ddde022019-08-05 13:49:08 -0700639 notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
640 }
641
642 private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
643 try {
644 Context contextForUser =
645 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
646 NotificationManager notificationManager =
647 (NotificationManager) contextForUser.getSystemService(
648 Context.NOTIFICATION_SERVICE);
649 notificationManager.notify(tag, id, notification);
650 } catch (PackageManager.NameNotFoundException e) {
651 Log.e(LOG_TAG, "unable to notify for user " + user);
652 e.printStackTrace();
653 }
654 }
655
656 private void cancelAsUser(String tag, int id, UserHandle user) {
657 try {
658 Context contextForUser =
659 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
660 NotificationManager notificationManager =
661 (NotificationManager) contextForUser.getSystemService(
662 Context.NOTIFICATION_SERVICE);
663 notificationManager.cancel(tag, id);
664 } catch (PackageManager.NameNotFoundException e) {
665 Log.e(LOG_TAG, "unable to cancel for user " + user);
666 e.printStackTrace();
667 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 }
669
670 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700671 * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 */
Jordan Liuc353b162019-07-23 15:54:41 -0700673 /* package */ void hideDataRoamingNotification() {
674 if (DBG) log("hideDataRoamingNotification()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700675 cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700676 }
677
678 /**
chen xubaf9fe52019-07-02 17:28:24 -0700679 * Shows the "Limited SIM functionality" warning notification, which appears when using a
680 * special carrier under dual sim. limited function applies for DSDS in general when two SIM
681 * cards share a single radio, thus the voice & data maybe impaired under certain scenarios.
682 */
683 public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) {
684 if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName
685 + " subId: " + subId);
686 if (mLimitedSimFunctionNotify.contains(subId)) {
687 // handle the case that user swipe the notification but condition triggers
688 // frequently which cause the same notification consistently displayed.
689 if (DBG) log("showLimitedSimFunctionWarningNotification, "
690 + "not display again if already displayed");
691 return;
692 }
693 // Navigate to "Network Selection Settings" which list all subscriptions.
694 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
Shuo Qian9a41a172020-04-24 19:19:31 -0700695 new Intent(ACTION_MOBILE_NETWORK_LIST), PendingIntent.FLAG_IMMUTABLE);
Chen Xue1bbfd22019-09-18 17:17:31 -0700696 // Display phone number from the other sub
697 String line1Num = null;
698 SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService(
699 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
700 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
701 for (SubscriptionInfo sub : subList) {
702 if (sub.getSubscriptionId() != subId) {
703 line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId());
704 }
705 }
chen xubaf9fe52019-07-02 17:28:24 -0700706 final CharSequence contentText = TextUtils.isEmpty(line1Num) ?
707 String.format(mContext.getText(
Chen Xu251ce8f2019-09-13 20:24:22 -0700708 R.string.limited_sim_function_notification_message).toString(), carrierName) :
chen xubaf9fe52019-07-02 17:28:24 -0700709 String.format(mContext.getText(
710 R.string.limited_sim_function_with_phone_num_notification_message).toString(),
Chen Xu251ce8f2019-09-13 20:24:22 -0700711 carrierName, line1Num);
chen xubaf9fe52019-07-02 17:28:24 -0700712 final Notification.Builder builder = new Notification.Builder(mContext)
713 .setSmallIcon(R.drawable.ic_sim_card)
714 .setContentTitle(mContext.getText(
715 R.string.limited_sim_function_notification_title))
716 .setContentText(contentText)
717 .setOnlyAlertOnce(true)
718 .setOngoing(true)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700719 .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY)
chen xubaf9fe52019-07-02 17:28:24 -0700720 .setContentIntent(contentIntent);
721 final Notification notification = new Notification.BigTextStyle(builder).bigText(
722 contentText).build();
723
Jordan Liu9ddde022019-08-05 13:49:08 -0700724 notifyAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700725 LIMITED_SIM_FUNCTION_NOTIFICATION,
726 notification, UserHandle.ALL);
727 mLimitedSimFunctionNotify.add(subId);
728 }
729
730 /**
731 * Dismiss the "Limited SIM functionality" warning notification for the given subId.
732 */
733 public void dismissLimitedSimFunctionWarningNotification(int subId) {
734 if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId);
735 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
736 // dismiss all notifications
737 for (int id : mLimitedSimFunctionNotify) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700738 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700739 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
740 }
741 mLimitedSimFunctionNotify.clear();
742 } else if (mLimitedSimFunctionNotify.contains(subId)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700743 cancelAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700744 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
745 mLimitedSimFunctionNotify.remove(subId);
746 }
747 }
748
749 /**
750 * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions.
751 */
752 public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() {
753 if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs");
754 // dismiss notification for inactive subscriptions.
755 // handle the corner case that SIM change by SIM refresh doesn't clear the notification
756 // from the old SIM if both old & new SIM configured to display the notification.
757 mLimitedSimFunctionNotify.removeIf(id -> {
758 if (!mSubscriptionManager.isActiveSubId(id)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700759 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700760 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
761 return true;
762 }
763 return false;
764 });
765 }
766
767 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 * Display the network selection "no service" notification
769 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700770 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700772 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 if (DBG) log("showNetworkSelection(" + operator + ")...");
774
Youming Ye0509b532018-09-14 16:21:17 -0700775 if (!TextUtils.isEmpty(operator)) {
776 operator = String.format(" (%s)", operator);
777 }
fionaxu8b7620d2017-05-01 16:22:17 -0700778 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700779 .setSmallIcon(android.R.drawable.stat_sys_warning)
780 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
781 .setContentText(
782 mContext.getString(R.string.notification_network_selection_text, operator))
783 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700784 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700785 .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786
787 // create the target network operators settings intent
788 Intent intent = new Intent(Intent.ACTION_MAIN);
789 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
790 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700791 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700792 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700793 mContext.getString(R.string.mobile_network_settings_package),
794 mContext.getString(R.string.mobile_network_settings_class)));
Pengquan Meng984ba422019-09-05 18:00:06 -0700795 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700796 builder.setContentIntent(
797 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700798 notifyAsUser(
irisykyang25202462018-11-13 14:49:54 +0800799 Integer.toString(subId) /* tag */,
fionaxu96ceebd2017-08-24 12:12:32 -0700800 SELECTED_OPERATOR_FAIL_NOTIFICATION,
801 builder.build(),
802 UserHandle.ALL);
irisykyang25202462018-11-13 14:49:54 +0800803 mSelectedUnavailableNotify.put(subId, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700804 }
805
806 /**
807 * Turn off the network selection "no service" notification
808 */
irisykyang25202462018-11-13 14:49:54 +0800809 private void cancelNetworkSelection(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 if (DBG) log("cancelNetworkSelection()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700811 cancelAsUser(
irisykyang25202462018-11-13 14:49:54 +0800812 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
813 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 }
815
816 /**
817 * Update notification about no service of user selected operator
818 *
819 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700820 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700822 void updateNetworkSelection(int serviceState, int subId) {
823 int phoneId = SubscriptionManager.getPhoneId(subId);
824 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
825 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
826 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800827 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700828 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
829 String selectedNetworkOperatorName =
830 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
831 // get the shared preference of network_selection.
832 // empty is auto mode, otherwise it is the operator alpha name
833 // in case there is no operator name, check the operator numeric
834 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
835 selectedNetworkOperatorName =
836 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
837 }
838 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700839 // if restoring manual selection is controlled by framework, then get network
840 // selection from shared preference, otherwise get from real network indicators.
Daniel Brightebb4eb72020-02-18 15:16:33 -0800841 boolean restoreSelection = !mContext.getResources().getBoolean(
842 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700843 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700844 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
845 } else {
fionaxud6aac662018-03-14 16:44:29 -0700846 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848
fionaxud6aac662018-03-14 16:44:29 -0700849 if (DBG) {
850 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
851 + (isManualSelection ? selectedNetworkOperatorName : ""));
852 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853
irisykyang25202462018-11-13 14:49:54 +0800854 if (isManualSelection) {
855 mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName);
856 shouldShowNotification(serviceState, subId);
Amit Mahajana60be872015-01-15 16:05:08 -0800857 } else {
irisykyang25202462018-11-13 14:49:54 +0800858 dismissNetworkSelectionNotification(subId);
859 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 }
861 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800862 if (DBG) log("updateNetworkSelection()..." + "state = " +
863 serviceState + " not updating network due to invalid subId " + subId);
irisykyang25202462018-11-13 14:49:54 +0800864 dismissNetworkSelectionNotificationForInactiveSubId();
865 }
866 }
867 }
868
869 private void dismissNetworkSelectionNotification(int subId) {
870 if (mSelectedUnavailableNotify.get(subId, false)) {
871 cancelNetworkSelection(subId);
872 mSelectedUnavailableNotify.remove(subId);
873 }
874 }
875
876 private void dismissNetworkSelectionNotificationForInactiveSubId() {
877 for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) {
878 int subId = mSelectedUnavailableNotify.keyAt(i);
879 if (!mSubscriptionManager.isActiveSubId(subId)) {
880 dismissNetworkSelectionNotification(subId);
881 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 }
883 }
884 }
885
886 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
887 if (mToast != null) {
888 mToast.cancel();
889 }
890
891 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
892 mToast.show();
893 }
894
895 private void log(String msg) {
896 Log.d(LOG_TAG, msg);
897 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700898
899 private void logi(String msg) {
900 Log.i(LOG_TAG, msg);
901 }
irisykyang25202462018-11-13 14:49:54 +0800902
irisykyang25202462018-11-13 14:49:54 +0800903 private void shouldShowNotification(int serviceState, int subId) {
Rambo Wangc30938c2022-08-10 22:54:23 +0000904 // "Network selection unavailable" notification should only show when network selection is
905 // visible to the end user. Some CC items e.g. KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL
906 // can be overridden to hide the network selection to the end user. In this case, the
907 // notification is not shown to avoid confusion to the end user.
908 if (!shouldDisplayNetworkSelectOptions(subId)) {
909 logi("Carrier configs refuse to show network selection not available notification");
910 return;
911 }
912
913 // In case network selection notification shows up repeatedly under
914 // unstable network condition. The logic is to check whether or not
915 // the service state keeps in no service condition for at least
916 // {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}.
917 // And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times.
918 // To avoid the notification showing up for the momentary state.
irisykyang25202462018-11-13 14:49:54 +0800919 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
920 if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE)
921 != ServiceState.STATE_OUT_OF_SERVICE) {
922 mOOSTimestamp.put(subId, getTimeStamp());
923 }
924 if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L)
925 >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS)
926 || mPendingEventCounter.get(subId, 0)
927 > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) {
928 showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId);
929 clearUpNetworkSelectionNotificationParam(subId);
930 } else {
931 startPendingNetworkSelectionNotification(subId);
932 }
933 } else {
934 dismissNetworkSelectionNotification(subId);
935 }
936 mPreviousServiceState.put(subId, serviceState);
937 if (DBG) {
938 log("shouldShowNotification()..." + " subId = " + subId
939 + " serviceState = " + serviceState
940 + " mOOSTimestamp = " + mOOSTimestamp
941 + " mPendingEventCounter = " + mPendingEventCounter);
942 }
943 }
944
Rambo Wangc30938c2022-08-10 22:54:23 +0000945 // TODO(b/243010310): merge methods below with Settings#MobileNetworkUtils and optimize them.
946 // The methods below are copied from com.android.settings.network.telephony.MobileNetworkUtils
947 // to make sure the network selection unavailable notification should not show when Network
948 // Selection menu is not present in Settings app.
949 private boolean shouldDisplayNetworkSelectOptions(int subId) {
950 final TelephonyManager telephonyManager = mTelephonyManager.createForSubscriptionId(subId);
951 final CarrierConfigManager carrierConfigManager = mContext.getSystemService(
952 CarrierConfigManager.class);
953 final PersistableBundle carrierConfig = carrierConfigManager.getConfigForSubId(subId);
954
955 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID
956 || carrierConfig == null
957 || !carrierConfig.getBoolean(
958 CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL)
959 || carrierConfig.getBoolean(
960 CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
961 || (carrierConfig.getBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL)
962 && !telephonyManager.isManualNetworkSelectionAllowed())) {
963 return false;
964 }
965
966 if (isWorldMode(carrierConfig)) {
967 final int networkMode = RadioAccessFamily.getNetworkTypeFromRaf(
968 (int) telephonyManager.getAllowedNetworkTypesForReason(
969 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
970 if (networkMode == RILConstants.NETWORK_MODE_LTE_CDMA_EVDO) {
971 return false;
972 }
973 if (shouldSpeciallyUpdateGsmCdma(telephonyManager, carrierConfig)) {
974 return false;
975 }
976 if (networkMode == RILConstants.NETWORK_MODE_LTE_GSM_WCDMA) {
977 return true;
978 }
979 }
980
981 return isGsmBasicOptions(telephonyManager, carrierConfig);
982 }
983
984 private static boolean isWorldMode(@NonNull PersistableBundle carrierConfig) {
985 return carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL);
986 }
987
988 private static boolean shouldSpeciallyUpdateGsmCdma(@NonNull TelephonyManager telephonyManager,
989 @NonNull PersistableBundle carrierConfig) {
990 if (!isWorldMode(carrierConfig)) {
991 return false;
992 }
993
994 final int networkMode = RadioAccessFamily.getNetworkTypeFromRaf(
995 (int) telephonyManager.getAllowedNetworkTypesForReason(
996 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER));
997 if (networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM
998 || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA
999 || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA
1000 || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA
1001 || networkMode
1002 == RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA
1003 || networkMode == RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA) {
1004 if (!isTdscdmaSupported(telephonyManager, carrierConfig)) {
1005 return true;
1006 }
1007 }
1008
1009 return false;
1010 }
1011
1012 private static boolean isTdscdmaSupported(@NonNull TelephonyManager telephonyManager,
1013 @NonNull PersistableBundle carrierConfig) {
1014 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL)) {
1015 return true;
1016 }
1017 final String[] numericArray = carrierConfig.getStringArray(
1018 CarrierConfigManager.KEY_SUPPORT_TDSCDMA_ROAMING_NETWORKS_STRING_ARRAY);
1019 if (numericArray == null) {
1020 return false;
1021 }
1022 final ServiceState serviceState = telephonyManager.getServiceState();
1023 final String operatorNumeric =
1024 (serviceState != null) ? serviceState.getOperatorNumeric() : null;
1025 if (operatorNumeric == null) {
1026 return false;
1027 }
1028 for (String numeric : numericArray) {
1029 if (operatorNumeric.equals(numeric)) {
1030 return true;
1031 }
1032 }
1033 return false;
1034 }
1035
1036 private static boolean isGsmBasicOptions(@NonNull TelephonyManager telephonyManager,
1037 @NonNull PersistableBundle carrierConfig) {
1038 if (!carrierConfig.getBoolean(
1039 CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
1040 && carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) {
1041 return true;
1042 }
1043
1044 if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
1045 return true;
1046 }
1047
1048 return false;
1049 }
1050 // END of TODO:(b/243010310): merge methods above with Settings#MobileNetworkUtils and optimize.
1051
irisykyang25202462018-11-13 14:49:54 +08001052 private void startPendingNetworkSelectionNotification(int subId) {
1053 if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
1054 if (DBG) {
1055 log("startPendingNetworkSelectionNotification: subId = " + subId);
1056 }
1057 mHandler.sendMessageDelayed(
1058 mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId),
1059 NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS);
1060 mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1);
1061 }
1062 }
1063
1064 private void clearUpNetworkSelectionNotificationParam(int subId) {
1065 if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
1066 mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId);
1067 }
1068 mPreviousServiceState.remove(subId);
1069 mOOSTimestamp.remove(subId);
1070 mPendingEventCounter.remove(subId);
1071 mSelectedNetworkOperatorName.remove(subId);
1072 }
1073
rambowang19b7b572023-01-25 16:07:08 -06001074 @VisibleForTesting
1075 public long getTimeStamp() {
irisykyang25202462018-11-13 14:49:54 +08001076 return SystemClock.elapsedRealtime();
1077 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078}