blob: db4890fc0187b8eaef73d50ee608757fe1e6c0b5 [file] [log] [blame]
fionaxua21a87b2016-12-13 17:15:11 -08001/*
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 */
16package com.android.carrierdefaultapp;
17
18import android.app.Notification;
19import android.app.NotificationManager;
20import android.app.PendingIntent;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Resources;
24import android.telephony.SubscriptionManager;
25import android.telephony.TelephonyManager;
26import android.util.Log;
27import com.android.internal.telephony.PhoneConstants;
28import com.android.carrierdefaultapp.R;
29/**
30 * This util class provides common logic for carrier actions
31 */
32public class CarrierActionUtils {
33 private static final String TAG = CarrierActionUtils.class.getSimpleName();
34
35 private static final String PORTAL_NOTIFICATION_TAG = "CarrierDefault.Portal.Notification";
36 private static final String NO_DATA_NOTIFICATION_TAG = "CarrierDefault.NoData.Notification";
37 private static final int PORTAL_NOTIFICATION_ID = 0;
38 private static final int NO_DATA_NOTIFICATION_ID = 1;
39 private static boolean ENABLE = true;
40
41 // A list of supported carrier action idx
42 public static final int CARRIER_ACTION_ENABLE_METERED_APNS = 0;
43 public static final int CARRIER_ACTION_DISABLE_METERED_APNS = 1;
44 public static final int CARRIER_ACTION_DISABLE_RADIO = 2;
45 public static final int CARRIER_ACTION_ENABLE_RADIO = 3;
46 public static final int CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION = 4;
47 public static final int CARRIER_ACTION_SHOW_NO_DATA_SERVICE_NOTIFICATION = 5;
48 public static final int CARRIER_ACTION_CANCEL_ALL_NOTIFICATIONS = 6;
49
50 public static void applyCarrierAction(int actionIdx, Intent intent, Context context) {
51 switch (actionIdx) {
52 case CARRIER_ACTION_ENABLE_METERED_APNS:
53 onEnableAllMeteredApns(intent, context);
54 break;
55 case CARRIER_ACTION_DISABLE_METERED_APNS:
56 onDisableAllMeteredApns(intent, context);
57 break;
58 case CARRIER_ACTION_DISABLE_RADIO:
59 onDisableRadio(intent, context);
60 break;
61 case CARRIER_ACTION_ENABLE_RADIO:
62 onEnableRadio(intent, context);
63 break;
64 case CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION:
65 onShowCaptivePortalNotification(intent, context);
66 break;
67 case CARRIER_ACTION_SHOW_NO_DATA_SERVICE_NOTIFICATION:
68 onShowNoDataServiceNotification(context);
69 break;
70 case CARRIER_ACTION_CANCEL_ALL_NOTIFICATIONS:
71 onCancelAllNotifications(context);
72 break;
73 default:
74 loge("unsupported carrier action index: " + actionIdx);
75 }
76 }
77
78 private static void onDisableAllMeteredApns(Intent intent, Context context) {
79 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
80 SubscriptionManager.getDefaultVoiceSubscriptionId());
81 logd("onDisableAllMeteredApns subId: " + subId);
82 final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class);
83 telephonyMgr.carrierActionSetMeteredApnsEnabled(subId, !ENABLE);
84 }
85
86 private static void onEnableAllMeteredApns(Intent intent, Context context) {
87 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
88 SubscriptionManager.getDefaultVoiceSubscriptionId());
89 logd("onEnableAllMeteredApns subId: " + subId);
90 final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class);
91 telephonyMgr.carrierActionSetMeteredApnsEnabled(subId, ENABLE);
92 }
93
94 private static void onDisableRadio(Intent intent, Context context) {
95 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
96 SubscriptionManager.getDefaultVoiceSubscriptionId());
97 logd("onDisableRadio subId: " + subId);
98 final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class);
99 telephonyMgr.carrierActionSetRadioEnabled(subId, !ENABLE);
100 }
101
102 private static void onEnableRadio(Intent intent, Context context) {
103 int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
104 SubscriptionManager.getDefaultVoiceSubscriptionId());
105 logd("onEnableRadio subId: " + subId);
106 final TelephonyManager telephonyMgr = context.getSystemService(TelephonyManager.class);
107 telephonyMgr.carrierActionSetRadioEnabled(subId, ENABLE);
108 }
109
110 private static void onShowCaptivePortalNotification(Intent intent, Context context) {
111 logd("onShowCaptivePortalNotification");
112 final NotificationManager notificationMgr = context.getSystemService(
113 NotificationManager.class);
114 Intent portalIntent = new Intent(context, CaptivePortalLaunchActivity.class);
115 portalIntent.putExtras(intent);
116 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, portalIntent,
117 PendingIntent.FLAG_UPDATE_CURRENT);
118 Notification notification = getNotification(context, R.string.portal_notification_id,
119 R.string.portal_notification_detail, pendingIntent);
120 try {
121 notificationMgr.notify(PORTAL_NOTIFICATION_TAG, PORTAL_NOTIFICATION_ID, notification);
122 } catch (NullPointerException npe) {
123 loge("setNotificationVisible: " + npe);
124 }
125 }
126
127 private static void onShowNoDataServiceNotification(Context context) {
128 logd("onShowNoDataServiceNotification");
129 final NotificationManager notificationMgr = context.getSystemService(
130 NotificationManager.class);
131 Notification notification = getNotification(context, R.string.no_data_notification_id,
132 R.string.no_data_notification_detail, null);
133 try {
134 notificationMgr.notify(NO_DATA_NOTIFICATION_TAG, NO_DATA_NOTIFICATION_ID, notification);
135 } catch (NullPointerException npe) {
136 loge("setNotificationVisible: " + npe);
137 }
138 }
139
140 private static void onCancelAllNotifications(Context context) {
141 logd("onCancelAllNotifications");
142 final NotificationManager notificationMgr = context.getSystemService(
143 NotificationManager.class);
144 notificationMgr.cancelAll();
145 }
146
147 private static Notification getNotification(Context context, int titleId, int textId,
148 PendingIntent pendingIntent) {
149 Resources resources = context.getResources();
150 Notification.Builder builder = new Notification.Builder(context)
151 .setContentTitle(resources.getString(titleId))
152 .setContentText(resources.getString(textId))
153 .setSmallIcon(R.drawable.ic_sim_card)
154 .setOngoing(true)
155 .setPriority(Notification.PRIORITY_HIGH)
156 .setDefaults(Notification.DEFAULT_ALL)
157 .setVisibility(Notification.VISIBILITY_PUBLIC)
158 .setLocalOnly(true)
159 .setWhen(System.currentTimeMillis())
160 .setShowWhen(false);
161
162 if (pendingIntent != null) {
163 builder.setContentIntent(pendingIntent);
164 }
165 return builder.build();
166 }
167
168 private static void logd(String s) {
169 Log.d(TAG, s);
170 }
171
172 private static void loge(String s) {
173 Log.e(TAG, s);
174 }
175}