IconMixin.java revision 58195c5316855122593366bd867ed51a91bd5c11
17514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam/*
27514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Copyright (C) 2017 The Android Open Source Project
37514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
47514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Licensed under the Apache License, Version 2.0 (the "License");
57514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * you may not use this file except in compliance with the License.
67514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * You may obtain a copy of the License at
77514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
87514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *      http://www.apache.org/licenses/LICENSE-2.0
97514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam *
107514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * Unless required by applicable law or agreed to in writing, software
117514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * distributed under the License is distributed on an "AS IS" BASIS,
127514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * See the License for the specific language governing permissions and
147514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * limitations under the License.
157514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam */
167514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
177514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lampackage com.android.setupwizardlib.template;
187514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
197514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.content.Context;
207514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.content.res.TypedArray;
217514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.graphics.drawable.Drawable;
227514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.util.AttributeSet;
2358195c5316855122593366bd867ed51a91bd5c11Maurice Lamimport android.view.View;
247514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.widget.ImageView;
257514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
267514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.R;
277514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.TemplateLayout;
287514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
297514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam/**
307514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * A {@link Mixin} for setting an icon on the template layout.
317514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam */
327514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lampublic class IconMixin implements Mixin {
337514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
347514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    private TemplateLayout mTemplateLayout;
357514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
367514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
377514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param layout The template layout that this Mixin is a part of.
387514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param attrs XML attributes given to the layout.
397514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param defStyleAttr The default style attribute as given to the constructor of the layout.
407514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
417514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public IconMixin(TemplateLayout layout, AttributeSet attrs, int defStyleAttr) {
427514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        mTemplateLayout = layout;
437514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final Context context = layout.getContext();
447514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
457514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final TypedArray a =
467514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                context.obtainStyledAttributes(attrs, R.styleable.SuwIconMixin, defStyleAttr, 0);
477514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
487514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final Drawable icon = a.getDrawable(R.styleable.SuwIconMixin_android_icon);
497514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (icon != null) {
507514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            setIcon(icon);
517514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
527514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
537514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        a.recycle();
547514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
557514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
567514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
577514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Sets the icon on this layout. The icon can also be set in XML using {@code android:icon}.
587514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
597514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param icon A drawable icon.
607514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
617514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public void setIcon(Drawable icon) {
627514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final ImageView iconView = getView();
637514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (iconView != null) {
647514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            iconView.setImageDrawable(icon);
6558195c5316855122593366bd867ed51a91bd5c11Maurice Lam            iconView.setVisibility(icon != null ? View.VISIBLE : View.GONE);
667514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
677514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
687514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
697514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
707514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The icon previously set in {@link #setIcon(Drawable)} or {@code android:icon}
717514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
727514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public Drawable getIcon() {
737514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final ImageView iconView = getView();
747514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return iconView != null ? iconView.getDrawable() : null;
757514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
767514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
777514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
787514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The ImageView responsible for displaying the icon.
797514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
807514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    protected ImageView getView() {
817514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return (ImageView) mTemplateLayout.findManagedViewById(R.id.suw_layout_icon);
827514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
837514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam}
84