Home | History | Annotate | Download | only in photoeditor
      1 /*
      2  * Copyright (C) 2010 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.photoeditor;
     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 /**
     27  * Represents image icons/buttons having various modes.
     28  */
     29 public class IconIndicator extends ImageView {
     30 
     31     private Drawable[] mIcons;
     32     private CharSequence[] mModes;
     33 
     34     public IconIndicator(Context context, AttributeSet attrs, int defStyle) {
     35         super(context, attrs, defStyle);
     36         TypedArray a = context
     37             .obtainStyledAttributes(attrs, R.styleable.IconIndicator, defStyle, 0);
     38         Drawable icons[] = loadIcons(context.getResources(), a.getResourceId(
     39             R.styleable.IconIndicator_icons, 0));
     40         CharSequence modes[] = a.getTextArray(R.styleable.IconIndicator_modes);
     41         a.recycle();
     42 
     43         setModesAndIcons(modes, icons);
     44         setImageDrawable(mIcons.length > 0 ? mIcons[0] : null);
     45     }
     46 
     47     public IconIndicator(Context context, AttributeSet attrs) {
     48         this(context, attrs, 0);
     49     }
     50 
     51     private Drawable[] loadIcons(Resources resources, int iconsId) {
     52         TypedArray array = resources.obtainTypedArray(iconsId);
     53         int n = array.length();
     54         Drawable drawable[] = new Drawable[n];
     55         for (int i = 0; i < n; ++i) {
     56             int id = array.getResourceId(i, 0);
     57             drawable[i] = id == 0 ? null : resources.getDrawable(id);
     58         }
     59         array.recycle();
     60         return drawable;
     61     }
     62 
     63     private void setModesAndIcons(CharSequence[] modes, Drawable icons[]) {
     64         if (modes.length != icons.length || icons.length == 0) {
     65             throw new IllegalArgumentException();
     66         }
     67         mIcons = icons;
     68         mModes = modes;
     69     }
     70 
     71     public void setMode(String mode) {
     72         for (int i = 0, n = mModes.length; i < n; ++i) {
     73             if (mModes[i].equals(mode)) {
     74                 setImageDrawable(mIcons[i]);
     75                 return;
     76             }
     77         }
     78         throw new IllegalArgumentException("unknown mode: " + mode);
     79     }
     80 }
     81