Gray out the "Remove account" button when there's a restriction

Fix: 243011338
Fix: 288887119
Test: robotest, manual
Change-Id: Ie3d6607f69a5e7c66bb750febc4d526d6d209ac6
This commit is contained in:
Jason Chiu
2023-08-17 14:32:00 +08:00
parent de9926a79b
commit b6c2108c93
6 changed files with 226 additions and 51 deletions

View File

@@ -34,19 +34,17 @@ import android.os.UserManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settings.widget.RestrictedButton;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.widget.LayoutPreference;
@@ -64,6 +62,7 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl
private Fragment mParentFragment;
private UserHandle mUserHandle;
private LayoutPreference mRemoveAccountPreference;
private RestrictedButton mRemoveAccountButton;
public RemoveAccountPreferenceController(Context context, Fragment parent) {
super(context);
@@ -75,8 +74,14 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mRemoveAccountPreference = screen.findPreference(KEY_REMOVE_ACCOUNT);
final Button removeAccountButton = mRemoveAccountPreference.findViewById(R.id.button);
removeAccountButton.setOnClickListener(this);
mRemoveAccountButton = mRemoveAccountPreference.findViewById(R.id.button);
mRemoveAccountButton.setOnClickListener(this);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
mRemoveAccountButton.updateState();
}
@Override
@@ -93,21 +98,13 @@ public class RemoveAccountPreferenceController extends AbstractPreferenceControl
public void onClick(View v) {
mMetricsFeatureProvider.logClickedPreference(mRemoveAccountPreference,
mMetricsFeatureProvider.getMetricsCategory(mParentFragment));
if (mUserHandle != null) {
final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
mContext, UserManager.DISALLOW_MODIFY_ACCOUNTS, mUserHandle.getIdentifier());
if (admin != null) {
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, admin);
return;
}
}
ConfirmRemoveAccountDialog.show(mParentFragment, mAccount, mUserHandle);
}
public void init(Account account, UserHandle userHandle) {
mAccount = account;
mUserHandle = userHandle;
mRemoveAccountButton.init(mUserHandle, UserManager.DISALLOW_MODIFY_ACCOUNTS);
}
/**

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2023 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.widget;
import android.content.Context;
import android.os.UserHandle;
import android.util.AttributeSet;
import android.widget.Button;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedLockUtilsInternal;
/**
* A preference with a plus button on the side representing an "add" action. The plus button will
* only be visible when a non-null click listener is registered.
*/
public class RestrictedButton extends Button {
private UserHandle mUserHandle;
private String mUserRestriction;
public RestrictedButton(Context context) {
super(context);
}
public RestrictedButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean performClick() {
EnforcedAdmin admin = getEnforcedAdmin();
if (admin != null) {
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, admin);
return false;
}
return super.performClick();
}
/** Initialize the button with {@link UserHandle} and a restriction */
public void init(UserHandle userHandle, String restriction) {
setAllowClickWhenDisabled(true);
mUserHandle = userHandle;
mUserRestriction = restriction;
}
/** Update the restriction state */
public void updateState() {
setEnabled(getEnforcedAdmin() == null);
}
private EnforcedAdmin getEnforcedAdmin() {
if (mUserHandle != null) {
EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
mContext, mUserRestriction, mUserHandle.getIdentifier());
if (admin != null) {
return admin;
}
}
return null;
}
}