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