OSDN Git Service

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