blob: fe4a0ba892e5ef6b6f2ab1ec1bee038e172e7b79 [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.Notification;
23import android.app.NotificationManager;
24import android.app.PendingIntent;
25import android.app.StatusBarManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
29import android.content.SharedPreferences;
Jordan Liu9ddde022019-08-05 13:49:08 -070030import android.content.pm.PackageManager;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080031import android.content.pm.ResolveInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080032import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.net.Uri;
irisykyang25202462018-11-13 14:49:54 +080034import android.os.Handler;
35import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070036import android.os.PersistableBundle;
irisykyang25202462018-11-13 14:49:54 +080037import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070039import android.os.UserHandle;
40import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.ContactsContract.PhoneLookup;
Jeff Davidson6d9bf522017-11-03 14:51:13 -070043import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070044import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080045import android.telecom.PhoneAccountHandle;
46import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070047import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.PhoneNumberUtils;
49import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080050import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080051import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080052import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080054import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.util.Log;
irisykyang25202462018-11-13 14:49:54 +080056import android.util.SparseArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070058
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070060import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070062import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070063import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070064
Meng Wang1a19e442019-10-11 10:09:00 -070065import java.util.ArrayList;
chen xubaf9fe52019-07-02 17:28:24 -070066import java.util.HashSet;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080067import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070068import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080069import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070070
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071/**
72 * NotificationManager-related utility code for the Phone app.
73 *
74 * This is a singleton object which acts as the interface to the
75 * framework's NotificationManager, and is used to display status bar
76 * icons and control other status bar-related behavior.
77 *
78 * @see PhoneGlobals.notificationMgr
79 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070080public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080081 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082 private static final boolean DBG =
83 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
84 // Do not check in with VDBG = true, since that may write PII to the system log.
85 private static final boolean VDBG = false;
86
Ta-wei Yenb29425b2016-09-21 17:28:14 -070087 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
88 "mwi_should_check_vvm_configuration_state_";
89
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070091 static final int MMI_NOTIFICATION = 1;
92 static final int NETWORK_SELECTION_NOTIFICATION = 2;
93 static final int VOICEMAIL_NOTIFICATION = 3;
94 static final int CALL_FORWARD_NOTIFICATION = 4;
Jordan Liuc353b162019-07-23 15:54:41 -070095 static final int DATA_ROAMING_NOTIFICATION = 5;
Santos Cordonf68db2e2014-07-02 14:40:44 -070096 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
chen xubaf9fe52019-07-02 17:28:24 -070097 static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
irisykyang25202462018-11-13 14:49:54 +080099 // Event for network selection notification.
100 private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1;
101
102 private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L;
103 private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10;
104
105 private static final int STATE_UNKNOWN_SERVICE = -1;
106
chen xubaf9fe52019-07-02 17:28:24 -0700107 private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST";
108
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 /** The singleton NotificationMgr instance. */
110 private static NotificationMgr sInstance;
111
112 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114 private Context mContext;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700116 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800118 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800119 private TelecomManager mTelecomManager;
120 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121
irisykyang25202462018-11-13 14:49:54 +0800122 // used to track the notification of selected network unavailable, per subscription id.
123 private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
chen xubaf9fe52019-07-02 17:28:24 -0700125 // used to track the notification of limited sim function under dual sim, per subscription id.
126 private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>();
127
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800128 // used to track whether the message waiting indicator is visible, per subscription id.
129 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
130
irisykyang25202462018-11-13 14:49:54 +0800131 // those flags are used to track whether to show network selection notification or not.
132 private SparseArray<Integer> mPreviousServiceState = new SparseArray<>();
133 private SparseArray<Long> mOOSTimestamp = new SparseArray<>();
134 private SparseArray<Integer> mPendingEventCounter = new SparseArray<>();
135 // maps each subId to selected network operator name.
136 private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>();
137
138 private final Handler mHandler = new Handler() {
139 @Override
140 public void handleMessage(Message msg) {
141 switch (msg.what) {
142 case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION:
143 int subId = (int) msg.obj;
144 TelephonyManager telephonyManager =
145 mTelephonyManager.createForSubscriptionId(subId);
146 if (telephonyManager.getServiceState() != null) {
147 shouldShowNotification(telephonyManager.getServiceState().getState(),
148 subId);
149 }
150 break;
151 }
152 }
153 };
154
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700155 /**
156 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700157 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 */
159 private NotificationMgr(PhoneGlobals app) {
160 mApp = app;
161 mContext = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162 mStatusBarManager =
163 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700164 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800165 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700166 mTelecomManager = app.getSystemService(TelecomManager.class);
Andrew Leed5165b02014-12-05 15:53:58 -0800167 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 }
169
170 /**
171 * Initialize the singleton NotificationMgr instance.
172 *
173 * This is only done once, at startup, from PhoneApp.onCreate().
174 * From then on, the NotificationMgr instance is available via the
175 * PhoneApp's public "notificationMgr" field, which is why there's no
176 * getInstance() method here.
177 */
178 /* package */ static NotificationMgr init(PhoneGlobals app) {
179 synchronized (NotificationMgr.class) {
180 if (sInstance == null) {
181 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182 } else {
183 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
184 }
185 return sInstance;
186 }
187 }
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 /** The projection to use when querying the phones table */
190 static final String[] PHONES_PROJECTION = new String[] {
191 PhoneLookup.NUMBER,
192 PhoneLookup.DISPLAY_NAME,
193 PhoneLookup._ID
194 };
195
196 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800197 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
198 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
199 * settings screen. The voicemail notification sound is suppressed.
200 *
201 * @param subId The subscription Id.
202 */
203 /* package */ void refreshMwi(int subId) {
204 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
205 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800206 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800207 if (mMwiVisible.keySet().size() == 1) {
208 Set<Integer> keySet = mMwiVisible.keySet();
209 Iterator<Integer> keyIt = keySet.iterator();
210 if (!keyIt.hasNext()) {
211 return;
212 }
213 subId = keyIt.next();
214 }
215 }
216 if (mMwiVisible.containsKey(subId)) {
217 boolean mwiVisible = mMwiVisible.get(subId);
218 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900219 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800220 }
221 }
222 }
223
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700224 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
225 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
226 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
227 + subId);
228 return;
229 }
230
231 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
232 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
233 .apply();
234 }
235
236 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
237 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
238 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
239 return true;
240 }
241 return PreferenceManager
242 .getDefaultSharedPreferences(mContext)
243 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
244 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800245 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 * Updates the message waiting indicator (voicemail) notification.
247 *
248 * @param visible true if there are messages waiting
249 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800250 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700251 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800252 }
253
254 /**
255 * Updates the message waiting indicator (voicemail) notification.
256 *
257 * @param subId the subId to update.
258 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700259 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
260 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800261 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700262 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800263 if (!PhoneGlobals.sVoiceCapable) {
264 // Do not show the message waiting indicator on devices which are not voice capable.
265 // These events *should* be blocked at the telephony layer for such devices.
266 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
267 return;
268 }
269
Nancy Chen2cf7f292015-05-15 11:00:10 -0700270 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800271 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800272 mMwiVisible.put(subId, visible);
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800275 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800276 Log.w(LOG_TAG, "Found null phone for: " + subId);
277 return;
278 }
279
280 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
281 if (subInfo == null) {
282 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800283 return;
284 }
285
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900287 if (mTelephonyManager.getPhoneCount() > 1) {
288 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
289 : R.drawable.stat_notify_voicemail_sub2;
290 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291
292 // This Notification can get a lot fancier once we have more
293 // information about the current voicemail messages.
294 // (For example, the current voicemail system can't tell
295 // us the caller-id or timestamp of a message, or tell us the
296 // message count.)
297
298 // But for now, the UI is ultra-simple: if the MWI indication
299 // is supposed to be visible, just show a single generic
300 // notification.
301
302 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800303 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 if (DBG) log("- got vm number: '" + vmNumber + "'");
305
Andrew Leea82b8202014-11-21 16:18:28 -0800306 // The voicemail number may be null because:
307 // (1) This phone has no voicemail number.
308 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
309 // happen when the device first boots if we get a MWI notification when we
310 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800311 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
312 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800314 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 }
316
Bryce Lee5dc90842015-08-11 07:57:14 -0700317 Integer vmCount = null;
318
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800319 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700320 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
322 notificationTitle = String.format(titleFormat, vmCount);
323 }
324
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800325 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
326 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
327
328 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700330 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
331
332 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800333 notificationText = mContext.getString(
334 R.string.notification_voicemail_no_vm_number);
335
336 // If the voicemail number if unknown, instead of calling voicemail, take the user
337 // to the voicemail settings.
338 notificationText = mContext.getString(
339 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700340 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800341 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700342 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800344 if (mTelephonyManager.getPhoneCount() > 1) {
345 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800346 } else {
347 notificationText = String.format(
348 mContext.getString(R.string.notification_voicemail_text_format),
349 PhoneNumberUtils.formatNumber(vmNumber));
350 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800351 intent = new Intent(
352 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700353 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800354 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 }
356
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800357 PendingIntent pendingIntent =
Shuo Qian9a41a172020-04-24 19:19:31 -0700358 PendingIntent.getActivity(mContext, subId /* requestCode */, intent,
359 PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360
Nancy Chenb4a92702014-12-04 15:57:29 -0800361 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700362 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700363 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700364 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 builder.setSmallIcon(resId)
366 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800367 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 .setContentTitle(notificationTitle)
369 .setContentText(notificationText)
370 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800371 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700372 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700373 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Jordan Liu575c0d02019-07-09 16:29:48 -0700374 .setChannelId(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
Ta-wei Yen282a9702017-05-30 17:32:29 -0700375 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376
Andrew Lee99d0ac22014-10-10 13:18:04 -0700377 final Notification notification = builder.build();
Meng Wang1a19e442019-10-11 10:09:00 -0700378 List<UserHandle> users = getUsersExcludeDying();
379 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700380 if (!hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700381 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700382 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800383 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700384 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700385 notifyAsUser(
Bryce Lee5dc90842015-08-11 07:57:14 -0700386 Integer.toString(subId) /* tag */,
387 VOICEMAIL_NOTIFICATION,
388 notification,
389 userHandle);
390 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700391 }
392 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700393 } else {
Meng Wang1a19e442019-10-11 10:09:00 -0700394 List<UserHandle> users = getUsersExcludeDying();
395 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700396 if (!hasUserRestriction(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800397 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700398 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800399 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700400 false, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700401 cancelAsUser(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800402 Integer.toString(subId) /* tag */,
403 VOICEMAIL_NOTIFICATION,
404 userHandle);
405 }
406 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700407 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 }
409 }
410
Meng Wang1a19e442019-10-11 10:09:00 -0700411 private List<UserHandle> getUsersExcludeDying() {
412 long[] serialNumbersOfUsers =
413 mUserManager.getSerialNumbersOfUsers(/* excludeDying= */ true);
414 List<UserHandle> users = new ArrayList<>(serialNumbersOfUsers.length);
415 for (long serialNumber : serialNumbersOfUsers) {
416 users.add(mUserManager.getUserForSerialNumber(serialNumber));
417 }
418 return users;
419 }
420
Amit Mahajan2f489b72019-10-08 11:21:52 -0700421 private boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
422 final List<UserManager.EnforcingUser> sources = mUserManager
423 .getUserRestrictionSources(restrictionKey, userHandle);
424 return (sources != null && !sources.isEmpty());
425 }
426
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800428 * Sends a broadcast with the voicemail notification information to the default dialer. This
429 * method is also used to indicate to the default dialer when to clear the
430 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700431 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800432 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700433 * @param count The number of pending voicemail messages to indicate on the notification. A
434 * Value of 0 is passed here to indicate that the notification should be cleared.
435 * @param number The voicemail phone number if specified.
436 * @param pendingIntent The intent that should be passed as the action to be taken.
437 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
438 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800439 * @param userHandle The user to receive the notification. Each user can have their own default
440 * dialer.
441 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700442 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800443 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
444 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700445 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800446
447 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
448 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700449 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700450 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800451 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
452 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700453 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700454 if (count != null) {
455 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
456 }
457
458 // Additional information about the voicemail notification beyond the count is only
459 // present when the count not specified or greater than 0. The value of 0 represents
460 // clearing the notification, which does not require additional information.
461 if (count == null || count > 0) {
462 if (!TextUtils.isEmpty(number)) {
463 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
464 }
465
466 if (pendingIntent != null) {
467 intent.putExtra(isSettingsIntent
468 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
469 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
470 pendingIntent);
471 }
472 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800473 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700474 return true;
475 }
476
477 return false;
478 }
479
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800480 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700481 String dialerPackage = mContext.getSystemService(TelecomManager.class)
Tyler Gunn83adc052020-01-28 09:12:21 -0800482 .getDefaultDialerPackage(userHandle);
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800483 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
484 .setPackage(dialerPackage);
485 }
486
487 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
488 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
489 if (intent == null) {
490 return false;
491 }
492
493 List<ResolveInfo> receivers = mContext.getPackageManager()
494 .queryBroadcastReceivers(intent, 0);
495 return receivers.size() > 0;
496 }
497
Bryce Lee5dc90842015-08-11 07:57:14 -0700498 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700499 * Updates the message call forwarding indicator notification.
500 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530501 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700502 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530503
504 /* package */ void updateCfi(int subId, boolean visible) {
505 updateCfi(subId, visible, false /* isRefresh */);
506 }
507
508 /**
509 * Updates the message call forwarding indicator notification.
510 *
511 * @param visible true if call forwarding enabled
512 */
513 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunna584e2c2017-09-19 11:40:12 -0700514 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 if (visible) {
516 // If Unconditional Call Forwarding (forward all calls) for VOICE
517 // is enabled, just show a notification. We'll default to expanded
518 // view for now, so the there is less confusion about the icon. If
519 // it is deemed too weird to have CF indications as expanded views,
520 // then we'll flip the flag back.
521
522 // TODO: We may want to take a look to see if the notification can
523 // display the target to forward calls to. This will require some
524 // effort though, since there are multiple layers of messages that
525 // will need to propagate that information.
526
Andrew Leed5165b02014-12-05 15:53:58 -0800527 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
528 if (subInfo == null) {
529 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
530 return;
531 }
532
533 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900534 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800535 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900536 int slotId = SubscriptionManager.getSlotIndex(subId);
537 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
538 : R.drawable.stat_sys_phone_call_forward_sub2;
Sarah Chin2c5f23d2020-11-02 21:52:11 -0800539 if (subInfo.getDisplayName() != null) {
540 notificationTitle = subInfo.getDisplayName().toString();
541 } else {
542 notificationTitle = mContext.getString(R.string.labelCF);
543 }
Andrew Leed5165b02014-12-05 15:53:58 -0800544 } else {
545 notificationTitle = mContext.getString(R.string.labelCF);
546 }
547
fionaxu8b7620d2017-05-01 16:22:17 -0700548 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900549 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800550 .setColor(subInfo.getIconTint())
551 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700552 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
553 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700554 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700555 .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530556 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557
Andrew Lee99d0ac22014-10-10 13:18:04 -0700558 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800559 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700560 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800561 SubscriptionInfoHelper.addExtrasToIntent(
562 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700563 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
Shuo Qian9a41a172020-04-24 19:19:31 -0700564 intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700565 notifyAsUser(
fionaxu96ceebd2017-08-24 12:12:32 -0700566 Integer.toString(subId) /* tag */,
567 CALL_FORWARD_NOTIFICATION,
568 builder.build(),
569 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 } else {
Meng Wang21487ab2019-11-21 10:44:42 -0800571 List<UserHandle> users = getUsersExcludeDying();
572 for (UserHandle user : users) {
573 if (mUserManager.isManagedProfile(user.getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900574 continue;
575 }
Jordan Liu9ddde022019-08-05 13:49:08 -0700576 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900577 Integer.toString(subId) /* tag */,
578 CALL_FORWARD_NOTIFICATION,
Meng Wang21487ab2019-11-21 10:44:42 -0800579 user);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900580 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700581 }
582 }
583
584 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700585 * Shows either:
586 * 1) the "Data roaming is on" notification, which
587 * appears when you're roaming and you have the "data roaming" feature turned on for the
588 * given {@code subId}.
589 * or
590 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700592 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700593 * @param subId which subscription it's notifying about.
594 * @param roamingOn whether currently roaming is on or off. If true, we show notification
595 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 */
Jordan Liuc353b162019-07-23 15:54:41 -0700597 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
598 if (DBG) {
599 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
600 + " on subId " + subId);
601 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602
603 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700604 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700605 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700606 PendingIntent contentIntent = PendingIntent.getActivity(
607 mContext, subId, intent, PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608
Jordan Liuc353b162019-07-23 15:54:41 -0700609 CharSequence contentTitle = mContext.getText(roamingOn
610 ? R.string.roaming_on_notification_title
611 : R.string.roaming_notification_title);
612 CharSequence contentText = mContext.getText(roamingOn
613 ? R.string.roaming_enabled_message
614 : R.string.roaming_reenable_message);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700615
fionaxu8b7620d2017-05-01 16:22:17 -0700616 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700617 .setSmallIcon(android.R.drawable.stat_sys_warning)
Jordan Liuc353b162019-07-23 15:54:41 -0700618 .setContentTitle(contentTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700619 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700620 .setContentText(contentText)
Jordan Liu575c0d02019-07-09 16:29:48 -0700621 .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
fionaxu96ceebd2017-08-24 12:12:32 -0700622 .setContentIntent(contentIntent);
623 final Notification notif =
624 new Notification.BigTextStyle(builder).bigText(contentText).build();
Jordan Liu9ddde022019-08-05 13:49:08 -0700625 notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
626 }
627
628 private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
629 try {
630 Context contextForUser =
631 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
632 NotificationManager notificationManager =
633 (NotificationManager) contextForUser.getSystemService(
634 Context.NOTIFICATION_SERVICE);
635 notificationManager.notify(tag, id, notification);
636 } catch (PackageManager.NameNotFoundException e) {
637 Log.e(LOG_TAG, "unable to notify for user " + user);
638 e.printStackTrace();
639 }
640 }
641
642 private void cancelAsUser(String tag, int id, 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.cancel(tag, id);
650 } catch (PackageManager.NameNotFoundException e) {
651 Log.e(LOG_TAG, "unable to cancel for user " + user);
652 e.printStackTrace();
653 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 }
655
656 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700657 * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 */
Jordan Liuc353b162019-07-23 15:54:41 -0700659 /* package */ void hideDataRoamingNotification() {
660 if (DBG) log("hideDataRoamingNotification()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700661 cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 }
663
664 /**
chen xubaf9fe52019-07-02 17:28:24 -0700665 * Shows the "Limited SIM functionality" warning notification, which appears when using a
666 * special carrier under dual sim. limited function applies for DSDS in general when two SIM
667 * cards share a single radio, thus the voice & data maybe impaired under certain scenarios.
668 */
669 public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) {
670 if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName
671 + " subId: " + subId);
672 if (mLimitedSimFunctionNotify.contains(subId)) {
673 // handle the case that user swipe the notification but condition triggers
674 // frequently which cause the same notification consistently displayed.
675 if (DBG) log("showLimitedSimFunctionWarningNotification, "
676 + "not display again if already displayed");
677 return;
678 }
679 // Navigate to "Network Selection Settings" which list all subscriptions.
680 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
Shuo Qian9a41a172020-04-24 19:19:31 -0700681 new Intent(ACTION_MOBILE_NETWORK_LIST), PendingIntent.FLAG_IMMUTABLE);
Chen Xue1bbfd22019-09-18 17:17:31 -0700682 // Display phone number from the other sub
683 String line1Num = null;
684 SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService(
685 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
686 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
687 for (SubscriptionInfo sub : subList) {
688 if (sub.getSubscriptionId() != subId) {
689 line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId());
690 }
691 }
chen xubaf9fe52019-07-02 17:28:24 -0700692 final CharSequence contentText = TextUtils.isEmpty(line1Num) ?
693 String.format(mContext.getText(
Chen Xu251ce8f2019-09-13 20:24:22 -0700694 R.string.limited_sim_function_notification_message).toString(), carrierName) :
chen xubaf9fe52019-07-02 17:28:24 -0700695 String.format(mContext.getText(
696 R.string.limited_sim_function_with_phone_num_notification_message).toString(),
Chen Xu251ce8f2019-09-13 20:24:22 -0700697 carrierName, line1Num);
chen xubaf9fe52019-07-02 17:28:24 -0700698 final Notification.Builder builder = new Notification.Builder(mContext)
699 .setSmallIcon(R.drawable.ic_sim_card)
700 .setContentTitle(mContext.getText(
701 R.string.limited_sim_function_notification_title))
702 .setContentText(contentText)
703 .setOnlyAlertOnce(true)
704 .setOngoing(true)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700705 .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY)
chen xubaf9fe52019-07-02 17:28:24 -0700706 .setContentIntent(contentIntent);
707 final Notification notification = new Notification.BigTextStyle(builder).bigText(
708 contentText).build();
709
Jordan Liu9ddde022019-08-05 13:49:08 -0700710 notifyAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700711 LIMITED_SIM_FUNCTION_NOTIFICATION,
712 notification, UserHandle.ALL);
713 mLimitedSimFunctionNotify.add(subId);
714 }
715
716 /**
717 * Dismiss the "Limited SIM functionality" warning notification for the given subId.
718 */
719 public void dismissLimitedSimFunctionWarningNotification(int subId) {
720 if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId);
721 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
722 // dismiss all notifications
723 for (int id : mLimitedSimFunctionNotify) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700724 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700725 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
726 }
727 mLimitedSimFunctionNotify.clear();
728 } else if (mLimitedSimFunctionNotify.contains(subId)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700729 cancelAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700730 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
731 mLimitedSimFunctionNotify.remove(subId);
732 }
733 }
734
735 /**
736 * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions.
737 */
738 public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() {
739 if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs");
740 // dismiss notification for inactive subscriptions.
741 // handle the corner case that SIM change by SIM refresh doesn't clear the notification
742 // from the old SIM if both old & new SIM configured to display the notification.
743 mLimitedSimFunctionNotify.removeIf(id -> {
744 if (!mSubscriptionManager.isActiveSubId(id)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700745 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700746 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
747 return true;
748 }
749 return false;
750 });
751 }
752
753 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 * Display the network selection "no service" notification
755 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700756 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700758 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 if (DBG) log("showNetworkSelection(" + operator + ")...");
760
Youming Ye0509b532018-09-14 16:21:17 -0700761 if (!TextUtils.isEmpty(operator)) {
762 operator = String.format(" (%s)", operator);
763 }
fionaxu8b7620d2017-05-01 16:22:17 -0700764 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700765 .setSmallIcon(android.R.drawable.stat_sys_warning)
766 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
767 .setContentText(
768 mContext.getString(R.string.notification_network_selection_text, operator))
769 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700770 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700771 .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772
773 // create the target network operators settings intent
774 Intent intent = new Intent(Intent.ACTION_MAIN);
775 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
776 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700777 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700778 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700779 mContext.getString(R.string.mobile_network_settings_package),
780 mContext.getString(R.string.mobile_network_settings_class)));
Pengquan Meng984ba422019-09-05 18:00:06 -0700781 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700782 builder.setContentIntent(
783 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700784 notifyAsUser(
irisykyang25202462018-11-13 14:49:54 +0800785 Integer.toString(subId) /* tag */,
fionaxu96ceebd2017-08-24 12:12:32 -0700786 SELECTED_OPERATOR_FAIL_NOTIFICATION,
787 builder.build(),
788 UserHandle.ALL);
irisykyang25202462018-11-13 14:49:54 +0800789 mSelectedUnavailableNotify.put(subId, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 }
791
792 /**
793 * Turn off the network selection "no service" notification
794 */
irisykyang25202462018-11-13 14:49:54 +0800795 private void cancelNetworkSelection(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 if (DBG) log("cancelNetworkSelection()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700797 cancelAsUser(
irisykyang25202462018-11-13 14:49:54 +0800798 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
799 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 }
801
802 /**
803 * Update notification about no service of user selected operator
804 *
805 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700806 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700808 void updateNetworkSelection(int serviceState, int subId) {
809 int phoneId = SubscriptionManager.getPhoneId(subId);
810 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
811 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
812 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800813 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700814 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
815 String selectedNetworkOperatorName =
816 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
817 // get the shared preference of network_selection.
818 // empty is auto mode, otherwise it is the operator alpha name
819 // in case there is no operator name, check the operator numeric
820 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
821 selectedNetworkOperatorName =
822 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
823 }
824 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700825 // if restoring manual selection is controlled by framework, then get network
826 // selection from shared preference, otherwise get from real network indicators.
Daniel Brightebb4eb72020-02-18 15:16:33 -0800827 boolean restoreSelection = !mContext.getResources().getBoolean(
828 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700829 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700830 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
831 } else {
fionaxud6aac662018-03-14 16:44:29 -0700832 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834
fionaxud6aac662018-03-14 16:44:29 -0700835 if (DBG) {
836 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
837 + (isManualSelection ? selectedNetworkOperatorName : ""));
838 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839
irisykyang25202462018-11-13 14:49:54 +0800840 if (isManualSelection) {
841 mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName);
842 shouldShowNotification(serviceState, subId);
Amit Mahajana60be872015-01-15 16:05:08 -0800843 } else {
irisykyang25202462018-11-13 14:49:54 +0800844 dismissNetworkSelectionNotification(subId);
845 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 }
847 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800848 if (DBG) log("updateNetworkSelection()..." + "state = " +
849 serviceState + " not updating network due to invalid subId " + subId);
irisykyang25202462018-11-13 14:49:54 +0800850 dismissNetworkSelectionNotificationForInactiveSubId();
851 }
852 }
853 }
854
855 private void dismissNetworkSelectionNotification(int subId) {
856 if (mSelectedUnavailableNotify.get(subId, false)) {
857 cancelNetworkSelection(subId);
858 mSelectedUnavailableNotify.remove(subId);
859 }
860 }
861
862 private void dismissNetworkSelectionNotificationForInactiveSubId() {
863 for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) {
864 int subId = mSelectedUnavailableNotify.keyAt(i);
865 if (!mSubscriptionManager.isActiveSubId(subId)) {
866 dismissNetworkSelectionNotification(subId);
867 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 }
869 }
870 }
871
872 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
873 if (mToast != null) {
874 mToast.cancel();
875 }
876
877 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
878 mToast.show();
879 }
880
881 private void log(String msg) {
882 Log.d(LOG_TAG, msg);
883 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700884
885 private void logi(String msg) {
886 Log.i(LOG_TAG, msg);
887 }
irisykyang25202462018-11-13 14:49:54 +0800888
889 /**
890 * In case network selection notification shows up repeatedly under
891 * unstable network condition. The logic is to check whether or not
892 * the service state keeps in no service condition for at least
893 * {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}.
894 * And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times.
895 * To avoid the notification showing up for the momentary state.
896 */
897 private void shouldShowNotification(int serviceState, int subId) {
898 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
899 if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE)
900 != ServiceState.STATE_OUT_OF_SERVICE) {
901 mOOSTimestamp.put(subId, getTimeStamp());
902 }
903 if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L)
904 >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS)
905 || mPendingEventCounter.get(subId, 0)
906 > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) {
907 showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId);
908 clearUpNetworkSelectionNotificationParam(subId);
909 } else {
910 startPendingNetworkSelectionNotification(subId);
911 }
912 } else {
913 dismissNetworkSelectionNotification(subId);
914 }
915 mPreviousServiceState.put(subId, serviceState);
916 if (DBG) {
917 log("shouldShowNotification()..." + " subId = " + subId
918 + " serviceState = " + serviceState
919 + " mOOSTimestamp = " + mOOSTimestamp
920 + " mPendingEventCounter = " + mPendingEventCounter);
921 }
922 }
923
924 private void startPendingNetworkSelectionNotification(int subId) {
925 if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
926 if (DBG) {
927 log("startPendingNetworkSelectionNotification: subId = " + subId);
928 }
929 mHandler.sendMessageDelayed(
930 mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId),
931 NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS);
932 mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1);
933 }
934 }
935
936 private void clearUpNetworkSelectionNotificationParam(int subId) {
937 if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
938 mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId);
939 }
940 mPreviousServiceState.remove(subId);
941 mOOSTimestamp.remove(subId);
942 mPendingEventCounter.remove(subId);
943 mSelectedNetworkOperatorName.remove(subId);
944 }
945
946 private static long getTimeStamp() {
947 return SystemClock.elapsedRealtime();
948 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949}