1/*
2 * Copyright (C) 2015 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 android.support.v7.widget;
18
19import android.graphics.Bitmap;
20import android.graphics.BitmapShader;
21import android.graphics.Shader;
22import android.graphics.drawable.AnimationDrawable;
23import android.graphics.drawable.BitmapDrawable;
24import android.graphics.drawable.ClipDrawable;
25import android.graphics.drawable.Drawable;
26import android.graphics.drawable.LayerDrawable;
27import android.graphics.drawable.ShapeDrawable;
28import android.graphics.drawable.shapes.RoundRectShape;
29import android.graphics.drawable.shapes.Shape;
30import android.support.v4.graphics.drawable.DrawableWrapper;
31import android.util.AttributeSet;
32import android.view.Gravity;
33import android.widget.ProgressBar;
34
35class AppCompatProgressBarHelper {
36
37    private static final int[] TINT_ATTRS = {
38            android.R.attr.indeterminateDrawable,
39            android.R.attr.progressDrawable
40    };
41
42    private final ProgressBar mView;
43
44    private Bitmap mSampleTile;
45
46    AppCompatProgressBarHelper(ProgressBar view) {
47        mView = view;
48    }
49
50    void loadFromAttributes(AttributeSet attrs, int defStyleAttr) {
51        TintTypedArray a = TintTypedArray.obtainStyledAttributes(mView.getContext(), attrs,
52                TINT_ATTRS, defStyleAttr, 0);
53
54        Drawable drawable = a.getDrawableIfKnown(0);
55        if (drawable != null) {
56            mView.setIndeterminateDrawable(tileifyIndeterminate(drawable));
57        }
58
59        drawable = a.getDrawableIfKnown(1);
60        if (drawable != null) {
61            mView.setProgressDrawable(tileify(drawable, false));
62        }
63
64        a.recycle();
65    }
66
67    /**
68     * Converts a drawable to a tiled version of itself. It will recursively
69     * traverse layer and state list drawables.
70     */
71    private Drawable tileify(Drawable drawable, boolean clip) {
72        if (drawable instanceof DrawableWrapper) {
73            Drawable inner = ((DrawableWrapper) drawable).getWrappedDrawable();
74            if (inner != null) {
75                inner = tileify(inner, clip);
76                ((DrawableWrapper) drawable).setWrappedDrawable(inner);
77            }
78        } else if (drawable instanceof LayerDrawable) {
79            LayerDrawable background = (LayerDrawable) drawable;
80            final int N = background.getNumberOfLayers();
81            Drawable[] outDrawables = new Drawable[N];
82
83            for (int i = 0; i < N; i++) {
84                int id = background.getId(i);
85                outDrawables[i] = tileify(background.getDrawable(i),
86                        (id == android.R.id.progress || id == android.R.id.secondaryProgress));
87            }
88            LayerDrawable newBg = new LayerDrawable(outDrawables);
89
90            for (int i = 0; i < N; i++) {
91                newBg.setId(i, background.getId(i));
92            }
93
94            return newBg;
95
96        } else if (drawable instanceof BitmapDrawable) {
97            final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
98            final Bitmap tileBitmap = bitmapDrawable.getBitmap();
99            if (mSampleTile == null) {
100                mSampleTile = tileBitmap;
101            }
102
103            final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
104            final BitmapShader bitmapShader = new BitmapShader(tileBitmap,
105                    Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
106            shapeDrawable.getPaint().setShader(bitmapShader);
107            shapeDrawable.getPaint().setColorFilter(bitmapDrawable.getPaint().getColorFilter());
108            return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,
109                    ClipDrawable.HORIZONTAL) : shapeDrawable;
110        }
111
112        return drawable;
113    }
114
115    /**
116     * Convert a AnimationDrawable for use as a barberpole animation.
117     * Each frame of the animation is wrapped in a ClipDrawable and
118     * given a tiling BitmapShader.
119     */
120    private Drawable tileifyIndeterminate(Drawable drawable) {
121        if (drawable instanceof AnimationDrawable) {
122            AnimationDrawable background = (AnimationDrawable) drawable;
123            final int N = background.getNumberOfFrames();
124            AnimationDrawable newBg = new AnimationDrawable();
125            newBg.setOneShot(background.isOneShot());
126
127            for (int i = 0; i < N; i++) {
128                Drawable frame = tileify(background.getFrame(i), true);
129                frame.setLevel(10000);
130                newBg.addFrame(frame, background.getDuration(i));
131            }
132            newBg.setLevel(10000);
133            drawable = newBg;
134        }
135        return drawable;
136    }
137
138    private Shape getDrawableShape() {
139        final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
140        return new RoundRectShape(roundedCorners, null, null);
141    }
142
143    Bitmap getSampleTime() {
144        return mSampleTile;
145    }
146
147}
148