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