blob: c2dece5626188c76ab7adad8c95c943b5ae540b2 [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;
Andrew Leed5165b02014-12-05 15:53:58 -0800539 notificationTitle = subInfo.getDisplayName().toString();
540 } else {
541 notificationTitle = mContext.getString(R.string.labelCF);
542 }
543
fionaxu8b7620d2017-05-01 16:22:17 -0700544 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900545 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800546 .setColor(subInfo.getIconTint())
547 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700548 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
549 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700550 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700551 .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530552 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700553
Andrew Lee99d0ac22014-10-10 13:18:04 -0700554 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800555 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700556 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800557 SubscriptionInfoHelper.addExtrasToIntent(
558 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700559 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
Shuo Qian9a41a172020-04-24 19:19:31 -0700560 intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700561 notifyAsUser(
fionaxu96ceebd2017-08-24 12:12:32 -0700562 Integer.toString(subId) /* tag */,
563 CALL_FORWARD_NOTIFICATION,
564 builder.build(),
565 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700566 } else {
Meng Wang21487ab2019-11-21 10:44:42 -0800567 List<UserHandle> users = getUsersExcludeDying();
568 for (UserHandle user : users) {
569 if (mUserManager.isManagedProfile(user.getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900570 continue;
571 }
Jordan Liu9ddde022019-08-05 13:49:08 -0700572 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900573 Integer.toString(subId) /* tag */,
574 CALL_FORWARD_NOTIFICATION,
Meng Wang21487ab2019-11-21 10:44:42 -0800575 user);
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900576 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 }
578 }
579
580 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700581 * Shows either:
582 * 1) the "Data roaming is on" notification, which
583 * appears when you're roaming and you have the "data roaming" feature turned on for the
584 * given {@code subId}.
585 * or
586 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700588 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700589 * @param subId which subscription it's notifying about.
590 * @param roamingOn whether currently roaming is on or off. If true, we show notification
591 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700592 */
Jordan Liuc353b162019-07-23 15:54:41 -0700593 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
594 if (DBG) {
595 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
596 + " on subId " + subId);
597 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598
599 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700600 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700601 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700602 PendingIntent contentIntent = PendingIntent.getActivity(
603 mContext, subId, intent, PendingIntent.FLAG_IMMUTABLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700604
Jordan Liuc353b162019-07-23 15:54:41 -0700605 CharSequence contentTitle = mContext.getText(roamingOn
606 ? R.string.roaming_on_notification_title
607 : R.string.roaming_notification_title);
608 CharSequence contentText = mContext.getText(roamingOn
609 ? R.string.roaming_enabled_message
610 : R.string.roaming_reenable_message);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700611
fionaxu8b7620d2017-05-01 16:22:17 -0700612 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700613 .setSmallIcon(android.R.drawable.stat_sys_warning)
Jordan Liuc353b162019-07-23 15:54:41 -0700614 .setContentTitle(contentTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700615 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700616 .setContentText(contentText)
Jordan Liu575c0d02019-07-09 16:29:48 -0700617 .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
fionaxu96ceebd2017-08-24 12:12:32 -0700618 .setContentIntent(contentIntent);
619 final Notification notif =
620 new Notification.BigTextStyle(builder).bigText(contentText).build();
Jordan Liu9ddde022019-08-05 13:49:08 -0700621 notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
622 }
623
624 private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
625 try {
626 Context contextForUser =
627 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
628 NotificationManager notificationManager =
629 (NotificationManager) contextForUser.getSystemService(
630 Context.NOTIFICATION_SERVICE);
631 notificationManager.notify(tag, id, notification);
632 } catch (PackageManager.NameNotFoundException e) {
633 Log.e(LOG_TAG, "unable to notify for user " + user);
634 e.printStackTrace();
635 }
636 }
637
638 private void cancelAsUser(String tag, int id, UserHandle user) {
639 try {
640 Context contextForUser =
641 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
642 NotificationManager notificationManager =
643 (NotificationManager) contextForUser.getSystemService(
644 Context.NOTIFICATION_SERVICE);
645 notificationManager.cancel(tag, id);
646 } catch (PackageManager.NameNotFoundException e) {
647 Log.e(LOG_TAG, "unable to cancel for user " + user);
648 e.printStackTrace();
649 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 }
651
652 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700653 * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 */
Jordan Liuc353b162019-07-23 15:54:41 -0700655 /* package */ void hideDataRoamingNotification() {
656 if (DBG) log("hideDataRoamingNotification()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700657 cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 }
659
660 /**
chen xubaf9fe52019-07-02 17:28:24 -0700661 * Shows the "Limited SIM functionality" warning notification, which appears when using a
662 * special carrier under dual sim. limited function applies for DSDS in general when two SIM
663 * cards share a single radio, thus the voice & data maybe impaired under certain scenarios.
664 */
665 public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) {
666 if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName
667 + " subId: " + subId);
668 if (mLimitedSimFunctionNotify.contains(subId)) {
669 // handle the case that user swipe the notification but condition triggers
670 // frequently which cause the same notification consistently displayed.
671 if (DBG) log("showLimitedSimFunctionWarningNotification, "
672 + "not display again if already displayed");
673 return;
674 }
675 // Navigate to "Network Selection Settings" which list all subscriptions.
676 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
Shuo Qian9a41a172020-04-24 19:19:31 -0700677 new Intent(ACTION_MOBILE_NETWORK_LIST), PendingIntent.FLAG_IMMUTABLE);
Chen Xue1bbfd22019-09-18 17:17:31 -0700678 // Display phone number from the other sub
679 String line1Num = null;
680 SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService(
681 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
682 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
683 for (SubscriptionInfo sub : subList) {
684 if (sub.getSubscriptionId() != subId) {
685 line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId());
686 }
687 }
chen xubaf9fe52019-07-02 17:28:24 -0700688 final CharSequence contentText = TextUtils.isEmpty(line1Num) ?
689 String.format(mContext.getText(
Chen Xu251ce8f2019-09-13 20:24:22 -0700690 R.string.limited_sim_function_notification_message).toString(), carrierName) :
chen xubaf9fe52019-07-02 17:28:24 -0700691 String.format(mContext.getText(
692 R.string.limited_sim_function_with_phone_num_notification_message).toString(),
Chen Xu251ce8f2019-09-13 20:24:22 -0700693 carrierName, line1Num);
chen xubaf9fe52019-07-02 17:28:24 -0700694 final Notification.Builder builder = new Notification.Builder(mContext)
695 .setSmallIcon(R.drawable.ic_sim_card)
696 .setContentTitle(mContext.getText(
697 R.string.limited_sim_function_notification_title))
698 .setContentText(contentText)
699 .setOnlyAlertOnce(true)
700 .setOngoing(true)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700701 .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY)
chen xubaf9fe52019-07-02 17:28:24 -0700702 .setContentIntent(contentIntent);
703 final Notification notification = new Notification.BigTextStyle(builder).bigText(
704 contentText).build();
705
Jordan Liu9ddde022019-08-05 13:49:08 -0700706 notifyAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700707 LIMITED_SIM_FUNCTION_NOTIFICATION,
708 notification, UserHandle.ALL);
709 mLimitedSimFunctionNotify.add(subId);
710 }
711
712 /**
713 * Dismiss the "Limited SIM functionality" warning notification for the given subId.
714 */
715 public void dismissLimitedSimFunctionWarningNotification(int subId) {
716 if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId);
717 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
718 // dismiss all notifications
719 for (int id : mLimitedSimFunctionNotify) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700720 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700721 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
722 }
723 mLimitedSimFunctionNotify.clear();
724 } else if (mLimitedSimFunctionNotify.contains(subId)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700725 cancelAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700726 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
727 mLimitedSimFunctionNotify.remove(subId);
728 }
729 }
730
731 /**
732 * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions.
733 */
734 public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() {
735 if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs");
736 // dismiss notification for inactive subscriptions.
737 // handle the corner case that SIM change by SIM refresh doesn't clear the notification
738 // from the old SIM if both old & new SIM configured to display the notification.
739 mLimitedSimFunctionNotify.removeIf(id -> {
740 if (!mSubscriptionManager.isActiveSubId(id)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700741 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700742 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
743 return true;
744 }
745 return false;
746 });
747 }
748
749 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 * Display the network selection "no service" notification
751 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700752 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700754 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 if (DBG) log("showNetworkSelection(" + operator + ")...");
756
Youming Ye0509b532018-09-14 16:21:17 -0700757 if (!TextUtils.isEmpty(operator)) {
758 operator = String.format(" (%s)", operator);
759 }
fionaxu8b7620d2017-05-01 16:22:17 -0700760 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700761 .setSmallIcon(android.R.drawable.stat_sys_warning)
762 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
763 .setContentText(
764 mContext.getString(R.string.notification_network_selection_text, operator))
765 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700766 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700767 .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768
769 // create the target network operators settings intent
770 Intent intent = new Intent(Intent.ACTION_MAIN);
771 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
772 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700773 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700774 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700775 mContext.getString(R.string.mobile_network_settings_package),
776 mContext.getString(R.string.mobile_network_settings_class)));
Pengquan Meng984ba422019-09-05 18:00:06 -0700777 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Shuo Qian9a41a172020-04-24 19:19:31 -0700778 builder.setContentIntent(
779 PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE));
Jordan Liu9ddde022019-08-05 13:49:08 -0700780 notifyAsUser(
irisykyang25202462018-11-13 14:49:54 +0800781 Integer.toString(subId) /* tag */,
fionaxu96ceebd2017-08-24 12:12:32 -0700782 SELECTED_OPERATOR_FAIL_NOTIFICATION,
783 builder.build(),
784 UserHandle.ALL);
irisykyang25202462018-11-13 14:49:54 +0800785 mSelectedUnavailableNotify.put(subId, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 }
787
788 /**
789 * Turn off the network selection "no service" notification
790 */
irisykyang25202462018-11-13 14:49:54 +0800791 private void cancelNetworkSelection(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 if (DBG) log("cancelNetworkSelection()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700793 cancelAsUser(
irisykyang25202462018-11-13 14:49:54 +0800794 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
795 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 }
797
798 /**
799 * Update notification about no service of user selected operator
800 *
801 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700802 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700804 void updateNetworkSelection(int serviceState, int subId) {
805 int phoneId = SubscriptionManager.getPhoneId(subId);
806 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
807 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
808 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800809 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700810 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
811 String selectedNetworkOperatorName =
812 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
813 // get the shared preference of network_selection.
814 // empty is auto mode, otherwise it is the operator alpha name
815 // in case there is no operator name, check the operator numeric
816 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
817 selectedNetworkOperatorName =
818 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
819 }
820 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700821 // if restoring manual selection is controlled by framework, then get network
822 // selection from shared preference, otherwise get from real network indicators.
Daniel Brightebb4eb72020-02-18 15:16:33 -0800823 boolean restoreSelection = !mContext.getResources().getBoolean(
824 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700825 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700826 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
827 } else {
fionaxud6aac662018-03-14 16:44:29 -0700828 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800829 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830
fionaxud6aac662018-03-14 16:44:29 -0700831 if (DBG) {
832 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
833 + (isManualSelection ? selectedNetworkOperatorName : ""));
834 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835
irisykyang25202462018-11-13 14:49:54 +0800836 if (isManualSelection) {
837 mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName);
838 shouldShowNotification(serviceState, subId);
Amit Mahajana60be872015-01-15 16:05:08 -0800839 } else {
irisykyang25202462018-11-13 14:49:54 +0800840 dismissNetworkSelectionNotification(subId);
841 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 }
843 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800844 if (DBG) log("updateNetworkSelection()..." + "state = " +
845 serviceState + " not updating network due to invalid subId " + subId);
irisykyang25202462018-11-13 14:49:54 +0800846 dismissNetworkSelectionNotificationForInactiveSubId();
847 }
848 }
849 }
850
851 private void dismissNetworkSelectionNotification(int subId) {
852 if (mSelectedUnavailableNotify.get(subId, false)) {
853 cancelNetworkSelection(subId);
854 mSelectedUnavailableNotify.remove(subId);
855 }
856 }
857
858 private void dismissNetworkSelectionNotificationForInactiveSubId() {
859 for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) {
860 int subId = mSelectedUnavailableNotify.keyAt(i);
861 if (!mSubscriptionManager.isActiveSubId(subId)) {
862 dismissNetworkSelectionNotification(subId);
863 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 }
865 }
866 }
867
868 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
869 if (mToast != null) {
870 mToast.cancel();
871 }
872
873 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
874 mToast.show();
875 }
876
877 private void log(String msg) {
878 Log.d(LOG_TAG, msg);
879 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700880
881 private void logi(String msg) {
882 Log.i(LOG_TAG, msg);
883 }
irisykyang25202462018-11-13 14:49:54 +0800884
885 /**
886 * In case network selection notification shows up repeatedly under
887 * unstable network condition. The logic is to check whether or not
888 * the service state keeps in no service condition for at least
889 * {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}.
890 * And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times.
891 * To avoid the notification showing up for the momentary state.
892 */
893 private void shouldShowNotification(int serviceState, int subId) {
894 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
895 if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE)
896 != ServiceState.STATE_OUT_OF_SERVICE) {
897 mOOSTimestamp.put(subId, getTimeStamp());
898 }
899 if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L)
900 >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS)
901 || mPendingEventCounter.get(subId, 0)
902 > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) {
903 showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId);
904 clearUpNetworkSelectionNotificationParam(subId);
905 } else {
906 startPendingNetworkSelectionNotification(subId);
907 }
908 } else {
909 dismissNetworkSelectionNotification(subId);
910 }
911 mPreviousServiceState.put(subId, serviceState);
912 if (DBG) {
913 log("shouldShowNotification()..." + " subId = " + subId
914 + " serviceState = " + serviceState
915 + " mOOSTimestamp = " + mOOSTimestamp
916 + " mPendingEventCounter = " + mPendingEventCounter);
917 }
918 }
919
920 private void startPendingNetworkSelectionNotification(int subId) {
921 if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
922 if (DBG) {
923 log("startPendingNetworkSelectionNotification: subId = " + subId);
924 }
925 mHandler.sendMessageDelayed(
926 mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId),
927 NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS);
928 mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1);
929 }
930 }
931
932 private void clearUpNetworkSelectionNotificationParam(int subId) {
933 if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
934 mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId);
935 }
936 mPreviousServiceState.remove(subId);
937 mOOSTimestamp.remove(subId);
938 mPendingEventCounter.remove(subId);
939 mSelectedNetworkOperatorName.remove(subId);
940 }
941
942 private static long getTimeStamp() {
943 return SystemClock.elapsedRealtime();
944 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945}