blob: 6486ae2c8f7bff5688f2c292cf31e6b69a8d346c [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;
Ta-wei Yen5bb19562016-11-16 11:05:37 -080045import android.telecom.DefaultDialerManager;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070046import android.telecom.PhoneAccount;
Andrew Leed5165b02014-12-05 15:53:58 -080047import android.telecom.PhoneAccountHandle;
48import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070049import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.telephony.PhoneNumberUtils;
51import android.telephony.ServiceState;
Andrew Lee2fcb6c32014-12-04 14:52:35 -080052import android.telephony.SubscriptionInfo;
Andrew Leea82b8202014-11-21 16:18:28 -080053import android.telephony.SubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -080054import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.text.TextUtils;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080056import android.util.ArrayMap;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.util.Log;
irisykyang25202462018-11-13 14:49:54 +080058import android.util.SparseArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.widget.Toast;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070060
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.Phone;
Jayachandran C2ef9a482017-05-12 22:07:47 -070062import com.android.internal.telephony.PhoneFactory;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.TelephonyCapabilities;
fionaxu8b7620d2017-05-01 16:22:17 -070064import com.android.internal.telephony.util.NotificationChannelController;
Andrew Leebf07f762015-04-07 19:05:50 -070065import com.android.phone.settings.VoicemailSettingsActivity;
Ta-wei Yenb29425b2016-09-21 17:28:14 -070066
Meng Wang1a19e442019-10-11 10:09:00 -070067import java.util.ArrayList;
chen xubaf9fe52019-07-02 17:28:24 -070068import java.util.HashSet;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080069import java.util.Iterator;
Andrew Lee99d0ac22014-10-10 13:18:04 -070070import java.util.List;
Tyler Gunn9c1071f2014-12-09 10:07:54 -080071import java.util.Set;
Andrew Lee99d0ac22014-10-10 13:18:04 -070072
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073/**
74 * NotificationManager-related utility code for the Phone app.
75 *
76 * This is a singleton object which acts as the interface to the
77 * framework's NotificationManager, and is used to display status bar
78 * icons and control other status bar-related behavior.
79 *
80 * @see PhoneGlobals.notificationMgr
81 */
Chiao Cheng312b9c92013-09-16 15:40:53 -070082public class NotificationMgr {
Andrew Leea82b8202014-11-21 16:18:28 -080083 private static final String LOG_TAG = NotificationMgr.class.getSimpleName();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 private static final boolean DBG =
85 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
86 // Do not check in with VDBG = true, since that may write PII to the system log.
87 private static final boolean VDBG = false;
88
Ta-wei Yenb29425b2016-09-21 17:28:14 -070089 private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX =
90 "mwi_should_check_vvm_configuration_state_";
91
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 // notification types
Santos Cordonf68db2e2014-07-02 14:40:44 -070093 static final int MMI_NOTIFICATION = 1;
94 static final int NETWORK_SELECTION_NOTIFICATION = 2;
95 static final int VOICEMAIL_NOTIFICATION = 3;
96 static final int CALL_FORWARD_NOTIFICATION = 4;
Jordan Liuc353b162019-07-23 15:54:41 -070097 static final int DATA_ROAMING_NOTIFICATION = 5;
Santos Cordonf68db2e2014-07-02 14:40:44 -070098 static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6;
chen xubaf9fe52019-07-02 17:28:24 -070099 static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
irisykyang25202462018-11-13 14:49:54 +0800101 // Event for network selection notification.
102 private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1;
103
104 private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L;
105 private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10;
106
107 private static final int STATE_UNKNOWN_SERVICE = -1;
108
chen xubaf9fe52019-07-02 17:28:24 -0700109 private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST";
110
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 /** The singleton NotificationMgr instance. */
112 private static NotificationMgr sInstance;
113
114 private PhoneGlobals mApp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115
116 private Context mContext;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 private StatusBarManager mStatusBarManager;
Andrew Lee99d0ac22014-10-10 13:18:04 -0700118 private UserManager mUserManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 private Toast mToast;
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800120 private SubscriptionManager mSubscriptionManager;
Andrew Leed5165b02014-12-05 15:53:58 -0800121 private TelecomManager mTelecomManager;
122 private TelephonyManager mTelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123
irisykyang25202462018-11-13 14:49:54 +0800124 // used to track the notification of selected network unavailable, per subscription id.
125 private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126
chen xubaf9fe52019-07-02 17:28:24 -0700127 // used to track the notification of limited sim function under dual sim, per subscription id.
128 private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>();
129
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800130 // used to track whether the message waiting indicator is visible, per subscription id.
131 private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>();
132
irisykyang25202462018-11-13 14:49:54 +0800133 // those flags are used to track whether to show network selection notification or not.
134 private SparseArray<Integer> mPreviousServiceState = new SparseArray<>();
135 private SparseArray<Long> mOOSTimestamp = new SparseArray<>();
136 private SparseArray<Integer> mPendingEventCounter = new SparseArray<>();
137 // maps each subId to selected network operator name.
138 private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>();
139
140 private final Handler mHandler = new Handler() {
141 @Override
142 public void handleMessage(Message msg) {
143 switch (msg.what) {
144 case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION:
145 int subId = (int) msg.obj;
146 TelephonyManager telephonyManager =
147 mTelephonyManager.createForSubscriptionId(subId);
148 if (telephonyManager.getServiceState() != null) {
149 shouldShowNotification(telephonyManager.getServiceState().getState(),
150 subId);
151 }
152 break;
153 }
154 }
155 };
156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 /**
158 * Private constructor (this is a singleton).
Santos Cordonf68db2e2014-07-02 14:40:44 -0700159 * @see #init(PhoneGlobals)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 */
161 private NotificationMgr(PhoneGlobals app) {
162 mApp = app;
163 mContext = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 mStatusBarManager =
165 (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700166 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800167 mSubscriptionManager = SubscriptionManager.from(mContext);
Andrew Leed5165b02014-12-05 15:53:58 -0800168 mTelecomManager = TelecomManager.from(mContext);
169 mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170 }
171
172 /**
173 * Initialize the singleton NotificationMgr instance.
174 *
175 * This is only done once, at startup, from PhoneApp.onCreate().
176 * From then on, the NotificationMgr instance is available via the
177 * PhoneApp's public "notificationMgr" field, which is why there's no
178 * getInstance() method here.
179 */
180 /* package */ static NotificationMgr init(PhoneGlobals app) {
181 synchronized (NotificationMgr.class) {
182 if (sInstance == null) {
183 sInstance = new NotificationMgr(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 } else {
185 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
186 }
187 return sInstance;
188 }
189 }
190
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 /** The projection to use when querying the phones table */
192 static final String[] PHONES_PROJECTION = new String[] {
193 PhoneLookup.NUMBER,
194 PhoneLookup.DISPLAY_NAME,
195 PhoneLookup._ID
196 };
197
198 /**
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800199 * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to
200 * refresh the voicemail intent on the indicator when the user changes it via the voicemail
201 * settings screen. The voicemail notification sound is suppressed.
202 *
203 * @param subId The subscription Id.
204 */
205 /* package */ void refreshMwi(int subId) {
206 // In a single-sim device, subId can be -1 which means "no sub id". In this case we will
207 // reference the single subid stored in the mMwiVisible map.
Ta-wei Yena1390d42017-12-04 15:11:33 -0800208 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800209 if (mMwiVisible.keySet().size() == 1) {
210 Set<Integer> keySet = mMwiVisible.keySet();
211 Iterator<Integer> keyIt = keySet.iterator();
212 if (!keyIt.hasNext()) {
213 return;
214 }
215 subId = keyIt.next();
216 }
217 }
218 if (mMwiVisible.containsKey(subId)) {
219 boolean mwiVisible = mMwiVisible.get(subId);
220 if (mwiVisible) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900221 mApp.notifier.updatePhoneStateListeners(true);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800222 }
223 }
224 }
225
Ta-wei Yenb29425b2016-09-21 17:28:14 -0700226 public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) {
227 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
228 Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId"
229 + subId);
230 return;
231 }
232
233 PreferenceManager.getDefaultSharedPreferences(mContext).edit()
234 .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled)
235 .apply();
236 }
237
238 private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) {
239 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
240 Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId);
241 return true;
242 }
243 return PreferenceManager
244 .getDefaultSharedPreferences(mContext)
245 .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true);
246 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800247 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 * Updates the message waiting indicator (voicemail) notification.
249 *
250 * @param visible true if there are messages waiting
251 */
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800252 /* package */ void updateMwi(int subId, boolean visible) {
Ta-wei Yen282a9702017-05-30 17:32:29 -0700253 updateMwi(subId, visible, false /* isRefresh */);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800254 }
255
256 /**
257 * Updates the message waiting indicator (voicemail) notification.
258 *
259 * @param subId the subId to update.
260 * @param visible true if there are messages waiting
Ta-wei Yen282a9702017-05-30 17:32:29 -0700261 * @param isRefresh {@code true} if the notification is a refresh and the user should not be
262 * notified again.
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800263 */
Ta-wei Yen282a9702017-05-30 17:32:29 -0700264 void updateMwi(int subId, boolean visible, boolean isRefresh) {
Andrew Leea82b8202014-11-21 16:18:28 -0800265 if (!PhoneGlobals.sVoiceCapable) {
266 // Do not show the message waiting indicator on devices which are not voice capable.
267 // These events *should* be blocked at the telephony layer for such devices.
268 Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring...");
269 return;
270 }
271
Nancy Chen2cf7f292015-05-15 11:00:10 -0700272 Phone phone = PhoneGlobals.getPhone(subId);
Yorke Lee67a62a22014-12-15 18:46:17 -0800273 Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible);
Andrew Leef8ad78f2014-12-15 16:17:29 -0800274 mMwiVisible.put(subId, visible);
275
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 if (visible) {
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800277 if (phone == null) {
Andrew Leed5165b02014-12-05 15:53:58 -0800278 Log.w(LOG_TAG, "Found null phone for: " + subId);
279 return;
280 }
281
282 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
283 if (subInfo == null) {
284 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800285 return;
286 }
287
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 int resId = android.R.drawable.stat_notify_voicemail;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900289 if (mTelephonyManager.getPhoneCount() > 1) {
290 resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1
291 : R.drawable.stat_notify_voicemail_sub2;
292 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293
294 // This Notification can get a lot fancier once we have more
295 // information about the current voicemail messages.
296 // (For example, the current voicemail system can't tell
297 // us the caller-id or timestamp of a message, or tell us the
298 // message count.)
299
300 // But for now, the UI is ultra-simple: if the MWI indication
301 // is supposed to be visible, just show a single generic
302 // notification.
303
304 String notificationTitle = mContext.getString(R.string.notification_voicemail_title);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800305 String vmNumber = phone.getVoiceMailNumber();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 if (DBG) log("- got vm number: '" + vmNumber + "'");
307
Andrew Leea82b8202014-11-21 16:18:28 -0800308 // The voicemail number may be null because:
309 // (1) This phone has no voicemail number.
310 // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may
311 // happen when the device first boots if we get a MWI notification when we
312 // register on the network before the SIM has loaded. In this case, the
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800313 // SubscriptionListener in CallNotifier will update this once the SIM is loaded.
314 if ((vmNumber == null) && !phone.getIccRecordsLoaded()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 if (DBG) log("- Null vm number: SIM records not loaded (yet)...");
Andrew Leea82b8202014-11-21 16:18:28 -0800316 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 }
318
Bryce Lee5dc90842015-08-11 07:57:14 -0700319 Integer vmCount = null;
320
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800321 if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) {
Bryce Lee5dc90842015-08-11 07:57:14 -0700322 vmCount = phone.getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 String titleFormat = mContext.getString(R.string.notification_voicemail_title_count);
324 notificationTitle = String.format(titleFormat, vmCount);
325 }
326
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800327 // This pathway only applies to PSTN accounts; only SIMS have subscription ids.
328 PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone);
329
330 Intent intent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 String notificationText;
Bryce Lee5dc90842015-08-11 07:57:14 -0700332 boolean isSettingsIntent = TextUtils.isEmpty(vmNumber);
333
334 if (isSettingsIntent) {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800335 notificationText = mContext.getString(
336 R.string.notification_voicemail_no_vm_number);
337
338 // If the voicemail number if unknown, instead of calling voicemail, take the user
339 // to the voicemail settings.
340 notificationText = mContext.getString(
341 R.string.notification_voicemail_no_vm_number);
Andrew Leebf07f762015-04-07 19:05:50 -0700342 intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL);
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800343 intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId);
Andrew Leebf07f762015-04-07 19:05:50 -0700344 intent.setClass(mContext, VoicemailSettingsActivity.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 } else {
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800346 if (mTelephonyManager.getPhoneCount() > 1) {
347 notificationText = subInfo.getDisplayName().toString();
Andrew Leed5165b02014-12-05 15:53:58 -0800348 } else {
349 notificationText = String.format(
350 mContext.getString(R.string.notification_voicemail_text_format),
351 PhoneNumberUtils.formatNumber(vmNumber));
352 }
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800353 intent = new Intent(
354 Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "",
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700355 null));
Tyler Gunn9c1071f2014-12-09 10:07:54 -0800356 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 }
358
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800359 PendingIntent pendingIntent =
360 PendingIntent.getActivity(mContext, subId /* requestCode */, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700361
Nancy Chenb4a92702014-12-04 15:57:29 -0800362 Resources res = mContext.getResources();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700363 PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId(
Ta-wei Yen9b37a872016-05-27 12:16:58 -0700364 subId);
fionaxu8b7620d2017-05-01 16:22:17 -0700365 Notification.Builder builder = new Notification.Builder(mContext);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 builder.setSmallIcon(resId)
367 .setWhen(System.currentTimeMillis())
Andrew Leed5165b02014-12-05 15:53:58 -0800368 .setColor(subInfo.getIconTint())
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700369 .setContentTitle(notificationTitle)
370 .setContentText(notificationText)
371 .setContentIntent(pendingIntent)
Nancy Chenb4a92702014-12-04 15:57:29 -0800372 .setColor(res.getColor(R.color.dialer_theme_color))
Jonathan Basseri3649bdb2015-04-30 22:39:11 -0700373 .setOngoing(carrierConfig.getBoolean(
fionaxu75b66a72017-04-19 19:01:56 -0700374 CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL))
Jordan Liu575c0d02019-07-09 16:29:48 -0700375 .setChannelId(NotificationChannelController.CHANNEL_ID_VOICE_MAIL)
Ta-wei Yen282a9702017-05-30 17:32:29 -0700376 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377
Andrew Lee99d0ac22014-10-10 13:18:04 -0700378 final Notification notification = builder.build();
Meng Wang1a19e442019-10-11 10:09:00 -0700379 List<UserHandle> users = getUsersExcludeDying();
380 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700381 if (!hasUserRestriction(
Yorke Lee047b1f92014-10-24 10:22:41 -0700382 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700383 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800384 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, vmNumber,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700385 pendingIntent, isSettingsIntent, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700386 notifyAsUser(
Bryce Lee5dc90842015-08-11 07:57:14 -0700387 Integer.toString(subId) /* tag */,
388 VOICEMAIL_NOTIFICATION,
389 notification,
390 userHandle);
391 }
Andrew Lee99d0ac22014-10-10 13:18:04 -0700392 }
393 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 } else {
Meng Wang1a19e442019-10-11 10:09:00 -0700395 List<UserHandle> users = getUsersExcludeDying();
396 for (UserHandle userHandle : users) {
Amit Mahajan2f489b72019-10-08 11:21:52 -0700397 if (!hasUserRestriction(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800398 UserManager.DISALLOW_OUTGOING_CALLS, userHandle)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700399 && !mUserManager.isManagedProfile(userHandle.getIdentifier())) {
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800400 if (!maybeSendVoicemailNotificationUsingDefaultDialer(phone, 0, null, null,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700401 false, userHandle, isRefresh)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700402 cancelAsUser(
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800403 Integer.toString(subId) /* tag */,
404 VOICEMAIL_NOTIFICATION,
405 userHandle);
406 }
407 }
Bryce Lee5dc90842015-08-11 07:57:14 -0700408 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 }
410 }
411
Meng Wang1a19e442019-10-11 10:09:00 -0700412 private List<UserHandle> getUsersExcludeDying() {
413 long[] serialNumbersOfUsers =
414 mUserManager.getSerialNumbersOfUsers(/* excludeDying= */ true);
415 List<UserHandle> users = new ArrayList<>(serialNumbersOfUsers.length);
416 for (long serialNumber : serialNumbersOfUsers) {
417 users.add(mUserManager.getUserForSerialNumber(serialNumber));
418 }
419 return users;
420 }
421
Amit Mahajan2f489b72019-10-08 11:21:52 -0700422 private boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) {
423 final List<UserManager.EnforcingUser> sources = mUserManager
424 .getUserRestrictionSources(restrictionKey, userHandle);
425 return (sources != null && !sources.isEmpty());
426 }
427
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700428 /**
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800429 * Sends a broadcast with the voicemail notification information to the default dialer. This
430 * method is also used to indicate to the default dialer when to clear the
431 * notification. A pending intent can be passed to the default dialer to indicate an action to
Bryce Lee5dc90842015-08-11 07:57:14 -0700432 * be taken as it would by a notification produced in this class.
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800433 * @param phone The phone the notification is sent from
Bryce Lee5dc90842015-08-11 07:57:14 -0700434 * @param count The number of pending voicemail messages to indicate on the notification. A
435 * Value of 0 is passed here to indicate that the notification should be cleared.
436 * @param number The voicemail phone number if specified.
437 * @param pendingIntent The intent that should be passed as the action to be taken.
438 * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings.
439 * otherwise, {@code false} to indicate the intent launches voicemail.
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800440 * @param userHandle The user to receive the notification. Each user can have their own default
441 * dialer.
442 * @return {@code true} if the default was notified of the notification.
Bryce Lee5dc90842015-08-11 07:57:14 -0700443 */
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800444 private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count,
445 String number, PendingIntent pendingIntent, boolean isSettingsIntent,
Ta-wei Yen282a9702017-05-30 17:32:29 -0700446 UserHandle userHandle, boolean isRefresh) {
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800447
448 if (shouldManageNotificationThroughDefaultDialer(userHandle)) {
449 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
Bryce Lee5dc90842015-08-11 07:57:14 -0700450 intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
Bryce Lee5dc90842015-08-11 07:57:14 -0700451 intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION);
Ta-wei Yena71a38b2017-02-24 18:19:27 -0800452 intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE,
453 PhoneUtils.makePstnPhoneAccountHandle(phone));
Ta-wei Yenafca2d62017-07-18 17:20:59 -0700454 intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh);
Bryce Lee5dc90842015-08-11 07:57:14 -0700455 if (count != null) {
456 intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count);
457 }
458
459 // Additional information about the voicemail notification beyond the count is only
460 // present when the count not specified or greater than 0. The value of 0 represents
461 // clearing the notification, which does not require additional information.
462 if (count == null || count > 0) {
463 if (!TextUtils.isEmpty(number)) {
464 intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number);
465 }
466
467 if (pendingIntent != null) {
468 intent.putExtra(isSettingsIntent
469 ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT
470 : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT,
471 pendingIntent);
472 }
473 }
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800474 mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE);
Bryce Lee5dc90842015-08-11 07:57:14 -0700475 return true;
476 }
477
478 return false;
479 }
480
Ta-wei Yen5bb19562016-11-16 11:05:37 -0800481 private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) {
482 String dialerPackage = DefaultDialerManager
483 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
484 return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION)
485 .setPackage(dialerPackage);
486 }
487
488 private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) {
489 Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle);
490 if (intent == null) {
491 return false;
492 }
493
494 List<ResolveInfo> receivers = mContext.getPackageManager()
495 .queryBroadcastReceivers(intent, 0);
496 return receivers.size() > 0;
497 }
498
Bryce Lee5dc90842015-08-11 07:57:14 -0700499 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700500 * Updates the message call forwarding indicator notification.
501 *
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530502 * @param visible true if call forwarding enabled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700503 */
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530504
505 /* package */ void updateCfi(int subId, boolean visible) {
506 updateCfi(subId, visible, false /* isRefresh */);
507 }
508
509 /**
510 * Updates the message call forwarding indicator notification.
511 *
512 * @param visible true if call forwarding enabled
513 */
514 /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) {
Tyler Gunna584e2c2017-09-19 11:40:12 -0700515 logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N"));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700516 if (visible) {
517 // If Unconditional Call Forwarding (forward all calls) for VOICE
518 // is enabled, just show a notification. We'll default to expanded
519 // view for now, so the there is less confusion about the icon. If
520 // it is deemed too weird to have CF indications as expanded views,
521 // then we'll flip the flag back.
522
523 // TODO: We may want to take a look to see if the notification can
524 // display the target to forward calls to. This will require some
525 // effort though, since there are multiple layers of messages that
526 // will need to propagate that information.
527
Andrew Leed5165b02014-12-05 15:53:58 -0800528 SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId);
529 if (subInfo == null) {
530 Log.w(LOG_TAG, "Found null subscription info for: " + subId);
531 return;
532 }
533
534 String notificationTitle;
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900535 int resId = R.drawable.stat_sys_phone_call_forward;
Andrew Leed5165b02014-12-05 15:53:58 -0800536 if (mTelephonyManager.getPhoneCount() > 1) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900537 int slotId = SubscriptionManager.getSlotIndex(subId);
538 resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1
539 : R.drawable.stat_sys_phone_call_forward_sub2;
Andrew Leed5165b02014-12-05 15:53:58 -0800540 notificationTitle = subInfo.getDisplayName().toString();
541 } else {
542 notificationTitle = mContext.getString(R.string.labelCF);
543 }
544
fionaxu8b7620d2017-05-01 16:22:17 -0700545 Notification.Builder builder = new Notification.Builder(mContext)
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900546 .setSmallIcon(resId)
Andrew Leed5165b02014-12-05 15:53:58 -0800547 .setColor(subInfo.getIconTint())
548 .setContentTitle(notificationTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700549 .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator))
550 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700551 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700552 .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD)
Srikanth Chintala4baf0b92017-11-14 15:52:47 +0530553 .setOnlyAlertOnce(isRefresh);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700554
Andrew Lee99d0ac22014-10-10 13:18:04 -0700555 Intent intent = new Intent(Intent.ACTION_MAIN);
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800556 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Andrew Lee99d0ac22014-10-10 13:18:04 -0700557 intent.setClassName("com.android.phone", "com.android.phone.CallFeaturesSetting");
Andrew Lee2fcb6c32014-12-04 14:52:35 -0800558 SubscriptionInfoHelper.addExtrasToIntent(
559 intent, mSubscriptionManager.getActiveSubscriptionInfo(subId));
fionaxu96ceebd2017-08-24 12:12:32 -0700560 builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */,
561 intent, 0));
Jordan Liu9ddde022019-08-05 13:49:08 -0700562 notifyAsUser(
fionaxu96ceebd2017-08-24 12:12:32 -0700563 Integer.toString(subId) /* tag */,
564 CALL_FORWARD_NOTIFICATION,
565 builder.build(),
566 UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 } else {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900568 List<UserInfo> users = mUserManager.getUsers(true);
569 for (UserInfo user : users) {
Jordan Liua55aaaa2019-08-28 15:33:05 -0700570 if (mUserManager.isManagedProfile(user.getUserHandle().getIdentifier())) {
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900571 continue;
572 }
573 UserHandle userHandle = user.getUserHandle();
Jordan Liu9ddde022019-08-05 13:49:08 -0700574 cancelAsUser(
Kazuya Ohshiro263737d2017-10-06 19:42:03 +0900575 Integer.toString(subId) /* tag */,
576 CALL_FORWARD_NOTIFICATION,
577 userHandle);
578 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 }
580 }
581
582 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700583 * Shows either:
584 * 1) the "Data roaming is on" notification, which
585 * appears when you're roaming and you have the "data roaming" feature turned on for the
586 * given {@code subId}.
587 * or
588 * 2) the "data disconnected due to roaming" notification, which
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 * appears when you lose data connectivity because you're roaming and
Pengquan Meng8783d932017-10-16 14:57:40 -0700590 * you have the "data roaming" feature turned off for the given {@code subId}.
Jordan Liuc353b162019-07-23 15:54:41 -0700591 * @param subId which subscription it's notifying about.
592 * @param roamingOn whether currently roaming is on or off. If true, we show notification
593 * 1) above; else we show notification 2).
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 */
Jordan Liuc353b162019-07-23 15:54:41 -0700595 /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) {
596 if (DBG) {
597 log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off")
598 + " on subId " + subId);
599 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600
601 // "Mobile network settings" screen / dialog
Nazanin Bakhshi682c77d2019-05-02 17:22:27 -0700602 Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
Jeff Davidson6d9bf522017-11-03 14:51:13 -0700603 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
Pengquan Meng8783d932017-10-16 14:57:40 -0700604 PendingIntent contentIntent = PendingIntent.getActivity(mContext, subId, intent, 0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700605
Jordan Liuc353b162019-07-23 15:54:41 -0700606 CharSequence contentTitle = mContext.getText(roamingOn
607 ? R.string.roaming_on_notification_title
608 : R.string.roaming_notification_title);
609 CharSequence contentText = mContext.getText(roamingOn
610 ? R.string.roaming_enabled_message
611 : R.string.roaming_reenable_message);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612
fionaxu8b7620d2017-05-01 16:22:17 -0700613 final Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700614 .setSmallIcon(android.R.drawable.stat_sys_warning)
Jordan Liuc353b162019-07-23 15:54:41 -0700615 .setContentTitle(contentTitle)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700616 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color))
fionaxu75b66a72017-04-19 19:01:56 -0700617 .setContentText(contentText)
Jordan Liu575c0d02019-07-09 16:29:48 -0700618 .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS)
fionaxu96ceebd2017-08-24 12:12:32 -0700619 .setContentIntent(contentIntent);
620 final Notification notif =
621 new Notification.BigTextStyle(builder).bigText(contentText).build();
Jordan Liu9ddde022019-08-05 13:49:08 -0700622 notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL);
623 }
624
625 private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
626 try {
627 Context contextForUser =
628 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
629 NotificationManager notificationManager =
630 (NotificationManager) contextForUser.getSystemService(
631 Context.NOTIFICATION_SERVICE);
632 notificationManager.notify(tag, id, notification);
633 } catch (PackageManager.NameNotFoundException e) {
634 Log.e(LOG_TAG, "unable to notify for user " + user);
635 e.printStackTrace();
636 }
637 }
638
639 private void cancelAsUser(String tag, int id, UserHandle user) {
640 try {
641 Context contextForUser =
642 mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
643 NotificationManager notificationManager =
644 (NotificationManager) contextForUser.getSystemService(
645 Context.NOTIFICATION_SERVICE);
646 notificationManager.cancel(tag, id);
647 } catch (PackageManager.NameNotFoundException e) {
648 Log.e(LOG_TAG, "unable to cancel for user " + user);
649 e.printStackTrace();
650 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 }
652
653 /**
Jordan Liuc353b162019-07-23 15:54:41 -0700654 * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700655 */
Jordan Liuc353b162019-07-23 15:54:41 -0700656 /* package */ void hideDataRoamingNotification() {
657 if (DBG) log("hideDataRoamingNotification()...");
Jordan Liu9ddde022019-08-05 13:49:08 -0700658 cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 }
660
661 /**
chen xubaf9fe52019-07-02 17:28:24 -0700662 * Shows the "Limited SIM functionality" warning notification, which appears when using a
663 * special carrier under dual sim. limited function applies for DSDS in general when two SIM
664 * cards share a single radio, thus the voice & data maybe impaired under certain scenarios.
665 */
666 public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) {
667 if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName
668 + " subId: " + subId);
669 if (mLimitedSimFunctionNotify.contains(subId)) {
670 // handle the case that user swipe the notification but condition triggers
671 // frequently which cause the same notification consistently displayed.
672 if (DBG) log("showLimitedSimFunctionWarningNotification, "
673 + "not display again if already displayed");
674 return;
675 }
676 // Navigate to "Network Selection Settings" which list all subscriptions.
677 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
678 new Intent(ACTION_MOBILE_NETWORK_LIST), 0);
Chen Xue1bbfd22019-09-18 17:17:31 -0700679 // Display phone number from the other sub
680 String line1Num = null;
681 SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService(
682 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
683 List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false);
684 for (SubscriptionInfo sub : subList) {
685 if (sub.getSubscriptionId() != subId) {
686 line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId());
687 }
688 }
chen xubaf9fe52019-07-02 17:28:24 -0700689 final CharSequence contentText = TextUtils.isEmpty(line1Num) ?
690 String.format(mContext.getText(
Chen Xu251ce8f2019-09-13 20:24:22 -0700691 R.string.limited_sim_function_notification_message).toString(), carrierName) :
chen xubaf9fe52019-07-02 17:28:24 -0700692 String.format(mContext.getText(
693 R.string.limited_sim_function_with_phone_num_notification_message).toString(),
Chen Xu251ce8f2019-09-13 20:24:22 -0700694 carrierName, line1Num);
chen xubaf9fe52019-07-02 17:28:24 -0700695 final Notification.Builder builder = new Notification.Builder(mContext)
696 .setSmallIcon(R.drawable.ic_sim_card)
697 .setContentTitle(mContext.getText(
698 R.string.limited_sim_function_notification_title))
699 .setContentText(contentText)
700 .setOnlyAlertOnce(true)
701 .setOngoing(true)
Jordan Liua55aaaa2019-08-28 15:33:05 -0700702 .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY)
chen xubaf9fe52019-07-02 17:28:24 -0700703 .setContentIntent(contentIntent);
704 final Notification notification = new Notification.BigTextStyle(builder).bigText(
705 contentText).build();
706
Jordan Liu9ddde022019-08-05 13:49:08 -0700707 notifyAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700708 LIMITED_SIM_FUNCTION_NOTIFICATION,
709 notification, UserHandle.ALL);
710 mLimitedSimFunctionNotify.add(subId);
711 }
712
713 /**
714 * Dismiss the "Limited SIM functionality" warning notification for the given subId.
715 */
716 public void dismissLimitedSimFunctionWarningNotification(int subId) {
717 if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId);
718 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
719 // dismiss all notifications
720 for (int id : mLimitedSimFunctionNotify) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700721 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700722 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
723 }
724 mLimitedSimFunctionNotify.clear();
725 } else if (mLimitedSimFunctionNotify.contains(subId)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700726 cancelAsUser(Integer.toString(subId),
chen xubaf9fe52019-07-02 17:28:24 -0700727 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
728 mLimitedSimFunctionNotify.remove(subId);
729 }
730 }
731
732 /**
733 * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions.
734 */
735 public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() {
736 if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs");
737 // dismiss notification for inactive subscriptions.
738 // handle the corner case that SIM change by SIM refresh doesn't clear the notification
739 // from the old SIM if both old & new SIM configured to display the notification.
740 mLimitedSimFunctionNotify.removeIf(id -> {
741 if (!mSubscriptionManager.isActiveSubId(id)) {
Jordan Liu9ddde022019-08-05 13:49:08 -0700742 cancelAsUser(Integer.toString(id),
chen xubaf9fe52019-07-02 17:28:24 -0700743 LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL);
744 return true;
745 }
746 return false;
747 });
748 }
749
750 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 * Display the network selection "no service" notification
752 * @param operator is the numeric operator number
Jayachandran C2ef9a482017-05-12 22:07:47 -0700753 * @param subId is the subscription ID
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 */
Jayachandran C2ef9a482017-05-12 22:07:47 -0700755 private void showNetworkSelection(String operator, int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 if (DBG) log("showNetworkSelection(" + operator + ")...");
757
Youming Ye0509b532018-09-14 16:21:17 -0700758 if (!TextUtils.isEmpty(operator)) {
759 operator = String.format(" (%s)", operator);
760 }
fionaxu8b7620d2017-05-01 16:22:17 -0700761 Notification.Builder builder = new Notification.Builder(mContext)
Andrew Lee99d0ac22014-10-10 13:18:04 -0700762 .setSmallIcon(android.R.drawable.stat_sys_warning)
763 .setContentTitle(mContext.getString(R.string.notification_network_selection_title))
764 .setContentText(
765 mContext.getString(R.string.notification_network_selection_text, operator))
766 .setShowWhen(false)
fionaxu75b66a72017-04-19 19:01:56 -0700767 .setOngoing(true)
Jordan Liu575c0d02019-07-09 16:29:48 -0700768 .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700769
770 // create the target network operators settings intent
771 Intent intent = new Intent(Intent.ACTION_MAIN);
772 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
773 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700774 // Use MobileNetworkSettings to handle the selection intent
Wei Liube964582015-08-21 11:57:00 -0700775 intent.setComponent(new ComponentName(
Malcolm Chen34d4fa52017-06-05 19:02:16 -0700776 mContext.getString(R.string.mobile_network_settings_package),
777 mContext.getString(R.string.mobile_network_settings_class)));
Pengquan Meng984ba422019-09-05 18:00:06 -0700778 intent.putExtra(Settings.EXTRA_SUB_ID, subId);
fionaxu96ceebd2017-08-24 12:12:32 -0700779 builder.setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0));
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.
823 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}