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