fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.carrierdefaultapp; |
| 17 | |
| 18 | import android.app.Notification; |
fionaxu | 009848b | 2017-04-04 14:13:11 -0700 | [diff] [blame] | 19 | import android.app.NotificationChannel; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 26 | import android.content.res.Resources; |
fionaxu | 5b834a8 | 2017-03-06 14:03:45 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 28 | import android.telephony.SubscriptionManager; |
| 29 | import android.telephony.TelephonyManager; |
fionaxu | 7569cc2 | 2017-09-01 13:39:55 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | import com.android.internal.telephony.PhoneConstants; |
chen xu | 3d59fdb | 2018-10-17 22:57:45 -0700 | [diff] [blame] | 33 | |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 34 | /** |
| 35 | * This util class provides common logic for carrier actions |
| 36 | */ |
| 37 | public class CarrierActionUtils { |
| 38 | private static final String TAG = CarrierActionUtils.class.getSimpleName(); |
| 39 | |
| 40 | private static final String PORTAL_NOTIFICATION_TAG = "CarrierDefault.Portal.Notification"; |
| 41 | private static final String NO_DATA_NOTIFICATION_TAG = "CarrierDefault.NoData.Notification"; |
fionaxu | 009848b | 2017-04-04 14:13:11 -0700 | [diff] [blame] | 42 | private static final String NOTIFICATION_CHANNEL_ID_MOBILE_DATA_STATUS = "mobile_data_status"; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 43 | private static final int PORTAL_NOTIFICATION_ID = 0; |
| 44 | private static final int NO_DATA_NOTIFICATION_ID = 1; |
| 45 | private static boolean ENABLE = true; |
| 46 | |
| 47 | // A list of supported carrier action idx |
| 48 | public static final int CARRIER_ACTION_ENABLE_METERED_APNS = 0; |
| 49 | public static final int CARRIER_ACTION_DISABLE_METERED_APNS = 1; |
| 50 | public static final int CARRIER_ACTION_DISABLE_RADIO = 2; |
| 51 | public static final int CARRIER_ACTION_ENABLE_RADIO = 3; |
| 52 | public static final int CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION = 4; |
| 53 | public static final int CARRIER_ACTION_SHOW_NO_DATA_SERVICE_NOTIFICATION = 5; |
| 54 | public static final int CARRIER_ACTION_CANCEL_ALL_NOTIFICATIONS = 6; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 55 | public static final int CARRIER_ACTION_ENABLE_DEFAULT_URL_HANDLER = 7; |
| 56 | public static final int CARRIER_ACTION_DISABLE_DEFAULT_URL_HANDLER = 8; |
| 57 | public static final int CARRIER_ACTION_REGISTER_DEFAULT_NETWORK_AVAIL = 9; |
| 58 | public static final int CARRIER_ACTION_DEREGISTER_DEFAULT_NETWORK_AVAIL = 10; |
fionaxu | 79d77bb | 2017-07-19 11:11:54 -0700 | [diff] [blame] | 59 | public static final int CARRIER_ACTION_RESET_ALL = 11; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 60 | |
| 61 | public static void applyCarrierAction(int actionIdx, Intent intent, Context context) { |
| 62 | switch (actionIdx) { |
| 63 | case CARRIER_ACTION_ENABLE_METERED_APNS: |
| 64 | onEnableAllMeteredApns(intent, context); |
| 65 | break; |
| 66 | case CARRIER_ACTION_DISABLE_METERED_APNS: |
| 67 | onDisableAllMeteredApns(intent, context); |
| 68 | break; |
| 69 | case CARRIER_ACTION_DISABLE_RADIO: |
| 70 | onDisableRadio(intent, context); |
| 71 | break; |
| 72 | case CARRIER_ACTION_ENABLE_RADIO: |
| 73 | onEnableRadio(intent, context); |
| 74 | break; |
| 75 | case CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION: |
| 76 | onShowCaptivePortalNotification(intent, context); |
| 77 | break; |
| 78 | case CARRIER_ACTION_SHOW_NO_DATA_SERVICE_NOTIFICATION: |
| 79 | onShowNoDataServiceNotification(context); |
| 80 | break; |
| 81 | case CARRIER_ACTION_CANCEL_ALL_NOTIFICATIONS: |
| 82 | onCancelAllNotifications(context); |
| 83 | break; |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 84 | case CARRIER_ACTION_ENABLE_DEFAULT_URL_HANDLER: |
| 85 | onEnableDefaultURLHandler(context); |
| 86 | break; |
| 87 | case CARRIER_ACTION_DISABLE_DEFAULT_URL_HANDLER: |
| 88 | onDisableDefaultURLHandler(context); |
| 89 | break; |
| 90 | case CARRIER_ACTION_REGISTER_DEFAULT_NETWORK_AVAIL: |
| 91 | onRegisterDefaultNetworkAvail(intent, context); |
| 92 | break; |
| 93 | case CARRIER_ACTION_DEREGISTER_DEFAULT_NETWORK_AVAIL: |
| 94 | onDeregisterDefaultNetworkAvail(intent, context); |
| 95 | break; |
fionaxu | 79d77bb | 2017-07-19 11:11:54 -0700 | [diff] [blame] | 96 | case CARRIER_ACTION_RESET_ALL: |
| 97 | onResetAllCarrierActions(intent, context); |
| 98 | break; |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 99 | default: |
| 100 | loge("unsupported carrier action index: " + actionIdx); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private static void onDisableAllMeteredApns(Intent intent, Context context) { |
| 105 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 106 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 107 | logd("onDisableAllMeteredApns subId: " + subId); |
| 108 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
chen xu | 3d59fdb | 2018-10-17 22:57:45 -0700 | [diff] [blame] | 109 | telephonyMgr.createForSubscriptionId(subId).setCarrierDataEnabled(!ENABLE); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private static void onEnableAllMeteredApns(Intent intent, Context context) { |
| 113 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 114 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 115 | logd("onEnableAllMeteredApns subId: " + subId); |
| 116 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
chen xu | 3d59fdb | 2018-10-17 22:57:45 -0700 | [diff] [blame] | 117 | telephonyMgr.createForSubscriptionId(subId).setCarrierDataEnabled(ENABLE); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 118 | } |
| 119 | |
fionaxu | d60a9d0 | 2017-05-23 14:55:27 -0700 | [diff] [blame] | 120 | private static void onEnableDefaultURLHandler(Context context) { |
| 121 | logd("onEnableDefaultURLHandler"); |
| 122 | final PackageManager pm = context.getPackageManager(); |
| 123 | pm.setComponentEnabledSetting( |
| 124 | new ComponentName(context, CaptivePortalLoginActivity.getAlias(context)), |
| 125 | PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); |
| 126 | } |
| 127 | |
| 128 | private static void onDisableDefaultURLHandler(Context context) { |
| 129 | logd("onDisableDefaultURLHandler"); |
| 130 | final PackageManager pm = context.getPackageManager(); |
| 131 | pm.setComponentEnabledSetting( |
| 132 | new ComponentName(context, CaptivePortalLoginActivity.getAlias(context)), |
| 133 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); |
| 134 | } |
| 135 | |
| 136 | private static void onRegisterDefaultNetworkAvail(Intent intent, Context context) { |
| 137 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 138 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 139 | logd("onRegisterDefaultNetworkAvail subId: " + subId); |
| 140 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 141 | telephonyMgr.carrierActionReportDefaultNetworkStatus(subId, true); |
| 142 | } |
| 143 | |
| 144 | private static void onDeregisterDefaultNetworkAvail(Intent intent, Context context) { |
| 145 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 146 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 147 | logd("onDeregisterDefaultNetworkAvail subId: " + subId); |
| 148 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 149 | telephonyMgr.carrierActionReportDefaultNetworkStatus(subId, false); |
| 150 | } |
| 151 | |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 152 | private static void onDisableRadio(Intent intent, Context context) { |
| 153 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 154 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 155 | logd("onDisableRadio subId: " + subId); |
| 156 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 157 | telephonyMgr.carrierActionSetRadioEnabled(subId, !ENABLE); |
| 158 | } |
| 159 | |
| 160 | private static void onEnableRadio(Intent intent, Context context) { |
| 161 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 162 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 163 | logd("onEnableRadio subId: " + subId); |
| 164 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 165 | telephonyMgr.carrierActionSetRadioEnabled(subId, ENABLE); |
| 166 | } |
| 167 | |
| 168 | private static void onShowCaptivePortalNotification(Intent intent, Context context) { |
| 169 | logd("onShowCaptivePortalNotification"); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 170 | Intent portalIntent = new Intent(context, CaptivePortalLoginActivity.class); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 171 | portalIntent.putExtras(intent); |
fionaxu | da57804 | 2017-03-10 10:16:09 -0800 | [diff] [blame] | 172 | portalIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
| 173 | | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 174 | PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, portalIntent, |
| 175 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 176 | Notification notification = getNotification(context, R.string.portal_notification_id, |
| 177 | R.string.portal_notification_detail, pendingIntent); |
| 178 | try { |
fionaxu | 92d759d | 2017-05-02 15:56:58 -0700 | [diff] [blame] | 179 | context.getSystemService(NotificationManager.class) |
| 180 | .notify(PORTAL_NOTIFICATION_TAG, PORTAL_NOTIFICATION_ID, notification); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 181 | } catch (NullPointerException npe) { |
| 182 | loge("setNotificationVisible: " + npe); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private static void onShowNoDataServiceNotification(Context context) { |
| 187 | logd("onShowNoDataServiceNotification"); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 188 | Notification notification = getNotification(context, R.string.no_data_notification_id, |
| 189 | R.string.no_data_notification_detail, null); |
| 190 | try { |
fionaxu | 92d759d | 2017-05-02 15:56:58 -0700 | [diff] [blame] | 191 | context.getSystemService(NotificationManager.class) |
| 192 | .notify(NO_DATA_NOTIFICATION_TAG, NO_DATA_NOTIFICATION_ID, notification); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 193 | } catch (NullPointerException npe) { |
| 194 | loge("setNotificationVisible: " + npe); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private static void onCancelAllNotifications(Context context) { |
| 199 | logd("onCancelAllNotifications"); |
fionaxu | 92d759d | 2017-05-02 15:56:58 -0700 | [diff] [blame] | 200 | context.getSystemService(NotificationManager.class).cancelAll(); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 201 | } |
| 202 | |
fionaxu | 79d77bb | 2017-07-19 11:11:54 -0700 | [diff] [blame] | 203 | private static void onResetAllCarrierActions(Intent intent, Context context) { |
| 204 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 205 | SubscriptionManager.getDefaultVoiceSubscriptionId()); |
| 206 | logd("onResetAllCarrierActions subId: " + subId); |
| 207 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 208 | telephonyMgr.carrierActionResetAll(subId); |
| 209 | } |
| 210 | |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 211 | private static Notification getNotification(Context context, int titleId, int textId, |
| 212 | PendingIntent pendingIntent) { |
fionaxu | 5b834a8 | 2017-03-06 14:03:45 -0800 | [diff] [blame] | 213 | final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class); |
| 214 | final Resources resources = context.getResources(); |
fionaxu | 7569cc2 | 2017-09-01 13:39:55 -0700 | [diff] [blame] | 215 | String spn = telephonyMgr.getSimOperatorName(); |
| 216 | if (TextUtils.isEmpty(spn)) { |
| 217 | // There is no consistent way to get the current carrier name as MNOs didn't |
| 218 | // bother to set EF_SPN. in the long term, we should display a generic wording if |
| 219 | // spn from subscription is not set. |
| 220 | spn = telephonyMgr.getNetworkOperatorName(); |
| 221 | } |
fionaxu | 5b834a8 | 2017-03-06 14:03:45 -0800 | [diff] [blame] | 222 | final Bundle extras = Bundle.forPair(Notification.EXTRA_SUBSTITUTE_APP_NAME, |
| 223 | resources.getString(R.string.android_system_label)); |
fionaxu | 92d759d | 2017-05-02 15:56:58 -0700 | [diff] [blame] | 224 | createNotificationChannels(context); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 225 | Notification.Builder builder = new Notification.Builder(context) |
| 226 | .setContentTitle(resources.getString(titleId)) |
fionaxu | 7569cc2 | 2017-09-01 13:39:55 -0700 | [diff] [blame] | 227 | .setContentText(String.format(resources.getString(textId), spn)) |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 228 | .setSmallIcon(R.drawable.ic_sim_card) |
fionaxu | 5b834a8 | 2017-03-06 14:03:45 -0800 | [diff] [blame] | 229 | .setColor(context.getColor( |
| 230 | com.android.internal.R.color.system_notification_accent_color)) |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 231 | .setOngoing(true) |
| 232 | .setPriority(Notification.PRIORITY_HIGH) |
| 233 | .setDefaults(Notification.DEFAULT_ALL) |
| 234 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
| 235 | .setLocalOnly(true) |
| 236 | .setWhen(System.currentTimeMillis()) |
fionaxu | 5b834a8 | 2017-03-06 14:03:45 -0800 | [diff] [blame] | 237 | .setShowWhen(false) |
fionaxu | 009848b | 2017-04-04 14:13:11 -0700 | [diff] [blame] | 238 | .setExtras(extras) |
| 239 | .setChannel(NOTIFICATION_CHANNEL_ID_MOBILE_DATA_STATUS); |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 240 | |
| 241 | if (pendingIntent != null) { |
| 242 | builder.setContentIntent(pendingIntent); |
| 243 | } |
| 244 | return builder.build(); |
| 245 | } |
| 246 | |
fionaxu | 92d759d | 2017-05-02 15:56:58 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Creates the notification channel and registers it with NotificationManager. Also used to |
| 249 | * update an existing channel's name. |
| 250 | */ |
| 251 | static void createNotificationChannels(Context context) { |
| 252 | context.getSystemService(NotificationManager.class) |
| 253 | .createNotificationChannel(new NotificationChannel( |
| 254 | NOTIFICATION_CHANNEL_ID_MOBILE_DATA_STATUS, |
| 255 | context.getResources().getString( |
| 256 | R.string.mobile_data_status_notification_channel_name), |
| 257 | NotificationManager.IMPORTANCE_DEFAULT)); |
| 258 | } |
| 259 | |
fionaxu | a21a87b | 2016-12-13 17:15:11 -0800 | [diff] [blame] | 260 | private static void logd(String s) { |
| 261 | Log.d(TAG, s); |
| 262 | } |
| 263 | |
| 264 | private static void loge(String s) { |
| 265 | Log.e(TAG, s); |
| 266 | } |
| 267 | } |