1498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani/*
2498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * Copyright (C) 2009 The Android Open Source Project
3498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani *
4498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * Licensed under the Apache License, Version 2.0 (the "License");
5498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * you may not use this file except in compliance with the License.
6498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * You may obtain a copy of the License at
7498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani *
8498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani *      http://www.apache.org/licenses/LICENSE-2.0
9498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani *
10498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * Unless required by applicable law or agreed to in writing, software
11498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * distributed under the License is distributed on an "AS IS" BASIS,
12498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * See the License for the specific language governing permissions and
14498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani * limitations under the License.
15498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani */
16498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
17498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasanipackage com.android.settings;
18498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
19498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.content.Context;
20498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.content.res.TypedArray;
21498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.graphics.drawable.Drawable;
22498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.preference.Preference;
23498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.util.AttributeSet;
24498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.view.View;
25498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasaniimport android.widget.ImageView;
26d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasaniimport android.widget.TextView;
27498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
28498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasanipublic class IconPreferenceScreen extends Preference {
29498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
30498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    private Drawable mIcon;
31498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
32d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    // Whether or not the text and icon should be highlighted (as selected)
33d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    private boolean mHighlight;
34d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani
35498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    public IconPreferenceScreen(Context context, AttributeSet attrs) {
36498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        this(context, attrs, 0);
37498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    }
38498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
39498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
40498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        super(context, attrs, defStyle);
41498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        setLayoutResource(R.layout.preference_icon);
42498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        TypedArray a = context.obtainStyledAttributes(attrs,
43498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani                R.styleable.IconPreferenceScreen, defStyle, 0);
44498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);
45498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    }
46498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani
47498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    @Override
48498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    public void onBindView(View view) {
49498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        super.onBindView(view);
50498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
51498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        if (imageView != null && mIcon != null) {
52498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani            imageView.setImageDrawable(mIcon);
53498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani        }
54d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        TextView textView = (TextView) view.findViewById(android.R.id.title);
55498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani    }
56b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar
57b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    /**
58b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     * Sets the icon for this Preference with a Drawable.
59b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     *
60b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     * @param icon The icon for this Preference
61b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     */
62b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    public void setIcon(Drawable icon) {
63b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar        if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
64b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar            mIcon = icon;
65b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar            notifyChanged();
66b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar        }
67b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    }
68b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar
69b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    /**
70b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     * Returns the icon of this Preference.
71b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     *
72b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     * @return The icon.
73b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     * @see #setIcon(Drawable)
74b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar     */
75b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    public Drawable getIcon() {
76b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar        return mIcon;
77b2dd90383bae9f3ca0a99b59d3f5992e7fd5ad48Anders Hammar    }
78d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani
79d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    public void setHighlighted(boolean highlight) {
80d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        mHighlight = highlight;
81d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani        notifyChanged();
82d79934731c8d33f6fc63b21c120b9ffba5d06f54Amith Yamasani    }
83498d90474b4208fd28cc2ae84d5759ddad7bfc0cAmith Yamasani}
84