Home | History | Annotate | Download | only in inputmethod
      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 
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.preference.CheckBoxPreference;
     25 import android.util.AttributeSet;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.widget.ImageView;
     29 import android.widget.TextView;
     30 
     31 public 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 ImageView mSettingsButton;
     38     private Intent mSettingsIntent;
     39 
     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
     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
     72     public void setEnabled(boolean enabled) {
     73         super.setEnabled(enabled);
     74         enableSettingsButton();
     75     }
     76 
     77     public void setFragmentIntent(SettingsPreferenceFragment fragment, Intent intent) {
     78         mFragment = fragment;
     79         mSettingsIntent = intent;
     80     }
     81 
     82     protected void onCheckBoxClicked() {
     83         if (isChecked()) {
     84             setChecked(false);
     85         } else {
     86             setChecked(true);
     87         }
     88     }
     89 
     90     protected void onSettingsButtonClicked() {
     91         if (mFragment != null && mSettingsIntent != null) {
     92             mFragment.startActivity(mSettingsIntent);
     93         }
     94     }
     95 
     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(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