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