Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
Ta-wei Yen | afca2d6 | 2017-07-18 17:20:59 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.READ_PHONE_STATE; |
| 20 | |
Rambo Wang | c30938c | 2022-08-10 22:54:23 +0000 | [diff] [blame] | 21 | import android.annotation.NonNull; |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Tyler Gunn | 99ae269 | 2020-12-09 11:35:21 -0800 | [diff] [blame] | 23 | import android.app.BroadcastOptions; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 24 | import android.app.Notification; |
| 25 | import android.app.NotificationManager; |
| 26 | import android.app.PendingIntent; |
| 27 | import android.app.StatusBarManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 28 | import android.content.ComponentName; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.SharedPreferences; |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 33 | import android.content.pm.ResolveInfo; |
Nancy Chen | b4a9270 | 2014-12-04 15:57:29 -0800 | [diff] [blame] | 34 | import android.content.res.Resources; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.Message; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 38 | import android.os.PersistableBundle; |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 39 | import android.os.SystemClock; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 40 | import android.os.SystemProperties; |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 41 | import android.os.UserHandle; |
| 42 | import android.os.UserManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 43 | import android.preference.PreferenceManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 44 | import android.provider.ContactsContract.PhoneLookup; |
Jeff Davidson | 6d9bf52 | 2017-11-03 14:51:13 -0700 | [diff] [blame] | 45 | import android.provider.Settings; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 46 | import android.telecom.PhoneAccount; |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 47 | import android.telecom.PhoneAccountHandle; |
| 48 | import android.telecom.TelecomManager; |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 49 | import android.telephony.CarrierConfigManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 50 | import android.telephony.PhoneNumberUtils; |
Rambo Wang | c30938c | 2022-08-10 22:54:23 +0000 | [diff] [blame] | 51 | import android.telephony.RadioAccessFamily; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 52 | import android.telephony.ServiceState; |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 53 | import android.telephony.SubscriptionInfo; |
Andrew Lee | a82b820 | 2014-11-21 16:18:28 -0800 | [diff] [blame] | 54 | import android.telephony.SubscriptionManager; |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 55 | import android.telephony.TelephonyManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 56 | import android.text.TextUtils; |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 57 | import android.util.ArrayMap; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 58 | import android.util.Log; |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 59 | import android.util.SparseArray; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 60 | import android.widget.Toast; |
Ta-wei Yen | b29425b | 2016-09-21 17:28:14 -0700 | [diff] [blame] | 61 | |
rambowang | 7d6f414 | 2022-08-13 20:45:20 -0500 | [diff] [blame] | 62 | import com.android.internal.annotations.VisibleForTesting; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 63 | import com.android.internal.telephony.Phone; |
Jayachandran C | 2ef9a48 | 2017-05-12 22:07:47 -0700 | [diff] [blame] | 64 | import com.android.internal.telephony.PhoneFactory; |
Rambo Wang | c30938c | 2022-08-10 22:54:23 +0000 | [diff] [blame] | 65 | import com.android.internal.telephony.RILConstants; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 66 | import com.android.internal.telephony.TelephonyCapabilities; |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 67 | import com.android.internal.telephony.util.NotificationChannelController; |
Andrew Lee | bf07f76 | 2015-04-07 19:05:50 -0700 | [diff] [blame] | 68 | import com.android.phone.settings.VoicemailSettingsActivity; |
Ta-wei Yen | b29425b | 2016-09-21 17:28:14 -0700 | [diff] [blame] | 69 | |
Meng Wang | 1a19e44 | 2019-10-11 10:09:00 -0700 | [diff] [blame] | 70 | import java.util.ArrayList; |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 71 | import java.util.HashSet; |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 72 | import java.util.Iterator; |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 73 | import java.util.List; |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 74 | import java.util.Set; |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 75 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 76 | /** |
| 77 | * NotificationManager-related utility code for the Phone app. |
| 78 | * |
| 79 | * This is a singleton object which acts as the interface to the |
| 80 | * framework's NotificationManager, and is used to display status bar |
| 81 | * icons and control other status bar-related behavior. |
| 82 | * |
| 83 | * @see PhoneGlobals.notificationMgr |
| 84 | */ |
Chiao Cheng | 312b9c9 | 2013-09-16 15:40:53 -0700 | [diff] [blame] | 85 | public class NotificationMgr { |
Andrew Lee | a82b820 | 2014-11-21 16:18:28 -0800 | [diff] [blame] | 86 | private static final String LOG_TAG = NotificationMgr.class.getSimpleName(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 87 | private static final boolean DBG = |
| 88 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
| 89 | // Do not check in with VDBG = true, since that may write PII to the system log. |
| 90 | private static final boolean VDBG = false; |
| 91 | |
Ta-wei Yen | b29425b | 2016-09-21 17:28:14 -0700 | [diff] [blame] | 92 | private static final String MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX = |
| 93 | "mwi_should_check_vvm_configuration_state_"; |
| 94 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 95 | // notification types |
Santos Cordon | f68db2e | 2014-07-02 14:40:44 -0700 | [diff] [blame] | 96 | static final int MMI_NOTIFICATION = 1; |
| 97 | static final int NETWORK_SELECTION_NOTIFICATION = 2; |
| 98 | static final int VOICEMAIL_NOTIFICATION = 3; |
| 99 | static final int CALL_FORWARD_NOTIFICATION = 4; |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 100 | static final int DATA_ROAMING_NOTIFICATION = 5; |
Santos Cordon | f68db2e | 2014-07-02 14:40:44 -0700 | [diff] [blame] | 101 | static final int SELECTED_OPERATOR_FAIL_NOTIFICATION = 6; |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 102 | static final int LIMITED_SIM_FUNCTION_NOTIFICATION = 7; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 103 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 104 | // Event for network selection notification. |
| 105 | private static final int EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION = 1; |
| 106 | |
| 107 | private static final long NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS = 10000L; |
| 108 | private static final int NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES = 10; |
| 109 | |
| 110 | private static final int STATE_UNKNOWN_SERVICE = -1; |
| 111 | |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 112 | private static final String ACTION_MOBILE_NETWORK_LIST = "android.settings.MOBILE_NETWORK_LIST"; |
| 113 | |
Tyler Gunn | 99ae269 | 2020-12-09 11:35:21 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Grant recipients of new voicemail broadcasts a 10sec allowlist so they can start a background |
| 116 | * service to do VVM processing. |
| 117 | */ |
| 118 | private final long VOICEMAIL_ALLOW_LIST_DURATION_MILLIS = 10000L; |
| 119 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 120 | /** The singleton NotificationMgr instance. */ |
| 121 | private static NotificationMgr sInstance; |
| 122 | |
| 123 | private PhoneGlobals mApp; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 124 | |
| 125 | private Context mContext; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 126 | private StatusBarManager mStatusBarManager; |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 127 | private UserManager mUserManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 128 | private Toast mToast; |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 129 | private SubscriptionManager mSubscriptionManager; |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 130 | private TelecomManager mTelecomManager; |
| 131 | private TelephonyManager mTelephonyManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 132 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 133 | // used to track the notification of selected network unavailable, per subscription id. |
| 134 | private SparseArray<Boolean> mSelectedUnavailableNotify = new SparseArray<>(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 135 | |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 136 | // used to track the notification of limited sim function under dual sim, per subscription id. |
| 137 | private Set<Integer> mLimitedSimFunctionNotify = new HashSet<>(); |
| 138 | |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 139 | // used to track whether the message waiting indicator is visible, per subscription id. |
| 140 | private ArrayMap<Integer, Boolean> mMwiVisible = new ArrayMap<Integer, Boolean>(); |
| 141 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 142 | // those flags are used to track whether to show network selection notification or not. |
| 143 | private SparseArray<Integer> mPreviousServiceState = new SparseArray<>(); |
| 144 | private SparseArray<Long> mOOSTimestamp = new SparseArray<>(); |
| 145 | private SparseArray<Integer> mPendingEventCounter = new SparseArray<>(); |
| 146 | // maps each subId to selected network operator name. |
| 147 | private SparseArray<String> mSelectedNetworkOperatorName = new SparseArray<>(); |
| 148 | |
| 149 | private final Handler mHandler = new Handler() { |
| 150 | @Override |
| 151 | public void handleMessage(Message msg) { |
| 152 | switch (msg.what) { |
| 153 | case EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION: |
| 154 | int subId = (int) msg.obj; |
| 155 | TelephonyManager telephonyManager = |
| 156 | mTelephonyManager.createForSubscriptionId(subId); |
| 157 | if (telephonyManager.getServiceState() != null) { |
| 158 | shouldShowNotification(telephonyManager.getServiceState().getState(), |
| 159 | subId); |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Private constructor (this is a singleton). |
Santos Cordon | f68db2e | 2014-07-02 14:40:44 -0700 | [diff] [blame] | 168 | * @see #init(PhoneGlobals) |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 169 | */ |
rambowang | 7d6f414 | 2022-08-13 20:45:20 -0500 | [diff] [blame] | 170 | @VisibleForTesting |
| 171 | /* package */ NotificationMgr(PhoneGlobals app) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 172 | mApp = app; |
| 173 | mContext = app; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 174 | mStatusBarManager = |
| 175 | (StatusBarManager) app.getSystemService(Context.STATUS_BAR_SERVICE); |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 176 | mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE); |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 177 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Tyler Gunn | 5ddfdc9 | 2019-10-31 13:08:23 -0700 | [diff] [blame] | 178 | mTelecomManager = app.getSystemService(TelecomManager.class); |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 179 | mTelephonyManager = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Initialize the singleton NotificationMgr instance. |
| 184 | * |
| 185 | * This is only done once, at startup, from PhoneApp.onCreate(). |
| 186 | * From then on, the NotificationMgr instance is available via the |
| 187 | * PhoneApp's public "notificationMgr" field, which is why there's no |
| 188 | * getInstance() method here. |
| 189 | */ |
| 190 | /* package */ static NotificationMgr init(PhoneGlobals app) { |
| 191 | synchronized (NotificationMgr.class) { |
| 192 | if (sInstance == null) { |
| 193 | sInstance = new NotificationMgr(app); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 194 | } else { |
| 195 | Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance); |
| 196 | } |
| 197 | return sInstance; |
| 198 | } |
| 199 | } |
| 200 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 201 | /** The projection to use when querying the phones table */ |
| 202 | static final String[] PHONES_PROJECTION = new String[] { |
| 203 | PhoneLookup.NUMBER, |
| 204 | PhoneLookup.DISPLAY_NAME, |
| 205 | PhoneLookup._ID |
| 206 | }; |
| 207 | |
| 208 | /** |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 209 | * Re-creates the message waiting indicator (voicemail) notification if it is showing. Used to |
| 210 | * refresh the voicemail intent on the indicator when the user changes it via the voicemail |
| 211 | * settings screen. The voicemail notification sound is suppressed. |
| 212 | * |
| 213 | * @param subId The subscription Id. |
| 214 | */ |
| 215 | /* package */ void refreshMwi(int subId) { |
| 216 | // In a single-sim device, subId can be -1 which means "no sub id". In this case we will |
| 217 | // reference the single subid stored in the mMwiVisible map. |
Ta-wei Yen | a1390d4 | 2017-12-04 15:11:33 -0800 | [diff] [blame] | 218 | if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 219 | if (mMwiVisible.keySet().size() == 1) { |
| 220 | Set<Integer> keySet = mMwiVisible.keySet(); |
| 221 | Iterator<Integer> keyIt = keySet.iterator(); |
| 222 | if (!keyIt.hasNext()) { |
| 223 | return; |
| 224 | } |
| 225 | subId = keyIt.next(); |
| 226 | } |
| 227 | } |
| 228 | if (mMwiVisible.containsKey(subId)) { |
| 229 | boolean mwiVisible = mMwiVisible.get(subId); |
| 230 | if (mwiVisible) { |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 231 | mApp.notifier.updatePhoneStateListeners(true); |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Ta-wei Yen | b29425b | 2016-09-21 17:28:14 -0700 | [diff] [blame] | 236 | public void setShouldCheckVisualVoicemailConfigurationForMwi(int subId, boolean enabled) { |
| 237 | if (!SubscriptionManager.isValidSubscriptionId(subId)) { |
| 238 | Log.e(LOG_TAG, "setShouldCheckVisualVoicemailConfigurationForMwi: invalid subId" |
| 239 | + subId); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | PreferenceManager.getDefaultSharedPreferences(mContext).edit() |
| 244 | .putBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, enabled) |
| 245 | .apply(); |
| 246 | } |
| 247 | |
| 248 | private boolean shouldCheckVisualVoicemailConfigurationForMwi(int subId) { |
| 249 | if (!SubscriptionManager.isValidSubscriptionId(subId)) { |
| 250 | Log.e(LOG_TAG, "shouldCheckVisualVoicemailConfigurationForMwi: invalid subId" + subId); |
| 251 | return true; |
| 252 | } |
| 253 | return PreferenceManager |
| 254 | .getDefaultSharedPreferences(mContext) |
| 255 | .getBoolean(MWI_SHOULD_CHECK_VVM_CONFIGURATION_KEY_PREFIX + subId, true); |
| 256 | } |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 257 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 258 | * Updates the message waiting indicator (voicemail) notification. |
| 259 | * |
| 260 | * @param visible true if there are messages waiting |
| 261 | */ |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 262 | /* package */ void updateMwi(int subId, boolean visible) { |
Ta-wei Yen | 282a970 | 2017-05-30 17:32:29 -0700 | [diff] [blame] | 263 | updateMwi(subId, visible, false /* isRefresh */); |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Updates the message waiting indicator (voicemail) notification. |
| 268 | * |
| 269 | * @param subId the subId to update. |
| 270 | * @param visible true if there are messages waiting |
Ta-wei Yen | 282a970 | 2017-05-30 17:32:29 -0700 | [diff] [blame] | 271 | * @param isRefresh {@code true} if the notification is a refresh and the user should not be |
| 272 | * notified again. |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 273 | */ |
Ta-wei Yen | 282a970 | 2017-05-30 17:32:29 -0700 | [diff] [blame] | 274 | void updateMwi(int subId, boolean visible, boolean isRefresh) { |
Andrew Lee | a82b820 | 2014-11-21 16:18:28 -0800 | [diff] [blame] | 275 | if (!PhoneGlobals.sVoiceCapable) { |
| 276 | // Do not show the message waiting indicator on devices which are not voice capable. |
| 277 | // These events *should* be blocked at the telephony layer for such devices. |
| 278 | Log.w(LOG_TAG, "Called updateMwi() on non-voice-capable device! Ignoring..."); |
| 279 | return; |
| 280 | } |
| 281 | |
Nancy Chen | 2cf7f29 | 2015-05-15 11:00:10 -0700 | [diff] [blame] | 282 | Phone phone = PhoneGlobals.getPhone(subId); |
Yorke Lee | 67a62a2 | 2014-12-15 18:46:17 -0800 | [diff] [blame] | 283 | Log.i(LOG_TAG, "updateMwi(): subId " + subId + " update to " + visible); |
Andrew Lee | f8ad78f | 2014-12-15 16:17:29 -0800 | [diff] [blame] | 284 | mMwiVisible.put(subId, visible); |
| 285 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 286 | if (visible) { |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 287 | if (phone == null) { |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 288 | Log.w(LOG_TAG, "Found null phone for: " + subId); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId); |
| 293 | if (subInfo == null) { |
| 294 | Log.w(LOG_TAG, "Found null subscription info for: " + subId); |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 298 | int resId = android.R.drawable.stat_notify_voicemail; |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 299 | if (mTelephonyManager.getPhoneCount() > 1) { |
| 300 | resId = (phone.getPhoneId() == 0) ? R.drawable.stat_notify_voicemail_sub1 |
| 301 | : R.drawable.stat_notify_voicemail_sub2; |
| 302 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 303 | |
| 304 | // This Notification can get a lot fancier once we have more |
| 305 | // information about the current voicemail messages. |
| 306 | // (For example, the current voicemail system can't tell |
| 307 | // us the caller-id or timestamp of a message, or tell us the |
| 308 | // message count.) |
| 309 | |
| 310 | // But for now, the UI is ultra-simple: if the MWI indication |
| 311 | // is supposed to be visible, just show a single generic |
| 312 | // notification. |
| 313 | |
| 314 | String notificationTitle = mContext.getString(R.string.notification_voicemail_title); |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 315 | String vmNumber = phone.getVoiceMailNumber(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 316 | if (DBG) log("- got vm number: '" + vmNumber + "'"); |
| 317 | |
Andrew Lee | a82b820 | 2014-11-21 16:18:28 -0800 | [diff] [blame] | 318 | // The voicemail number may be null because: |
| 319 | // (1) This phone has no voicemail number. |
| 320 | // (2) This phone has a voicemail number, but the SIM isn't ready yet. This may |
| 321 | // happen when the device first boots if we get a MWI notification when we |
| 322 | // register on the network before the SIM has loaded. In this case, the |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 323 | // SubscriptionListener in CallNotifier will update this once the SIM is loaded. |
| 324 | if ((vmNumber == null) && !phone.getIccRecordsLoaded()) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 325 | if (DBG) log("- Null vm number: SIM records not loaded (yet)..."); |
Andrew Lee | a82b820 | 2014-11-21 16:18:28 -0800 | [diff] [blame] | 326 | return; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 329 | Integer vmCount = null; |
| 330 | |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 331 | if (TelephonyCapabilities.supportsVoiceMessageCount(phone)) { |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 332 | vmCount = phone.getVoiceMessageCount(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 333 | String titleFormat = mContext.getString(R.string.notification_voicemail_title_count); |
| 334 | notificationTitle = String.format(titleFormat, vmCount); |
| 335 | } |
| 336 | |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 337 | // This pathway only applies to PSTN accounts; only SIMS have subscription ids. |
| 338 | PhoneAccountHandle phoneAccountHandle = PhoneUtils.makePstnPhoneAccountHandle(phone); |
| 339 | |
| 340 | Intent intent; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 341 | String notificationText; |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 342 | boolean isSettingsIntent = TextUtils.isEmpty(vmNumber); |
| 343 | |
| 344 | if (isSettingsIntent) { |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 345 | notificationText = mContext.getString( |
| 346 | R.string.notification_voicemail_no_vm_number); |
| 347 | |
| 348 | // If the voicemail number if unknown, instead of calling voicemail, take the user |
| 349 | // to the voicemail settings. |
| 350 | notificationText = mContext.getString( |
| 351 | R.string.notification_voicemail_no_vm_number); |
Andrew Lee | bf07f76 | 2015-04-07 19:05:50 -0700 | [diff] [blame] | 352 | intent = new Intent(VoicemailSettingsActivity.ACTION_ADD_VOICEMAIL); |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 353 | intent.putExtra(SubscriptionInfoHelper.SUB_ID_EXTRA, subId); |
Andrew Lee | bf07f76 | 2015-04-07 19:05:50 -0700 | [diff] [blame] | 354 | intent.setClass(mContext, VoicemailSettingsActivity.class); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 355 | } else { |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 356 | if (mTelephonyManager.getPhoneCount() > 1) { |
| 357 | notificationText = subInfo.getDisplayName().toString(); |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 358 | } else { |
| 359 | notificationText = String.format( |
| 360 | mContext.getString(R.string.notification_voicemail_text_format), |
| 361 | PhoneNumberUtils.formatNumber(vmNumber)); |
| 362 | } |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 363 | intent = new Intent( |
| 364 | Intent.ACTION_CALL, Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 365 | null)); |
Tyler Gunn | 9c1071f | 2014-12-09 10:07:54 -0800 | [diff] [blame] | 366 | intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandle); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 367 | } |
Oli Thompson | df9e139 | 2023-03-13 15:48:59 +0000 | [diff] [blame^] | 368 | PendingIntent pendingIntent; |
| 369 | UserHandle subAssociatedUserHandle = |
| 370 | mSubscriptionManager.getSubscriptionUserHandle(subId); |
| 371 | if (subAssociatedUserHandle == null) { |
| 372 | pendingIntent = PendingIntent.getActivity(mContext, subId /* requestCode */, intent, |
| 373 | PendingIntent.FLAG_IMMUTABLE); |
| 374 | } else { |
| 375 | pendingIntent = PendingIntent.getActivityAsUser(mContext, subId /* requestCode */, |
| 376 | intent, PendingIntent.FLAG_IMMUTABLE, null, subAssociatedUserHandle); |
| 377 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 378 | |
Nancy Chen | b4a9270 | 2014-12-04 15:57:29 -0800 | [diff] [blame] | 379 | Resources res = mContext.getResources(); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 380 | PersistableBundle carrierConfig = PhoneGlobals.getInstance().getCarrierConfigForSubId( |
Ta-wei Yen | 9b37a87 | 2016-05-27 12:16:58 -0700 | [diff] [blame] | 381 | subId); |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 382 | Notification.Builder builder = new Notification.Builder(mContext); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 383 | builder.setSmallIcon(resId) |
| 384 | .setWhen(System.currentTimeMillis()) |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 385 | .setColor(subInfo.getIconTint()) |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 386 | .setContentTitle(notificationTitle) |
| 387 | .setContentText(notificationText) |
| 388 | .setContentIntent(pendingIntent) |
Nancy Chen | b4a9270 | 2014-12-04 15:57:29 -0800 | [diff] [blame] | 389 | .setColor(res.getColor(R.color.dialer_theme_color)) |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 390 | .setOngoing(carrierConfig.getBoolean( |
fionaxu | 75b66a7 | 2017-04-19 19:01:56 -0700 | [diff] [blame] | 391 | CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL)) |
Jordan Liu | 575c0d0 | 2019-07-09 16:29:48 -0700 | [diff] [blame] | 392 | .setChannelId(NotificationChannelController.CHANNEL_ID_VOICE_MAIL) |
Ta-wei Yen | 282a970 | 2017-05-30 17:32:29 -0700 | [diff] [blame] | 393 | .setOnlyAlertOnce(isRefresh); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 394 | |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 395 | final Notification notification = builder.build(); |
Meng Wang | 1a19e44 | 2019-10-11 10:09:00 -0700 | [diff] [blame] | 396 | List<UserHandle> users = getUsersExcludeDying(); |
| 397 | for (UserHandle userHandle : users) { |
Oli Thompson | df9e139 | 2023-03-13 15:48:59 +0000 | [diff] [blame^] | 398 | boolean isManagedUser = mUserManager.isManagedProfile(userHandle.getIdentifier()); |
| 399 | if (!hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, userHandle) |
| 400 | && (userHandle.equals(subAssociatedUserHandle) |
| 401 | || (subAssociatedUserHandle == null && !isManagedUser)) |
| 402 | && !maybeSendVoicemailNotificationUsingDefaultDialer(phone, vmCount, |
| 403 | vmNumber, pendingIntent, isSettingsIntent, userHandle, isRefresh)) { |
| 404 | notifyAsUser( |
| 405 | Integer.toString(subId) /* tag */, |
| 406 | VOICEMAIL_NOTIFICATION, |
| 407 | notification, |
| 408 | userHandle); |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 411 | } else { |
Oli Thompson | df9e139 | 2023-03-13 15:48:59 +0000 | [diff] [blame^] | 412 | cancelAsUser(Integer.toString(subId) /* tag */, VOICEMAIL_NOTIFICATION, UserHandle.ALL); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Meng Wang | 1a19e44 | 2019-10-11 10:09:00 -0700 | [diff] [blame] | 416 | private List<UserHandle> getUsersExcludeDying() { |
| 417 | long[] serialNumbersOfUsers = |
| 418 | mUserManager.getSerialNumbersOfUsers(/* excludeDying= */ true); |
| 419 | List<UserHandle> users = new ArrayList<>(serialNumbersOfUsers.length); |
| 420 | for (long serialNumber : serialNumbersOfUsers) { |
| 421 | users.add(mUserManager.getUserForSerialNumber(serialNumber)); |
| 422 | } |
| 423 | return users; |
| 424 | } |
| 425 | |
Amit Mahajan | 2f489b7 | 2019-10-08 11:21:52 -0700 | [diff] [blame] | 426 | private boolean hasUserRestriction(String restrictionKey, UserHandle userHandle) { |
| 427 | final List<UserManager.EnforcingUser> sources = mUserManager |
| 428 | .getUserRestrictionSources(restrictionKey, userHandle); |
| 429 | return (sources != null && !sources.isEmpty()); |
| 430 | } |
| 431 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 432 | /** |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 433 | * Sends a broadcast with the voicemail notification information to the default dialer. This |
| 434 | * method is also used to indicate to the default dialer when to clear the |
| 435 | * notification. A pending intent can be passed to the default dialer to indicate an action to |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 436 | * be taken as it would by a notification produced in this class. |
Ta-wei Yen | a71a38b | 2017-02-24 18:19:27 -0800 | [diff] [blame] | 437 | * @param phone The phone the notification is sent from |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 438 | * @param count The number of pending voicemail messages to indicate on the notification. A |
| 439 | * Value of 0 is passed here to indicate that the notification should be cleared. |
| 440 | * @param number The voicemail phone number if specified. |
| 441 | * @param pendingIntent The intent that should be passed as the action to be taken. |
| 442 | * @param isSettingsIntent {@code true} to indicate the pending intent is to launch settings. |
| 443 | * otherwise, {@code false} to indicate the intent launches voicemail. |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 444 | * @param userHandle The user to receive the notification. Each user can have their own default |
| 445 | * dialer. |
| 446 | * @return {@code true} if the default was notified of the notification. |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 447 | */ |
Ta-wei Yen | a71a38b | 2017-02-24 18:19:27 -0800 | [diff] [blame] | 448 | private boolean maybeSendVoicemailNotificationUsingDefaultDialer(Phone phone, Integer count, |
| 449 | String number, PendingIntent pendingIntent, boolean isSettingsIntent, |
Ta-wei Yen | 282a970 | 2017-05-30 17:32:29 -0700 | [diff] [blame] | 450 | UserHandle userHandle, boolean isRefresh) { |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 451 | |
| 452 | if (shouldManageNotificationThroughDefaultDialer(userHandle)) { |
| 453 | Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle); |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 454 | intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 455 | intent.setAction(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION); |
Ta-wei Yen | a71a38b | 2017-02-24 18:19:27 -0800 | [diff] [blame] | 456 | intent.putExtra(TelephonyManager.EXTRA_PHONE_ACCOUNT_HANDLE, |
| 457 | PhoneUtils.makePstnPhoneAccountHandle(phone)); |
Ta-wei Yen | afca2d6 | 2017-07-18 17:20:59 -0700 | [diff] [blame] | 458 | intent.putExtra(TelephonyManager.EXTRA_IS_REFRESH, isRefresh); |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 459 | if (count != null) { |
| 460 | intent.putExtra(TelephonyManager.EXTRA_NOTIFICATION_COUNT, count); |
| 461 | } |
| 462 | |
| 463 | // Additional information about the voicemail notification beyond the count is only |
| 464 | // present when the count not specified or greater than 0. The value of 0 represents |
| 465 | // clearing the notification, which does not require additional information. |
| 466 | if (count == null || count > 0) { |
| 467 | if (!TextUtils.isEmpty(number)) { |
| 468 | intent.putExtra(TelephonyManager.EXTRA_VOICEMAIL_NUMBER, number); |
| 469 | } |
| 470 | |
| 471 | if (pendingIntent != null) { |
| 472 | intent.putExtra(isSettingsIntent |
| 473 | ? TelephonyManager.EXTRA_LAUNCH_VOICEMAIL_SETTINGS_INTENT |
| 474 | : TelephonyManager.EXTRA_CALL_VOICEMAIL_INTENT, |
| 475 | pendingIntent); |
| 476 | } |
| 477 | } |
Tyler Gunn | 99ae269 | 2020-12-09 11:35:21 -0800 | [diff] [blame] | 478 | |
| 479 | BroadcastOptions bopts = BroadcastOptions.makeBasic(); |
| 480 | bopts.setTemporaryAppWhitelistDuration(VOICEMAIL_ALLOW_LIST_DURATION_MILLIS); |
| 481 | mContext.sendBroadcastAsUser(intent, userHandle, READ_PHONE_STATE, bopts.toBundle()); |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 482 | return true; |
| 483 | } |
| 484 | |
| 485 | return false; |
| 486 | } |
| 487 | |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 488 | private Intent getShowVoicemailIntentForDefaultDialer(UserHandle userHandle) { |
Tyler Gunn | 5ddfdc9 | 2019-10-31 13:08:23 -0700 | [diff] [blame] | 489 | String dialerPackage = mContext.getSystemService(TelecomManager.class) |
Tyler Gunn | 83adc05 | 2020-01-28 09:12:21 -0800 | [diff] [blame] | 490 | .getDefaultDialerPackage(userHandle); |
Ta-wei Yen | 5bb1956 | 2016-11-16 11:05:37 -0800 | [diff] [blame] | 491 | return new Intent(TelephonyManager.ACTION_SHOW_VOICEMAIL_NOTIFICATION) |
| 492 | .setPackage(dialerPackage); |
| 493 | } |
| 494 | |
| 495 | private boolean shouldManageNotificationThroughDefaultDialer(UserHandle userHandle) { |
| 496 | Intent intent = getShowVoicemailIntentForDefaultDialer(userHandle); |
| 497 | if (intent == null) { |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | List<ResolveInfo> receivers = mContext.getPackageManager() |
| 502 | .queryBroadcastReceivers(intent, 0); |
| 503 | return receivers.size() > 0; |
| 504 | } |
| 505 | |
Bryce Lee | 5dc9084 | 2015-08-11 07:57:14 -0700 | [diff] [blame] | 506 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 507 | * Updates the message call forwarding indicator notification. |
| 508 | * |
Srikanth Chintala | 4baf0b9 | 2017-11-14 15:52:47 +0530 | [diff] [blame] | 509 | * @param visible true if call forwarding enabled |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 510 | */ |
Srikanth Chintala | 4baf0b9 | 2017-11-14 15:52:47 +0530 | [diff] [blame] | 511 | |
| 512 | /* package */ void updateCfi(int subId, boolean visible) { |
| 513 | updateCfi(subId, visible, false /* isRefresh */); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Updates the message call forwarding indicator notification. |
| 518 | * |
| 519 | * @param visible true if call forwarding enabled |
| 520 | */ |
| 521 | /* package */ void updateCfi(int subId, boolean visible, boolean isRefresh) { |
Tyler Gunn | a584e2c | 2017-09-19 11:40:12 -0700 | [diff] [blame] | 522 | logi("updateCfi: subId= " + subId + ", visible=" + (visible ? "Y" : "N")); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 523 | if (visible) { |
| 524 | // If Unconditional Call Forwarding (forward all calls) for VOICE |
| 525 | // is enabled, just show a notification. We'll default to expanded |
| 526 | // view for now, so the there is less confusion about the icon. If |
| 527 | // it is deemed too weird to have CF indications as expanded views, |
| 528 | // then we'll flip the flag back. |
| 529 | |
| 530 | // TODO: We may want to take a look to see if the notification can |
| 531 | // display the target to forward calls to. This will require some |
| 532 | // effort though, since there are multiple layers of messages that |
| 533 | // will need to propagate that information. |
| 534 | |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 535 | SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(subId); |
| 536 | if (subInfo == null) { |
| 537 | Log.w(LOG_TAG, "Found null subscription info for: " + subId); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | String notificationTitle; |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 542 | int resId = R.drawable.stat_sys_phone_call_forward; |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 543 | if (mTelephonyManager.getPhoneCount() > 1) { |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 544 | int slotId = SubscriptionManager.getSlotIndex(subId); |
| 545 | resId = (slotId == 0) ? R.drawable.stat_sys_phone_call_forward_sub1 |
| 546 | : R.drawable.stat_sys_phone_call_forward_sub2; |
Sarah Chin | 2c5f23d | 2020-11-02 21:52:11 -0800 | [diff] [blame] | 547 | if (subInfo.getDisplayName() != null) { |
| 548 | notificationTitle = subInfo.getDisplayName().toString(); |
| 549 | } else { |
| 550 | notificationTitle = mContext.getString(R.string.labelCF); |
| 551 | } |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 552 | } else { |
| 553 | notificationTitle = mContext.getString(R.string.labelCF); |
| 554 | } |
| 555 | |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 556 | Notification.Builder builder = new Notification.Builder(mContext) |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 557 | .setSmallIcon(resId) |
Andrew Lee | d5165b0 | 2014-12-05 15:53:58 -0800 | [diff] [blame] | 558 | .setColor(subInfo.getIconTint()) |
| 559 | .setContentTitle(notificationTitle) |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 560 | .setContentText(mContext.getString(R.string.sum_cfu_enabled_indicator)) |
| 561 | .setShowWhen(false) |
fionaxu | 75b66a7 | 2017-04-19 19:01:56 -0700 | [diff] [blame] | 562 | .setOngoing(true) |
Jordan Liu | 575c0d0 | 2019-07-09 16:29:48 -0700 | [diff] [blame] | 563 | .setChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD) |
Srikanth Chintala | 4baf0b9 | 2017-11-14 15:52:47 +0530 | [diff] [blame] | 564 | .setOnlyAlertOnce(isRefresh); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 565 | |
Tyler Gunn | 5bc2171 | 2021-04-30 15:07:59 -0700 | [diff] [blame] | 566 | Intent intent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS); |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 567 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Andrew Lee | 2fcb6c3 | 2014-12-04 14:52:35 -0800 | [diff] [blame] | 568 | SubscriptionInfoHelper.addExtrasToIntent( |
| 569 | intent, mSubscriptionManager.getActiveSubscriptionInfo(subId)); |
fionaxu | 96ceebd | 2017-08-24 12:12:32 -0700 | [diff] [blame] | 570 | builder.setContentIntent(PendingIntent.getActivity(mContext, subId /* requestCode */, |
Shuo Qian | 9a41a17 | 2020-04-24 19:19:31 -0700 | [diff] [blame] | 571 | intent, PendingIntent.FLAG_IMMUTABLE)); |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 572 | notifyAsUser( |
fionaxu | 96ceebd | 2017-08-24 12:12:32 -0700 | [diff] [blame] | 573 | Integer.toString(subId) /* tag */, |
| 574 | CALL_FORWARD_NOTIFICATION, |
| 575 | builder.build(), |
| 576 | UserHandle.ALL); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 577 | } else { |
Meng Wang | 21487ab | 2019-11-21 10:44:42 -0800 | [diff] [blame] | 578 | List<UserHandle> users = getUsersExcludeDying(); |
| 579 | for (UserHandle user : users) { |
| 580 | if (mUserManager.isManagedProfile(user.getIdentifier())) { |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 581 | continue; |
| 582 | } |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 583 | cancelAsUser( |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 584 | Integer.toString(subId) /* tag */, |
| 585 | CALL_FORWARD_NOTIFICATION, |
Meng Wang | 21487ab | 2019-11-21 10:44:42 -0800 | [diff] [blame] | 586 | user); |
Kazuya Ohshiro | 263737d | 2017-10-06 19:42:03 +0900 | [diff] [blame] | 587 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 592 | * Shows either: |
| 593 | * 1) the "Data roaming is on" notification, which |
| 594 | * appears when you're roaming and you have the "data roaming" feature turned on for the |
| 595 | * given {@code subId}. |
| 596 | * or |
| 597 | * 2) the "data disconnected due to roaming" notification, which |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 598 | * appears when you lose data connectivity because you're roaming and |
Pengquan Meng | 8783d93 | 2017-10-16 14:57:40 -0700 | [diff] [blame] | 599 | * you have the "data roaming" feature turned off for the given {@code subId}. |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 600 | * @param subId which subscription it's notifying about. |
| 601 | * @param roamingOn whether currently roaming is on or off. If true, we show notification |
| 602 | * 1) above; else we show notification 2). |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 603 | */ |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 604 | /* package */ void showDataRoamingNotification(int subId, boolean roamingOn) { |
| 605 | if (DBG) { |
| 606 | log("showDataRoamingNotification() roaming " + (roamingOn ? "on" : "off") |
| 607 | + " on subId " + subId); |
| 608 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 609 | |
| 610 | // "Mobile network settings" screen / dialog |
Nazanin Bakhshi | 682c77d | 2019-05-02 17:22:27 -0700 | [diff] [blame] | 611 | Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS); |
Jeff Davidson | 6d9bf52 | 2017-11-03 14:51:13 -0700 | [diff] [blame] | 612 | intent.putExtra(Settings.EXTRA_SUB_ID, subId); |
Shuo Qian | 9a41a17 | 2020-04-24 19:19:31 -0700 | [diff] [blame] | 613 | PendingIntent contentIntent = PendingIntent.getActivity( |
| 614 | mContext, subId, intent, PendingIntent.FLAG_IMMUTABLE); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 615 | |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 616 | CharSequence contentTitle = mContext.getText(roamingOn |
| 617 | ? R.string.roaming_on_notification_title |
| 618 | : R.string.roaming_notification_title); |
| 619 | CharSequence contentText = mContext.getText(roamingOn |
| 620 | ? R.string.roaming_enabled_message |
| 621 | : R.string.roaming_reenable_message); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 622 | |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 623 | final Notification.Builder builder = new Notification.Builder(mContext) |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 624 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 625 | .setContentTitle(contentTitle) |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 626 | .setColor(mContext.getResources().getColor(R.color.dialer_theme_color)) |
fionaxu | 75b66a7 | 2017-04-19 19:01:56 -0700 | [diff] [blame] | 627 | .setContentText(contentText) |
Jordan Liu | 575c0d0 | 2019-07-09 16:29:48 -0700 | [diff] [blame] | 628 | .setChannelId(NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS) |
fionaxu | 96ceebd | 2017-08-24 12:12:32 -0700 | [diff] [blame] | 629 | .setContentIntent(contentIntent); |
| 630 | final Notification notif = |
| 631 | new Notification.BigTextStyle(builder).bigText(contentText).build(); |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 632 | notifyAsUser(null /* tag */, DATA_ROAMING_NOTIFICATION, notif, UserHandle.ALL); |
| 633 | } |
| 634 | |
| 635 | private void notifyAsUser(String tag, int id, Notification notification, UserHandle user) { |
| 636 | try { |
| 637 | Context contextForUser = |
| 638 | mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 639 | NotificationManager notificationManager = |
| 640 | (NotificationManager) contextForUser.getSystemService( |
| 641 | Context.NOTIFICATION_SERVICE); |
| 642 | notificationManager.notify(tag, id, notification); |
| 643 | } catch (PackageManager.NameNotFoundException e) { |
| 644 | Log.e(LOG_TAG, "unable to notify for user " + user); |
| 645 | e.printStackTrace(); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | private void cancelAsUser(String tag, int id, UserHandle user) { |
| 650 | try { |
| 651 | Context contextForUser = |
| 652 | mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 653 | NotificationManager notificationManager = |
| 654 | (NotificationManager) contextForUser.getSystemService( |
| 655 | Context.NOTIFICATION_SERVICE); |
| 656 | notificationManager.cancel(tag, id); |
| 657 | } catch (PackageManager.NameNotFoundException e) { |
| 658 | Log.e(LOG_TAG, "unable to cancel for user " + user); |
| 659 | e.printStackTrace(); |
| 660 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /** |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 664 | * Turns off the "data disconnected due to roaming" or "Data roaming is on" notification. |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 665 | */ |
Jordan Liu | c353b16 | 2019-07-23 15:54:41 -0700 | [diff] [blame] | 666 | /* package */ void hideDataRoamingNotification() { |
| 667 | if (DBG) log("hideDataRoamingNotification()..."); |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 668 | cancelAsUser(null, DATA_ROAMING_NOTIFICATION, UserHandle.ALL); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 672 | * Shows the "Limited SIM functionality" warning notification, which appears when using a |
| 673 | * special carrier under dual sim. limited function applies for DSDS in general when two SIM |
| 674 | * cards share a single radio, thus the voice & data maybe impaired under certain scenarios. |
| 675 | */ |
| 676 | public void showLimitedSimFunctionWarningNotification(int subId, @Nullable String carrierName) { |
| 677 | if (DBG) log("showLimitedSimFunctionWarningNotification carrier: " + carrierName |
| 678 | + " subId: " + subId); |
| 679 | if (mLimitedSimFunctionNotify.contains(subId)) { |
| 680 | // handle the case that user swipe the notification but condition triggers |
| 681 | // frequently which cause the same notification consistently displayed. |
| 682 | if (DBG) log("showLimitedSimFunctionWarningNotification, " |
| 683 | + "not display again if already displayed"); |
| 684 | return; |
| 685 | } |
| 686 | // Navigate to "Network Selection Settings" which list all subscriptions. |
| 687 | PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, |
Shuo Qian | 9a41a17 | 2020-04-24 19:19:31 -0700 | [diff] [blame] | 688 | new Intent(ACTION_MOBILE_NETWORK_LIST), PendingIntent.FLAG_IMMUTABLE); |
Chen Xu | e1bbfd2 | 2019-09-18 17:17:31 -0700 | [diff] [blame] | 689 | // Display phone number from the other sub |
| 690 | String line1Num = null; |
| 691 | SubscriptionManager subMgr = (SubscriptionManager) mContext.getSystemService( |
| 692 | Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 693 | List<SubscriptionInfo> subList = subMgr.getActiveSubscriptionInfoList(false); |
| 694 | for (SubscriptionInfo sub : subList) { |
| 695 | if (sub.getSubscriptionId() != subId) { |
| 696 | line1Num = mTelephonyManager.getLine1Number(sub.getSubscriptionId()); |
| 697 | } |
| 698 | } |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 699 | final CharSequence contentText = TextUtils.isEmpty(line1Num) ? |
| 700 | String.format(mContext.getText( |
Chen Xu | 251ce8f | 2019-09-13 20:24:22 -0700 | [diff] [blame] | 701 | R.string.limited_sim_function_notification_message).toString(), carrierName) : |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 702 | String.format(mContext.getText( |
| 703 | R.string.limited_sim_function_with_phone_num_notification_message).toString(), |
Chen Xu | 251ce8f | 2019-09-13 20:24:22 -0700 | [diff] [blame] | 704 | carrierName, line1Num); |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 705 | final Notification.Builder builder = new Notification.Builder(mContext) |
| 706 | .setSmallIcon(R.drawable.ic_sim_card) |
| 707 | .setContentTitle(mContext.getText( |
| 708 | R.string.limited_sim_function_notification_title)) |
| 709 | .setContentText(contentText) |
| 710 | .setOnlyAlertOnce(true) |
| 711 | .setOngoing(true) |
Jordan Liu | a55aaaa | 2019-08-28 15:33:05 -0700 | [diff] [blame] | 712 | .setChannelId(NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY) |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 713 | .setContentIntent(contentIntent); |
| 714 | final Notification notification = new Notification.BigTextStyle(builder).bigText( |
| 715 | contentText).build(); |
| 716 | |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 717 | notifyAsUser(Integer.toString(subId), |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 718 | LIMITED_SIM_FUNCTION_NOTIFICATION, |
| 719 | notification, UserHandle.ALL); |
| 720 | mLimitedSimFunctionNotify.add(subId); |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Dismiss the "Limited SIM functionality" warning notification for the given subId. |
| 725 | */ |
| 726 | public void dismissLimitedSimFunctionWarningNotification(int subId) { |
| 727 | if (DBG) log("dismissLimitedSimFunctionWarningNotification subId: " + subId); |
| 728 | if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
| 729 | // dismiss all notifications |
| 730 | for (int id : mLimitedSimFunctionNotify) { |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 731 | cancelAsUser(Integer.toString(id), |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 732 | LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL); |
| 733 | } |
| 734 | mLimitedSimFunctionNotify.clear(); |
| 735 | } else if (mLimitedSimFunctionNotify.contains(subId)) { |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 736 | cancelAsUser(Integer.toString(subId), |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 737 | LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL); |
| 738 | mLimitedSimFunctionNotify.remove(subId); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Dismiss the "Limited SIM functionality" warning notification for all inactive subscriptions. |
| 744 | */ |
| 745 | public void dismissLimitedSimFunctionWarningNotificationForInactiveSubs() { |
| 746 | if (DBG) log("dismissLimitedSimFunctionWarningNotificationForInactiveSubs"); |
| 747 | // dismiss notification for inactive subscriptions. |
| 748 | // handle the corner case that SIM change by SIM refresh doesn't clear the notification |
| 749 | // from the old SIM if both old & new SIM configured to display the notification. |
| 750 | mLimitedSimFunctionNotify.removeIf(id -> { |
| 751 | if (!mSubscriptionManager.isActiveSubId(id)) { |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 752 | cancelAsUser(Integer.toString(id), |
chen xu | baf9fe5 | 2019-07-02 17:28:24 -0700 | [diff] [blame] | 753 | LIMITED_SIM_FUNCTION_NOTIFICATION, UserHandle.ALL); |
| 754 | return true; |
| 755 | } |
| 756 | return false; |
| 757 | }); |
| 758 | } |
| 759 | |
| 760 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 761 | * Display the network selection "no service" notification |
| 762 | * @param operator is the numeric operator number |
Jayachandran C | 2ef9a48 | 2017-05-12 22:07:47 -0700 | [diff] [blame] | 763 | * @param subId is the subscription ID |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 764 | */ |
Jayachandran C | 2ef9a48 | 2017-05-12 22:07:47 -0700 | [diff] [blame] | 765 | private void showNetworkSelection(String operator, int subId) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 766 | if (DBG) log("showNetworkSelection(" + operator + ")..."); |
| 767 | |
Youming Ye | 0509b53 | 2018-09-14 16:21:17 -0700 | [diff] [blame] | 768 | if (!TextUtils.isEmpty(operator)) { |
| 769 | operator = String.format(" (%s)", operator); |
| 770 | } |
fionaxu | 8b7620d | 2017-05-01 16:22:17 -0700 | [diff] [blame] | 771 | Notification.Builder builder = new Notification.Builder(mContext) |
Andrew Lee | 99d0ac2 | 2014-10-10 13:18:04 -0700 | [diff] [blame] | 772 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 773 | .setContentTitle(mContext.getString(R.string.notification_network_selection_title)) |
| 774 | .setContentText( |
| 775 | mContext.getString(R.string.notification_network_selection_text, operator)) |
| 776 | .setShowWhen(false) |
fionaxu | 75b66a7 | 2017-04-19 19:01:56 -0700 | [diff] [blame] | 777 | .setOngoing(true) |
Jordan Liu | 575c0d0 | 2019-07-09 16:29:48 -0700 | [diff] [blame] | 778 | .setChannelId(NotificationChannelController.CHANNEL_ID_ALERT); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 779 | |
| 780 | // create the target network operators settings intent |
| 781 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 782 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 783 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Malcolm Chen | 34d4fa5 | 2017-06-05 19:02:16 -0700 | [diff] [blame] | 784 | // Use MobileNetworkSettings to handle the selection intent |
Wei Liu | be96458 | 2015-08-21 11:57:00 -0700 | [diff] [blame] | 785 | intent.setComponent(new ComponentName( |
Malcolm Chen | 34d4fa5 | 2017-06-05 19:02:16 -0700 | [diff] [blame] | 786 | mContext.getString(R.string.mobile_network_settings_package), |
| 787 | mContext.getString(R.string.mobile_network_settings_class))); |
Pengquan Meng | 984ba42 | 2019-09-05 18:00:06 -0700 | [diff] [blame] | 788 | intent.putExtra(Settings.EXTRA_SUB_ID, subId); |
Shuo Qian | 9a41a17 | 2020-04-24 19:19:31 -0700 | [diff] [blame] | 789 | builder.setContentIntent( |
| 790 | PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE)); |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 791 | notifyAsUser( |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 792 | Integer.toString(subId) /* tag */, |
fionaxu | 96ceebd | 2017-08-24 12:12:32 -0700 | [diff] [blame] | 793 | SELECTED_OPERATOR_FAIL_NOTIFICATION, |
| 794 | builder.build(), |
| 795 | UserHandle.ALL); |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 796 | mSelectedUnavailableNotify.put(subId, true); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Turn off the network selection "no service" notification |
| 801 | */ |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 802 | private void cancelNetworkSelection(int subId) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 803 | if (DBG) log("cancelNetworkSelection()..."); |
Jordan Liu | 9ddde02 | 2019-08-05 13:49:08 -0700 | [diff] [blame] | 804 | cancelAsUser( |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 805 | Integer.toString(subId) /* tag */, SELECTED_OPERATOR_FAIL_NOTIFICATION, |
| 806 | UserHandle.ALL); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Update notification about no service of user selected operator |
| 811 | * |
| 812 | * @param serviceState Phone service state |
Jayachandran C | 2ef9a48 | 2017-05-12 22:07:47 -0700 | [diff] [blame] | 813 | * @param subId The subscription ID |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 814 | */ |
Jayachandran C | 2ef9a48 | 2017-05-12 22:07:47 -0700 | [diff] [blame] | 815 | void updateNetworkSelection(int serviceState, int subId) { |
| 816 | int phoneId = SubscriptionManager.getPhoneId(subId); |
| 817 | Phone phone = SubscriptionManager.isValidPhoneId(phoneId) ? |
| 818 | PhoneFactory.getPhone(phoneId) : PhoneFactory.getDefaultPhone(); |
| 819 | if (TelephonyCapabilities.supportsNetworkSelection(phone)) { |
Amit Mahajan | a60be87 | 2015-01-15 16:05:08 -0800 | [diff] [blame] | 820 | if (SubscriptionManager.isValidSubscriptionId(subId)) { |
fionaxu | 996a1c3 | 2018-04-13 15:00:37 -0700 | [diff] [blame] | 821 | SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext); |
| 822 | String selectedNetworkOperatorName = |
| 823 | sp.getString(Phone.NETWORK_SELECTION_NAME_KEY + subId, ""); |
| 824 | // get the shared preference of network_selection. |
| 825 | // empty is auto mode, otherwise it is the operator alpha name |
| 826 | // in case there is no operator name, check the operator numeric |
| 827 | if (TextUtils.isEmpty(selectedNetworkOperatorName)) { |
| 828 | selectedNetworkOperatorName = |
| 829 | sp.getString(Phone.NETWORK_SELECTION_KEY + subId, ""); |
| 830 | } |
| 831 | boolean isManualSelection; |
fionaxu | d6aac66 | 2018-03-14 16:44:29 -0700 | [diff] [blame] | 832 | // if restoring manual selection is controlled by framework, then get network |
| 833 | // selection from shared preference, otherwise get from real network indicators. |
Daniel Bright | ebb4eb7 | 2020-02-18 15:16:33 -0800 | [diff] [blame] | 834 | boolean restoreSelection = !mContext.getResources().getBoolean( |
| 835 | com.android.internal.R.bool.skip_restoring_network_selection); |
fionaxu | d6aac66 | 2018-03-14 16:44:29 -0700 | [diff] [blame] | 836 | if (restoreSelection) { |
fionaxu | d6aac66 | 2018-03-14 16:44:29 -0700 | [diff] [blame] | 837 | isManualSelection = !TextUtils.isEmpty(selectedNetworkOperatorName); |
| 838 | } else { |
fionaxu | d6aac66 | 2018-03-14 16:44:29 -0700 | [diff] [blame] | 839 | isManualSelection = phone.getServiceStateTracker().mSS.getIsManualSelection(); |
Amit Mahajan | a60be87 | 2015-01-15 16:05:08 -0800 | [diff] [blame] | 840 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 841 | |
fionaxu | d6aac66 | 2018-03-14 16:44:29 -0700 | [diff] [blame] | 842 | if (DBG) { |
| 843 | log("updateNetworkSelection()..." + "state = " + serviceState + " new network " |
| 844 | + (isManualSelection ? selectedNetworkOperatorName : "")); |
| 845 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 846 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 847 | if (isManualSelection) { |
| 848 | mSelectedNetworkOperatorName.put(subId, selectedNetworkOperatorName); |
| 849 | shouldShowNotification(serviceState, subId); |
Amit Mahajan | a60be87 | 2015-01-15 16:05:08 -0800 | [diff] [blame] | 850 | } else { |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 851 | dismissNetworkSelectionNotification(subId); |
| 852 | clearUpNetworkSelectionNotificationParam(subId); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 853 | } |
| 854 | } else { |
Amit Mahajan | a60be87 | 2015-01-15 16:05:08 -0800 | [diff] [blame] | 855 | if (DBG) log("updateNetworkSelection()..." + "state = " + |
| 856 | serviceState + " not updating network due to invalid subId " + subId); |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 857 | dismissNetworkSelectionNotificationForInactiveSubId(); |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | private void dismissNetworkSelectionNotification(int subId) { |
| 863 | if (mSelectedUnavailableNotify.get(subId, false)) { |
| 864 | cancelNetworkSelection(subId); |
| 865 | mSelectedUnavailableNotify.remove(subId); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | private void dismissNetworkSelectionNotificationForInactiveSubId() { |
| 870 | for (int i = 0; i < mSelectedUnavailableNotify.size(); i++) { |
| 871 | int subId = mSelectedUnavailableNotify.keyAt(i); |
| 872 | if (!mSubscriptionManager.isActiveSubId(subId)) { |
| 873 | dismissNetworkSelectionNotification(subId); |
| 874 | clearUpNetworkSelectionNotificationParam(subId); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /* package */ void postTransientNotification(int notifyId, CharSequence msg) { |
| 880 | if (mToast != null) { |
| 881 | mToast.cancel(); |
| 882 | } |
| 883 | |
| 884 | mToast = Toast.makeText(mContext, msg, Toast.LENGTH_LONG); |
| 885 | mToast.show(); |
| 886 | } |
| 887 | |
| 888 | private void log(String msg) { |
| 889 | Log.d(LOG_TAG, msg); |
| 890 | } |
Tyler Gunn | a584e2c | 2017-09-19 11:40:12 -0700 | [diff] [blame] | 891 | |
| 892 | private void logi(String msg) { |
| 893 | Log.i(LOG_TAG, msg); |
| 894 | } |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 895 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 896 | private void shouldShowNotification(int serviceState, int subId) { |
Rambo Wang | c30938c | 2022-08-10 22:54:23 +0000 | [diff] [blame] | 897 | // "Network selection unavailable" notification should only show when network selection is |
| 898 | // visible to the end user. Some CC items e.g. KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL |
| 899 | // can be overridden to hide the network selection to the end user. In this case, the |
| 900 | // notification is not shown to avoid confusion to the end user. |
| 901 | if (!shouldDisplayNetworkSelectOptions(subId)) { |
| 902 | logi("Carrier configs refuse to show network selection not available notification"); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | // In case network selection notification shows up repeatedly under |
| 907 | // unstable network condition. The logic is to check whether or not |
| 908 | // the service state keeps in no service condition for at least |
| 909 | // {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS}. |
| 910 | // And checking {@link #NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES} times. |
| 911 | // To avoid the notification showing up for the momentary state. |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 912 | if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) { |
| 913 | if (mPreviousServiceState.get(subId, STATE_UNKNOWN_SERVICE) |
| 914 | != ServiceState.STATE_OUT_OF_SERVICE) { |
| 915 | mOOSTimestamp.put(subId, getTimeStamp()); |
| 916 | } |
| 917 | if ((getTimeStamp() - mOOSTimestamp.get(subId, 0L) |
| 918 | >= NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS) |
| 919 | || mPendingEventCounter.get(subId, 0) |
| 920 | > NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIMES) { |
| 921 | showNetworkSelection(mSelectedNetworkOperatorName.get(subId), subId); |
| 922 | clearUpNetworkSelectionNotificationParam(subId); |
| 923 | } else { |
| 924 | startPendingNetworkSelectionNotification(subId); |
| 925 | } |
| 926 | } else { |
| 927 | dismissNetworkSelectionNotification(subId); |
| 928 | } |
| 929 | mPreviousServiceState.put(subId, serviceState); |
| 930 | if (DBG) { |
| 931 | log("shouldShowNotification()..." + " subId = " + subId |
| 932 | + " serviceState = " + serviceState |
| 933 | + " mOOSTimestamp = " + mOOSTimestamp |
| 934 | + " mPendingEventCounter = " + mPendingEventCounter); |
| 935 | } |
| 936 | } |
| 937 | |
Rambo Wang | c30938c | 2022-08-10 22:54:23 +0000 | [diff] [blame] | 938 | // TODO(b/243010310): merge methods below with Settings#MobileNetworkUtils and optimize them. |
| 939 | // The methods below are copied from com.android.settings.network.telephony.MobileNetworkUtils |
| 940 | // to make sure the network selection unavailable notification should not show when Network |
| 941 | // Selection menu is not present in Settings app. |
| 942 | private boolean shouldDisplayNetworkSelectOptions(int subId) { |
| 943 | final TelephonyManager telephonyManager = mTelephonyManager.createForSubscriptionId(subId); |
| 944 | final CarrierConfigManager carrierConfigManager = mContext.getSystemService( |
| 945 | CarrierConfigManager.class); |
| 946 | final PersistableBundle carrierConfig = carrierConfigManager.getConfigForSubId(subId); |
| 947 | |
| 948 | if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID |
| 949 | || carrierConfig == null |
| 950 | || !carrierConfig.getBoolean( |
| 951 | CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL) |
| 952 | || carrierConfig.getBoolean( |
| 953 | CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL) |
| 954 | || (carrierConfig.getBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL) |
| 955 | && !telephonyManager.isManualNetworkSelectionAllowed())) { |
| 956 | return false; |
| 957 | } |
| 958 | |
| 959 | if (isWorldMode(carrierConfig)) { |
| 960 | final int networkMode = RadioAccessFamily.getNetworkTypeFromRaf( |
| 961 | (int) telephonyManager.getAllowedNetworkTypesForReason( |
| 962 | TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)); |
| 963 | if (networkMode == RILConstants.NETWORK_MODE_LTE_CDMA_EVDO) { |
| 964 | return false; |
| 965 | } |
| 966 | if (shouldSpeciallyUpdateGsmCdma(telephonyManager, carrierConfig)) { |
| 967 | return false; |
| 968 | } |
| 969 | if (networkMode == RILConstants.NETWORK_MODE_LTE_GSM_WCDMA) { |
| 970 | return true; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | return isGsmBasicOptions(telephonyManager, carrierConfig); |
| 975 | } |
| 976 | |
| 977 | private static boolean isWorldMode(@NonNull PersistableBundle carrierConfig) { |
| 978 | return carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL); |
| 979 | } |
| 980 | |
| 981 | private static boolean shouldSpeciallyUpdateGsmCdma(@NonNull TelephonyManager telephonyManager, |
| 982 | @NonNull PersistableBundle carrierConfig) { |
| 983 | if (!isWorldMode(carrierConfig)) { |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | final int networkMode = RadioAccessFamily.getNetworkTypeFromRaf( |
| 988 | (int) telephonyManager.getAllowedNetworkTypesForReason( |
| 989 | TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)); |
| 990 | if (networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM |
| 991 | || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA |
| 992 | || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA |
| 993 | || networkMode == RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA |
| 994 | || networkMode |
| 995 | == RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA |
| 996 | || networkMode == RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA) { |
| 997 | if (!isTdscdmaSupported(telephonyManager, carrierConfig)) { |
| 998 | return true; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | return false; |
| 1003 | } |
| 1004 | |
| 1005 | private static boolean isTdscdmaSupported(@NonNull TelephonyManager telephonyManager, |
| 1006 | @NonNull PersistableBundle carrierConfig) { |
| 1007 | if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL)) { |
| 1008 | return true; |
| 1009 | } |
| 1010 | final String[] numericArray = carrierConfig.getStringArray( |
| 1011 | CarrierConfigManager.KEY_SUPPORT_TDSCDMA_ROAMING_NETWORKS_STRING_ARRAY); |
| 1012 | if (numericArray == null) { |
| 1013 | return false; |
| 1014 | } |
| 1015 | final ServiceState serviceState = telephonyManager.getServiceState(); |
| 1016 | final String operatorNumeric = |
| 1017 | (serviceState != null) ? serviceState.getOperatorNumeric() : null; |
| 1018 | if (operatorNumeric == null) { |
| 1019 | return false; |
| 1020 | } |
| 1021 | for (String numeric : numericArray) { |
| 1022 | if (operatorNumeric.equals(numeric)) { |
| 1023 | return true; |
| 1024 | } |
| 1025 | } |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | private static boolean isGsmBasicOptions(@NonNull TelephonyManager telephonyManager, |
| 1030 | @NonNull PersistableBundle carrierConfig) { |
| 1031 | if (!carrierConfig.getBoolean( |
| 1032 | CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL) |
| 1033 | && carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) { |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | if (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
| 1041 | return false; |
| 1042 | } |
| 1043 | // END of TODO:(b/243010310): merge methods above with Settings#MobileNetworkUtils and optimize. |
| 1044 | |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 1045 | private void startPendingNetworkSelectionNotification(int subId) { |
| 1046 | if (!mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) { |
| 1047 | if (DBG) { |
| 1048 | log("startPendingNetworkSelectionNotification: subId = " + subId); |
| 1049 | } |
| 1050 | mHandler.sendMessageDelayed( |
| 1051 | mHandler.obtainMessage(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId), |
| 1052 | NETWORK_SELECTION_NOTIFICATION_MAX_PENDING_TIME_IN_MS); |
| 1053 | mPendingEventCounter.put(subId, mPendingEventCounter.get(subId, 0) + 1); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | private void clearUpNetworkSelectionNotificationParam(int subId) { |
| 1058 | if (mHandler.hasMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId)) { |
| 1059 | mHandler.removeMessages(EVENT_PENDING_NETWORK_SELECTION_NOTIFICATION, subId); |
| 1060 | } |
| 1061 | mPreviousServiceState.remove(subId); |
| 1062 | mOOSTimestamp.remove(subId); |
| 1063 | mPendingEventCounter.remove(subId); |
| 1064 | mSelectedNetworkOperatorName.remove(subId); |
| 1065 | } |
| 1066 | |
rambowang | 19b7b57 | 2023-01-25 16:07:08 -0600 | [diff] [blame] | 1067 | @VisibleForTesting |
| 1068 | public long getTimeStamp() { |
irisykyang | 2520246 | 2018-11-13 14:49:54 +0800 | [diff] [blame] | 1069 | return SystemClock.elapsedRealtime(); |
| 1070 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1071 | } |