1/*
2 * Copyright (C) 2011 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 com.android.camera.ListPreference;
20import com.android.camera.R;
21
22import android.content.Context;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.accessibility.AccessibilityEvent;
26import android.widget.Button;
27import android.widget.CompoundButton;
28import android.widget.CompoundButton.OnCheckedChangeListener;
29import android.widget.Switch;
30
31/* A switch setting control which turns on/off the setting. */
32public class InLineSettingSwitch extends InLineSettingItem {
33    private Switch mSwitch;
34
35    OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
36        public void onCheckedChanged(CompoundButton buttonView, boolean desiredState) {
37            changeIndex(desiredState ? 1 : 0);
38        }
39    };
40
41    public InLineSettingSwitch(Context context, AttributeSet attrs) {
42        super(context, attrs);
43    }
44
45    @Override
46    protected void onFinishInflate() {
47        super.onFinishInflate();
48        mSwitch = (Switch) findViewById(R.id.setting_switch);
49        mSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
50    }
51
52    @Override
53    public void initialize(ListPreference preference) {
54        super.initialize(preference);
55        // Add content descriptions for the increment and decrement buttons.
56        mSwitch.setContentDescription(getContext().getResources().getString(
57                R.string.accessibility_switch, mPreference.getTitle()));
58    }
59
60    @Override
61    protected void updateView() {
62        mSwitch.setOnCheckedChangeListener(null);
63        if (mOverrideValue == null) {
64            mSwitch.setChecked(mIndex == 1);
65        } else {
66            int index = mPreference.findIndexOfValue(mOverrideValue);
67            mSwitch.setChecked(index == 1);
68        }
69        mSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
70    }
71
72    @Override
73    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
74        onPopulateAccessibilityEvent(event);
75        return true;
76    }
77
78    @Override
79    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
80        super.onPopulateAccessibilityEvent(event);
81        event.getText().add(mPreference.getTitle());
82    }
83}
84