OSDN Git Service

Back-port ag/2491664 am: 1d21738bd6
[android-x86/packages-apps-Settings.git] / src / com / android / settings / ResetNetwork.java
1 /*
2  * Copyright (C) 2015 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;
18
19 import android.app.Activity;
20 import android.app.Fragment;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.res.Resources;
25 import android.graphics.drawable.Drawable;
26 import android.os.Bundle;
27 import android.os.Environment;
28 import android.os.SystemProperties;
29 import android.os.Process;
30 import android.os.UserManager;
31 import android.preference.Preference;
32 import android.preference.PreferenceActivity;
33 import android.telephony.SubscriptionInfo;
34 import android.telephony.SubscriptionManager;
35 import android.text.TextUtils;
36 import android.util.Log;
37 import android.view.LayoutInflater;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.widget.ArrayAdapter;
41 import android.widget.Button;
42 import android.widget.CheckBox;
43 import android.widget.LinearLayout;
44 import android.widget.Spinner;
45 import android.widget.TextView;
46
47 import com.android.internal.logging.MetricsLogger;
48 import com.android.internal.telephony.PhoneConstants;
49
50 import java.util.ArrayList;
51 import java.util.List;
52
53 /**
54  * Confirm and execute a reset of the device's network settings to a clean "just out of the box"
55  * state.  Multiple confirmations are required: first, a general "are you sure you want to do this?"
56  * prompt, followed by a keyguard pattern trace if the user has defined one, followed by a final
57  * strongly-worded "THIS WILL RESET EVERYTHING" prompt.  If at any time the phone is allowed to go
58  * to sleep, is locked, et cetera, then the confirmation sequence is abandoned.
59  *
60  * This is the initial screen.
61  */
62 public class ResetNetwork extends InstrumentedFragment {
63     private static final String TAG = "ResetNetwork";
64
65     // Arbitrary to avoid conficts
66     private static final int KEYGUARD_REQUEST = 55;
67
68     private List<SubscriptionInfo> mSubscriptions;
69
70     private View mContentView;
71     private Spinner mSubscriptionSpinner;
72     private Button mInitiateButton;
73
74     /**
75      * Keyguard validation is run using the standard {@link ConfirmLockPattern}
76      * component as a subactivity
77      * @param request the request code to be returned once confirmation finishes
78      * @return true if confirmation launched
79      */
80     private boolean runKeyguardConfirmation(int request) {
81         Resources res = getActivity().getResources();
82         return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
83                 request, res.getText(R.string.reset_network_title));
84     }
85
86     @Override
87     public void onActivityResult(int requestCode, int resultCode, Intent data) {
88         super.onActivityResult(requestCode, resultCode, data);
89
90         if (requestCode != KEYGUARD_REQUEST) {
91             return;
92         }
93
94         // If the user entered a valid keyguard trace, present the final
95         // confirmation prompt; otherwise, go back to the initial state.
96         if (resultCode == Activity.RESULT_OK) {
97             showFinalConfirmation();
98         } else {
99             establishInitialState();
100         }
101     }
102
103     private void showFinalConfirmation() {
104         Bundle args = new Bundle();
105         if (mSubscriptions != null && mSubscriptions.size() > 0) {
106             int selectedIndex = mSubscriptionSpinner.getSelectedItemPosition();
107             SubscriptionInfo subscription = mSubscriptions.get(selectedIndex);
108             args.putInt(PhoneConstants.SUBSCRIPTION_KEY, subscription.getSubscriptionId());
109         }
110         ((SettingsActivity) getActivity()).startPreferencePanel(ResetNetworkConfirm.class.getName(),
111                 args, R.string.reset_network_confirm_title, null, null, 0);
112     }
113
114     /**
115      * If the user clicks to begin the reset sequence, we next require a
116      * keyguard confirmation if the user has currently enabled one.  If there
117      * is no keyguard available, we simply go to the final confirmation prompt.
118      */
119     private final Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
120
121         @Override
122         public void onClick(View v) {
123             if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
124                 showFinalConfirmation();
125             }
126         }
127     };
128
129     /**
130      * In its initial state, the activity presents a button for the user to
131      * click in order to initiate a confirmation sequence.  This method is
132      * called from various other points in the code to reset the activity to
133      * this base state.
134      *
135      * <p>Reinflating views from resources is expensive and prevents us from
136      * caching widget pointers, so we use a single-inflate pattern:  we lazy-
137      * inflate each view, caching all of the widget pointers we'll need at the
138      * time, then simply reuse the inflated views directly whenever we need
139      * to change contents.
140      */
141     private void establishInitialState() {
142         mSubscriptionSpinner = (Spinner) mContentView.findViewById(R.id.reset_network_subscription);
143
144         mSubscriptions = SubscriptionManager.from(getActivity()).getActiveSubscriptionInfoList();
145         if (mSubscriptions != null && mSubscriptions.size() > 0) {
146             // Get the default subscription in the order of data, voice, sms, first up.
147             int defaultSubscription = SubscriptionManager.getDefaultDataSubId();
148             if (!SubscriptionManager.isUsableSubIdValue(defaultSubscription)) {
149                 defaultSubscription = SubscriptionManager.getDefaultVoiceSubId();
150             }
151             if (!SubscriptionManager.isUsableSubIdValue(defaultSubscription)) {
152                 defaultSubscription = SubscriptionManager.getDefaultSmsSubId();
153             }
154             if (!SubscriptionManager.isUsableSubIdValue(defaultSubscription)) {
155                 defaultSubscription = SubscriptionManager.getDefaultSubId();
156             }
157
158             int selectedIndex = 0;
159             int size = mSubscriptions.size();
160             List<String> subscriptionNames = new ArrayList<>();
161             for (SubscriptionInfo record : mSubscriptions) {
162                 if (record.getSubscriptionId() == defaultSubscription) {
163                     // Set the first selected value to the default
164                     selectedIndex = subscriptionNames.size();
165                 }
166                 String name = record.getDisplayName().toString();
167                 if (TextUtils.isEmpty(name)) {
168                     name = record.getNumber();
169                 }
170                 if (TextUtils.isEmpty(name)) {
171                     name = record.getCarrierName().toString();
172                 }
173                 if (TextUtils.isEmpty(name)) {
174                     name = String.format("MCC:%s MNC:%s Slot:%s Id:%s", record.getMcc(),
175                             record.getMnc(), record.getSimSlotIndex(), record.getSubscriptionId());
176                 }
177                 subscriptionNames.add(name);
178             }
179             ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
180                     android.R.layout.simple_spinner_item, subscriptionNames);
181             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
182             mSubscriptionSpinner.setAdapter(adapter);
183             mSubscriptionSpinner.setSelection(selectedIndex);
184             mSubscriptionSpinner.setVisibility(View.VISIBLE);
185         } else {
186             mSubscriptionSpinner.setVisibility(View.INVISIBLE);
187         }
188         mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_reset_network);
189         mInitiateButton.setOnClickListener(mInitiateListener);
190     }
191
192     @Override
193     public View onCreateView(LayoutInflater inflater, ViewGroup container,
194             Bundle savedInstanceState) {
195         if (!Process.myUserHandle().isOwner()
196                 || UserManager.get(getActivity()).hasUserRestriction(
197                 UserManager.DISALLOW_NETWORK_RESET)) {
198             return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
199         }
200
201         mContentView = inflater.inflate(R.layout.reset_network, null);
202
203         establishInitialState();
204         return mContentView;
205     }
206
207     @Override
208     protected int getMetricsCategory() {
209         return MetricsLogger.RESET_NETWORK;
210     }
211 }