2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.widget.ImageButton;
24 import android.view.View;
25 import android.view.accessibility.AccessibilityEvent;
27 import com.android.camera2.R;
30 * A toggle button that supports two or more states with images rendererd on top
32 * The button is initialized in an XML layout file with an array reference of
33 * image ids (e.g. imageIds="@array/camera_flashmode_icons").
34 * Each image in the referenced array represents a single integer state.
35 * Every time the user touches the button it gets set to next state in line,
36 * with the corresponding image drawn onto the face of the button.
37 * State wraps back to 0 on user touch when button is already at n-1 state.
39 public class MultiToggleImageButton extends ImageButton {
41 * Listener inteface for button state changes.
43 public interface OnStateChangeListener {
45 * @param view the MultiToggleImageButton that received the touch event
46 * @param state the new state the button is in
48 public abstract void stateChanged(View view, int state);
51 private OnStateChangeListener mOnStateChangeListener;
53 private int[] mImageIds;
54 private int[] mDescIds;
57 public MultiToggleImageButton(Context context) {
62 public MultiToggleImageButton(Context context, AttributeSet attrs) {
63 super(context, attrs);
65 parseAttributes(context, attrs);
69 public MultiToggleImageButton(Context context, AttributeSet attrs, int defStyle) {
70 super(context, attrs, defStyle);
72 parseAttributes(context, attrs);
77 * Set the state change listener.
79 * @param onStateChangeListener the listener to set
81 public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {
82 mOnStateChangeListener = onStateChangeListener;
86 * Get the current button state.
89 public int getState() {
94 * Set the current button state, thus causing the state change listener to
97 * @param state the desired state
99 public void setState(int state) {
100 setState(state, true);
104 * Set the current button state.
106 * @param state the desired state
107 * @param callListener should the state change listener be called?
109 public void setState(int state, boolean callListener) {
111 setImageResource(mImageIds[mState]);
112 String oldContentDescription = String.valueOf(getContentDescription());
113 String newContentDescription = getResources().getString(mDescIds[mState]);
114 if (oldContentDescription != null && !oldContentDescription.isEmpty()
115 && !oldContentDescription.equals(newContentDescription)) {
116 setContentDescription(newContentDescription);
117 String announceChange = getResources().getString(
118 R.string.button_change_announcement, newContentDescription);
119 announceForAccessibility(announceChange);
121 super.setImageLevel(mLevel);
122 if (callListener && mOnStateChangeListener != null) {
123 mOnStateChangeListener.stateChanged(this, getState());
127 private void nextState() {
128 int state = mState + 1;
129 if (state >= mImageIds.length) {
135 protected void init() {
136 this.setOnClickListener(new View.OnClickListener() {
138 public void onClick(View v) {
144 private void parseAttributes(Context context, AttributeSet attrs) {
145 TypedArray a = context.getTheme().obtainStyledAttributes(
147 R.styleable.MultiToggleImageButton,
149 int imageIds = a.getResourceId(R.styleable.MultiToggleImageButton_imageIds, 0);
150 overrideImageIds(imageIds);
152 int descIds = a.getResourceId(R.styleable.MultiToggleImageButton_contentDescriptionIds, 0);
153 overrideContentDescriptions(descIds);
159 * Override the image ids of this button.
161 public void overrideImageIds(int resId) {
162 TypedArray ids = null;
164 ids = getResources().obtainTypedArray(resId);
165 mImageIds = new int[ids.length()];
166 for (int i = 0; i < ids.length(); i++) {
167 mImageIds[i] = ids.getResourceId(i, 0);
177 * Override the content descriptions of this button.
179 public void overrideContentDescriptions(int resId) {
180 TypedArray ids = null;
182 ids = getResources().obtainTypedArray(resId);
183 mDescIds = new int[ids.length()];
184 for (int i = 0; i < ids.length(); i++) {
185 mDescIds[i] = ids.getResourceId(i, 0);
195 public void setImageLevel(int level) {
196 super.setImageLevel(level);