OSDN Git Service

Merge "replace temporary metrics categories with permanent ones." into mnc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / sim / SimBootReceiver.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.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.res.Resources;
28 import android.provider.Settings;
29 import android.support.v4.app.NotificationCompat;
30 import android.telephony.SubscriptionInfo;
31 import android.telephony.SubscriptionManager;
32 import android.telephony.TelephonyManager;
33 import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
34
35 import com.android.settings.Utils;
36
37 import java.util.List;
38
39 public class SimBootReceiver extends BroadcastReceiver {
40     private static final String TAG = "SimBootReceiver";
41     private static final int NOTIFICATION_ID = 1;
42
43     private TelephonyManager mTelephonyManager;
44     private Context mContext;
45     private SubscriptionManager mSubscriptionManager;
46
47     @Override
48     public void onReceive(Context context, Intent intent) {
49         mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
50         mContext = context;
51         mSubscriptionManager = SubscriptionManager.from(mContext);
52         mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
53     }
54
55     private void detectChangeAndNotify() {
56         final int numSlots = mTelephonyManager.getSimCount();
57         final boolean isInProvisioning = Settings.Global.getInt(mContext.getContentResolver(),
58                 Settings.Global.DEVICE_PROVISIONED, 0) == 0;
59         boolean notificationSent = false;
60         int numSIMsDetected = 0;
61         int lastSIMSlotDetected = -1;
62
63         // Do not create notifications on single SIM devices or when provisiong.
64         if (numSlots < 2 || isInProvisioning) {
65             return;
66         }
67
68         // Cancel any previous notifications
69         cancelNotification(mContext);
70
71         // Clear defaults for any subscriptions which no longer exist
72         mSubscriptionManager.clearDefaultsForInactiveSubIds();
73
74         boolean dataSelected = SubscriptionManager.isUsableSubIdValue(
75                 SubscriptionManager.getDefaultDataSubId());
76         boolean smsSelected = SubscriptionManager.isUsableSubIdValue(
77                 SubscriptionManager.getDefaultSmsSubId());
78
79         // If data and sms defaults are selected, dont show notification (Calls default is optional)
80         if (dataSelected && smsSelected) {
81             return;
82         }
83
84         // We wait until SubscriptionManager returns a valid list of Subscription informations
85         // by checking if the list is empty.
86         // This is not completely correct, but works for most cases.
87         // See Bug: 18377252
88         List<SubscriptionInfo> sil = mSubscriptionManager.getActiveSubscriptionInfoList();
89         if (sil == null || sil.size() < 1) {
90             return;
91         } else {
92             // Create a notification to tell the user that some defaults are missing
93             createNotification(mContext);
94
95             if (sil.size() == 1) {
96                 // If there is only one subscription, ask if user wants to use if for everything
97                 Intent intent = new Intent(mContext, SimDialogActivity.class);
98                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
99                 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.PREFERRED_PICK);
100                 intent.putExtra(SimDialogActivity.PREFERRED_SIM, sil.get(0).getSimSlotIndex());
101                 mContext.startActivity(intent);
102             } else if (!dataSelected) {
103                 // TODO(sanketpadawe): This should not be shown if the user is looking at the
104                 // SimSettings page - its just annoying
105                 // If there are mulitple, ensure they pick default data
106                 Intent intent = new Intent(mContext, SimDialogActivity.class);
107                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
108                 intent.putExtra(SimDialogActivity.DIALOG_TYPE_KEY, SimDialogActivity.DATA_PICK);
109                 mContext.startActivity(intent);
110             }
111         }
112
113     }
114
115     private void createNotification(Context context){
116         final Resources resources = context.getResources();
117
118         // TODO(sanketpadawe): This notification should not be dissmissable by the user
119         NotificationCompat.Builder builder =
120                 new NotificationCompat.Builder(context)
121                 .setSmallIcon(R.drawable.ic_sim_card_alert_white_48dp)
122                 .setColor(context.getColor(R.color.sim_noitification))
123                 .setContentTitle(resources.getString(R.string.sim_notification_title))
124                 .setContentText(resources.getString(R.string.sim_notification_summary));
125         Intent resultIntent = new Intent(context, SimSettingsActivity.class);
126         resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
127         PendingIntent resultPendingIntent =
128                 PendingIntent.getActivity(
129                 context,
130                 0,
131                 resultIntent,
132                 PendingIntent.FLAG_CANCEL_CURRENT
133                 );
134         builder.setContentIntent(resultPendingIntent);
135         NotificationManager notificationManager =
136                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
137         notificationManager.notify(NOTIFICATION_ID, builder.build());
138     }
139
140     public static void cancelNotification(Context context) {
141         NotificationManager notificationManager =
142                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
143         notificationManager.cancel(NOTIFICATION_ID);
144     }
145
146     private final OnSubscriptionsChangedListener mSubscriptionListener =
147             new OnSubscriptionsChangedListener() {
148         @Override
149         public void onSubscriptionsChanged() {
150             detectChangeAndNotify();
151         }
152     };
153
154 }