IconListPreference.java revision f5bffc476b8293b30bb500bed2ab7914bb4ca411
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 17package com.android.camera; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.content.res.TypedArray; 22import android.util.AttributeSet; 23 24import com.android.camera.R; 25 26import java.util.List; 27 28/** A {@code ListPreference} where each entry has a corresponding icon. */ 29public class IconListPreference extends ListPreference { 30 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 mIconIds = getIds(res, a.getResourceId( 41 R.styleable.IconListPreference_icons, 0)); 42 mLargeIconIds = getIds(res, a.getResourceId( 43 R.styleable.IconListPreference_largeIcons, 0)); 44 mImageIds = getIds(res, a.getResourceId( 45 R.styleable.IconListPreference_images, 0)); 46 if (mImageIds == null) mImageIds = mLargeIconIds; 47 a.recycle(); 48 } 49 50 public int[] getIconIds() { 51 return mIconIds; 52 } 53 54 public int[] getLargeIconIds() { 55 return mLargeIconIds; 56 } 57 58 public int[] getImageIds() { 59 return mImageIds; 60 } 61 62 public void setIconIds(int[] iconIds) { 63 mIconIds = iconIds; 64 } 65 66 public void setLargeIconIds(int[] largeIconIds) { 67 mLargeIconIds = largeIconIds; 68 } 69 70 private int[] getIds(Resources res, int iconsRes) { 71 if (iconsRes == 0) return null; 72 TypedArray array = res.obtainTypedArray(iconsRes); 73 int n = array.length(); 74 int ids[] = new int[n]; 75 for (int i = 0; i < n; ++i) { 76 ids[i] = array.getResourceId(i, 0); 77 } 78 array.recycle(); 79 return ids; 80 } 81 82 @Override 83 public void filterUnsupported(List<String> supported) { 84 CharSequence entryValues[] = getEntryValues(); 85 IntArray iconIds = new IntArray(); 86 IntArray largeIconIds = new IntArray(); 87 IntArray imageIds = new IntArray(); 88 89 // We allow mIconsIds to be null, but not mLargeIconIds. The reason is that if large icons 90 // are unspecified, the on screen icons will be blank which is a bug. 91 for (int i = 0, len = entryValues.length; i < len; i++) { 92 if (supported.indexOf(entryValues[i].toString()) >= 0) { 93 if (mIconIds != null) iconIds.add(mIconIds[i]); 94 largeIconIds.add(mLargeIconIds[i]); 95 if (mImageIds != null) imageIds.add(mImageIds[i]); 96 } 97 } 98 if (mIconIds != null) mIconIds = iconIds.toArray(new int[iconIds.size()]); 99 mLargeIconIds = largeIconIds.toArray(new int[largeIconIds.size()]); 100 if (mImageIds != null) mImageIds = imageIds.toArray(new int[imageIds.size()]); 101 super.filterUnsupported(supported); 102 } 103} 104