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.inputmethod.latin.settings;
18
19import android.content.Context;
20import android.preference.Preference;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.RadioButton;
24
25import com.android.inputmethod.latin.R;
26
27/**
28 * Radio Button preference
29 */
30public class RadioButtonPreference extends Preference {
31    interface OnRadioButtonClickedListener {
32        /**
33         * Called when this preference needs to be saved its state.
34         *
35         * @param preference This preference.
36         */
37        public void onRadioButtonClicked(RadioButtonPreference preference);
38    }
39
40    private boolean mIsSelected;
41    private RadioButton mRadioButton;
42    private OnRadioButtonClickedListener mListener;
43    private final View.OnClickListener mClickListener = new View.OnClickListener() {
44        @Override
45        public void onClick(final View v) {
46            if (mListener != null) {
47                mListener.onRadioButtonClicked(RadioButtonPreference.this);
48            }
49        }
50    };
51
52    public RadioButtonPreference(final Context context) {
53        this(context, null);
54    }
55
56    public RadioButtonPreference(final Context context, final AttributeSet attrs) {
57        this(context, attrs, android.R.attr.preferenceStyle);
58    }
59
60    public RadioButtonPreference(final Context context, final AttributeSet attrs,
61            final int defStyleAttr) {
62        super(context, attrs, defStyleAttr);
63        setWidgetLayoutResource(R.layout.radio_button_preference_widget);
64    }
65
66    public void setOnRadioButtonClickedListener(final OnRadioButtonClickedListener listener) {
67        mListener = listener;
68    }
69
70    @Override
71    protected void onBindView(final View view) {
72        super.onBindView(view);
73        mRadioButton = (RadioButton)view.findViewById(R.id.radio_button);
74        mRadioButton.setChecked(mIsSelected);
75        mRadioButton.setOnClickListener(mClickListener);
76        view.setOnClickListener(mClickListener);
77    }
78
79    public boolean isSelected() {
80        return mIsSelected;
81    }
82
83    public void setSelected(final boolean selected) {
84        if (selected == mIsSelected) {
85            return;
86        }
87        mIsSelected = selected;
88        if (mRadioButton != null) {
89            mRadioButton.setChecked(selected);
90        }
91        notifyChanged();
92    }
93}
94