Merge "[Settings] Code Refactor"

This commit is contained in:
Bonian Chen
2022-11-18 11:46:43 +00:00
committed by Gerrit Code Review
4 changed files with 243 additions and 35 deletions

View File

@@ -24,8 +24,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.telephony.SubscriptionInfo;
@@ -46,12 +44,10 @@ import androidx.annotation.VisibleForTesting;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
import com.android.settings.network.ResetNetworkRestrictionViewBuilder;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.password.ChooseLockSettingsHelper;
import com.android.settings.password.ConfirmLockPattern;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.development.DevelopmentSettingsEnabler;
import java.util.ArrayList;
@@ -115,7 +111,7 @@ public class ResetNetwork extends InstrumentedFragment {
// confirmation prompt; otherwise, go back to the initial state.
if (resultCode == Activity.RESULT_OK) {
showFinalConfirmation();
} else {
} else if (mContentView != null) {
establishInitialState(getActiveSubscriptionInfoList());
}
}
@@ -252,6 +248,10 @@ public class ResetNetwork extends InstrumentedFragment {
public void onResume() {
super.onResume();
if (mContentView == null) {
return;
}
// update options if subcription has been changed
List<SubscriptionInfo> updatedSubscriptions = getActiveSubscriptionInfoList();
if ((mSubscriptions != null)
@@ -277,18 +277,10 @@ public class ResetNetwork extends InstrumentedFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final UserManager um = UserManager.get(getActivity());
final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
getActivity(), UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId());
if (!um.isAdminUser() || RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) {
return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
} else if (admin != null) {
new ActionDisabledByAdminDialogHelper(getActivity())
.prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin)
.setOnDismissListener(__ -> getActivity().finish())
.show();
return new View(getContext());
View view = (new ResetNetworkRestrictionViewBuilder(getActivity())).build();
if (view != null) {
Log.w(TAG, "Access deny.");
return view;
}
mContentView = inflater.inflate(R.layout.reset_network, null);

View File

@@ -16,8 +16,6 @@
package com.android.settings;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.settings.SettingsEnums;
@@ -35,8 +33,6 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.os.RecoverySystem;
import android.os.UserHandle;
import android.os.UserManager;
import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.telephony.TelephonyManager;
@@ -52,9 +48,8 @@ import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
import com.android.settings.network.ResetNetworkRestrictionViewBuilder;
import com.android.settings.network.apn.ApnSettings;
import com.android.settingslib.RestrictedLockUtilsInternal;
/**
* Confirm and execute a reset of the network settings to a clean "just out of the box"
@@ -262,17 +257,10 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
mActivity, UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId());
if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mActivity,
UserManager.DISALLOW_NETWORK_RESET, UserHandle.myUserId())) {
return inflater.inflate(R.layout.network_reset_disallowed_screen, null);
} else if (admin != null) {
new ActionDisabledByAdminDialogHelper(mActivity)
.prepareDialogBuilder(UserManager.DISALLOW_NETWORK_RESET, admin)
.setOnDismissListener(__ -> mActivity.finish())
.show();
return new View(mActivity);
View view = (new ResetNetworkRestrictionViewBuilder(mActivity)).build();
if (view != null) {
Log.w(TAG, "Access deny.");
return view;
}
mContentView = inflater.inflate(R.layout.reset_network_confirm, null);
establishFinalConfirmationState();

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import android.app.Activity;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* A builder for creating restriction View when constructing UI for resetting network
* configuration.
*/
public class ResetNetworkRestrictionViewBuilder extends NetworkResetRestrictionChecker {
@VisibleForTesting
static final String mRestriction = UserManager.DISALLOW_NETWORK_RESET;
protected Activity mActivity;
protected LayoutInflater mInflater;
/**
* Constructor of builder.
*
* @param activity Activity to present restriction View.
*/
public ResetNetworkRestrictionViewBuilder(Activity activity) {
super(activity);
mActivity = activity;
}
/**
* Option for configuring LayoutInflater.
*
* @param inflater LayoutInflater
* @return this builder
*/
public ResetNetworkRestrictionViewBuilder setLayoutInflater(LayoutInflater inflater) {
mInflater = inflater;
return this;
}
/**
* Try to provide a View if access to reset network is not allowed.
* @return a View which presenting information of restrictions.
* {@code null} when no restriction on accessing.
*/
public View build() {
if (hasUserRestriction()) {
return operationNotAllow();
}
// Not allow when this option is restricted.
EnforcedAdmin admin = getEnforceAdminByRestriction();
if (admin == null) {
return null;
}
createRestrictDialogBuilder(admin)
.setOnDismissListener(dialogInterface -> mActivity.finish())
.show();
return createEmptyView();
}
@VisibleForTesting
protected LayoutInflater getLayoutInflater() {
if (mInflater != null) {
return mInflater;
}
return mActivity.getLayoutInflater();
}
@VisibleForTesting
protected View operationNotAllow() {
return getLayoutInflater().inflate(R.layout.network_reset_disallowed_screen, null);
}
@VisibleForTesting
protected EnforcedAdmin getEnforceAdminByRestriction() {
return RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
mActivity, mRestriction, UserHandle.myUserId());
}
@VisibleForTesting
protected AlertDialog.Builder createRestrictDialogBuilder(EnforcedAdmin admin) {
return (new ActionDisabledByAdminDialogHelper(mActivity))
.prepareDialogBuilder(mRestriction, admin);
}
@VisibleForTesting
protected View createEmptyView() {
return new ViewStub(mActivity);
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.os.UserManager;
import android.view.View;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import androidx.appcompat.app.AlertDialog;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class ResetNetworkRestrictionViewBuilderTest {
@Mock
private Activity mActivity;
@Mock
private UserManager mUserManager;
@Mock
private View mView;
@Mock
private AlertDialog.Builder mAlertDialogBuilder;
private ResetNetworkRestrictionViewBuilder mBuilder;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(mUserManager).when(mActivity).getSystemService(Context.USER_SERVICE);
}
@Test
public void build_getNull_whenNoRestriction() {
mBuilder = new ResetNetworkRestrictionViewBuilder(mActivity) {
@Override
protected boolean hasUserRestriction() { return false; }
@Override
protected EnforcedAdmin getEnforceAdminByRestriction() { return null; }
};
assertThat(mBuilder.build()).isNull();
}
@Test
public void build_getView_whenUserRestriction() {
mBuilder = new ResetNetworkRestrictionViewBuilder(mActivity) {
@Override
protected boolean hasUserRestriction() { return true; }
@Override
protected View operationNotAllow() { return mView; }
};
assertThat(mBuilder.build()).isNotNull();
}
@Test
public void build_getView_whenEnforceAdminRestriction() {
doReturn(mAlertDialogBuilder).when(mAlertDialogBuilder).setOnDismissListener(any());
String restriction = ResetNetworkRestrictionViewBuilder.mRestriction;
EnforcedAdmin admin = RestrictedLockUtils.EnforcedAdmin
.createDefaultEnforcedAdminWithRestriction(restriction);
mBuilder = new ResetNetworkRestrictionViewBuilder(mActivity) {
@Override
protected boolean hasUserRestriction() { return false; }
@Override
protected EnforcedAdmin getEnforceAdminByRestriction() { return admin; }
@Override
protected AlertDialog.Builder createRestrictDialogBuilder(EnforcedAdmin admin) {
return mAlertDialogBuilder;
}
@Override
protected View createEmptyView() { return mView; }
};
assertThat(mBuilder.build()).isNotNull();
}
}