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