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 com.android.settings.R; 20 import com.android.settings.Settings.SimSettingsActivity; 21 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.SharedPreferences; 28 import android.content.SharedPreferences.Editor; 29 import android.content.res.Resources; 30 import android.provider.Settings; 31 import android.support.v4.app.NotificationCompat; 32 import android.telephony.SubscriptionInfo; 33 import android.telephony.SubscriptionManager; 34 import android.telephony.TelephonyManager; 35 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; 36 37 import com.android.settings.Utils; 38 39 import java.util.List; 40 41 public class SimBootReceiver extends BroadcastReceiver { 42 private static final String TAG = "SimBootReceiver"; 43 private static final int SLOT_EMPTY = -1; 44 private static final int NOTIFICATION_ID = 1; 45 private static final String SHARED_PREFERENCES_NAME = "sim_state"; 46 private static final String SLOT_PREFIX = "sim_slot_"; 47 private static final int INVALID_SLOT = -2; // Used when upgrading from K to LMR1 48 49 private SharedPreferences mSharedPreferences = null; 50 private TelephonyManager mTelephonyManager; 51 private Context mContext; 52 private SubscriptionManager mSubscriptionManager; 53 54 @Override onReceive(Context context, Intent intent)55 public void onReceive(Context context, Intent intent) { 56 mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 57 mContext = context; 58 mSubscriptionManager = SubscriptionManager.from(mContext); 59 mSharedPreferences = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, 60 Context.MODE_PRIVATE); 61 62 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener); 63 } 64 detectChangeAndNotify()65 private void detectChangeAndNotify() { 66 final int numSlots = mTelephonyManager.getSimCount(); 67 final boolean isInProvisioning = Settings.Global.getInt(mContext.getContentResolver(), 68 Settings.Global.DEVICE_PROVISIONED, 0) == 0; 69 boolean notificationSent = false; 70 int numSIMsDetected = 0; 71 int lastSIMSlotDetected = -1; 72 73 // Do not create notifications on single SIM devices or when provisiong. 74 if (numSlots < 2 || isInProvisioning) { 75 return; 76 } 77 78 // We wait until SubscriptionManager returns a valid list of Subscription informations 79 // by checking if the list is empty. 80 // This is not completely correct, but works for most cases. 81 // See Bug: 18377252 82 List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList(); 83 if (sil == null || sil.size() < 1) { 84 return; 85 } 86 87 for (int i = 0; i < numSlots; i++) { 88 final SubscriptionInfo sir = Utils.findRecordBySlotId(mContext, i); 89 final String key = SLOT_PREFIX+i; 90 final int lastSubId = getLastSubId(key); 91 92 if (sir != null) { 93 numSIMsDetected++; 94 final int currentSubId = sir.getSubscriptionId(); 95 if (lastSubId == INVALID_SLOT) { 96 setLastSubId(key, currentSubId); 97 } else if (lastSubId != currentSubId) { 98 createNotification(mContext); 99 setLastSubId(key, currentSubId); 100 notificationSent = true; 101 } 102 lastSIMSlotDetected = i; 103 } else if (lastSubId != SLOT_EMPTY) { 104 createNotification(mContext); 105 setLastSubId(key, SLOT_EMPTY); 106 notificationSent = true; 107 } 108 } 109 110 if (notificationSent) { 111 Intent intent = new Intent(mContext, SimDialogActivity.class); 112 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 113 if (numSIMsDetected == 1) { 114 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.PREFERRED_PICK); 115 intent.putExtra(SimDialogActivity.PREFERRED_SIM, lastSIMSlotDetected); 116 } else { 117 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK); 118 } 119 mContext.startActivity(intent); 120 } 121 } 122 getLastSubId(String strSlotId)123 private int getLastSubId(String strSlotId) { 124 return mSharedPreferences.getInt(strSlotId, INVALID_SLOT); 125 } 126 setLastSubId(String strSlotId, int value)127 private void setLastSubId(String strSlotId, int value) { 128 Editor editor = mSharedPreferences.edit(); 129 editor.putInt(strSlotId, value); 130 editor.commit(); 131 } 132 createNotification(Context context)133 private void createNotification(Context context){ 134 final Resources resources = context.getResources(); 135 136 NotificationCompat.Builder builder = 137 new NotificationCompat.Builder(context) 138 .setSmallIcon(R.drawable.ic_sim_card_alert_white_48dp) 139 .setColor(resources.getColor(R.color.sim_noitification)) 140 .setContentTitle(resources.getString(R.string.sim_notification_title)) 141 .setContentText(resources.getString(R.string.sim_notification_summary)); 142 Intent resultIntent = new Intent(context, SimSettingsActivity.class); 143 resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 144 PendingIntent resultPendingIntent = 145 PendingIntent.getActivity( 146 context, 147 0, 148 resultIntent, 149 PendingIntent.FLAG_CANCEL_CURRENT 150 ); 151 builder.setContentIntent(resultPendingIntent); 152 NotificationManager notificationManager = 153 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 154 notificationManager.notify(NOTIFICATION_ID, builder.build()); 155 } 156 cancelNotification(Context context)157 public static void cancelNotification(Context context) { 158 NotificationManager notificationManager = 159 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 160 notificationManager.cancel(NOTIFICATION_ID); 161 } 162 163 private final OnSubscriptionsChangedListener mSubscriptionListener = 164 new OnSubscriptionsChangedListener() { 165 @Override 166 public void onSubscriptionsChanged() { 167 detectChangeAndNotify(); 168 } 169 }; 170 171 } 172