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.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothManager;
21 import android.content.Context;
22 import android.net.ConnectivityManager;
23 import android.net.NetworkPolicyManager;
24 import android.net.wifi.WifiManager;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.TelephonyManager;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.Toast;
35 
36 import com.android.ims.ImsManager;
37 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
38 import com.android.internal.telephony.PhoneConstants;
39 import com.android.settingslib.RestrictedLockUtils;
40 
41 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
42 
43 /**
44  * Confirm and execute a reset of the network settings to a clean "just out of the box"
45  * state.  Multiple confirmations are required: first, a general "are you sure
46  * you want to do this?" prompt, followed by a keyguard pattern trace if the user
47  * has defined one, followed by a final strongly-worded "THIS WILL RESET EVERYTHING"
48  * prompt.  If at any time the phone is allowed to go to sleep, is
49  * locked, et cetera, then the confirmation sequence is abandoned.
50  *
51  * This is the confirmation screen.
52  */
53 public class ResetNetworkConfirm extends OptionsMenuFragment {
54 
55     private View mContentView;
56     private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
57 
58     /**
59      * The user has gone through the multiple confirmation, so now we go ahead
60      * and reset the network settings to its factory-default state.
61      */
62     private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
63 
64         @Override
65         public void onClick(View v) {
66             if (Utils.isMonkeyRunning()) {
67                 return;
68             }
69             // TODO maybe show a progress dialog if this ends up taking a while
70             Context context = getActivity();
71 
72             ConnectivityManager connectivityManager = (ConnectivityManager)
73                     context.getSystemService(Context.CONNECTIVITY_SERVICE);
74             if (connectivityManager != null) {
75                 connectivityManager.factoryReset();
76             }
77 
78             WifiManager wifiManager = (WifiManager)
79                     context.getSystemService(Context.WIFI_SERVICE);
80             if (wifiManager != null) {
81                 wifiManager.factoryReset();
82             }
83 
84             TelephonyManager telephonyManager = (TelephonyManager)
85                     context.getSystemService(Context.TELEPHONY_SERVICE);
86             if (telephonyManager != null) {
87                 telephonyManager.factoryReset(mSubId);
88             }
89 
90             NetworkPolicyManager policyManager = (NetworkPolicyManager)
91                     context.getSystemService(Context.NETWORK_POLICY_SERVICE);
92             if (policyManager != null) {
93                 String subscriberId = telephonyManager.getSubscriberId(mSubId);
94                 policyManager.factoryReset(subscriberId);
95             }
96 
97             BluetoothManager btManager = (BluetoothManager)
98                     context.getSystemService(Context.BLUETOOTH_SERVICE);
99             if (btManager != null) {
100                 BluetoothAdapter btAdapter = btManager.getAdapter();
101                 if (btAdapter != null) {
102                     btAdapter.factoryReset();
103                 }
104             }
105 
106             ImsManager.factoryReset(context);
107 
108             Toast.makeText(context, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT)
109                     .show();
110         }
111     };
112 
113     /**
114      * Configure the UI for the final confirmation interaction
115      */
establishFinalConfirmationState()116     private void establishFinalConfirmationState() {
117         mContentView.findViewById(R.id.execute_reset_network)
118                 .setOnClickListener(mFinalClickListener);
119     }
120 
121     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)122     public View onCreateView(LayoutInflater inflater, ViewGroup container,
123             Bundle savedInstanceState) {
124         final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
125                 getActivity(), UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId());
126         if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
127                 UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) {
128             return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
129         } else if (admin != null) {
130             View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
131             ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
132             view.setVisibility(View.VISIBLE);
133             return view;
134         }
135         mContentView = inflater.inflate(R.layout.reset_network_confirm, null);
136         establishFinalConfirmationState();
137         return mContentView;
138     }
139 
140     @Override
onCreate(Bundle savedInstanceState)141     public void onCreate(Bundle savedInstanceState) {
142         super.onCreate(savedInstanceState);
143 
144         Bundle args = getArguments();
145         if (args != null) {
146             mSubId = args.getInt(PhoneConstants.SUBSCRIPTION_KEY,
147                     SubscriptionManager.INVALID_SUBSCRIPTION_ID);
148         }
149     }
150 
151     @Override
getMetricsCategory()152     public int getMetricsCategory() {
153         return MetricsEvent.RESET_NETWORK_CONFIRM;
154     }
155 }
156