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