blob: 410965b6f19d1473e0e4a72eb85860534cd57322 [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;
Andrew Lee99d0ac22014-10-10 13:18:04 -070032import android.content.pm.UserInfo;
Nancy Chenb4a92702014-12-04 15:57:29 -080033import android.content.res.Resources;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.net.Uri;
irisykyang25202462018-11-13 14:49:54 +080035import android.os.Handler;
36import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070037import android.os.PersistableBundle;
irisykyang25202462018-11-13 14:49:54 +080038import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.SystemProperties;
Andrew Lee99d0ac22014-10-10 13:18:04 -070040import android.os.UserHandle;
41import android.os.UserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.preference.PreferenceManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.provider.ContactsContract.PhoneLookup;
Jeff Davidson6d9bf522017-11-03 14:51:13 -070044import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070045import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080046import android.telecom.PhoneAccountHandle;
47import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.PhoneNumberUtils;
50import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080051import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080052import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080053import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080055import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
irisykyang25202462018-11-13 14:49:54 +080057import android.util.SparseArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070059
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070061import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070063import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070064import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070065
Meng Wang1a19e442019-10-11 10:09:00 -070066import java.util.ArrayList;
chen xubaf9fe52019-07-02 17:28:24 -070067import java.util.HashSet;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080068import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070069import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080070import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070071
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072/**
73 * NotificationManager-related utility code for the Phone app.
74 *
75 * This is a singleton object which acts as the interface to the
76 * framework's NotificationManager, and is used to display status bar
77 * icons and control other status bar-related behavior.
78 *
79 * @see PhoneGlobals.notificationMgr
80 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070081public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080082 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083 private static final boolean DBG =
84 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
85 // Do not check in with VDBG = true, since that may write PII to the system log.
86 private static final boolean VDBG = false;
87
Ta-wei Yenb29425b2016-09-21 17:28:14 -070088 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
89 "mwi_should_check_vvm_configuration_state_";
90
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070092 static final int MMI_NOTIFICATION = 1;
93 static final int NETWORK_SELECTION_NOTIFICATION = 2;
94 static final int VOICEMAIL_NOTIFICATION = 3;
95 static final int CALL_FORWARD_NOTIFICATION = 4;
Jordan Liuc353b162019-07-23 15:54:41 -070096 static final int DATA_ROAMING_NOTIFICATION = 5;
Santos Cordonf68db2e2014-07-02 14:40:44 -070097 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
chen xubaf9fe52019-07-02 17:28:24 -070098 static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099
irisykyang25202462018-11-13 14:49:54 +0800100 // Event for network selection notification.
101 private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1;
102
103 private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L;
104 private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10;
105
106 private static final int STATE_UNKNOWN_SERVICE = -1;
107
chen xubaf9fe52019-07-02 17:28:24 -0700108 private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST";
109
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 /** The singleton NotificationMgr instance. */
111 private static NotificationMgr sInstance;
112
113 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114
115 private Context mContext;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700117 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800119 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800120 private TelecomManager mTelecomManager;
121 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122
irisykyang25202462018-11-13 14:49:54 +0800123 // used to track the notification of selected network unavailable, per subscription id.
124 private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125
chen xubaf9fe52019-07-02 17:28:24 -0700126 // used to track the notification of limited sim function under dual sim, per subscription id.
127 private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>();
128
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800129 // used to track whether the message waiting indicator is visible, per subscription id.
130 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
131
irisykyang25202462018-11-13 14:49:54 +0800132 // those flags are used to track whether to show network selection notification or not.
133 private SparseArray<Integer> mPreviousServiceState = new SparseArray<>();
134 private SparseArray<Long> mOOSTimestamp = new SparseArray<>();
135 private SparseArray<Integer> mPendingEventCounter = new SparseArray<>();
136 // maps each subId to selected network operator name.
137 private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>();
138
139 private final Handler mHandler = new Handler() {
140 @Override
141 public void handleMessage(Message msg) {
142 switch (msg.what) {
143 case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION:
144 int subId = (int) msg.obj;
145 TelephonyManager telephonyManager =
146 mTelephonyManager.createForSubscriptionId(subId);
147 if (telephonyManager.getServiceState() != null) {
148 shouldShowNotification(telephonyManager.getServiceState().getState(),
149 subId);
150 }
151 break;
152 }
153 }
154 };
155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 /**
157 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700158 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 */
160 private NotificationMgr(PhoneGlobals app) {
161 mApp = app;
162 mContext = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163 mStatusBarManager =
164 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700165 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800166 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -0700167 mTelecomManager = app.getSystemService(TelecomManager.class);
Andrew Leed5165b02014-12-05 15:53:58 -0800168 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700169 }
170
171 /**
172 * Initialize the singleton NotificationMgr instance.
173 *
174 * This is only done once, at startup, from PhoneApp.onCreate().
175 * From then on, the NotificationMgr instance is available via the
176 * PhoneApp's public "notificationMgr" field, which is why there's no
177 * getInstance() method here.
178 */
179 /* package */ static NotificationMgr init(PhoneGlobals app) {
180 synchronized (NotificationMgr.class) {
181 if (sInstance == null) {
182 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 } else {
184 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
185 }
186 return sInstance;
187 }
188 }
189
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 /** The projection to use when querying the phones table */
191 static final String[] PHONES_PROJECTION = new String[] {
192 PhoneLookup.NUMBER,
193 PhoneLookup.DISPLAY_NAME,
194 PhoneLookup._ID
195 };
196
197 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800198 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
199 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
200 * settings screen. The voicemail notification sound is suppressed.
201 *
202 * @param subId The subscription Id.
203 */
204 /* package */ void refreshMwi(int subId) {
205 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
206 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800207 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800208 if (mMwiVisible.keySet().size() == 1) {
209 Set<Integer> keySet = mMwiVisible.keySet();
210 Iterator<Integer> keyIt = keySet.iterator();
211 if (!keyIt.hasNext()) {
212 return;
213 }
214 subId = keyIt.next();
215 }
216 }
217 if (mMwiVisible.containsKey(subId)) {
218 boolean mwiVisible = mMwiVisible.get(subId);
219 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900220 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800221 }
222 }
223 }
224
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700225 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
226 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
227 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
228 + subId);
229 return;
230 }
231
232 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
233 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
234 .apply();
235 }
236
237 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
238 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
239 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
240 return true;
241 }
242 return PreferenceManager
243 .getDefaultSharedPreferences(mContext)
244 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
245 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800246 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 * Updates the message waiting indicator (voicemail) notification.
248 *
249 * @param visible true if there are messages waiting
250 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800251 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700252 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800253 }
254
255 /**
256 * Updates the message waiting indicator (voicemail) notification.
257 *
258 * @param subId the subId to update.
259 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700260 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
261 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800262 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700263 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800264 if (!PhoneGlobals.sVoiceCapable) {
265 // Do not show the message waiting indicator on devices which are not voice capable.
266 // These events *should* be blocked at the telephony layer for such devices.
267 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
268 return;
269 }
270
Nancy Chen2cf7f292015-05-15 11:00:10 -0700271 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800272 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800273 mMwiVisible.put(subId, visible);
274
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800276 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800277 Log.w(LOG_TAG, "Found null phone for: " + subId);
278 return;
279 }
280
281 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
282 if (subInfo == null) {
283 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800284 return;
285 }
286
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900288 if (mTelephonyManager.getPhoneCount() > 1) {
289 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
290 : R.drawable.stat_notify_voicemail_sub2;
291 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292
293 // This Notification can get a lot fancier once we have more
294 // information about the current voicemail messages.
295 // (For example, the current voicemail system can't tell
296 // us the caller-id or timestamp of a message, or tell us the
297 // message count.)
298
299 // But for now, the UI is ultra-simple: if the MWI indication
300 // is supposed to be visible, just show a single generic
301 // notification.
302
303 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800304 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 if (DBG) log("- got vm number: '" + vmNumber + "'");
306
Andrew Leea82b8202014-11-21 16:18:28 -0800307 // The voicemail number may be null because:
308 // (1) This phone has no voicemail number.
309 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
310 // happen when the device first boots if we get a MWI notification when we
311 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800312 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
313 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800315 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 }
317
Bryce Lee5dc90842015-08-11 07:57:14 -0700318 Integer vmCount = null;
319
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800320 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700321 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
323 notificationTitle = String.format(titleFormat, vmCount);
324 }
325
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800326 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
327 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
328
329 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700331 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
332
333 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800334 notificationText = mContext.getString(
335 R.string.notification_voicemail_no_vm_number);
336
337 // If the voicemail number if unknown, instead of calling voicemail, take the user
338 // to the voicemail settings.
339 notificationText = mContext.getString(
340 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700341 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800342 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700343 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800345 if (mTelephonyManager.getPhoneCount() > 1) {
346 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800347 } else {
348 notificationText = String.format(
349 mContext.getString(R.string.notification_voicemail_text_format),
350 PhoneNumberUtils.formatNumber(vmNumber));
351 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800352 intent = new Intent(
353 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700354 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800355 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700356 }
357
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800358 PendingIntent pendingIntent =
359 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
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)
482 .getDefaultDialerPackage(userHandle.getIdentifier());
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 */,
560 intent, 0));
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 {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900567 List<UserInfo> users = mUserManager.getUsers(true);
568 for (UserInfo user : users) {
Jordan Liua55aaaa2019-08-28 15:33:05 -0700569 if (mUserManager.isManagedProfile(user.getUserHandle().getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900570 continue;
571 }
572 UserHandle userHandle = user.getUserHandle();
Jordan Liu9ddde022019-08-05 13:49:08 -0700573 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900574 Integer.toString(subId) /* tag */,
575 CALL_FORWARD_NOTIFICATION,
576 userHandle);
577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 }
579 }
580
581 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700582 * Shows either:
583 * 1) the "Data roaming is on" notification, which
584 * appears when you're roaming and you have the "data roaming" feature turned on for the
585 * given {@code subId}.
586 * or
587 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700589 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700590 * @param subId which subscription it's notifying about.
591 * @param roamingOn whether currently roaming is on or off. If true, we show notification
592 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 */
Jordan Liuc353b162019-07-23 15:54:41 -0700594 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
595 if (DBG) {
596 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
597 + " on subId " + subId);
598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599
600 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700601 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700602 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Pengquan Meng8783d932017-10-16 14:57:40 -0700603 PendingIntent contentIntent = PendingIntent.getActivity(mContext, subId, intent, 0);
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,
677 new Intent(ACTION_MOBILE_NETWORK_LIST), 0);
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);
fionaxu96ceebd2017-08-24 12:12:32 -0700778 builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
Jordan Liu9ddde022019-08-05 13:49:08 -0700779 notifyAsUser(
irisykyang25202462018-11-13 14:49:54 +0800780 Integer.toString(subId) /* tag */,
fionaxu96ceebd2017-08-24 12:12:32 -0700781 SELECTED_OPERATOR_FAIL_NOTIFICATION,
782 builder.build(),
783 UserHandle.ALL);
irisykyang25202462018-11-13 14:49:54 +0800784 mSelectedUnavailableNotify.put(subId, true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 }
786
787 /**
788 * Turn off the network selection "no service" notification
789 */
irisykyang25202462018-11-13 14:49:54 +0800790 private void cancelNetworkSelection(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 if (DBG) log("cancelNetworkSelection()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700792 cancelAsUser(
irisykyang25202462018-11-13 14:49:54 +0800793 Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION,
794 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 }
796
797 /**
798 * Update notification about no service of user selected operator
799 *
800 * @param serviceState Phone service state
Jayachandran C2ef9a482017-05-12 22:07:47 -0700801 * @param subId The subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700803 void updateNetworkSelection(int serviceState, int subId) {
804 int phoneId = SubscriptionManager.getPhoneId(subId);
805 Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ?
806 PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone();
807 if (TelephonyCapabilities.supportsNetworkSelection(phone)) {
Amit Mahajana60be872015-01-15 16:05:08 -0800808 if (SubscriptionManager.isValidSubscriptionId(subId)) {
fionaxu996a1c32018-04-13 15:00:37 -0700809 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
810 String selectedNetworkOperatorName =
811 sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, "");
812 // get the shared preference of network_selection.
813 // empty is auto mode, otherwise it is the operator alpha name
814 // in case there is no operator name, check the operator numeric
815 if (TextUtils.isEmpty(selectedNetworkOperatorName)) {
816 selectedNetworkOperatorName =
817 sp.getString(Phone.NETWORK_SELECTION_KEY + subId, "");
818 }
819 boolean isManualSelection;
fionaxud6aac662018-03-14 16:44:29 -0700820 // if restoring manual selection is controlled by framework, then get network
821 // selection from shared preference, otherwise get from real network indicators.
822 boolean restoreSelection = !mContext.getResources().getBoolean(
823 com.android.internal.R.bool.skip_restoring_network_selection);
fionaxud6aac662018-03-14 16:44:29 -0700824 if (restoreSelection) {
fionaxud6aac662018-03-14 16:44:29 -0700825 isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName);
826 } else {
fionaxud6aac662018-03-14 16:44:29 -0700827 isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection();
Amit Mahajana60be872015-01-15 16:05:08 -0800828 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700829
fionaxud6aac662018-03-14 16:44:29 -0700830 if (DBG) {
831 log("updateNetworkSelection()..." + "state = " + serviceState + " new network "
832 + (isManualSelection ? selectedNetworkOperatorName : ""));
833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834
irisykyang25202462018-11-13 14:49:54 +0800835 if (isManualSelection) {
836 mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName);
837 shouldShowNotification(serviceState, subId);
Amit Mahajana60be872015-01-15 16:05:08 -0800838 } else {
irisykyang25202462018-11-13 14:49:54 +0800839 dismissNetworkSelectionNotification(subId);
840 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 }
842 } else {
Amit Mahajana60be872015-01-15 16:05:08 -0800843 if (DBG) log("updateNetworkSelection()..." + "state = " +
844 serviceState + " not updating network due to invalid subId " + subId);
irisykyang25202462018-11-13 14:49:54 +0800845 dismissNetworkSelectionNotificationForInactiveSubId();
846 }
847 }
848 }
849
850 private void dismissNetworkSelectionNotification(int subId) {
851 if (mSelectedUnavailableNotify.get(subId, false)) {
852 cancelNetworkSelection(subId);
853 mSelectedUnavailableNotify.remove(subId);
854 }
855 }
856
857 private void dismissNetworkSelectionNotificationForInactiveSubId() {
858 for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) {
859 int subId = mSelectedUnavailableNotify.keyAt(i);
860 if (!mSubscriptionManager.isActiveSubId(subId)) {
861 dismissNetworkSelectionNotification(subId);
862 clearUpNetworkSelectionNotificationParam(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 }
864 }
865 }
866
867 /* package */ void postTransientNotification(int notifyId, CharSequence msg) {
868 if (mToast != null) {
869 mToast.cancel();
870 }
871
872 mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
873 mToast.show();
874 }
875
876 private void log(String msg) {
877 Log.d(LOG_TAG, msg);
878 }
Tyler Gunna584e2c2017-09-19 11:40:12 -0700879
880 private void logi(String msg) {
881 Log.i(LOG_TAG, msg);
882 }
irisykyang25202462018-11-13 14:49:54 +0800883
884 /**
885 * In case network selection notification shows up repeatedly under
886 * unstable network condition. The logic is to check whether or not
887 * the service state keeps in no service condition for at least
888 * {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}.
889 * And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times.
890 * To avoid the notification showing up for the momentary state.
891 */
892 private void shouldShowNotification(int serviceState, int subId) {
893 if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
894 if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE)
895 != ServiceState.STATE_OUT_OF_SERVICE) {
896 mOOSTimestamp.put(subId, getTimeStamp());
897 }
898 if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L)
899 >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS)
900 || mPendingEventCounter.get(subId, 0)
901 > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) {
902 showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId);
903 clearUpNetworkSelectionNotificationParam(subId);
904 } else {
905 startPendingNetworkSelectionNotification(subId);
906 }
907 } else {
908 dismissNetworkSelectionNotification(subId);
909 }
910 mPreviousServiceState.put(subId, serviceState);
911 if (DBG) {
912 log("shouldShowNotification()..." + " subId = " + subId
913 + " serviceState = " + serviceState
914 + " mOOSTimestamp = " + mOOSTimestamp
915 + " mPendingEventCounter = " + mPendingEventCounter);
916 }
917 }
918
919 private void startPendingNetworkSelectionNotification(int subId) {
920 if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
921 if (DBG) {
922 log("startPendingNetworkSelectionNotification: subId = " + subId);
923 }
924 mHandler.sendMessageDelayed(
925 mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId),
926 NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS);
927 mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1);
928 }
929 }
930
931 private void clearUpNetworkSelectionNotificationParam(int subId) {
932 if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) {
933 mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId);
934 }
935 mPreviousServiceState.remove(subId);
936 mOOSTimestamp.remove(subId);
937 mPendingEventCounter.remove(subId);
938 mSelectedNetworkOperatorName.remove(subId);
939 }
940
941 private static long getTimeStamp() {
942 return SystemClock.elapsedRealtime();
943 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944}