[Settings] Code Refactor

Adjust code structure for maintainance.

Bug: 259611847
Test: auto test
Change-Id: I1fab8d0347f64c01ee768e4bb9ea838a9e7ad4fc
This commit is contained in:
Bonian Chen
2022-11-18 14:37:13 +00:00
parent 5a34a5b3de
commit c3028291ed
5 changed files with 366 additions and 139 deletions

View File

@@ -19,23 +19,12 @@ package com.android.settings;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkPolicyManager;
import android.net.Uri;
import android.net.VpnManager;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.os.RecoverySystem;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -48,8 +37,10 @@ import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.network.ResetNetworkOperationBuilder;
import com.android.settings.network.ResetNetworkRestrictionViewBuilder;
import com.android.settings.network.apn.ApnSettings;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Confirm and execute a reset of the network settings to a clean "just out of the box"
@@ -90,54 +81,25 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
@Override
protected Boolean doInBackground(Void... params) {
boolean isResetSucceed = true;
ConnectivityManager connectivityManager = (ConnectivityManager)
mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
connectivityManager.factoryReset();
}
VpnManager vpnManager = mContext.getSystemService(VpnManager.class);
if (vpnManager != null) {
vpnManager.factoryReset();
}
WifiManager wifiManager = (WifiManager)
mContext.getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
wifiManager.factoryReset();
}
p2pFactoryReset(mContext);
final AtomicBoolean resetEsimSuccess = new AtomicBoolean(true);
ResetNetworkOperationBuilder builder =
(new ResetNetworkOperationBuilder(mContext))
.resetConnectivityManager()
.resetVpnManager()
.resetWifiManager()
.resetWifiP2pManager(Looper.getMainLooper());
if (mEraseEsim) {
isResetSucceed = RecoverySystem.wipeEuiccData(mContext, mPackageName);
builder = builder.resetEsim(mContext.getPackageName(),
success -> { resetEsimSuccess.set(success); }
);
}
builder.resetTelephonyAndNetworkPolicyManager(mSubId)
.resetBluetoothManager()
.resetApn(mSubId)
.build()
.run();
TelephonyManager telephonyManager = (TelephonyManager)
mContext.getSystemService(TelephonyManager.class)
.createForSubscriptionId(mSubId);
if (telephonyManager != null) {
telephonyManager.resetSettings();
}
NetworkPolicyManager policyManager = (NetworkPolicyManager)
mContext.getSystemService(Context.NETWORK_POLICY_SERVICE);
if (policyManager != null) {
String subscriberId = telephonyManager.getSubscriberId();
policyManager.factoryReset(subscriberId);
}
BluetoothManager btManager = (BluetoothManager)
mContext.getSystemService(Context.BLUETOOTH_SERVICE);
if (btManager != null) {
BluetoothAdapter btAdapter = btManager.getAdapter();
if (btAdapter != null) {
btAdapter.clearBluetooth();
}
}
restoreDefaultApn(mContext);
boolean isResetSucceed = resetEsimSuccess.get();
Log.d(TAG, "network factoryReset complete. succeeded: "
+ String.valueOf(isResetSucceed));
return isResetSucceed;
@@ -201,20 +163,6 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
}
};
@VisibleForTesting
void p2pFactoryReset(Context context) {
WifiP2pManager wifiP2pManager = (WifiP2pManager)
context.getSystemService(Context.WIFI_P2P_SERVICE);
if (wifiP2pManager != null) {
WifiP2pManager.Channel channel = wifiP2pManager.initialize(
context.getApplicationContext(), context.getMainLooper(),
null /* listener */);
if (channel != null) {
wifiP2pManager.factoryReset(channel, null /* listener */);
}
}
}
private ProgressDialog getProgressDialog(Context context) {
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setIndeterminate(true);
@@ -224,20 +172,6 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
return progressDialog;
}
/**
* Restore APN settings to default.
*/
private void restoreDefaultApn(Context context) {
Uri uri = Uri.parse(ApnSettings.RESTORE_CARRIERS_URI);
if (SubscriptionManager.isUsableSubscriptionId(mSubId)) {
uri = Uri.withAppendedPath(uri, "subId/" + String.valueOf(mSubId));
}
ContentResolver resolver = context.getContentResolver();
resolver.delete(uri, null, null);
}
/**
* Configure the UI for the final confirmation interaction
*/