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.res.ColorStateList;
207514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.os.Build;
217514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.os.Build.VERSION_CODES;
227514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.support.annotation.Nullable;
237514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.view.View;
247514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.view.ViewStub;
257514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport android.widget.ProgressBar;
267514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
277514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.R;
287514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lamimport com.android.setupwizardlib.TemplateLayout;
297514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
307514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam/**
317514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam * A {@link Mixin} for showing a progress bar.
327514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam */
337514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lampublic class ProgressBarMixin implements Mixin {
347514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
357514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    private TemplateLayout mTemplateLayout;
367514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
377514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    @Nullable
387514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    private ColorStateList mColor;
397514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
407514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
417514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param layout The layout this mixin belongs to.
427514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
437514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public ProgressBarMixin(TemplateLayout layout) {
447514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        mTemplateLayout = layout;
457514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
467514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
477514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
487514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return True if the progress bar is currently shown.
497514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
507514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public boolean isShown() {
517514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final View progressBar = mTemplateLayout.findManagedViewById(R.id.suw_layout_progress);
527514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return progressBar != null && progressBar.getVisibility() == View.VISIBLE;
537514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
547514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
557514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
567514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Sets whether the progress bar is shown. If the progress bar has not been inflated from the
577514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * stub, this method will inflate the progress bar.
587514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
597514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @param shown True to show the progress bar, false to hide it.
607514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
617514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public void setShown(boolean shown) {
627514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (shown) {
637514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            View progressBar = getProgressBar();
647514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            if (progressBar != null) {
657514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                progressBar.setVisibility(View.VISIBLE);
667514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            }
677514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        } else {
687514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            View progressBar = peekProgressBar();
697514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            if (progressBar != null) {
707514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                progressBar.setVisibility(View.GONE);
717514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            }
727514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
737514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
747514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
757514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
767514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Gets the progress bar in the layout. If the progress bar has not been used before, it will be
777514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * installed (i.e. inflated from its view stub).
787514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
797514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The progress bar of this layout. May be null only if the template used doesn't have a
807514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *         progress bar built-in.
817514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
827514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    private ProgressBar getProgressBar() {
837514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        final View progressBar = peekProgressBar();
847514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (progressBar == null) {
857514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            final ViewStub progressBarStub =
867514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                    (ViewStub) mTemplateLayout.findManagedViewById(R.id.suw_layout_progress_stub);
877514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            if (progressBarStub != null) {
887514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                progressBarStub.inflate();
897514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            }
907514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            setColor(mColor);
917514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
927514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return peekProgressBar();
937514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
947514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
957514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
967514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Gets the progress bar in the layout only if it has been installed.
977514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * {@link #setShown(boolean)} should be called before this to ensure the progress bar
987514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * is set up correctly.
997514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *
1007514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The progress bar of this layout, or null if the progress bar is not installed. The
1017514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *         null case can happen either if {@link #setShown(boolean)} with true was
1027514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     *         not called before this, or if the template does not contain a progress bar.
1037514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
1047514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public ProgressBar peekProgressBar() {
1057514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return (ProgressBar) mTemplateLayout.findManagedViewById(R.id.suw_layout_progress);
1067514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
1077514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
1087514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
1097514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * Sets the color of the indeterminate progress bar. This method is a no-op on SDK < 21.
1107514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
1117514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public void setColor(@Nullable ColorStateList color) {
1127514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        mColor = color;
1137514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
1147514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            final ProgressBar bar = peekProgressBar();
1157514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            if (bar != null) {
1167514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam                bar.setIndeterminateTintList(color);
117339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                if (Build.VERSION.SDK_INT >= VERSION_CODES.M || color != null) {
118339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // There is a bug in Lollipop where setting the progress tint color to null
119339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // will crash with "java.lang.NullPointerException: Attempt to invoke virtual
120339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // method 'int android.graphics.Paint.getAlpha()' on a null object reference"
121339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // at android.graphics.drawable.NinePatchDrawable.draw(:250)
122339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // The bug doesn't affect ProgressBar on M because it uses ShapeDrawable instead
123339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    // of NinePatchDrawable. (commit 6a8253fdc9f4574c28b4beeeed90580ffc93734a)
124339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                    bar.setProgressBackgroundTintList(color);
125339cbc0f38d81adda4d2f9bf44a5514e2f027031Maurice Lam                }
1267514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam            }
1277514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        }
1287514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
1297514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam
1307514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    /**
1317514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * @return The color previously set in {@link #setColor(ColorStateList)}, or null if the color
1327514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     * is not set. In case of null, the color of the progress bar will be inherited from the theme.
1337514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam     */
1347514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    @Nullable
1357514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    public ColorStateList getColor() {
1367514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam        return mColor;
1377514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam    }
1387514f1cee29b3feb4822ce16945c1c312057d24fMaurice Lam}
139