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.settings.inputmethod;
18
19import com.android.settings.R;
20import com.android.settings.SettingsPreferenceFragment;
21
22import android.content.Context;
23import android.content.Intent;
24import android.preference.CheckBoxPreference;
25import android.util.AttributeSet;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31public class CheckBoxAndSettingsPreference extends CheckBoxPreference {
32    private static final float DISABLED_ALPHA = 0.4f;
33
34    private SettingsPreferenceFragment mFragment;
35    private TextView mTitleText;
36    private TextView mSummaryText;
37    private View mCheckBox;
38    private ImageView mSetingsButton;
39    private Intent mSettingsIntent;
40
41    public CheckBoxAndSettingsPreference(Context context, AttributeSet attrs) {
42        super(context, attrs);
43        setLayoutResource(R.layout.preference_inputmethod);
44        setWidgetLayoutResource(R.layout.preference_inputmethod_widget);
45    }
46
47    @Override
48    protected void onBindView(View view) {
49        super.onBindView(view);
50        mCheckBox = view.findViewById(R.id.inputmethod_pref);
51        mCheckBox.setOnClickListener(
52                new OnClickListener() {
53                    @Override
54                    public void onClick(View arg0) {
55                        onCheckBoxClicked();
56                    }
57                });
58        mSetingsButton = (ImageView)view.findViewById(R.id.inputmethod_settings);
59        mTitleText = (TextView)view.findViewById(android.R.id.title);
60        mSummaryText = (TextView)view.findViewById(android.R.id.summary);
61        mSetingsButton.setOnClickListener(
62                new OnClickListener() {
63                    @Override
64                    public void onClick(View arg0) {
65                        onSettingsButtonClicked(arg0);
66                    }
67                });
68        enableSettingsButton();
69    }
70
71
72    @Override
73    public void setEnabled(boolean enabled) {
74        super.setEnabled(enabled);
75        enableSettingsButton();
76    }
77
78    public void setFragmentIntent(SettingsPreferenceFragment fragment, Intent intent) {
79        mFragment = fragment;
80        mSettingsIntent = intent;
81    }
82
83    protected void onCheckBoxClicked() {
84        if (isChecked()) {
85            setChecked(false);
86        } else {
87            setChecked(true);
88        }
89    }
90
91    protected void onSettingsButtonClicked(View arg0) {
92        if (mFragment != null && mSettingsIntent != null) {
93            mFragment.startActivity(mSettingsIntent);
94        }
95    }
96
97    private void enableSettingsButton() {
98        if (mSetingsButton != null) {
99            if (mSettingsIntent == null) {
100                mSetingsButton.setVisibility(View.GONE);
101            } else {
102                final boolean checked = isChecked();
103                mSetingsButton.setEnabled(checked);
104                mSetingsButton.setClickable(checked);
105                mSetingsButton.setFocusable(checked);
106                if (!checked) {
107                    mSetingsButton.setAlpha(DISABLED_ALPHA);
108                }
109            }
110        }
111        if (mTitleText != null) {
112            mTitleText.setEnabled(true);
113        }
114        if (mSummaryText != null) {
115            mSummaryText.setEnabled(true);
116        }
117    }
118}
119