diff options
author | Maurice Lam <yukl@google.com> | 2016-12-19 11:46:55 -0800 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2017-01-10 10:47:33 -0800 |
commit | 7514f1cee29b3feb4822ce16945c1c312057d24f (patch) | |
tree | d345c2b45f0b7641dae6301f2b2c50457c525474 /library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java | |
parent | 035ba6bda68e78bbb49424300bdd3f0fb305b9e7 (diff) | |
download | setupwizard-7514f1cee29b3feb4822ce16945c1c312057d24f.tar.gz |
Implement Mixins for Templates
Implement Mixin functionalities, which defines part of a template
layout, making it reusable in different layouts. For example, the
HeaderMixin allows setting and getting the header text via the mixin,
which allows for clients which uses multiple different layuots to
simplify their code via something like
layout.getMixin(HeaderMixin.class).setText("Foobar");
Bug: 34163318
Test: ./gradlew connectedAndroidTest
Change-Id: I4348c8bb5b8e640b49c2be0c79c70aa85cf0ebc0
Diffstat (limited to 'library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java')
-rw-r--r-- | library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java b/library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java new file mode 100644 index 0000000..d5c038e --- /dev/null +++ b/library/main/src/com/android/setupwizardlib/template/ProgressBarMixin.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.template; + +import android.content.res.ColorStateList; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.support.annotation.Nullable; +import android.view.View; +import android.view.ViewStub; +import android.widget.ProgressBar; + +import com.android.setupwizardlib.R; +import com.android.setupwizardlib.TemplateLayout; + +/** + * A {@link Mixin} for showing a progress bar. + */ +public class ProgressBarMixin implements Mixin { + + private TemplateLayout mTemplateLayout; + + @Nullable + private ColorStateList mColor; + + /** + * @param layout The layout this mixin belongs to. + */ + public ProgressBarMixin(TemplateLayout layout) { + mTemplateLayout = layout; + } + + /** + * @return True if the progress bar is currently shown. + */ + public boolean isShown() { + final View progressBar = mTemplateLayout.findManagedViewById(R.id.suw_layout_progress); + return progressBar != null && progressBar.getVisibility() == View.VISIBLE; + } + + /** + * Sets whether the progress bar is shown. If the progress bar has not been inflated from the + * stub, this method will inflate the progress bar. + * + * @param shown True to show the progress bar, false to hide it. + */ + public void setShown(boolean shown) { + if (shown) { + View progressBar = getProgressBar(); + if (progressBar != null) { + progressBar.setVisibility(View.VISIBLE); + } + } else { + View progressBar = peekProgressBar(); + if (progressBar != null) { + progressBar.setVisibility(View.GONE); + } + } + } + + /** + * Gets the progress bar in the layout. If the progress bar has not been used before, it will be + * installed (i.e. inflated from its view stub). + * + * @return The progress bar of this layout. May be null only if the template used doesn't have a + * progress bar built-in. + */ + private ProgressBar getProgressBar() { + final View progressBar = peekProgressBar(); + if (progressBar == null) { + final ViewStub progressBarStub = + (ViewStub) mTemplateLayout.findManagedViewById(R.id.suw_layout_progress_stub); + if (progressBarStub != null) { + progressBarStub.inflate(); + } + setColor(mColor); + } + return peekProgressBar(); + } + + /** + * Gets the progress bar in the layout only if it has been installed. + * {@link #setShown(boolean)} should be called before this to ensure the progress bar + * is set up correctly. + * + * @return The progress bar of this layout, or null if the progress bar is not installed. The + * null case can happen either if {@link #setShown(boolean)} with true was + * not called before this, or if the template does not contain a progress bar. + */ + public ProgressBar peekProgressBar() { + return (ProgressBar) mTemplateLayout.findManagedViewById(R.id.suw_layout_progress); + } + + /** + * Sets the color of the indeterminate progress bar. This method is a no-op on SDK < 21. + */ + public void setColor(@Nullable ColorStateList color) { + mColor = color; + if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + final ProgressBar bar = peekProgressBar(); + if (bar != null) { + bar.setIndeterminateTintList(color); + bar.setProgressBackgroundTintList(color); + } + } + } + + /** + * @return The color previously set in {@link #setColor(ColorStateList)}, or null if the color + * is not set. In case of null, the color of the progress bar will be inherited from the theme. + */ + @Nullable + public ColorStateList getColor() { + return mColor; + } +} |