• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.settings.sim;
18 
19 import static android.provider.Settings.ENABLE_MMS_DATA_REQUEST_REASON_INCOMING_MMS;
20 import static android.provider.Settings.ENABLE_MMS_DATA_REQUEST_REASON_OUTGOING_MMS;
21 import static android.provider.Settings.EXTRA_ENABLE_MMS_DATA_REQUEST_REASON;
22 import static android.provider.Settings.EXTRA_SUB_ID;
23 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE;
24 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_ALL;
25 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DATA;
26 import static android.telephony.TelephonyManager.EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_NONE;
27 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_NAMES;
28 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_WARNING_TYPE;
29 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_WARNING_TYPE_DUAL_CDMA;
30 import static android.telephony.TelephonyManager.EXTRA_SIM_COMBINATION_WARNING_TYPE_NONE;
31 import static android.telephony.TelephonyManager.EXTRA_SUBSCRIPTION_ID;
32 import static android.telephony.data.ApnSetting.TYPE_MMS;
33 
34 import android.app.Notification;
35 import android.app.NotificationChannel;
36 import android.app.NotificationManager;
37 import android.app.PendingIntent;
38 import android.content.BroadcastReceiver;
39 import android.content.Context;
40 import android.content.Intent;
41 import android.content.res.Resources;
42 import android.provider.Settings;
43 import android.telephony.SubscriptionInfo;
44 import android.telephony.SubscriptionManager;
45 import android.telephony.TelephonyManager;
46 import android.util.Log;
47 
48 import com.android.internal.annotations.VisibleForTesting;
49 import com.android.settings.R;
50 import com.android.settings.network.SubscriptionUtil;
51 import com.android.settings.network.telephony.MobileNetworkActivity;
52 import com.android.settingslib.HelpUtils;
53 
54 public class SimSelectNotification extends BroadcastReceiver {
55     private static final String TAG = "SimSelectNotification";
56     @VisibleForTesting
57     public static final int SIM_SELECT_NOTIFICATION_ID = 1;
58     @VisibleForTesting
59     public static final int ENABLE_MMS_NOTIFICATION_ID = 2;
60     @VisibleForTesting
61     public static final int SIM_WARNING_NOTIFICATION_ID = 3;
62 
63     @VisibleForTesting
64     public static final String SIM_SELECT_NOTIFICATION_CHANNEL =
65             "sim_select_notification_channel";
66 
67     @VisibleForTesting
68     public static final String ENABLE_MMS_NOTIFICATION_CHANNEL =
69             "enable_mms_notification_channel";
70 
71     @VisibleForTesting
72     public static final String SIM_WARNING_NOTIFICATION_CHANNEL =
73             "sim_warning_notification_channel";
74 
75     @Override
onReceive(Context context, Intent intent)76     public void onReceive(Context context, Intent intent) {
77         String action = intent.getAction();
78 
79         if (action == null) {
80             Log.w(TAG, "Received unexpected intent with null action.");
81             return;
82         }
83 
84         switch (action) {
85             case TelephonyManager.ACTION_PRIMARY_SUBSCRIPTION_LIST_CHANGED:
86                 onPrimarySubscriptionListChanged(context, intent);
87                 break;
88             case Settings.ACTION_ENABLE_MMS_DATA_REQUEST:
89                 onEnableMmsDataRequest(context, intent);
90                 break;
91             default:
92                 Log.w(TAG, "Received unexpected intent " + intent.getAction());
93         }
94     }
95 
onEnableMmsDataRequest(Context context, Intent intent)96     private void onEnableMmsDataRequest(Context context, Intent intent) {
97         // Getting subId from extra.
98         int subId = intent.getIntExtra(EXTRA_SUB_ID, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
99         if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
100             subId = SubscriptionManager.getDefaultSmsSubscriptionId();
101         }
102 
103         SubscriptionManager subscriptionManager = ((SubscriptionManager) context.getSystemService(
104                 Context.TELEPHONY_SUBSCRIPTION_SERVICE));
105         if (!subscriptionManager.isActiveSubId(subId)) {
106             Log.w(TAG, "onEnableMmsDataRequest invalid sub ID " + subId);
107             return;
108         }
109         final SubscriptionInfo info = subscriptionManager.getActiveSubscriptionInfo(subId);
110         if (info == null) {
111             Log.w(TAG, "onEnableMmsDataRequest null SubscriptionInfo for sub ID " + subId);
112             return;
113         }
114 
115         // Getting request reason from extra, which will determine the notification title.
116         CharSequence notificationTitle = null;
117         int requestReason = intent.getIntExtra(EXTRA_ENABLE_MMS_DATA_REQUEST_REASON, -1);
118         if (requestReason == ENABLE_MMS_DATA_REQUEST_REASON_INCOMING_MMS) {
119             notificationTitle = context.getResources().getText(
120                     R.string.enable_receiving_mms_notification_title);
121         } else if (requestReason == ENABLE_MMS_DATA_REQUEST_REASON_OUTGOING_MMS) {
122             notificationTitle = context.getResources().getText(
123                     R.string.enable_sending_mms_notification_title);
124         } else {
125             Log.w(TAG, "onEnableMmsDataRequest invalid request reason " + requestReason);
126             return;
127         }
128 
129         TelephonyManager tm = ((TelephonyManager) context.getSystemService(
130                 Context.TELEPHONY_SERVICE)).createForSubscriptionId(subId);
131 
132         if (tm.isDataEnabledForApn(TYPE_MMS)) {
133             Log.w(TAG, "onEnableMmsDataRequest MMS data already enabled on sub ID " + subId);
134             return;
135         }
136 
137         CharSequence notificationSummary = context.getResources().getString(
138                 R.string.enable_mms_notification_summary, SubscriptionUtil.getDisplayName(info));
139 
140         cancelEnableMmsNotification(context);
141 
142         createEnableMmsNotification(context, notificationTitle, notificationSummary, subId);
143     }
144 
onPrimarySubscriptionListChanged(Context context, Intent intent)145     private void onPrimarySubscriptionListChanged(Context context, Intent intent) {
146         startSimSelectDialogIfNeeded(context, intent);
147         sendSimCombinationWarningIfNeeded(context, intent);
148     }
149 
startSimSelectDialogIfNeeded(Context context, Intent intent)150     private void startSimSelectDialogIfNeeded(Context context, Intent intent) {
151         int dialogType = intent.getIntExtra(EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE,
152                 EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_NONE);
153 
154         if (dialogType == EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_NONE) {
155             return;
156         }
157 
158         // Cancel any previous notifications
159         cancelSimSelectNotification(context);
160         // Create a notification to tell the user that some defaults are missing
161         createSimSelectNotification(context);
162 
163         if (dialogType == EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_ALL) {
164             int subId = intent.getIntExtra(EXTRA_SUBSCRIPTION_ID,
165                     SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
166             int slotIndex = SubscriptionManager.getSlotIndex(subId);
167             // If there is only one subscription, ask if user wants to use if for everything
168             Intent newIntent = new Intent(context, SimDialogActivity.class);
169             newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
170             newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY,
171                     SimDialogActivity.PREFERRED_PICK);
172             newIntent.putExtra(SimDialogActivity.PREFERRED_SIM, slotIndex);
173             context.startActivity(newIntent);
174         } else if (dialogType == EXTRA_DEFAULT_SUBSCRIPTION_SELECT_TYPE_DATA) {
175             // If there are multiple, ensure they pick default data
176             Intent newIntent = new Intent(context, SimDialogActivity.class);
177             newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
178             newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK);
179             context.startActivity(newIntent);
180         }
181     }
182 
sendSimCombinationWarningIfNeeded(Context context, Intent intent)183     private void sendSimCombinationWarningIfNeeded(Context context, Intent intent) {
184         final int warningType = intent.getIntExtra(EXTRA_SIM_COMBINATION_WARNING_TYPE,
185                 EXTRA_SIM_COMBINATION_WARNING_TYPE_NONE);
186 
187         if (warningType == EXTRA_SIM_COMBINATION_WARNING_TYPE_DUAL_CDMA) {
188             // Cancel any previous notifications
189             cancelSimCombinationWarningNotification(context);
190             // Create a notification to tell the user that some defaults are missing
191             createSimCombinationWarningNotification(context, intent);
192         }
193     }
194 
createSimSelectNotification(Context context)195     private void createSimSelectNotification(Context context){
196         final Resources resources = context.getResources();
197 
198         NotificationChannel notificationChannel = new NotificationChannel(
199                 SIM_SELECT_NOTIFICATION_CHANNEL,
200                 resources.getText(R.string.sim_selection_channel_title),
201                 NotificationManager.IMPORTANCE_LOW);
202 
203         Notification.Builder builder =
204                 new Notification.Builder(context, SIM_SELECT_NOTIFICATION_CHANNEL)
205                 .setSmallIcon(R.drawable.ic_sim_card_alert_white_48dp)
206                 .setColor(context.getColor(R.color.sim_noitification))
207                 .setContentTitle(resources.getText(R.string.sim_notification_title))
208                 .setContentText(resources.getText(R.string.sim_notification_summary))
209                 .setAutoCancel(true);
210         Intent resultIntent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
211         resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
212         PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
213                 PendingIntent.FLAG_CANCEL_CURRENT);
214         builder.setContentIntent(resultPendingIntent);
215         NotificationManager notificationManager =
216                 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
217         notificationManager.createNotificationChannel(notificationChannel);
218         notificationManager.notify(SIM_SELECT_NOTIFICATION_ID, builder.build());
219     }
220 
cancelSimSelectNotification(Context context)221     public static void cancelSimSelectNotification(Context context) {
222         NotificationManager notificationManager =
223                 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
224         notificationManager.cancel(SIM_SELECT_NOTIFICATION_ID);
225     }
226 
createEnableMmsNotification(Context context, CharSequence titleString, CharSequence notificationSummary, int subId)227     private void createEnableMmsNotification(Context context, CharSequence titleString,
228             CharSequence notificationSummary, int subId) {
229         final Resources resources = context.getResources();
230 
231         NotificationChannel notificationChannel = new NotificationChannel(
232                 ENABLE_MMS_NOTIFICATION_CHANNEL,
233                 resources.getText(R.string.enable_mms_notification_channel_title),
234                 NotificationManager.IMPORTANCE_HIGH);
235 
236         Notification.Builder builder =
237                 new Notification.Builder(context, ENABLE_MMS_NOTIFICATION_CHANNEL)
238                         .setSmallIcon(R.drawable.ic_settings_24dp)
239                         .setColor(context.getColor(R.color.sim_noitification))
240                         .setContentTitle(titleString)
241                         .setContentText(notificationSummary)
242                         .setStyle(new Notification.BigTextStyle().bigText(notificationSummary))
243                         .setAutoCancel(true);
244 
245         // Create the pending intent that will lead to the subscription setting page.
246         Intent resultIntent = new Intent(Settings.ACTION_MMS_MESSAGE_SETTING);
247         resultIntent.setClass(context, MobileNetworkActivity.class);
248         resultIntent.putExtra(Settings.EXTRA_SUB_ID, subId);
249         resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
250         PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
251                 PendingIntent.FLAG_CANCEL_CURRENT);
252         builder.setContentIntent(resultPendingIntent);
253 
254         // Notify the notification.
255         NotificationManager notificationManager =
256                 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
257         notificationManager.createNotificationChannel(notificationChannel);
258         notificationManager.notify(ENABLE_MMS_NOTIFICATION_ID, builder.build());
259     }
260 
cancelEnableMmsNotification(Context context)261     private void cancelEnableMmsNotification(Context context) {
262         NotificationManager notificationManager =
263                 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
264         notificationManager.cancel(ENABLE_MMS_NOTIFICATION_ID);
265     }
266 
createSimCombinationWarningNotification(Context context, Intent intent)267     private void createSimCombinationWarningNotification(Context context, Intent intent){
268         final Resources resources = context.getResources();
269         final String simNames = intent.getStringExtra(EXTRA_SIM_COMBINATION_NAMES);
270 
271         if (simNames == null) {
272             return;
273         }
274 
275         CharSequence dualCdmaSimWarningSummary = resources.getString(
276                 R.string.dual_cdma_sim_warning_notification_summary, simNames);
277 
278         NotificationChannel notificationChannel = new NotificationChannel(
279                 SIM_WARNING_NOTIFICATION_CHANNEL,
280                 resources.getText(R.string.dual_cdma_sim_warning_notification_channel_title),
281                 NotificationManager.IMPORTANCE_HIGH);
282 
283         Notification.Builder builder =
284                 new Notification.Builder(context, SIM_WARNING_NOTIFICATION_CHANNEL)
285                         .setSmallIcon(R.drawable.ic_sim_card_alert_white_48dp)
286                         .setColor(context.getColor(R.color.sim_noitification))
287                         .setContentTitle(resources.getText(
288                                 R.string.sim_combination_warning_notification_title))
289                         .setContentText(dualCdmaSimWarningSummary)
290                         .setStyle(new Notification.BigTextStyle().bigText(
291                                 dualCdmaSimWarningSummary))
292                         .setAutoCancel(true);
293 
294         // Create the pending intent that will lead to the helper page.
295         Intent resultIntent = HelpUtils.getHelpIntent(
296                 context,
297                 context.getString(R.string.help_uri_sim_combination_warning),
298                 context.getClass().getName());
299         PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
300                 PendingIntent.FLAG_CANCEL_CURRENT);
301         builder.setContentIntent(resultPendingIntent);
302 
303         NotificationManager notificationManager =
304                 context.getSystemService(NotificationManager.class);
305         notificationManager.createNotificationChannel(notificationChannel);
306         notificationManager.notify(SIM_WARNING_NOTIFICATION_ID, builder.build());
307     }
308 
cancelSimCombinationWarningNotification(Context context)309     public static void cancelSimCombinationWarningNotification(Context context) {
310         NotificationManager notificationManager =
311                 context.getSystemService(NotificationManager.class);
312         notificationManager.cancel(SIM_WARNING_NOTIFICATION_ID);
313     }
314 }
315