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 android.app.NotificationChannel; 20 import android.app.NotificationManager; 21 import android.app.PendingIntent; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.res.Resources; 26 import android.provider.Settings; 27 import android.support.v4.app.NotificationCompat; 28 import android.telephony.SubscriptionInfo; 29 import android.telephony.SubscriptionManager; 30 import android.telephony.TelephonyManager; 31 import android.util.Log; 32 33 import com.android.internal.telephony.IccCardConstants; 34 import com.android.settings.R; 35 import com.android.settings.Settings.SimSettingsActivity; 36 import com.android.settings.Utils; 37 38 import java.util.List; 39 40 public class SimSelectNotification extends BroadcastReceiver { 41 private static final String TAG = "SimSelectNotification"; 42 private static final int NOTIFICATION_ID = 1; 43 44 private static final String SIM_SELECT_NOTIFICATION_CHANNEL = 45 "sim_select_notification_channel"; 46 47 @Override onReceive(Context context, Intent intent)48 public void onReceive(Context context, Intent intent) { 49 final TelephonyManager telephonyManager = (TelephonyManager) 50 context.getSystemService(Context.TELEPHONY_SERVICE); 51 final SubscriptionManager subscriptionManager = SubscriptionManager.from(context); 52 final int numSlots = telephonyManager.getSimCount(); 53 54 // Do not create notifications on single SIM devices or when provisioning i.e. Setup Wizard. 55 if (numSlots < 2 || !Utils.isDeviceProvisioned(context)) { 56 return; 57 } 58 59 // Cancel any previous notifications 60 cancelNotification(context); 61 62 // If sim state is not ABSENT or LOADED then ignore 63 String simStatus = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE); 64 if (!(IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(simStatus) || 65 IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(simStatus))) { 66 Log.d(TAG, "sim state is not Absent or Loaded"); 67 return; 68 } else { 69 Log.d(TAG, "simstatus = " + simStatus); 70 } 71 72 int state; 73 for (int i = 0; i < numSlots; i++) { 74 state = telephonyManager.getSimState(i); 75 if (!(state == TelephonyManager.SIM_STATE_ABSENT 76 || state == TelephonyManager.SIM_STATE_READY 77 || state == TelephonyManager.SIM_STATE_UNKNOWN)) { 78 Log.d(TAG, "All sims not in valid state yet"); 79 return; 80 } 81 } 82 83 List<SubscriptionInfo> sil = subscriptionManager.getActiveSubscriptionInfoList(); 84 if (sil == null || sil.size() < 1) { 85 Log.d(TAG, "Subscription list is empty"); 86 return; 87 } 88 89 // Clear defaults for any subscriptions which no longer exist 90 subscriptionManager.clearDefaultsForInactiveSubIds(); 91 92 boolean dataSelected = SubscriptionManager.isUsableSubIdValue( 93 SubscriptionManager.getDefaultDataSubscriptionId()); 94 boolean smsSelected = SubscriptionManager.isUsableSubIdValue( 95 SubscriptionManager.getDefaultSmsSubscriptionId()); 96 97 // If data and sms defaults are selected, dont show notification (Calls default is optional) 98 if (dataSelected && smsSelected) { 99 Log.d(TAG, "Data & SMS default sims are selected. No notification"); 100 return; 101 } 102 103 // Create a notification to tell the user that some defaults are missing 104 createNotification(context); 105 106 if (sil.size() == 1) { 107 // If there is only one subscription, ask if user wants to use if for everything 108 Intent newIntent = new Intent(context, SimDialogActivity.class); 109 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 110 newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.PREFERRED_PICK); 111 newIntent.putExtra(SimDialogActivity.PREFERRED_SIM, sil.get(0).getSimSlotIndex()); 112 context.startActivity(newIntent); 113 } else if (!dataSelected) { 114 // If there are mulitple, ensure they pick default data 115 Intent newIntent = new Intent(context, SimDialogActivity.class); 116 newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 117 newIntent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK); 118 context.startActivity(newIntent); 119 } 120 } 121 createNotification(Context context)122 private void createNotification(Context context){ 123 final Resources resources = context.getResources(); 124 125 NotificationChannel notificationChannel = new NotificationChannel( 126 SIM_SELECT_NOTIFICATION_CHANNEL, 127 resources.getString(R.string.sim_selection_channel_title), 128 NotificationManager.IMPORTANCE_LOW); 129 130 NotificationCompat.Builder builder = 131 new NotificationCompat.Builder(context, SIM_SELECT_NOTIFICATION_CHANNEL) 132 .setSmallIcon(R.drawable.ic_sim_card_alert_white_48dp) 133 .setColor(context.getColor(R.color.sim_noitification)) 134 .setContentTitle(resources.getString(R.string.sim_notification_title)) 135 .setContentText(resources.getString(R.string.sim_notification_summary)); 136 Intent resultIntent = new Intent(context, SimSettingsActivity.class); 137 resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 138 PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 139 PendingIntent.FLAG_CANCEL_CURRENT); 140 builder.setContentIntent(resultPendingIntent); 141 NotificationManager notificationManager = 142 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 143 notificationManager.createNotificationChannel(notificationChannel); 144 notificationManager.notify(NOTIFICATION_ID, builder.build()); 145 } 146 cancelNotification(Context context)147 public static void cancelNotification(Context context) { 148 NotificationManager notificationManager = 149 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 150 notificationManager.cancel(NOTIFICATION_ID); 151 } 152 } 153