IconListPreference.java revision 7add00693c1ec910bc8700fe046ee18cbe4e1148
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
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    public void setLargeIconIds(int[] largeIconIds) {
55        mLargeIconIds = largeIconIds;
56    }
57
58    public void setIconIds(int[] iconIds) {
59        mIconIds = iconIds;
60    }
61
62    private int[] getIconIds(Resources res, int iconsRes) {
63        if (iconsRes == 0) return null;
64        TypedArray array = res.obtainTypedArray(iconsRes);
65        int n = array.length();
66        int ids[] = new int[n];
67        for (int i = 0; i < n; ++i) {
68            ids[i] = array.getResourceId(i, 0);
69        }
70        array.recycle();
71        return ids;
72    }
73
74    @Override
75    public void filterUnsupported(List<String> supported) {
76        CharSequence entryValues[] = getEntryValues();
77        IntArray iconIds = new IntArray();
78        IntArray largeIconIds = new IntArray();
79
80        for (int i = 0, len = entryValues.length; i < len; i++) {
81            if (supported.indexOf(entryValues[i].toString()) >= 0) {
82                iconIds.add(mIconIds[i]);
83                largeIconIds.add(mLargeIconIds[i]);
84            }
85        }
86        int size = iconIds.size();
87        mIconIds = iconIds.toArray(new int[size]);
88        mLargeIconIds = iconIds.toArray(new int[size]);
89        super.filterUnsupported(supported);
90    }
91}
92