1 /* 2 * Copyright (C) 2020 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.settings.accessibility; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.View; 22 import android.view.ViewTreeObserver; 23 import android.widget.FrameLayout; 24 import android.widget.ListView; 25 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 /** Preference that easier preview by matching name to color. */ 32 public class PaletteListPreference extends Preference { 33 34 private ListView mListView; 35 private ViewTreeObserver.OnPreDrawListener mPreDrawListener; 36 37 /** 38 * Constructs a new PaletteListPreference with the given context's theme and the supplied 39 * attribute set. 40 * 41 * @param context The Context this is associated with, through which it can access the current 42 * theme, resources, etc. 43 * @param attrs The attributes of the XML tag that is inflating the view. 44 */ PaletteListPreference(Context context, AttributeSet attrs)45 public PaletteListPreference(Context context, AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 49 /** 50 * Constructs a new PaletteListPreference with the given context's theme, the supplied 51 * attribute set, and default style attribute. 52 * 53 * @param context The Context this is associated with, through which it can access the 54 * current theme, resources, etc. 55 * @param attrs The attributes of the XML tag that is inflating the view. 56 * @param defStyleAttr An attribute in the current theme that contains a reference to a style 57 * resource that supplies default 58 * values for the view. Can be 0 to not look for 59 * defaults. 60 */ PaletteListPreference(Context context, AttributeSet attrs, int defStyleAttr)61 public PaletteListPreference(Context context, AttributeSet attrs, int defStyleAttr) { 62 super(context, attrs, defStyleAttr); 63 setLayoutResource(R.layout.daltonizer_preview); 64 initPreDrawListener(); 65 } 66 67 @Override onBindViewHolder(PreferenceViewHolder holder)68 public void onBindViewHolder(PreferenceViewHolder holder) { 69 super.onBindViewHolder(holder); 70 71 final View rootView = holder.itemView; 72 mListView = rootView.findViewById(R.id.palette_listView); 73 if (mPreDrawListener != null) { 74 mListView.getViewTreeObserver().addOnPreDrawListener(mPreDrawListener); 75 } 76 } 77 initPreDrawListener()78 private void initPreDrawListener() { 79 mPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { 80 @Override 81 public boolean onPreDraw() { 82 if (mListView == null) { 83 return false; 84 } 85 86 final int listViewHeight = mListView.getMeasuredHeight(); 87 final int listViewWidth = mListView.getMeasuredWidth(); 88 89 // Removes the callback after get result of measure view. 90 final ViewTreeObserver viewTreeObserver = mListView.getViewTreeObserver(); 91 if (viewTreeObserver.isAlive()) { 92 viewTreeObserver.removeOnPreDrawListener(this); 93 } 94 mPreDrawListener = null; 95 96 // Resets layout parameters to display whole items from listView. 97 final FrameLayout.LayoutParams layoutParams = 98 (FrameLayout.LayoutParams) mListView.getLayoutParams(); 99 layoutParams.height = listViewHeight * mListView.getAdapter().getCount(); 100 layoutParams.width = listViewWidth; 101 mListView.setLayoutParams(layoutParams); 102 103 return true; 104 } 105 }; 106 } 107 } 108