Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 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 | |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.content.res.TypedArray; |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.widget.ImageView; |
| 25 | |
Owen Lin | 37acf79 | 2009-09-17 17:55:37 +0800 | [diff] [blame] | 26 | public class IconIndicator extends ImageView { |
| 27 | |
| 28 | private Drawable[] mIcons; |
| 29 | private CharSequence[] mModes; |
| 30 | |
| 31 | public IconIndicator(Context context, AttributeSet attrs, int defStyle) { |
| 32 | super(context, attrs, defStyle); |
| 33 | TypedArray a = context.obtainStyledAttributes( |
| 34 | attrs, R.styleable.IconIndicator, defStyle, 0); |
| 35 | Drawable icons[] = loadIcons(context.getResources(), |
| 36 | a.getResourceId(R.styleable.IconIndicator_icons, 0)); |
| 37 | CharSequence modes[] = |
| 38 | a.getTextArray(R.styleable.IconIndicator_modes); |
| 39 | a.recycle(); |
| 40 | |
| 41 | setModesAndIcons(modes, icons); |
| 42 | setImageDrawable(mIcons.length > 0 ? mIcons[0]: null); |
| 43 | } |
| 44 | |
| 45 | public IconIndicator(Context context, AttributeSet attrs) { |
| 46 | this(context, attrs, 0); |
| 47 | } |
| 48 | |
| 49 | private Drawable[] loadIcons(Resources resources, int iconsId) { |
| 50 | TypedArray array = resources.obtainTypedArray(iconsId); |
| 51 | int n = array.length(); |
| 52 | Drawable drawable[] = new Drawable[n]; |
| 53 | for (int i = 0; i < n; ++i) { |
| 54 | int id = array.getResourceId(i, 0); |
| 55 | drawable[i] = id == 0 ? null : resources.getDrawable(id); |
| 56 | } |
| 57 | array.recycle(); |
| 58 | return drawable; |
| 59 | } |
| 60 | |
| 61 | private void setModesAndIcons(CharSequence[] modes, Drawable icons[]) { |
| 62 | if (modes.length != icons.length || icons.length == 0) { |
| 63 | throw new IllegalArgumentException(); |
| 64 | } |
| 65 | mIcons = icons; |
| 66 | mModes = modes; |
| 67 | } |
| 68 | |
| 69 | public void setMode(String mode) { |
| 70 | for (int i = 0, n = mModes.length; i < n; ++i) { |
| 71 | if (mModes[i].equals(mode)) { |
| 72 | setImageDrawable(mIcons[i]); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | throw new IllegalArgumentException("unknown mode: " + mode); |
| 77 | } |
| 78 | } |