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
17package com.android.settings;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothManager;
21import android.content.Context;
22import android.net.ConnectivityManager;
23import android.net.NetworkPolicyManager;
24import android.net.wifi.WifiManager;
25import android.os.Bundle;
26import android.os.UserHandle;
27import android.os.UserManager;
28import android.telephony.SubscriptionManager;
29import android.telephony.TelephonyManager;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.Button;
34import android.widget.Toast;
35
36import com.android.ims.ImsManager;
37import com.android.internal.logging.MetricsProto.MetricsEvent;
38import com.android.internal.telephony.PhoneConstants;
39import com.android.settingslib.RestrictedLockUtils;
40
41import 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 */
53public 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     */
116    private void establishFinalConfirmationState() {
117        mContentView.findViewById(R.id.execute_reset_network)
118                .setOnClickListener(mFinalClickListener);
119    }
120
121    @Override
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
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
152    protected int getMetricsCategory() {
153        return MetricsEvent.RESET_NETWORK_CONFIRM;
154    }
155}
156