IconMixin.java revision 3514ad526af3d95f61383ec374ea4c384ba9b540
1/*
2 * Copyright (C) 2017 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.setupwizardlib.template;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.drawable.Drawable;
22import android.support.annotation.DrawableRes;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.ImageView;
26
27import com.android.setupwizardlib.R;
28import com.android.setupwizardlib.TemplateLayout;
29
30/**
31 * A {@link Mixin} for setting an icon on the template layout.
32 */
33public class IconMixin implements Mixin {
34
35    private TemplateLayout mTemplateLayout;
36
37    /**
38     * @param layout The template layout that this Mixin is a part of.
39     * @param attrs XML attributes given to the layout.
40     * @param defStyleAttr The default style attribute as given to the constructor of the layout.
41     */
42    public IconMixin(TemplateLayout layout, AttributeSet attrs, int defStyleAttr) {
43        mTemplateLayout = layout;
44        final Context context = layout.getContext();
45
46        final TypedArray a =
47                context.obtainStyledAttributes(attrs, R.styleable.SuwIconMixin, defStyleAttr, 0);
48
49        final @DrawableRes int icon = a.getResourceId(R.styleable.SuwIconMixin_android_icon, 0);
50        if (icon != 0) {
51            setIcon(icon);
52        }
53
54        a.recycle();
55    }
56
57    /**
58     * Sets the icon on this layout. The icon can also be set in XML using {@code android:icon}.
59     *
60     * @param icon A drawable icon.
61     */
62    public void setIcon(Drawable icon) {
63        final ImageView iconView = getView();
64        if (iconView != null) {
65            iconView.setImageDrawable(icon);
66            iconView.setVisibility(icon != null ? View.VISIBLE : View.GONE);
67        }
68    }
69
70    /**
71     * Sets the icon on this layout. The icon can also be set in XML using {@code android:icon}.
72     *
73     * @param icon A drawable icon resource.
74     */
75    public void setIcon(@DrawableRes int icon) {
76        final ImageView iconView = getView();
77        if (iconView != null) {
78            // Note: setImageResource on the ImageView is overridden in AppCompatImageView for
79            // support lib users, which enables vector drawable compat to work on versions pre-L.
80            iconView.setImageResource(icon);
81            iconView.setVisibility(icon != 0 ? View.VISIBLE : View.GONE);
82        }
83    }
84
85    /**
86     * @return The icon previously set in {@link #setIcon(Drawable)} or {@code android:icon}
87     */
88    public Drawable getIcon() {
89        final ImageView iconView = getView();
90        return iconView != null ? iconView.getDrawable() : null;
91    }
92
93    /**
94     * Sets the content description of the icon view
95     */
96    public void setContentDescription(CharSequence description) {
97        final ImageView iconView = getView();
98        if (iconView != null) {
99            iconView.setContentDescription(description);
100        }
101    }
102
103    /**
104     * @return The content description of the icon view
105     */
106    public CharSequence getContentDescription() {
107        final ImageView iconView = getView();
108        return iconView != null ? iconView.getContentDescription() : null;
109    }
110
111    /**
112     * @return The ImageView responsible for displaying the icon.
113     */
114    protected ImageView getView() {
115        return (ImageView) mTemplateLayout.findManagedViewById(R.id.suw_layout_icon);
116    }
117}
118