blob: 1be727385910f8921177dfcbce18c92f99819d92 [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
chen xubaf9fe52019-07-02 17:28:24 -070021import android.annotation.Nullable;
Tyler Gunn99ae2692020-12-09 11:35:21 -080022import android.app.BroadcastOptions;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Notification;
24import android.app.NotificationManager;
25import android.app.PendingIntent;
26import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.Context;
29import android.content.Intent;
30import android.content.SharedPreferences;
Jordan Liu9ddde022019-08-05 13:49:08 -070031import android.content.pm.PackageManager;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080032import android.content.pm.ResolveInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080033import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.net.Uri;
irisykyang25202462018-11-13 14:49:54 +080035import android.os.Handler;
36import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070037import android.os.PersistableBundle;
irisykyang25202462018-11-13 14:49:54 +080038import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070040import android.os.UserHandle;
41import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.provider.ContactsContract.PhoneLookup;
Jeff Davidson6d9bf522017-11-03 14:51:13 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080046import android.telecom.PhoneAccountHandle;
47import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.PhoneNumberUtils;
50import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080051import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080052import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080053import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080055import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
irisykyang25202462018-11-13 14:49:54 +080057import android.util.SparseArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070059
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070061import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070063import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070064import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070065
Meng Wang1a19e442019-10-11 10:09:00 -070066import java.util.ArrayList;
chen xubaf9fe52019-07-02 17:28:24 -070067import java.util.HashSet;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080068import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070069import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080070import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070071
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072/**
73 * NotificationManager-related utility code for the Phone app.
74 *
75 * This is a singleton object which acts as the interface to the
76 * framework's NotificationManager, and is used to display status bar
77 * icons and control other status bar-related behavior.
78 *
79 * @see PhoneGlobals.notificationMgr
80 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070081public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080082 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 private static final boolean DBG =
84 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
85 // Do not check in with VDBG = true, since that may write PII to the system log.
86 private static final boolean VDBG = false;
87
Ta-wei Yenb29425b2016-09-21 17:28:14 -070088 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
89 "mwi_should_check_vvm_configuration_state_";
90
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070092 static final int MMI_NOTIFICATION = 1;
93 static final int NETWORK_SELECTION_NOTIFICATION = 2;
94 static final int VOICEMAIL_NOTIFICATION = 3;
95 static final int CALL_FORWARD_NOTIFICATION = 4;
Jordan Liuc353b162019-07-23 15:54:41 -070096 static final int DATA_ROAMING_NOTIFICATION = 5;
Santos Cordonf68db2e2014-07-02 14:40:44 -070097 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
chen xubaf9fe52019-07-02 17:28:24 -070098 static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
irisykyang25202462018-11-13 14:49:54 +0800100 // Event for network selection notification.
101 private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1;
102
103 private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L;
104 private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10;
105
106 private static final int STATE_UNKNOWN_SERVICE = -1;
107
chen xubaf9fe52019-07-02 17:28:24 -0700108 private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST";
109
Tyler Gunn99ae2692020-12-09 11:35:21 -0800110 /**
111 * Grant recipients of new voicemail broadcasts a 10sec allowlist so they can start a background
112 * service to do VVM processing.
113 */
114 private final long VOICEMAIL_ALLOW_LIST_DURATION_MILLIS = 10000L;
115
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 /** The singleton NotificationMgr instance. */
117 private static NotificationMgr sInstance;
118
119 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120
121 private Context mContext;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700123 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800125 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800126 private TelecomManager mTelecomManager;
127 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
irisykyang25202462018-11-13 14:49:54 +0800129 // used to track the notification of selected network unavailable, per subscription id.
130 private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700131
chen xubaf9fe52019-07-02 17:28:24 -0700132 // used to track the notification of limited sim function under dual sim, per subscription id.
133 private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>();
134
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800135 // used to track whether the message waiting indicator is visible, per subscription id.
136 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
137
irisykyang25202462018-11-13 14:49:54 +0800138 // those flags are used to track whether to show network selection notification or not.
139 private SparseArray<Integer> mPreviousServiceState = new SparseArray<>();
140 private SparseArray<Long> mOOSTimestamp = new SparseArray<>();
141 private SparseArray<Integer> mPendingEventCounter = new SparseArray<>();
142 // maps each subId to selected network operator name.
143 private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>();
144
145 private final Handler mHandler = new Handler() {
146 @Override
147 public void handleMessage(Message msg) {
148 switch (msg.what) {
149 case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION:
150 int subId = (int) msg.obj;
151 TelephonyManager telephonyManager =
152 mTelephonyManager.createForSubscriptionId(subId);
153 if (telephonyManager.getServiceState() != null) {
154 shouldShowNotification(telephonyManager.getServiceState().getState(),
155 subId);
156 }
157 break;
158 }
159 }
160 };
161
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 /**
163 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700164 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 */
166 private NotificationMgr(PhoneGlobals app) {
167 mApp = app;
168 mContext = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 mStatusBarManager =
170 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700171 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800172 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700173 mTelecomManager = app.getSystemService(TelecomManager.class);
Andrew Leed5165b02014-12-05 15:53:58 -0800174 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175 }
176
177 /**
178 * Initialize the singleton NotificationMgr instance.
179 *
180 * This is only done once, at startup, from PhoneApp.onCreate().
181 * From then on, the NotificationMgr instance is available via the
182 * PhoneApp's public "notificationMgr" field, which is why there's no
183 * getInstance() method here.
184 */
185 /* package */ static NotificationMgr init(PhoneGlobals app) {
186 synchronized (NotificationMgr.class) {
187 if (sInstance == null) {
188 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 } else {
190 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
191 }
192 return sInstance;
193 }
194 }
195
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 /** The projection to use when querying the phones table */
197 static final String[] PHONES_PROJECTION = new String[] {
198 PhoneLookup.NUMBER,
199 PhoneLookup.DISPLAY_NAME,
200 PhoneLookup._ID
201 };
202
203 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800204 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
205 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
206 * settings screen. The voicemail notification sound is suppressed.
207 *
208 * @param subId The subscription Id.
209 */
210 /* package */ void refreshMwi(int subId) {
211 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
212 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800213 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800214 if (mMwiVisible.keySet().size() == 1) {
215 Set<Integer> keySet = mMwiVisible.keySet();
216 Iterator<Integer> keyIt = keySet.iterator();
217 if (!keyIt.hasNext()) {
218 return;
219 }
220 subId = keyIt.next();
221 }
222 }
223 if (mMwiVisible.containsKey(subId)) {
224 boolean mwiVisible = mMwiVisible.get(subId);
225 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900226 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800227 }
228 }
229 }
230
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700231 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
232 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
233 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
234 + subId);
235 return;
236 }
237
238 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
239 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
240 .apply();
241 }
242
243 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
244 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
245 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
246 return true;
247 }
248 return PreferenceManager
249 .getDefaultSharedPreferences(mContext)
250 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
251 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800252 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 * Updates the message waiting indicator (voicemail) notification.
254 *
255 * @param visible true if there are messages waiting
256 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800257 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700258 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800259 }
260
261 /**
262 * Updates the message waiting indicator (voicemail) notification.
263 *
264 * @param subId the subId to update.
265 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700266 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
267 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800268 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700269 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800270 if (!PhoneGlobals.sVoiceCapable) {
271 // Do not show the message waiting indicator on devices which are not voice capable.
272 // These events *should* be blocked at the telephony layer for such devices.
273 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
274 return;
275 }
276
Nancy Chen2cf7f292015-05-15 11:00:10 -0700277 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800278 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800279 mMwiVisible.put(subId, visible);
280
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800282 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800283 Log.w(LOG_TAG, "Found null phone for: " + subId);
284 return;
285 }
286
287 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
288 if (subInfo == null) {
289 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800290 return;
291 }
292
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900294 if (mTelephonyManager.getPhoneCount() > 1) {
295 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
296 : R.drawable.stat_notify_voicemail_sub2;
297 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298
299 // This Notification can get a lot fancier once we have more
300 // information about the current voicemail messages.
301 // (For example, the current voicemail system can't tell
302 // us the caller-id or timestamp of a message, or tell us the
303 // message count.)
304
305 // But for now, the UI is ultra-simple: if the MWI indication
306 // is supposed to be visible, just show a single generic
307 // notification.
308
309 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800310 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 if (DBG) log("- got vm number: '" + vmNumber + "'");
312
Andrew Leea82b8202014-11-21 16:18:28 -0800313 // The voicemail number may be null because:
314 // (1) This phone has no voicemail number.
315 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
316 // happen when the device first boots if we get a MWI notification when we
317 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800318 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
319 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800321 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 }
323
Bryce Lee5dc90842015-08-11 07:57:14 -0700324 Integer vmCount = null;
325
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800326 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700327 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
329 notificationTitle = String.format(titleFormat, vmCount);
330 }
331
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800332 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
333 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
334
335 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700337 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
338
339 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800340 notificationText = mContext.getString(
341 R.string.notification_voicemail_no_vm_number);
342
343 // If the voicemail number if unknown, instead of calling voicemail, take the user
344 // to the voicemail settings.
345 notificationText = mContext.getString(
346 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700347 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800348 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700349 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800351 if (mTelephonyManager.getPhoneCount() > 1) {
352 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800353 } else {
354 notificationText = String.format(
355 mContext.getString(R.string.notification_voicemail_text_format),
356 PhoneNumberUtils.formatNumber(vmNumber));
357 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800358 intent = new Intent(
359 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700360 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800361 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700362 }
363
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800364 PendingIntent pendingIntent =
Shuo Qian9a41a172020-04-24 19:19:31 -0700365 PendingIntent.getActivity(mContext, subId /* requestCode */, intent,
366 PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700367
Nancy Chenb4a92702014-12-04 15:57:29 -0800368 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700369 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700370 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700371 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700372 builder.setSmallIcon(resId)
373 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800374 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700375 .setContentTitle(notificationTitle)
376 .setContentText(notificationText)
377 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800378 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700379 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700380 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Jordan Liu575c0d02019-07-09 16:29:48 -0700381 .setChannelId(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
Ta-wei Yen282a9702017-05-30 17:32:29 -0700382 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383
Andrew Lee99d0ac22014-10-10 13:18:04 -0700384 final Notification notification = builder.build();
Meng Wang1a19e442019-10-11 10:09:00 -0700385 List<UserHandle> users = getUsersExcludeDying();
386 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700387 if (!hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700388 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700389 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800390 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700391 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700392 notifyAsUser(
Bryce Lee5dc90842015-08-11 07:57:14 -0700393 Integer.toString(subId) /* tag */,
394 VOICEMAIL_NOTIFICATION,
395 notification,
396 userHandle);
397 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700398 }
399 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700400 } else {
Meng Wang1a19e442019-10-11 10:09:00 -0700401 List<UserHandle> users = getUsersExcludeDying();
402 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700403 if (!hasUserRestriction(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800404 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700405 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800406 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700407 false, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700408 cancelAsUser(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800409 Integer.toString(subId) /* tag */,
410 VOICEMAIL_NOTIFICATION,
411 userHandle);
412 }
413 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700414 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700415 }
416 }
417
Meng Wang1a19e442019-10-11 10:09:00 -0700418 private List<UserHandle> getUsersExcludeDying() {
419 long[] serialNumbersOfUsers =
420 mUserManager.getSerialNumbersOfUsers(/* excludeDying= */ true);
421 List<UserHandle> users = new ArrayList<>(serialNumbersOfUsers.length);
422 for (long serialNumber : serialNumbersOfUsers) {
423 users.add(mUserManager.getUserForSerialNumber(serialNumber));
424 }
425 return users;
426 }
427
Amit Mahajan2f489b72019-10-08 11:21:52 -0700428 private boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
429 final List<UserManager.EnforcingUser> sources = mUserManager
430 .getUserRestrictionSources(restrictionKey, userHandle);
431 return (sources != null && !sources.isEmpty());
432 }
433
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800435 * Sends a broadcast with the voicemail notification information to the default dialer. This
436 * method is also used to indicate to the default dialer when to clear the
437 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700438 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800439 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700440 * @param count The number of pending voicemail messages to indicate on the notification. A
441 * Value of 0 is passed here to indicate that the notification should be cleared.
442 * @param number The voicemail phone number if specified.
443 * @param pendingIntent The intent that should be passed as the action to be taken.
444 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
445 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800446 * @param userHandle The user to receive the notification. Each user can have their own default
447 * dialer.
448 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700449 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800450 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
451 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700452 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800453
454 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
455 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700456 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700457 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800458 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
459 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700460 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700461 if (count != null) {
462 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
463 }
464
465 // Additional information about the voicemail notification beyond the count is only
466 // present when the count not specified or greater than 0. The value of 0 represents
467 // clearing the notification, which does not require additional information.
468 if (count == null || count > 0) {
469 if (!TextUtils.isEmpty(number)) {
470 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
471 }
472
473 if (pendingIntent != null) {
474 intent.putExtra(isSettingsIntent
475 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
476 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
477 pendingIntent);
478 }
479 }
Tyler Gunn99ae2692020-12-09 11:35:21 -0800480
481 BroadcastOptions bopts = BroadcastOptions.makeBasic();
482 bopts.setTemporaryAppWhitelistDuration(VOICEMAIL_ALLOW_LIST_DURATION_MILLIS);
483 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE, bopts.toBundle());
Bryce Lee5dc90842015-08-11 07:57:14 -0700484 return true;
485 }
486
487 return false;
488 }
489
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800490 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700491 String dialerPackage = mContext.getSystemService(TelecomManager.class)
Tyler Gunn83adc052020-01-28 09:12:21 -0800492 .getDefaultDialerPackage(userHandle);
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800493 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
494 .setPackage(dialerPackage);
495 }
496
497 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
498 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
499 if (intent == null) {
500 return false;
501 }
502
503 List<ResolveInfo> receivers = mContext.getPackageManager()
504 .queryBroadcastReceivers(intent, 0);
505 return receivers.size() > 0;
506 }
507
Bryce Lee5dc90842015-08-11 07:57:14 -0700508 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700509 * Updates the message call forwarding indicator notification.
510 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530511 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530513
514 /* package */ void updateCfi(int subId, boolean visible) {
515 updateCfi(subId, visible, false /* isRefresh */);
516 }
517
518 /**
519 * Updates the message call forwarding indicator notification.
520 *
521 * @param visible true if call forwarding enabled
522 */
523 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunna584e2c2017-09-19 11:40:12 -0700524 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 if (visible) {
526 // If Unconditional Call Forwarding (forward all calls) for VOICE
527 // is enabled, just show a notification. We'll default to expanded
528 // view for now, so the there is less confusion about the icon. If
529 // it is deemed too weird to have CF indications as expanded views,
530 // then we'll flip the flag back.
531
532 // TODO: We may want to take a look to see if the notification can
533 // display the target to forward calls to. This will require some
534 // effort though, since there are multiple layers of messages that
535 // will need to propagate that information.
536
Andrew Leed5165b02014-12-05 15:53:58 -0800537 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
538 if (subInfo == null) {
539 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
540 return;
541 }
542
543 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900544 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800545 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900546 int slotId = SubscriptionManager.getSlotIndex(subId);
547 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
548 : R.drawable.stat_sys_phone_call_forward_sub2;
Andrew Leed5165b02014-12-05 15:53:58 -0800549 notificationTitle = subInfo.getDisplayName().toString();
550 } else {
551 notificationTitle = mContext.getString(R.string.labelCF);
552 }
553
fionaxu8b7620d2017-05-01 16:22:17 -0700554 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900555 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800556 .setColor(subInfo.getIconTint())
557 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700558 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
559 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700560 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700561 .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530562 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563
Andrew Lee99d0ac22014-10-10 13:18:04 -0700564 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800565 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700566 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800567 SubscriptionInfoHelper.addExtrasToIntent(
568 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700569 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
Shuo Qian9a41a172020-04-24 19:19:31 -0700570 intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700571 notifyAsUser(
fionaxu96ceebd2017-08-24 12:12:32 -0700572 Integer.toString(subId) /* tag */,
573 CALL_FORWARD_NOTIFICATION,
574 builder.build(),
575 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576 } else {
Meng Wang21487ab2019-11-21 10:44:42 -0800577 List<UserHandle> users = getUsersExcludeDying();
578 for (UserHandle user : users) {
579 if (mUserManager.isManagedProfile(user.getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900580 continue;
581 }
Jordan Liu9ddde022019-08-05 13:49:08 -0700582 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900583 Integer.toString(subId) /* tag */,
584 CALL_FORWARD_NOTIFICATION,
Meng Wang21487ab2019-11-21 10:44:42 -0800585 user);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900586 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 }
588 }
589
590 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700591 * Shows either:
592 * 1) the "Data roaming is on" notification, which
593 * appears when you're roaming and you have the "data roaming" feature turned on for the
594 * given {@code subId}.
595 * or
596 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700597 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700598 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700599 * @param subId which subscription it's notifying about.
600 * @param roamingOn whether currently roaming is on or off. If true, we show notification
601 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 */
Jordan Liuc353b162019-07-23 15:54:41 -0700603 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
604 if (DBG) {
605 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
606 + " on subId " + subId);
607 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608
609 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700610 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700611 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700612 PendingIntent contentIntent = PendingIntent.getActivity(
613 mContext, subId, intent, PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614
Jordan Liuc353b162019-07-23 15:54:41 -0700615 CharSequence contentTitle = mContext.getText(roamingOn
616 ? R.string.roaming_on_notification_title
617 : R.string.roaming_notification_title);
618 CharSequence contentText = mContext.getText(roamingOn
619 ? R.string.roaming_enabled_message
620 : R.string.roaming_reenable_message);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621
fionaxu8b7620d2017-05-01 16:22:17 -0700622 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700623 .setSmallIcon(android.R.drawable.stat_sys_warning)
Jordan Liuc353b162019-07-23 15:54:41 -0700624 .setContentTitle(contentTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700625 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700626 .setContentText(contentText)
Jordan Liu575c0d02019-07-09 16:29:48 -0700627 .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
fionaxu96ceebd2017-08-24 12:12:32 -0700628 .setContentIntent(contentIntent);
629 final Notification notif =
630 new Notification.BigTextStyle(builder).bigText(contentText).build();
Jordan Liu9ddde022019-08-05 13:49:08 -0700631 notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
632 }
633
634 private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
635 try {
636 Context contextForUser =
637 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
638 NotificationManager notificationManager =
639 (NotificationManager) contextForUser.getSystemService(
640 Context.NOTIFICATION_SERVICE);
641 notificationManager.notify(tag, id, notification);
642 } catch (PackageManager.NameNotFoundException e) {
643 Log.e(LOG_TAG, "unable to notify for user " + user);
644 e.printStackTrace();
645 }
646 }
647
648 private void cancelAsUser(String tag, int id, UserHandle user) {
649 try {
650 Context contextForUser =
651 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
652 NotificationManager notificationManager =
653 (NotificationManager) contextForUser.getSystemService(
654 Context.NOTIFICATION_SERVICE);
655 notificationManager.cancel(tag, id);
656 } catch (PackageManager.NameNotFoundException e) {
657 Log.e(LOG_TAG, "unable to cancel for user " + user);
658 e.printStackTrace();
659 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660 }
661
662 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700663 * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 */
Jordan Liuc353b162019-07-23 15:54:41 -0700665 /* package */ void hideDataRoamingNotification() {
666 if (DBG) log("hideDataRoamingNotification()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700667 cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 }
669
670 /**
chen xubaf9fe52019-07-02 17:28:24 -0700671 * Shows the "Limited SIM functionality" warning notification, which appears when using a
672 * special carrier under dual sim. limited function applies for DSDS in general when two SIM
673 * cards share a single radio, thus the voice & data maybe impaired under certain scenarios.
674 */
675 public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) {
676 if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName
677 + " subId: " + subId);
678 if (mLimitedSimFunctionNotify.contains(subId)) {
679 // handle the case that user swipe the notification but condition triggers
680 // frequently which cause the same notification consistently displayed.
681 if (DBG) log("showLimitedSimFunctionWarningNotification, "
682 + "not display again if already displayed");
683 return;
684 }
685 // Navigate to "Network Selection Settings" which list all subscriptions.
686 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
Shuo Qian9a41a172020-04-24 19:19:31 -0700687 new Intent(ACTION_MOBILE_NETWORK_LIST), PendingIntent.FLAG_IMMUTABLE);
Chen Xue1bbfd22019-09-18 17:17:31 -0700688 // Display phone number from the other sub
689 String line1Num = null;
690 SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService(
691 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
692 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
693 for (SubscriptionInfo sub : subList) {
694 if (sub.getSubscriptionId() != subId) {
695 line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId());
696 }
697 }
chen xubaf9fe52019-07-02 17:28:24 -0700698 final CharSequence contentText = TextUtils.isEmpty(line1Num) ?
699 String.format(mContext.getText(
Chen Xu251ce8f2019-09-13 20:24:22 -0700700 R.string.limited_sim_function_notification_message).toString(), carrierName) :
chen xubaf9fe52019-07-02 17:28:24 -0700701 String.format(mContext.getText(
702 R.string.limited_sim_function_with_phone_num_notification_message).toString(),
Chen Xu251ce8f2019-09-13 20:24:22 -0700703 carrierName, line1Num);
chen xubaf9fe52019-07-02 17:28:24 -0700704 final Notification.Builder builder = new Notification.Builder(mContext)
705 .setSmallIcon(R.drawable.ic_sim_card)
706 .setContentTitle(mContext.getText(
707 R.string.limited_sim_function_notification_title))
708 .setContentText(contentText)
709 .setOnlyAlertOnce(true)
710 .setOngoing(true)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700711 .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY)
chen xubaf9fe52019-07-02 17:28:24 -0700712 .setContentIntent(contentIntent);
713 final Notification notification = new Notification.BigTextStyle(builder).bigText(
714 contentText).build();
715
Jordan Liu9ddde022019-08-05 13:49:08 -0700716 notifyAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700717 LIMITED_SIM_FUNCTION_NOTIFICATION,
718 notification, UserHandle.ALL);
719 mLimitedSimFunctionNotify.add(subId);
720 }
721
722 /**
723 * Dismiss the "Limited SIM functionality" warning notification for the given subId.
724 */
725 public void dismissLimitedSimFunctionWarningNotification(int subId) {
726 if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId);
727 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
728 // dismiss all notifications
729 for (int id : mLimitedSimFunctionNotify) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700730 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700731 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
732 }
733 mLimitedSimFunctionNotify.clear();
734 } else if (mLimitedSimFunctionNotify.contains(subId)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700735 cancelAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700736 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
737 mLimitedSimFunctionNotify.remove(subId);
738 }
739 }
740
741 /**
742 * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions.
743 */
744 public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() {
745 if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs");
746 // dismiss notification for inactive subscriptions.
747 // handle the corner case that SIM change by SIM refresh doesn't clear the notification
748 // from the old SIM if both old & new SIM configured to display the notification.
749 mLimitedSimFunctionNotify.removeIf(id -> {
750 if (!mSubscriptionManager.isActiveSubId(id)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700751 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700752 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
753 return true;
754 }
755 return false;
756 });
757 }
758
759 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 * Display the network selection "no service" notification
761 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700762 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700763 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700764 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765 if (DBG) log("showNetworkSelection(" + operator + ")...");
766
Youming Ye0509b532018-09-14 16:21:17 -0700767 if (!TextUtils.isEmpty(operator)) {
768 operator = String.format(" (%s)", operator);
769 }
fionaxu8b7620d2017-05-01 16:22:17 -0700770 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700771 .setSmallIcon(android.R.drawable.stat_sys_warning)
772 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
773 .setContentText(
774 mContext.getString(R.string.notification_network_selection_text, operator))
775 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700776 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700777 .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778
779 // create the target network operators settings intent
780 Intent intent = new Intent(Intent.ACTION_MAIN);
781 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
782 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700783 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700784 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700785 mContext.getString(R.string.mobile_network_settings_package),
786 mContext.getString(R.string.mobile_network_settings_class)));
Pengquan Meng984ba422019-09-05 18:00:06 -0700787 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700788 builder.setContentIntent(
789 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700790 notifyAsUser(
irisykyang25202462018-11-13 14:49:54 +0800791 Integer.toString(subId) /* tag */,
fionaxu96ceebd2017-08-24 12:12:32 -0700792 SELECTED_OPERATOR_FAIL_NOTIFICATION,
793 builder.build(),
794 UserHandle.ALL);
irisykyang25202462018-11-13 14:49:54 +0800795 mSelectedUnavailableNotify.put(subId, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 }
797
798 /**
799 * Turn off the network selection "no service" notification
800 */
irisykyang25202462018-11-13 14:49:54 +0800801 private void cancelNetworkSelection(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 if (DBG) log("cancelNetworkSelection()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700803 cancelAsUser(
irisykyang25202462018-11-13 14:49:54 +0800804 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
805 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 }
807
808 /**
809 * Update notification about no service of user selected operator
810 *
811 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700812 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700813 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700814 void updateNetworkSelection(int serviceState, int subId) {
815 int phoneId = SubscriptionManager.getPhoneId(subId);
816 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
817 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
818 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800819 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700820 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
821 String selectedNetworkOperatorName =
822 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
823 // get the shared preference of network_selection.
824 // empty is auto mode, otherwise it is the operator alpha name
825 // in case there is no operator name, check the operator numeric
826 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
827 selectedNetworkOperatorName =
828 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
829 }
830 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700831 // if restoring manual selection is controlled by framework, then get network
832 // selection from shared preference, otherwise get from real network indicators.
Daniel Brightebb4eb72020-02-18 15:16:33 -0800833 boolean restoreSelection = !mContext.getResources().getBoolean(
834 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700835 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700836 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
837 } else {
fionaxud6aac662018-03-14 16:44:29 -0700838 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800839 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840
fionaxud6aac662018-03-14 16:44:29 -0700841 if (DBG) {
842 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
843 + (isManualSelection ? selectedNetworkOperatorName : ""));
844 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845
irisykyang25202462018-11-13 14:49:54 +0800846 if (isManualSelection) {
847 mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName);
848 shouldShowNotification(serviceState, subId);
Amit Mahajana60be872015-01-15 16:05:08 -0800849 } else {
irisykyang25202462018-11-13 14:49:54 +0800850 dismissNetworkSelectionNotification(subId);
851 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 }
853 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800854 if (DBG) log("updateNetworkSelection()..." + "state = " +
855 serviceState + " not updating network due to invalid subId " + subId);
irisykyang25202462018-11-13 14:49:54 +0800856 dismissNetworkSelectionNotificationForInactiveSubId();
857 }
858 }
859 }
860
861 private void dismissNetworkSelectionNotification(int subId) {
862 if (mSelectedUnavailableNotify.get(subId, false)) {
863 cancelNetworkSelection(subId);
864 mSelectedUnavailableNotify.remove(subId);
865 }
866 }
867
868 private void dismissNetworkSelectionNotificationForInactiveSubId() {
869 for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) {
870 int subId = mSelectedUnavailableNotify.keyAt(i);
871 if (!mSubscriptionManager.isActiveSubId(subId)) {
872 dismissNetworkSelectionNotification(subId);
873 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 }
875 }
876 }
877
878 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
879 if (mToast != null) {
880 mToast.cancel();
881 }
882
883 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
884 mToast.show();
885 }
886
887 private void log(String msg) {
888 Log.d(LOG_TAG, msg);
889 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700890
891 private void logi(String msg) {
892 Log.i(LOG_TAG, msg);
893 }
irisykyang25202462018-11-13 14:49:54 +0800894
895 /**
896 * In case network selection notification shows up repeatedly under
897 * unstable network condition. The logic is to check whether or not
898 * the service state keeps in no service condition for at least
899 * {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}.
900 * And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times.
901 * To avoid the notification showing up for the momentary state.
902 */
903 private void shouldShowNotification(int serviceState, int subId) {
904 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
905 if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE)
906 != ServiceState.STATE_OUT_OF_SERVICE) {
907 mOOSTimestamp.put(subId, getTimeStamp());
908 }
909 if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L)
910 >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS)
911 || mPendingEventCounter.get(subId, 0)
912 > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) {
913 showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId);
914 clearUpNetworkSelectionNotificationParam(subId);
915 } else {
916 startPendingNetworkSelectionNotification(subId);
917 }
918 } else {
919 dismissNetworkSelectionNotification(subId);
920 }
921 mPreviousServiceState.put(subId, serviceState);
922 if (DBG) {
923 log("shouldShowNotification()..." + " subId = " + subId
924 + " serviceState = " + serviceState
925 + " mOOSTimestamp = " + mOOSTimestamp
926 + " mPendingEventCounter = " + mPendingEventCounter);
927 }
928 }
929
930 private void startPendingNetworkSelectionNotification(int subId) {
931 if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
932 if (DBG) {
933 log("startPendingNetworkSelectionNotification: subId = " + subId);
934 }
935 mHandler.sendMessageDelayed(
936 mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId),
937 NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS);
938 mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1);
939 }
940 }
941
942 private void clearUpNetworkSelectionNotificationParam(int subId) {
943 if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
944 mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId);
945 }
946 mPreviousServiceState.remove(subId);
947 mOOSTimestamp.remove(subId);
948 mPendingEventCounter.remove(subId);
949 mSelectedNetworkOperatorName.remove(subId);
950 }
951
952 private static long getTimeStamp() {
953 return SystemClock.elapsedRealtime();
954 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955}