blob: 02b68d8ccbc521ff94108c3d3db8fadbc37680b8 [file] [log] [blame]
Fan Zhang035ff932017-03-22 10:54:03 -07001/*
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
17package com.android.settingslib;
18
Fan Zhang874425d2018-04-06 15:55:12 -070019import android.annotation.IntDef;
Fan Zhang035ff932017-03-22 10:54:03 -070020import android.content.Context;
21import android.support.v7.preference.Preference;
22import android.support.v7.preference.PreferenceViewHolder;
23import android.util.AttributeSet;
24import android.view.View;
Fan Zhanga93c40e2017-10-29 12:58:21 -070025import android.widget.ImageView;
26import android.widget.LinearLayout;
Fan Zhang035ff932017-03-22 10:54:03 -070027
Fan Zhang874425d2018-04-06 15:55:12 -070028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
Fan Zhang035ff932017-03-22 10:54:03 -070031public class TwoTargetPreference extends Preference {
32
Fan Zhang874425d2018-04-06 15:55:12 -070033 @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 Zhanga93c40e2017-10-29 12:58:21 -070044 private int mSmallIconSize;
Fan Zhang874425d2018-04-06 15:55:12 -070045 private int mMediumIconSize;
Fan Zhanga93c40e2017-10-29 12:58:21 -070046
Fan Zhang035ff932017-03-22 10:54:03 -070047 public TwoTargetPreference(Context context, AttributeSet attrs,
48 int defStyleAttr, int defStyleRes) {
49 super(context, attrs, defStyleAttr, defStyleRes);
Fan Zhanga93c40e2017-10-29 12:58:21 -070050 init(context);
Fan Zhang035ff932017-03-22 10:54:03 -070051 }
52
53 public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) {
54 super(context, attrs, defStyleAttr);
Fan Zhanga93c40e2017-10-29 12:58:21 -070055 init(context);
Fan Zhang035ff932017-03-22 10:54:03 -070056 }
57
58 public TwoTargetPreference(Context context, AttributeSet attrs) {
59 super(context, attrs);
Fan Zhanga93c40e2017-10-29 12:58:21 -070060 init(context);
Fan Zhang035ff932017-03-22 10:54:03 -070061 }
62
63 public TwoTargetPreference(Context context) {
64 super(context);
Fan Zhanga93c40e2017-10-29 12:58:21 -070065 init(context);
Fan Zhang035ff932017-03-22 10:54:03 -070066 }
67
Fan Zhanga93c40e2017-10-29 12:58:21 -070068 private void init(Context context) {
Fan Zhang035ff932017-03-22 10:54:03 -070069 setLayoutResource(R.layout.preference_two_target);
Fan Zhanga93c40e2017-10-29 12:58:21 -070070 mSmallIconSize = context.getResources().getDimensionPixelSize(
71 R.dimen.two_target_pref_small_icon_size);
Fan Zhang874425d2018-04-06 15:55:12 -070072 mMediumIconSize = context.getResources().getDimensionPixelSize(
73 R.dimen.two_target_pref_medium_icon_size);
Fan Zhang035ff932017-03-22 10:54:03 -070074 final int secondTargetResId = getSecondTargetResId();
75 if (secondTargetResId != 0) {
76 setWidgetLayoutResource(secondTargetResId);
77 }
78 }
79
Fan Zhang874425d2018-04-06 15:55:12 -070080 public void setIconSize(@IconSize int iconSize) {
81 mIconSize = iconSize;
Fan Zhanga93c40e2017-10-29 12:58:21 -070082 }
83
Fan Zhang035ff932017-03-22 10:54:03 -070084 @Override
85 public void onBindViewHolder(PreferenceViewHolder holder) {
86 super.onBindViewHolder(holder);
Fan Zhang874425d2018-04-06 15:55:12 -070087 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 Zhanga93c40e2017-10-29 12:58:21 -070096 }
Fan Zhang035ff932017-03-22 10:54:03 -070097 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}