Merge "Policy transparency dialog should be shown when SwitchBar is restricted." into pi-dev

This commit is contained in:
Yueming Wang
2018-05-02 14:58:47 +00:00
committed by Android (Google) Code Review
2 changed files with 36 additions and 2 deletions

View File

@@ -25,6 +25,7 @@ import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.ColorInt; import android.support.annotation.ColorInt;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.annotation.VisibleForTesting;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.TextAppearanceSpan; import android.text.style.TextAppearanceSpan;
@@ -132,6 +133,17 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
(switchView, isChecked) -> setTextViewLabelAndBackground(isChecked)); (switchView, isChecked) -> setTextViewLabelAndBackground(isChecked));
mRestrictedIcon = findViewById(R.id.restricted_icon); mRestrictedIcon = findViewById(R.id.restricted_icon);
mRestrictedIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDisabledByAdmin) {
mMetricsFeatureProvider.count(mContext,
mMetricsTag + "/switch_bar|restricted", 1);
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context,
mEnforcedAdmin);
}
}
});
// Default is hide // Default is hide
setVisibility(View.GONE); setVisibility(View.GONE);
@@ -196,6 +208,11 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mSwitch.setEnabled(enabled); mSwitch.setEnabled(enabled);
} }
@VisibleForTesting
View getDelegatingView() {
return mDisabledByAdmin ? mRestrictedIcon : mSwitch;
}
/** /**
* If admin is not null, disables the text and switch but keeps the view clickable. * 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 * Otherwise, calls setEnabled which will enables the entire view including
@@ -216,6 +233,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mRestrictedIcon.setVisibility(View.GONE); mRestrictedIcon.setVisibility(View.GONE);
setEnabled(true); setEnabled(true);
} }
setTouchDelegate(new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()),
getDelegatingView()));
} }
public final ToggleSwitch getSwitch() { public final ToggleSwitch getSwitch() {
@@ -228,7 +247,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
mSwitch.setOnCheckedChangeListener(this); mSwitch.setOnCheckedChangeListener(this);
// Make the entire bar work as a switch // Make the entire bar work as a switch
post(() -> setTouchDelegate( post(() -> setTouchDelegate(
new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()), mSwitch))); new TouchDelegate(new Rect(0, 0, getWidth(), getHeight()),
getDelegatingView())));
} }
} }
@@ -242,7 +262,8 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if ((w > 0) && (h > 0)) { if ((w > 0) && (h > 0)) {
setTouchDelegate(new TouchDelegate(new Rect(0, 0, w, h), mSwitch)); setTouchDelegate(new TouchDelegate(new Rect(0, 0, w, h),
getDelegatingView()));
} }
} }

View File

@@ -26,6 +26,7 @@ import android.widget.TextView;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -82,4 +83,16 @@ public class SwitchBarTest {
assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText()) assertThat(((TextView) mBar.findViewById(R.id.switch_text)).getText())
.isEqualTo(mContext.getString(onText)); .isEqualTo(mContext.getString(onText));
} }
@Test
public void disabledByAdmin_shouldDelegateToRestrictedIcon() {
mBar.setDisabledByAdmin(new EnforcedAdmin());
assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.restricted_icon);
}
@Test
public void notDisabledByAdmin_shouldDelegateToSwitch() {
mBar.setDisabledByAdmin(null);
assertThat(mBar.getDelegatingView().getId()).isEqualTo(R.id.switch_widget);
}
} }