Commit db035f87 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Android] Extract helper methods from ManagedPreferenceDelegate

A follow-up to https://crrev.com/c/1047870.

This CL removes initPreference, onBindViewToPreference and
onClickPreference helper methods from ManagedPreferenceDelegate
interface, adding corresponding static methods to
ManagedPreferencesUtils instead. It also moves null checks into these
methods, making ManagedPreferenceDelegate param @Nullable.

Bug: None
Change-Id: I53e3669962c21e68a00b83dcde38b229d0b44364
Reviewed-on: https://chromium-review.googlesource.com/1057472Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558657}
parent 42a17384
......@@ -29,19 +29,19 @@ public class ChromeBaseCheckBoxPreference extends CheckBoxPreference {
*/
public void setManagedPreferenceDelegate(ManagedPreferenceDelegate delegate) {
mManagedPrefDelegate = delegate;
if (mManagedPrefDelegate != null) mManagedPrefDelegate.initPreference(this);
ManagedPreferencesUtils.initPreference(mManagedPrefDelegate, this);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
((TextView) view.findViewById(android.R.id.title)).setSingleLine(false);
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
ManagedPreferencesUtils.onBindViewToPreference(mManagedPrefDelegate, this, view);
}
@Override
protected void onClick() {
if (mManagedPrefDelegate != null && mManagedPrefDelegate.onClickPreference(this)) return;
if (ManagedPreferencesUtils.onClickPreference(mManagedPrefDelegate, this)) return;
super.onClick();
}
}
......@@ -31,19 +31,19 @@ public class ChromeBaseListPreference extends ListPreference {
*/
public void setManagedPreferenceDelegate(ManagedPreferenceDelegate delegate) {
mManagedPrefDelegate = delegate;
if (mManagedPrefDelegate != null) mManagedPrefDelegate.initPreference(this);
ManagedPreferencesUtils.initPreference(mManagedPrefDelegate, this);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
((TextView) view.findViewById(android.R.id.title)).setSingleLine(false);
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
ManagedPreferencesUtils.onBindViewToPreference(mManagedPrefDelegate, this, view);
}
@Override
protected void onClick() {
if (mManagedPrefDelegate != null && mManagedPrefDelegate.onClickPreference(this)) return;
if (ManagedPreferencesUtils.onClickPreference(mManagedPrefDelegate, this)) return;
super.onClick();
}
......
......@@ -41,19 +41,19 @@ public class ChromeBasePreference extends Preference {
*/
public void setManagedPreferenceDelegate(ManagedPreferenceDelegate delegate) {
mManagedPrefDelegate = delegate;
if (mManagedPrefDelegate != null) mManagedPrefDelegate.initPreference(this);
ManagedPreferencesUtils.initPreference(mManagedPrefDelegate, this);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
((TextView) view.findViewById(android.R.id.title)).setSingleLine(false);
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
ManagedPreferencesUtils.onBindViewToPreference(mManagedPrefDelegate, this, view);
}
@Override
protected void onClick() {
if (mManagedPrefDelegate != null && mManagedPrefDelegate.onClickPreference(this)) return;
if (ManagedPreferencesUtils.onClickPreference(mManagedPrefDelegate, this)) return;
super.onClick();
}
}
......@@ -47,7 +47,7 @@ public class ChromeSwitchPreference extends SwitchPreference {
*/
public void setManagedPreferenceDelegate(ManagedPreferenceDelegate delegate) {
mManagedPrefDelegate = delegate;
if (mManagedPrefDelegate != null) mManagedPrefDelegate.initPreference(this);
ManagedPreferencesUtils.initPreference(mManagedPrefDelegate, this);
}
/**
......@@ -90,12 +90,12 @@ public class ChromeSwitchPreference extends SwitchPreference {
summary.setVisibility(View.GONE);
}
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
ManagedPreferencesUtils.onBindViewToPreference(mManagedPrefDelegate, this, view);
}
@Override
protected void onClick() {
if (mManagedPrefDelegate != null && mManagedPrefDelegate.onClickPreference(this)) return;
if (ManagedPreferencesUtils.onClickPreference(mManagedPrefDelegate, this)) return;
super.onClick();
}
}
......@@ -5,10 +5,6 @@
package org.chromium.chrome.browser.preferences;
import android.preference.Preference;
import android.view.View;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.util.ViewUtils;
/**
* A delegate that determines whether a Preference is managed by enterprise policy. This is used
......@@ -56,72 +52,7 @@ public interface ManagedPreferenceDelegate {
*/
// TODO(bauerb): Rename to isPreferenceClickDisabled.
default boolean isPreferenceClickDisabledByPolicy(Preference preference) {
return isPreferenceControlledByPolicy(preference) ||
isPreferenceControlledByCustodian(preference);
}
/**
* Initializes the Preference based on the state of any policies that may affect it,
* e.g. by showing a managed icon or disabling clicks on the preference.
*
* This should be called once, before the preference is displayed.
*/
default void initPreference(Preference preference) {
if (isPreferenceControlledByPolicy(preference)) {
preference.setIcon(ManagedPreferencesUtils.getManagedByEnterpriseIconId());
} else if (isPreferenceControlledByCustodian(preference)) {
preference.setIcon(R.drawable.ic_account_child_grey600_36dp);
}
if (isPreferenceClickDisabledByPolicy(preference)) {
// Disable the views and prevent the Preference from mucking with the enabled state.
preference.setShouldDisableView(false);
// Prevent default click behavior.
preference.setFragment(null);
preference.setIntent(null);
preference.setOnPreferenceClickListener(null);
}
}
/**
* Disables the Preference's views if the preference is not clickable.
*
* Note: this disables the View instead of disabling the Preference, so that the Preference
* still receives click events, which will trigger a "Managed by your administrator" toast.
*
* This should be called from the Preference's onBindView() method.
*
* @param preference The Preference that owns the view
* @param view The View that was bound to the Preference
*/
default void onBindViewToPreference(Preference preference, View view) {
if (isPreferenceClickDisabledByPolicy(preference)) {
ViewUtils.setEnabledRecursive(view, false);
}
}
/**
* Intercepts the click event if the given Preference is managed and shows a toast in that case.
*
* This should be called from the Preference's onClick() method.
*
* @param preference The Preference that was clicked.
* @return true if the click event was handled by this helper and shouldn't be further
* propagated; false otherwise.
*/
default boolean onClickPreference(Preference preference) {
if (!isPreferenceClickDisabledByPolicy(preference)) return false;
if (isPreferenceControlledByPolicy(preference)) {
ManagedPreferencesUtils.showManagedByAdministratorToast(preference.getContext());
} else if (isPreferenceControlledByCustodian(preference)) {
ManagedPreferencesUtils.showManagedByParentToast(preference.getContext());
} else {
// If the preference is disabled, it should be either because it's managed by enterprise
// policy or by the custodian.
assert false;
}
return true;
return isPreferenceControlledByPolicy(preference)
|| isPreferenceControlledByCustodian(preference);
}
}
\ No newline at end of file
......@@ -5,8 +5,12 @@
package org.chromium.chrome.browser.preferences;
import android.content.Context;
import android.preference.Preference;
import android.support.annotation.Nullable;
import android.view.View;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.util.ViewUtils;
import org.chromium.ui.widget.Toast;
/**
......@@ -46,4 +50,84 @@ public class ManagedPreferencesUtils {
public static int getManagedByEnterpriseIconId() {
return R.drawable.controlled_setting_mandatory;
}
/**
* Initializes the Preference based on the state of any policies that may affect it,
* e.g. by showing a managed icon or disabling clicks on the preference.
*
* This should be called once, before the preference is displayed.
*
* @param delegate The delegate that controls whether the preference is managed. May be null,
* then this method does nothing.
* @param preference The Preference that is being initialized
*/
public static void initPreference(
@Nullable ManagedPreferenceDelegate delegate, Preference preference) {
if (delegate == null) return;
if (delegate.isPreferenceControlledByPolicy(preference)) {
preference.setIcon(getManagedByEnterpriseIconId());
} else if (delegate.isPreferenceControlledByCustodian(preference)) {
preference.setIcon(R.drawable.ic_account_child_grey600_36dp);
}
if (delegate.isPreferenceClickDisabledByPolicy(preference)) {
// Disable the views and prevent the Preference from mucking with the enabled state.
preference.setShouldDisableView(false);
// Prevent default click behavior.
preference.setFragment(null);
preference.setIntent(null);
preference.setOnPreferenceClickListener(null);
}
}
/**
* Disables the Preference's views if the preference is not clickable.
*
* Note: this disables the View instead of disabling the Preference, so that the Preference
* still receives click events, which will trigger a "Managed by your administrator" toast.
*
* This should be called from the Preference's onBindView() method.
*
* @param delegate The delegate that controls whether the preference is managed. May be null,
* then this method does nothing.
* @param preference The Preference that owns the view
* @param view The View that was bound to the Preference
*/
public static void onBindViewToPreference(
@Nullable ManagedPreferenceDelegate delegate, Preference preference, View view) {
if (delegate != null && delegate.isPreferenceClickDisabledByPolicy(preference)) {
ViewUtils.setEnabledRecursive(view, false);
}
}
/**
* Intercepts the click event if the given Preference is managed and shows a toast in that case.
*
* This should be called from the Preference's onClick() method.
*
* @param delegate The delegate that controls whether the preference is managed. May be null,
* then this method does nothing and returns false.
* @param preference The Preference that was clicked.
* @return true if the click event was handled by this helper and shouldn't be further
* propagated; false otherwise.
*/
public static boolean onClickPreference(
@Nullable ManagedPreferenceDelegate delegate, Preference preference) {
if (delegate == null || !delegate.isPreferenceClickDisabledByPolicy(preference)) {
return false;
}
if (delegate.isPreferenceControlledByPolicy(preference)) {
showManagedByAdministratorToast(preference.getContext());
} else if (delegate.isPreferenceControlledByCustodian(preference)) {
showManagedByParentToast(preference.getContext());
} else {
// If the preference is disabled, it should be either because it's managed by enterprise
// policy or by the custodian.
assert false;
}
return true;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment