blob: 026e4e6f7d23c73b9f95275dc6c26f1bd305bdc7 [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);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070064 ImageView settingsButton = (ImageView) view.findViewById(R.id.settings_button);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070065 if (mSettingsIntent != null) {
66 CheckBox checkbox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070067 if (checkbox.isChecked()) {
68 settingsButton.setOnClickListener(new OnClickListener() {
69 public void onClick(View view) {
70 getContext().startActivity(mSettingsIntent);
71 }
72 });
73 }
74 settingsButton.setVisibility(View.VISIBLE);
75 if (checkbox.isChecked() && isEnabled()) {
76 settingsButton.setAlpha(255);
77 } else {
78 settingsButton.setAlpha(sDimAlpha);
79 }
80 } else {
81 settingsButton.setVisibility(View.GONE);
Svetoslav Ganov6953a6c2011-07-12 15:01:15 -070082 view.findViewById(R.id.divider).setVisibility(View.GONE);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070083 }
84 }
85}