blob: 6acdfe8df563b7beba93c243f0e04fbabd3d8d4a [file] [log] [blame]
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -07001/*
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;
18
19import android.content.Context;
20import android.content.Intent;
21import android.preference.CheckBoxPreference;
22import android.util.TypedValue;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.widget.CheckBox;
26import android.widget.ImageView;
27
28/**
29 * CheckBox preference that optionally shows an icon for launching a settings
30 * {@link android.app.Activity}. The settings activity, if intent for launching
31 * it was provided, can be stared only if the CheckBox in is checked.
32 */
33public class SettingsCheckBoxPreference extends CheckBoxPreference {
34
35 // Integer.MIN_VALUE means not initalized
36 private static int sDimAlpha = Integer.MIN_VALUE;
37
38 private final Intent mSettingsIntent;
39
40 /**
41 * Creates a new instance.
42 *
43 * @param context Context for accessing resources.
44 * @param settingsIntent Intent to use as settings for the item represented by
45 * this preference. Pass <code>null</code> if there is no associated
46 * settings activity.
47 */
48 public SettingsCheckBoxPreference(Context context, Intent settingsIntent) {
49 super(context);
50
51 if (sDimAlpha == Integer.MIN_VALUE) {
52 TypedValue outValue = new TypedValue();
53 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
54 sDimAlpha = (int) (outValue.getFloat() * 255);
55 }
56
57 mSettingsIntent = settingsIntent;
58 setWidgetLayoutResource(R.layout.preference_settings_checkbox_widget);
59 }
60
61 @Override
62 protected void onBindView(View view) {
63 super.onBindView(view);
64
65 ImageView settingsButton = (ImageView) view.findViewById(R.id.settings_button);
66 if (settingsButton == null) {
67 return;
68 }
69 if (mSettingsIntent != null) {
70 CheckBox checkbox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
71 if (checkbox == null) {
72 return;
73 }
74 if (checkbox.isChecked()) {
75 settingsButton.setOnClickListener(new OnClickListener() {
76 public void onClick(View view) {
77 getContext().startActivity(mSettingsIntent);
78 }
79 });
80 }
81 settingsButton.setVisibility(View.VISIBLE);
82 if (checkbox.isChecked() && isEnabled()) {
83 settingsButton.setAlpha(255);
84 } else {
85 settingsButton.setAlpha(sDimAlpha);
86 }
87 } else {
88 settingsButton.setVisibility(View.GONE);
89 }
90 }
91}