Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settingslib; |
| 18 | |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.support.v7.preference.Preference; |
| 22 | import android.support.v7.preference.PreferenceViewHolder; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.View; |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 25 | import android.widget.ImageView; |
| 26 | import android.widget.LinearLayout; |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 27 | |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
| 30 | |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 31 | public class TwoTargetPreference extends Preference { |
| 32 | |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 33 | @IntDef({ICON_SIZE_DEFAULT, ICON_SIZE_MEDIUM, ICON_SIZE_SMALL}) |
| 34 | @Retention(RetentionPolicy.SOURCE) |
| 35 | public @interface IconSize { |
| 36 | } |
| 37 | |
| 38 | public static final int ICON_SIZE_DEFAULT = 0; |
| 39 | public static final int ICON_SIZE_MEDIUM = 1; |
| 40 | public static final int ICON_SIZE_SMALL = 2; |
| 41 | |
| 42 | @IconSize |
| 43 | private int mIconSize; |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 44 | private int mSmallIconSize; |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 45 | private int mMediumIconSize; |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 46 | |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 47 | public TwoTargetPreference(Context context, AttributeSet attrs, |
| 48 | int defStyleAttr, int defStyleRes) { |
| 49 | super(context, attrs, defStyleAttr, defStyleRes); |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 50 | init(context); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 54 | super(context, attrs, defStyleAttr); |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 55 | init(context); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public TwoTargetPreference(Context context, AttributeSet attrs) { |
| 59 | super(context, attrs); |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 60 | init(context); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public TwoTargetPreference(Context context) { |
| 64 | super(context); |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 65 | init(context); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 68 | private void init(Context context) { |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 69 | setLayoutResource(R.layout.preference_two_target); |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 70 | mSmallIconSize = context.getResources().getDimensionPixelSize( |
| 71 | R.dimen.two_target_pref_small_icon_size); |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 72 | mMediumIconSize = context.getResources().getDimensionPixelSize( |
| 73 | R.dimen.two_target_pref_medium_icon_size); |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 74 | final int secondTargetResId = getSecondTargetResId(); |
| 75 | if (secondTargetResId != 0) { |
| 76 | setWidgetLayoutResource(secondTargetResId); |
| 77 | } |
| 78 | } |
| 79 | |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 80 | public void setIconSize(@IconSize int iconSize) { |
| 81 | mIconSize = iconSize; |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public void onBindViewHolder(PreferenceViewHolder holder) { |
| 86 | super.onBindViewHolder(holder); |
Fan Zhang | 874425d | 2018-04-06 15:55:12 -0700 | [diff] [blame] | 87 | final ImageView icon = holder.itemView.findViewById(android.R.id.icon); |
| 88 | switch (mIconSize) { |
| 89 | case ICON_SIZE_SMALL: |
| 90 | icon.setLayoutParams(new LinearLayout.LayoutParams(mSmallIconSize, mSmallIconSize)); |
| 91 | break; |
| 92 | case ICON_SIZE_MEDIUM: |
| 93 | icon.setLayoutParams( |
| 94 | new LinearLayout.LayoutParams(mMediumIconSize, mMediumIconSize)); |
| 95 | break; |
Fan Zhang | a93c40e | 2017-10-29 12:58:21 -0700 | [diff] [blame] | 96 | } |
Fan Zhang | 035ff93 | 2017-03-22 10:54:03 -0700 | [diff] [blame] | 97 | final View divider = holder.findViewById(R.id.two_target_divider); |
| 98 | final View widgetFrame = holder.findViewById(android.R.id.widget_frame); |
| 99 | final boolean shouldHideSecondTarget = shouldHideSecondTarget(); |
| 100 | if (divider != null) { |
| 101 | divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); |
| 102 | } |
| 103 | if (widgetFrame != null) { |
| 104 | widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | protected boolean shouldHideSecondTarget() { |
| 109 | return getSecondTargetResId() == 0; |
| 110 | } |
| 111 | |
| 112 | protected int getSecondTargetResId() { |
| 113 | return 0; |
| 114 | } |
| 115 | } |