Home | History | Annotate | Download | only in settingslib
      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 
     19 import android.annotation.IntDef;
     20 import android.content.Context;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.widget.ImageView;
     24 import android.widget.LinearLayout;
     25 
     26 import androidx.preference.Preference;
     27 import androidx.preference.PreferenceViewHolder;
     28 
     29 import java.lang.annotation.Retention;
     30 import java.lang.annotation.RetentionPolicy;
     31 
     32 public class TwoTargetPreference extends Preference {
     33 
     34     @IntDef({ICON_SIZE_DEFAULT, ICON_SIZE_MEDIUM, ICON_SIZE_SMALL})
     35     @Retention(RetentionPolicy.SOURCE)
     36     public @interface IconSize {
     37     }
     38 
     39     public static final int ICON_SIZE_DEFAULT = 0;
     40     public static final int ICON_SIZE_MEDIUM = 1;
     41     public static final int ICON_SIZE_SMALL = 2;
     42 
     43     @IconSize
     44     private int mIconSize;
     45     private int mSmallIconSize;
     46     private int mMediumIconSize;
     47 
     48     public TwoTargetPreference(Context context, AttributeSet attrs,
     49             int defStyleAttr, int defStyleRes) {
     50         super(context, attrs, defStyleAttr, defStyleRes);
     51         init(context);
     52     }
     53 
     54     public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     55         super(context, attrs, defStyleAttr);
     56         init(context);
     57     }
     58 
     59     public TwoTargetPreference(Context context, AttributeSet attrs) {
     60         super(context, attrs);
     61         init(context);
     62     }
     63 
     64     public TwoTargetPreference(Context context) {
     65         super(context);
     66         init(context);
     67     }
     68 
     69     private void init(Context context) {
     70         setLayoutResource(R.layout.preference_two_target);
     71         mSmallIconSize = context.getResources().getDimensionPixelSize(
     72                 R.dimen.two_target_pref_small_icon_size);
     73         mMediumIconSize = context.getResources().getDimensionPixelSize(
     74                 R.dimen.two_target_pref_medium_icon_size);
     75         final int secondTargetResId = getSecondTargetResId();
     76         if (secondTargetResId != 0) {
     77             setWidgetLayoutResource(secondTargetResId);
     78         }
     79     }
     80 
     81     public void setIconSize(@IconSize int iconSize) {
     82         mIconSize = iconSize;
     83     }
     84 
     85     @Override
     86     public void onBindViewHolder(PreferenceViewHolder holder) {
     87         super.onBindViewHolder(holder);
     88         final ImageView icon = holder.itemView.findViewById(android.R.id.icon);
     89         switch (mIconSize) {
     90             case ICON_SIZE_SMALL:
     91                 icon.setLayoutParams(new LinearLayout.LayoutParams(mSmallIconSize, mSmallIconSize));
     92                 break;
     93             case ICON_SIZE_MEDIUM:
     94                 icon.setLayoutParams(
     95                         new LinearLayout.LayoutParams(mMediumIconSize, mMediumIconSize));
     96                 break;
     97         }
     98         final View divider = holder.findViewById(R.id.two_target_divider);
     99         final View widgetFrame = holder.findViewById(android.R.id.widget_frame);
    100         final boolean shouldHideSecondTarget = shouldHideSecondTarget();
    101         if (divider != null) {
    102             divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
    103         }
    104         if (widgetFrame != null) {
    105             widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
    106         }
    107     }
    108 
    109     protected boolean shouldHideSecondTarget() {
    110         return getSecondTargetResId() == 0;
    111     }
    112 
    113     protected int getSecondTargetResId() {
    114         return 0;
    115     }
    116 }
    117