OSDN Git Service

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