Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 873f1e96 authored by Fan Zhang's avatar Fan Zhang Committed by android-build-merger
Browse files

Merge "Allow 3 variants of icon size in TwoTargetPreference." into pi-dev

am: e5b621a6

Change-Id: Iab570d7d755d56637c337d8b64b5a0f752fa9c09
parents b70bb9f7 e5b621a6
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
    <dimen name="user_spinner_item_height">56dp</dimen>

    <dimen name="two_target_pref_small_icon_size">24dp</dimen>
    <dimen name="two_target_pref_medium_icon_size">32dp</dimen>

    <!-- Lock icon for preferences locked by admin -->
    <dimen name="restricted_icon_size">16dp</dimen>
    <dimen name="restricted_icon_padding">4dp</dimen>
+29 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib;

import android.annotation.IntDef;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
@@ -24,10 +25,24 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class TwoTargetPreference extends Preference {

    private boolean mUseSmallIcon;
    @IntDef({ICON_SIZE_DEFAULT, ICON_SIZE_MEDIUM, ICON_SIZE_SMALL})
    @Retention(RetentionPolicy.SOURCE)
    public @interface IconSize {
    }

    public static final int ICON_SIZE_DEFAULT = 0;
    public static final int ICON_SIZE_MEDIUM = 1;
    public static final int ICON_SIZE_SMALL = 2;

    @IconSize
    private int mIconSize;
    private int mSmallIconSize;
    private int mMediumIconSize;

    public TwoTargetPreference(Context context, AttributeSet attrs,
            int defStyleAttr, int defStyleRes) {
@@ -54,22 +69,30 @@ public class TwoTargetPreference extends Preference {
        setLayoutResource(R.layout.preference_two_target);
        mSmallIconSize = context.getResources().getDimensionPixelSize(
                R.dimen.two_target_pref_small_icon_size);
        mMediumIconSize = context.getResources().getDimensionPixelSize(
                R.dimen.two_target_pref_medium_icon_size);
        final int secondTargetResId = getSecondTargetResId();
        if (secondTargetResId != 0) {
            setWidgetLayoutResource(secondTargetResId);
        }
    }

    public void setUseSmallIcon(boolean useSmallIcon) {
        mUseSmallIcon = useSmallIcon;
    public void setIconSize(@IconSize int iconSize) {
        mIconSize = iconSize;
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        if (mUseSmallIcon) {
            ImageView icon = holder.itemView.findViewById(android.R.id.icon);
        final ImageView icon = holder.itemView.findViewById(android.R.id.icon);
        switch (mIconSize) {
            case ICON_SIZE_SMALL:
                icon.setLayoutParams(new LinearLayout.LayoutParams(mSmallIconSize, mSmallIconSize));
                break;
            case ICON_SIZE_MEDIUM:
                icon.setLayoutParams(
                        new LinearLayout.LayoutParams(mMediumIconSize, mMediumIconSize));
                break;
        }
        final View divider = holder.findViewById(R.id.two_target_divider);
        final View widgetFrame = holder.findViewById(android.R.id.widget_frame);
+23 −4
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.settingslib;

import static com.android.settingslib.TwoTargetPreference.ICON_SIZE_DEFAULT;
import static com.android.settingslib.TwoTargetPreference.ICON_SIZE_MEDIUM;
import static com.android.settingslib.TwoTargetPreference.ICON_SIZE_SMALL;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@@ -75,8 +78,8 @@ public class TwoTargetPreferenceTest {
    }

    @Test
    public void bind_smallIcon_shouldUseSmallIcon() {
        mPreference.setUseSmallIcon(true);
    public void bind_smallIcon_shouldUseSmallIconSize() {
        mPreference.setIconSize(ICON_SIZE_SMALL);

        mPreference.onBindViewHolder(mViewHolder);

@@ -91,8 +94,24 @@ public class TwoTargetPreferenceTest {
    }

    @Test
    public void bind_normalIcon_shouldUseNormalIcon() {
        mPreference.setUseSmallIcon(false);
    public void bind_mediumIcon_shouldUseMediumIconSize() {
        mPreference.setIconSize(ICON_SIZE_MEDIUM);

        mPreference.onBindViewHolder(mViewHolder);

        final int size = mContext.getResources().getDimensionPixelSize(
                R.dimen.two_target_pref_medium_icon_size);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mViewHolder
                .findViewById(android.R.id.icon)
                .getLayoutParams();

        assertThat(layoutParams.width).isEqualTo(size);
        assertThat(layoutParams.height).isEqualTo(size);
    }

    @Test
    public void bind_defaultIcon_shouldUseDefaultIconSize() {
        mPreference.setIconSize(ICON_SIZE_DEFAULT);

        mPreference.onBindViewHolder(mViewHolder);