OSDN Git Service

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