Home | History | Annotate | Download | only in camera
      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 
     31     private int mIconIds[];
     32     private int mLargeIconIds[];
     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         mIconIds = getIconIds(res, a.getResourceId(
     40                 R.styleable.IconListPreference_icons, 0));
     41         mLargeIconIds = getIconIds(res, a.getResourceId(
     42                 R.styleable.IconListPreference_largeIcons, 0));
     43         a.recycle();
     44     }
     45 
     46     public int[] getLargeIconIds() {
     47         return mLargeIconIds;
     48     }
     49 
     50     public int[] getIconIds() {
     51         return mIconIds;
     52     }
     53 
     54     private int[] getIconIds(Resources res, int iconsRes) {
     55         if (iconsRes == 0) return null;
     56         TypedArray array = res.obtainTypedArray(iconsRes);
     57         int n = array.length();
     58         int ids[] = new int[n];
     59         for (int i = 0; i < n; ++i) {
     60             ids[i] = array.getResourceId(i, 0);
     61         }
     62         array.recycle();
     63         return ids;
     64     }
     65 
     66     @Override
     67     public void filterUnsupported(List<String> supported) {
     68         CharSequence entryValues[] = getEntryValues();
     69         IntArray iconIds = new IntArray();
     70         IntArray largeIconIds = new IntArray();
     71 
     72         for (int i = 0, len = entryValues.length; i < len; i++) {
     73             if (supported.indexOf(entryValues[i].toString()) >= 0) {
     74                 iconIds.add(mIconIds[i]);
     75                 largeIconIds.add(mLargeIconIds[i]);
     76             }
     77         }
     78         int size = iconIds.size();
     79         mIconIds = iconIds.toArray(new int[size]);
     80         mLargeIconIds = iconIds.toArray(new int[size]);
     81         super.filterUnsupported(supported);
     82     }
     83 }
     84