1/*
2 * Copyright (C) 2013 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 */
16package com.android.camera;
17
18import android.content.Context;
19import android.content.res.TypedArray;
20import android.util.AttributeSet;
21import android.view.View;
22
23/*
24 * A toggle button that supports two states with images rendererd on top for each state.
25 */
26public class ToggleImageButton extends MultiToggleImageButton {
27    /*
28     * Listener interface for button state changes.
29     */
30    public interface OnStateChangeListener {
31        /*
32         * @param view the ToggleImageButton that received the touch event
33         * @param state the new state the button is in
34         */
35        public abstract void stateChanged(View view, boolean state);
36    }
37
38    private OnStateChangeListener mOnStateChangeListener;
39        public ToggleImageButton(Context context) {
40        super(context);
41    }
42
43    public ToggleImageButton(Context context, AttributeSet attrs) {
44        super(context, attrs);
45    }
46
47    public ToggleImageButton(Context context, AttributeSet attrs, int defStyle) {
48        super(context, attrs, defStyle);
49    }
50
51    /*
52     * Set the state change listener.
53     * @param onStateChangeListener the listener to set
54     */
55    public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {
56        mOnStateChangeListener = onStateChangeListener;
57    }
58
59    /*
60     * Set the current button state, thus causing the state change listener to get called.
61     * @param state the desired state
62     */
63    public void setState(boolean state) {
64        super.setState(state ? 1 : 0);
65    }
66
67    @Override
68    protected void init() {
69        super.init();
70        super.setOnStateChangeListener(new MultiToggleImageButton.OnStateChangeListener() {
71            @Override
72            public void stateChanged(View v, int state) {
73                if (mOnStateChangeListener != null) {
74                    mOnStateChangeListener.stateChanged(v, (state == 1));
75                }
76            }
77        });
78    }
79}