IconListPreference.java revision 3889cf31101cfd4d336f1ce5ae5122c2cb3c0fdc
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.graphics.drawable.Drawable;
23import android.util.AttributeSet;
24
25import com.google.android.camera.R;
26
27/** A {@code ListPreference} where each entry has a corresponding icon. */
28public class IconListPreference extends ListPreference {
29
30    private final int mIconIds[];
31    private final int mLargeIconIds[];
32
33    private Drawable mIcons[];
34    private final Resources mResources;
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        mResources = context.getResources();
41        mIconIds = getIconIds(a.getResourceId(
42                R.styleable.IconListPreference_icons, 0));
43        mLargeIconIds = getIconIds(a.getResourceId(
44                R.styleable.IconListPreference_largeIcons, 0));
45        a.recycle();
46    }
47
48    public Drawable[] getIcons() {
49        if (mIcons == null) {
50            int n = mIconIds.length;
51            Drawable[] drawable = new Drawable[n];
52            int[] id = mIconIds;
53            for (int i = 0; i < n; ++i) {
54                drawable[i] = id[i] == 0 ? null : mResources.getDrawable(id[i]);
55            }
56            mIcons = drawable;
57        }
58        return mIcons;
59    }
60
61    public int[] getLargeIconIds() {
62        return mLargeIconIds;
63    }
64
65    public int[] getIconIds() {
66        return mIconIds;
67    }
68
69    private int[] getIconIds(int iconsRes) {
70        if (iconsRes == 0) return null;
71        TypedArray array = mResources.obtainTypedArray(iconsRes);
72        int n = array.length();
73        int ids[] = new int[n];
74        for (int i = 0; i < n; ++i) {
75            ids[i] = array.getResourceId(i, 0);
76        }
77        array.recycle();
78        return ids;
79    }
80
81    public void setIcons(Drawable[] icons) {
82        mIcons = icons;
83    }
84}
85