Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc695c77 authored by Stuart Scott's avatar Stuart Scott Committed by Android (Google) Code Review
Browse files

Merge "Move reset network settings into framework."

parents 25512e82 73281fbc
Loading
Loading
Loading
Loading
+17 −92
Original line number Diff line number Diff line
@@ -18,19 +18,12 @@ package com.android.settings;

import android.app.Fragment;
import android.content.Context;
import android.net.IConnectivityManager;
import android.net.NetworkPolicy;
import android.net.ConnectivityManager;
import android.net.NetworkPolicyManager;
import android.net.NetworkTemplate;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -40,11 +33,7 @@ import android.widget.Spinner;
import android.widget.Toast;

import com.android.internal.logging.MetricsLogger;
import com.android.internal.net.VpnConfig;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.PhoneFactory;
import com.android.settings.net.NetworkPolicyEditor;

import java.util.ArrayList;
import java.util.List;
@@ -77,93 +66,29 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
            }
            // TODO maybe show a progress dialog if this ends up taking a while

            IConnectivityManager connectivityService = IConnectivityManager.Stub.asInterface(
                    ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
            WifiManager wifiManager = (WifiManager)
                    getActivity().getSystemService(Context.WIFI_SERVICE);
            TelephonyManager telephonyManager = (TelephonyManager)
                    getActivity().getSystemService(Context.TELEPHONY_SERVICE);
            NetworkPolicyManager policyManager = NetworkPolicyManager.from(getActivity());
            NetworkPolicyEditor policyEditor = new NetworkPolicyEditor(policyManager);
            policyEditor.read();

            // Turn airplane mode off
            try {
                connectivityService.setAirplaneMode(false);
            } catch (RemoteException e) {
                // Well, we tried
            }

            // Turn wifi on
            wifiManager.setWifiEnabled(true);

            // Delete all Wifi SSIDs
            List<WifiConfiguration> networks = wifiManager.getConfiguredNetworks();
            if (networks != null) {
                for (WifiConfiguration config : networks) {
                    wifiManager.removeNetwork(config.networkId);
                }
                wifiManager.saveConfiguration();
            ConnectivityManager connectivityManager = (ConnectivityManager)
                    getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                connectivityManager.factoryReset();
            }

            // Turn mobile hotspot off
            wifiManager.setWifiApEnabled(null, false);

            // Un-tether
            try {
                for (String tether : connectivityService.getTetheredIfaces()) {
                    connectivityService.untether(tether);
                }
            } catch (RemoteException e) {
                // Well, we tried
            WifiManager wifiManager = (WifiManager)
                    getActivity().getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                wifiManager.factoryReset();
            }

            // Turn VPN off
            try {
                VpnConfig vpnConfig = connectivityService.getVpnConfig();
                if (vpnConfig != null) {
                    if (vpnConfig.legacy) {
                        connectivityService.prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN);
                    } else {
                        // Prevent this app from initiating VPN connections in the future without
                        // user intervention.
                        connectivityService.setVpnPackageAuthorization(false);
                        connectivityService.prepareVpn(vpnConfig.user, VpnConfig.LEGACY_VPN);
                    }
                }
            } catch (RemoteException e) {
                // Well, we tried
            TelephonyManager telephonyManager = (TelephonyManager)
                    getActivity().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                telephonyManager.factoryReset(mSubId);
            }

            if (SubscriptionManager.isUsableSubIdValue(mSubId)) {
                // Turn mobile data on
                telephonyManager.setDataEnabled(mSubId, true);

                // Set mobile network selection mode to automatic
                // TODO set network selection mode to automatic
                // phone.setNetworkSelectionModeAutomatic(null);

                // Set preferred mobile network type to manufacturer's recommended
                // int networkType = ; // TODO get manufacturer's default
                // telephonyManager.setPreferredNetworkType(networkType);

                // Turn roaming to manufacturer's default
                // boolean enabled = ; // TODO get manufacturer's default
                // SubscriptionManager.from(getContext()).setDataRoaming(enabled, mSubId);

            NetworkPolicyManager policyManager = (NetworkPolicyManager)
                    getActivity().getSystemService(Context.NETWORK_POLICY_SERVICE);
            if (policyManager != null) {
                String subscriberId = telephonyManager.getSubscriberId(mSubId);
                NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(subscriberId);
                // Turn mobile data limit off
                policyEditor.setPolicyLimitBytes(template, NetworkPolicy.LIMIT_DISABLED);
            }

            // Turn restrict background data off
            policyManager.setRestrictBackground(false);

            // Remove app's "restrict background data" flag
            for (int uid : policyManager.getUidsWithPolicy(
                    NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND)) {
                policyManager.setUidPolicy(uid, NetworkPolicyManager.POLICY_NONE);
                policyManager.factoryReset(subscriberId);
            }

            Toast.makeText(getActivity(), R.string.reset_network_complete_toast, Toast.LENGTH_SHORT)