1 /* 2 * Copyright (C) 2009 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.camera; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.content.res.TypedArray; 22 import android.util.AttributeSet; 23 24 import java.util.List; 25 26 /** A {@code ListPreference} where each entry has a corresponding icon. */ 27 public class IconListPreference extends ListPreference { 28 private int mSingleIconId; 29 private int mIconIds[]; 30 private int mLargeIconIds[]; 31 private int mImageIds[]; 32 private boolean mUseSingleIcon; 33 34 public IconListPreference(Context context, AttributeSet attrs) { 35 super(context, attrs); 36 TypedArray a = context.obtainStyledAttributes( 37 attrs, R.styleable.IconListPreference, 0, 0); 38 Resources res = context.getResources(); 39 mSingleIconId = a.getResourceId( 40 R.styleable.IconListPreference_singleIcon, 0); 41 mIconIds = getIds(res, a.getResourceId( 42 R.styleable.IconListPreference_icons, 0)); 43 mLargeIconIds = getIds(res, a.getResourceId( 44 R.styleable.IconListPreference_largeIcons, 0)); 45 mImageIds = getIds(res, a.getResourceId( 46 R.styleable.IconListPreference_images, 0)); 47 a.recycle(); 48 } 49 50 public int getSingleIcon() { 51 return mSingleIconId; 52 } 53 54 public int[] getIconIds() { 55 return mIconIds; 56 } 57 58 public int[] getLargeIconIds() { 59 return mLargeIconIds; 60 } 61 62 public int[] getImageIds() { 63 return mImageIds; 64 } 65 66 public boolean getUseSingleIcon() { 67 return mUseSingleIcon; 68 } 69 70 public void setIconIds(int[] iconIds) { 71 mIconIds = iconIds; 72 } 73 74 public void setLargeIconIds(int[] largeIconIds) { 75 mLargeIconIds = largeIconIds; 76 } 77 78 public void setUseSingleIcon(boolean useSingle) { 79 mUseSingleIcon = useSingle; 80 } 81 82 private int[] getIds(Resources res, int iconsRes) { 83 if (iconsRes == 0) return null; 84 TypedArray array = res.obtainTypedArray(iconsRes); 85 int n = array.length(); 86 int ids[] = new int[n]; 87 for (int i = 0; i < n; ++i) { 88 ids[i] = array.getResourceId(i, 0); 89 } 90 array.recycle(); 91 return ids; 92 } 93 94 @Override 95 public void filterUnsupported(List<String> supported) { 96 CharSequence entryValues[] = getEntryValues(); 97 IntArray iconIds = new IntArray(); 98 IntArray largeIconIds = new IntArray(); 99 IntArray imageIds = new IntArray(); 100 101 for (int i = 0, len = entryValues.length; i < len; i++) { 102 if (supported.indexOf(entryValues[i].toString()) >= 0) { 103 if (mIconIds != null) iconIds.add(mIconIds[i]); 104 if (mLargeIconIds != null) largeIconIds.add(mLargeIconIds[i]); 105 if (mImageIds != null) imageIds.add(mImageIds[i]); 106 } 107 } 108 if (mIconIds != null) mIconIds = iconIds.toArray(new int[iconIds.size()]); 109 if (mLargeIconIds != null) { 110 mLargeIconIds = largeIconIds.toArray(new int[largeIconIds.size()]); 111 } 112 if (mImageIds != null) mImageIds = imageIds.toArray(new int[imageIds.size()]); 113 super.filterUnsupported(supported); 114 } 115 } 116