1/*
2 * Copyright (C) 2014 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
17package com.android.camera.ui;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.View;
24
25import com.android.camera2.R;
26
27/**
28 * A LienearLayout for a set of {@link android.view.View}s,
29 * one of which can be selected at any time.
30 */
31public class RadioOptions extends TopRightWeightedLayout {
32    /**
33     * Listener for responding to {@link android.view.View} click events.
34     */
35    public interface OnOptionClickListener {
36        /**
37         * Override to respond to  {@link android.view.View} click events.
38         * @param v {@link android.view.View} that was clicked.
39         */
40        public void onOptionClicked(View v);
41    }
42
43    private Drawable mBackground;
44    private OnOptionClickListener mOnOptionClickListener;
45
46    /**
47     * Set the OnOptionClickListener.
48     * @params listener The listener to set.
49     */
50    public void setOnOptionClickListener(OnOptionClickListener listener) {
51        mOnOptionClickListener = listener;
52    }
53
54    /**
55     * Constructor that is called when inflating a view from XML.
56     * @params context The Context the view is running in, through which it can access the current theme, resources, etc.
57     * @params attrs The attributes of the XML tag that is inflating the view.
58     */
59    public RadioOptions(Context context, AttributeSet attrs) {
60        super(context, attrs);
61
62        TypedArray a = context.getTheme().obtainStyledAttributes(
63            attrs,
64            R.styleable.RadioOptions,
65            0, 0);
66        int drawableId = a.getResourceId(R.styleable.RadioOptions_selected_drawable, 0);
67        if (drawableId > 0) {
68            mBackground = context.getResources()
69                .getDrawable(drawableId);
70        }
71    }
72
73    @Override
74    public void onFinishInflate() {
75        super.onFinishInflate();
76        updateListeners();
77    }
78
79    /**
80     * Update each child {@link android.view.View}'s {@link android.view.View.OnClickListener}.
81     * Call this if the child views are added after the OnOptionClickListener,
82     * e.g. if the child views are added programatically.
83     */
84    public void updateListeners() {
85        View.OnClickListener onClickListener = new View.OnClickListener() {
86            @Override
87            public void onClick(View button) {
88                setSelectedOptionByView(button);
89            }
90        };
91
92        for (int i = 0; i < getChildCount(); i++) {
93            View button = getChildAt(i);
94            button.setOnClickListener(onClickListener);
95        }
96    }
97
98    /**
99     * Sets a child {@link android.view.View} as selected by tag.
100     * @param tag Tag that identifies a child {@link android.view.View}. No effect if view not found.
101     */
102    public void setSelectedOptionByTag(Object tag) {
103        View button = findViewWithTag(tag);
104        setSelectedOptionByView(button);
105    }
106
107    /**
108     * Sets a child {@link android.view.View} as selected by id.
109     * @param id Resource ID  that identifies a child {@link android.view.View}. No effect if view not found.
110     */
111    public void setSeletedOptionById(int id) {
112        View button = findViewById(id);
113        setSelectedOptionByView(button);
114    }
115
116    private void setSelectedOptionByView(View view) {
117        if (view != null) {
118            // Reset all button states.
119            for (int i = 0; i < getChildCount(); i++) {
120                getChildAt(i).setBackground(null);
121            }
122
123            // Highlight the appropriate button.
124            view.setBackground(mBackground);
125            if (mOnOptionClickListener != null) {
126                mOnOptionClickListener.onOptionClicked(view);
127            }
128        }
129    }
130}