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.graphics.drawable.Drawable;
     23 import android.util.AttributeSet;
     24 import android.widget.ImageView;
     25 
     26 import com.android.camera.R;
     27 
     28 /**
     29  * This class draws an icon which changes according to the mode. For example,
     30  * The flash icon can have on, off, and auto modes. The user can use
     31  * {@link #setMode(String)} to change the mode (and the icon).
     32  */
     33 public class IconIndicator extends ImageView {
     34 
     35     private Drawable[] mIcons;
     36     private CharSequence[] mModes;
     37 
     38     public IconIndicator(Context context, AttributeSet attrs, int defStyle) {
     39         super(context, attrs, defStyle);
     40         TypedArray a = context.obtainStyledAttributes(
     41                 attrs, R.styleable.IconIndicator, defStyle, 0);
     42         Drawable icons[] = loadIcons(context.getResources(),
     43                 a.getResourceId(R.styleable.IconIndicator_icons, 0));
     44         CharSequence modes[] =
     45                 a.getTextArray(R.styleable.IconIndicator_modes);
     46         a.recycle();
     47 
     48         setModesAndIcons(modes, icons);
     49         setImageDrawable(mIcons.length > 0 ? mIcons[0] : null);
     50     }
     51 
     52     public IconIndicator(Context context, AttributeSet attrs) {
     53         this(context, attrs, 0);
     54     }
     55 
     56     private Drawable[] loadIcons(Resources resources, int iconsId) {
     57         TypedArray array = resources.obtainTypedArray(iconsId);
     58         int n = array.length();
     59         Drawable drawable[] = new Drawable[n];
     60         for (int i = 0; i < n; ++i) {
     61             int id = array.getResourceId(i, 0);
     62             drawable[i] = id == 0 ? null : resources.getDrawable(id);
     63         }
     64         array.recycle();
     65         return drawable;
     66     }
     67 
     68     private void setModesAndIcons(CharSequence[] modes, Drawable icons[]) {
     69         if (modes.length != icons.length || icons.length == 0) {
     70             throw new IllegalArgumentException();
     71         }
     72         mIcons = icons;
     73         mModes = modes;
     74     }
     75 
     76     public void setMode(String mode) {
     77         for (int i = 0, n = mModes.length; i < n; ++i) {
     78             if (mModes[i].equals(mode)) {
     79                 setImageDrawable(mIcons[i]);
     80                 return;
     81             }
     82         }
     83         throw new IllegalArgumentException("unknown mode: " + mode);
     84     }
     85 }
     86