1 /* 2 * Copyright (C) 2022 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 17 package com.android.settings.network; 18 19 import android.app.Activity; 20 import android.os.UserHandle; 21 import android.os.UserManager; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewStub; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.appcompat.app.AlertDialog; 28 29 import com.android.settings.R; 30 import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper; 31 import com.android.settingslib.RestrictedLockUtilsInternal; 32 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 33 34 /** 35 * A builder for creating restriction View when constructing UI for resetting network 36 * configuration. 37 */ 38 public class ResetNetworkRestrictionViewBuilder extends NetworkResetRestrictionChecker { 39 40 @VisibleForTesting 41 static final String mRestriction = UserManager.DISALLOW_NETWORK_RESET; 42 43 protected Activity mActivity; 44 protected LayoutInflater mInflater; 45 46 /** 47 * Constructor of builder. 48 * 49 * @param activity Activity to present restriction View. 50 */ ResetNetworkRestrictionViewBuilder(Activity activity)51 public ResetNetworkRestrictionViewBuilder(Activity activity) { 52 super(activity); 53 mActivity = activity; 54 } 55 56 /** 57 * Option for configuring LayoutInflater. 58 * 59 * @param inflater LayoutInflater 60 * @return this builder 61 */ setLayoutInflater(LayoutInflater inflater)62 public ResetNetworkRestrictionViewBuilder setLayoutInflater(LayoutInflater inflater) { 63 mInflater = inflater; 64 return this; 65 } 66 67 /** 68 * Try to provide a View if access to reset network is not allowed. 69 * @return a View which presenting information of restrictions. 70 * {@code null} when no restriction on accessing. 71 */ build()72 public View build() { 73 if (hasUserRestriction()) { 74 return operationNotAllow(); 75 } 76 77 // Not allow when this option is restricted. 78 EnforcedAdmin admin = getEnforceAdminByRestriction(); 79 if (admin == null) { 80 return null; 81 } 82 83 createRestrictDialogBuilder(admin) 84 .setOnDismissListener(dialogInterface -> mActivity.finish()) 85 .show(); 86 return createEmptyView(); 87 } 88 89 @VisibleForTesting getLayoutInflater()90 protected LayoutInflater getLayoutInflater() { 91 if (mInflater != null) { 92 return mInflater; 93 } 94 return mActivity.getLayoutInflater(); 95 } 96 97 @VisibleForTesting operationNotAllow()98 protected View operationNotAllow() { 99 return getLayoutInflater().inflate(R.layout.network_reset_disallowed_screen, null); 100 } 101 102 @VisibleForTesting getEnforceAdminByRestriction()103 protected EnforcedAdmin getEnforceAdminByRestriction() { 104 return RestrictedLockUtilsInternal.checkIfRestrictionEnforced( 105 mActivity, mRestriction, UserHandle.myUserId()); 106 } 107 108 @VisibleForTesting createRestrictDialogBuilder(EnforcedAdmin admin)109 protected AlertDialog.Builder createRestrictDialogBuilder(EnforcedAdmin admin) { 110 return (new ActionDisabledByAdminDialogHelper(mActivity)) 111 .prepareDialogBuilder(mRestriction, admin); 112 } 113 114 @VisibleForTesting createEmptyView()115 protected View createEmptyView() { 116 return new ViewStub(mActivity); 117 } 118 } 119