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