Remove RestrictedLockUtils dependency in MainSwitchPreference
- Extract related things to SettingsMainSwitchBar Bug: 180226735 Test: robotest Change-Id: Ibb0e687519ef48f85f47028374437e00ac2adabd
This commit is contained in:
@@ -15,7 +15,6 @@ android_library {
|
||||
|
||||
static_libs: [
|
||||
"androidx.preference_preference",
|
||||
"SettingsLibRestrictedLockUtils",
|
||||
],
|
||||
|
||||
sdk_version: "system_current",
|
||||
|
||||
@@ -24,16 +24,12 @@ import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -48,12 +44,8 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
|
||||
|
||||
private View mAboveDivider;
|
||||
private View mBelowDivider;
|
||||
private TextView mTextView;
|
||||
private ImageView mRestrictedIcon;
|
||||
private Switch mSwitch;
|
||||
|
||||
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
private boolean mDisabledByAdmin;
|
||||
protected TextView mTextView;
|
||||
protected Switch mSwitch;
|
||||
|
||||
public MainSwitchBar(Context context) {
|
||||
this(context, null);
|
||||
@@ -81,14 +73,6 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
|
||||
|
||||
addOnSwitchChangeListener((switchView, isChecked) -> setChecked(isChecked));
|
||||
|
||||
mRestrictedIcon = findViewById(R.id.restricted_icon);
|
||||
mRestrictedIcon.setOnClickListener((View v) -> {
|
||||
if (mDisabledByAdmin) {
|
||||
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, mEnforcedAdmin);
|
||||
onRestrictedIconClick();
|
||||
}
|
||||
});
|
||||
|
||||
setChecked(mSwitch.isChecked());
|
||||
|
||||
if (attrs != null) {
|
||||
@@ -110,7 +94,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
|
||||
|
||||
@Override
|
||||
public boolean performClick() {
|
||||
return getDelegatingView().performClick();
|
||||
return mSwitch.performClick();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,52 +172,15 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If admin is not null, disables the text and switch but keeps the view clickable.
|
||||
* Otherwise, calls setEnabled which will enables the entire view including
|
||||
* the text and switch.
|
||||
*/
|
||||
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||
mEnforcedAdmin = admin;
|
||||
if (admin != null) {
|
||||
super.setEnabled(true);
|
||||
mDisabledByAdmin = true;
|
||||
mTextView.setEnabled(false);
|
||||
mSwitch.setEnabled(false);
|
||||
mSwitch.setVisibility(View.GONE);
|
||||
mRestrictedIcon.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
mDisabledByAdmin = false;
|
||||
mSwitch.setVisibility(View.VISIBLE);
|
||||
mRestrictedIcon.setVisibility(View.GONE);
|
||||
setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable the text and switch.
|
||||
*/
|
||||
public void setEnabled(boolean enabled) {
|
||||
if (enabled && mDisabledByAdmin) {
|
||||
setDisabledByAdmin(null);
|
||||
return;
|
||||
}
|
||||
super.setEnabled(enabled);
|
||||
mTextView.setEnabled(enabled);
|
||||
mSwitch.setEnabled(enabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the restricted icon clicked.
|
||||
*/
|
||||
protected void onRestrictedIconClick() {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
View getDelegatingView() {
|
||||
return mDisabledByAdmin ? mRestrictedIcon : mSwitch;
|
||||
}
|
||||
|
||||
private void propagateChecked(boolean isChecked) {
|
||||
final int count = mSwitchChangeListeners.size();
|
||||
for (int n = 0; n < count; n++) {
|
||||
|
||||
@@ -24,8 +24,6 @@ import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.preference.TwoStatePreference;
|
||||
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -41,8 +39,6 @@ public class MainSwitchPreference extends TwoStatePreference {
|
||||
private MainSwitchBar mMainSwitchBar;
|
||||
private CharSequence mTitle;
|
||||
|
||||
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
public MainSwitchPreference(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
@@ -115,7 +111,6 @@ public class MainSwitchPreference extends TwoStatePreference {
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setChecked(checked);
|
||||
mMainSwitchBar.setTitle(mTitle);
|
||||
mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
|
||||
mMainSwitchBar.show();
|
||||
}
|
||||
}
|
||||
@@ -142,18 +137,6 @@ public class MainSwitchPreference extends TwoStatePreference {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If admin is not null, disables the text and switch but keeps the view clickable.
|
||||
* Otherwise, calls setEnabled which will enables the entire view including
|
||||
* the text and switch.
|
||||
*/
|
||||
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||
mEnforcedAdmin = admin;
|
||||
if (mMainSwitchBar != null) {
|
||||
mMainSwitchBar.setDisabledByAdmin(mEnforcedAdmin);
|
||||
}
|
||||
}
|
||||
|
||||
private void registerListenerToSwitchBar() {
|
||||
for (OnMainSwitchChangeListener listener : mSwitchChangeListeners) {
|
||||
mMainSwitchBar.addOnSwitchChangeListener(listener);
|
||||
|
||||
@@ -23,8 +23,6 @@ import android.view.View;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -80,11 +78,4 @@ public class MainSwitchBarTest {
|
||||
|
||||
assertThat(mBar.getVisibility()).isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disabledByAdmin_shouldDelegateToRestrictedIcon() {
|
||||
mBar.setDisabledByAdmin(new RestrictedLockUtils.EnforcedAdmin());
|
||||
|
||||
assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.restricted_icon);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user