blob: 70b5ac185a18c43cec676afcf3392804cd46c8ee [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 /**
Svetoslav Ganovabc95ee2011-07-17 10:08:34 -070041 * Creates a new instance. The constructor is checking whether the
42 * settings intent is resolved to an activity and acts accordingly.
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070043 *
44 * @param context Context for accessing resources.
Svetoslav Ganovabc95ee2011-07-17 10:08:34 -070045 * @param intent Intent to use as settings for the item represented by
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070046 * this preference. Pass <code>null</code> if there is no associated
47 * settings activity.
48 */
Svetoslav Ganovabc95ee2011-07-17 10:08:34 -070049 public SettingsCheckBoxPreference(Context context, Intent intent) {
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070050 super(context);
51
52 if (sDimAlpha == Integer.MIN_VALUE) {
53 TypedValue outValue = new TypedValue();
54 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, outValue, true);
55 sDimAlpha = (int) (outValue.getFloat() * 255);
56 }
57
Svetoslav Ganovabc95ee2011-07-17 10:08:34 -070058 if (intent != null
59 && !context.getPackageManager().queryIntentActivities(intent, 0).isEmpty()) {
60 mSettingsIntent = intent;
61 } else {
62 mSettingsIntent = null;
63 }
64
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070065 setWidgetLayoutResource(R.layout.preference_settings_checkbox_widget);
66 }
67
68 @Override
69 protected void onBindView(View view) {
70 super.onBindView(view);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070071 ImageView settingsButton = (ImageView) view.findViewById(R.id.settings_button);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070072 if (mSettingsIntent != null) {
73 CheckBox checkbox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070074 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);
Svetoslav Ganov6953a6c2011-07-12 15:01:15 -070089 view.findViewById(R.id.divider).setVisibility(View.GONE);
Svetoslav Ganov6f0b4d82011-05-26 16:45:43 -070090 }
91 }
92}