DrawableWrapper.java revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
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.graphics.drawable;
18
19import com.android.internal.R;
20
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.annotation.NonNull;
25import android.annotation.Nullable;
26import android.content.pm.ActivityInfo.Config;
27import android.content.res.ColorStateList;
28import android.content.res.Resources;
29import android.content.res.Resources.Theme;
30import android.content.res.TypedArray;
31import android.graphics.Canvas;
32import android.graphics.ColorFilter;
33import android.graphics.Insets;
34import android.graphics.Outline;
35import android.graphics.PixelFormat;
36import android.graphics.PorterDuff;
37import android.graphics.Rect;
38import android.util.AttributeSet;
39import android.util.DisplayMetrics;
40import android.view.View;
41
42import java.io.IOException;
43
44/**
45 * Drawable container with only one child element.
46 */
47public abstract class DrawableWrapper extends Drawable implements Drawable.Callback {
48    private DrawableWrapperState mState;
49    private Drawable mDrawable;
50    private boolean mMutated;
51
52    DrawableWrapper(DrawableWrapperState state, Resources res) {
53        mState = state;
54
55        updateLocalState(res);
56    }
57
58    /**
59     * Creates a new wrapper around the specified drawable.
60     *
61     * @param dr the drawable to wrap
62     */
63    public DrawableWrapper(@Nullable Drawable dr) {
64        mState = null;
65        mDrawable = dr;
66    }
67
68    /**
69     * Initializes local dynamic properties from state. This should be called
70     * after significant state changes, e.g. from the One True Constructor and
71     * after inflating or applying a theme.
72     */
73    private void updateLocalState(Resources res) {
74        if (mState != null && mState.mDrawableState != null) {
75            final Drawable dr = mState.mDrawableState.newDrawable(res);
76            setDrawable(dr);
77        }
78    }
79
80    /**
81     * Sets the wrapped drawable.
82     *
83     * @param dr the wrapped drawable
84     */
85    public void setDrawable(@Nullable Drawable dr) {
86        if (mDrawable != null) {
87            mDrawable.setCallback(null);
88        }
89
90        mDrawable = dr;
91
92        if (dr != null) {
93            dr.setCallback(this);
94
95            // Only call setters for data that's stored in the base Drawable.
96            dr.setVisible(isVisible(), true);
97            dr.setState(getState());
98            dr.setLevel(getLevel());
99            dr.setBounds(getBounds());
100            dr.setLayoutDirection(getLayoutDirection());
101
102            if (mState != null) {
103                mState.mDrawableState = dr.getConstantState();
104            }
105        }
106
107        invalidateSelf();
108    }
109
110    /**
111     * @return the wrapped drawable
112     */
113    @Nullable
114    public Drawable getDrawable() {
115        return mDrawable;
116    }
117
118    @Override
119    public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
120            @NonNull AttributeSet attrs, @Nullable Theme theme)
121            throws XmlPullParserException, IOException {
122        super.inflate(r, parser, attrs, theme);
123
124        final DrawableWrapperState state = mState;
125        if (state == null) {
126            return;
127        }
128
129        // The density may have changed since the last update. This will
130        // apply scaling to any existing constant state properties.
131        final int densityDpi = r.getDisplayMetrics().densityDpi;
132        final int targetDensity = densityDpi == 0 ? DisplayMetrics.DENSITY_DEFAULT : densityDpi;
133        state.setDensity(targetDensity);
134
135        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.DrawableWrapper);
136        updateStateFromTypedArray(a);
137        a.recycle();
138
139        inflateChildDrawable(r, parser, attrs, theme);
140    }
141
142    @Override
143    public void applyTheme(@NonNull Theme t) {
144        super.applyTheme(t);
145
146        // If we load the drawable later as part of updating from the typed
147        // array, it will already be themed correctly. So, we can theme the
148        // local drawable first.
149        if (mDrawable != null && mDrawable.canApplyTheme()) {
150            mDrawable.applyTheme(t);
151        }
152
153        final DrawableWrapperState state = mState;
154        if (state == null) {
155            return;
156        }
157
158        final int densityDpi = t.getResources().getDisplayMetrics().densityDpi;
159        final int density = densityDpi == 0 ? DisplayMetrics.DENSITY_DEFAULT : densityDpi;
160        state.setDensity(density);
161
162        if (state.mThemeAttrs != null) {
163            final TypedArray a = t.resolveAttributes(
164                    state.mThemeAttrs, R.styleable.DrawableWrapper);
165            updateStateFromTypedArray(a);
166            a.recycle();
167        }
168    }
169
170    /**
171     * Updates constant state properties from the provided typed array.
172     * <p>
173     * Implementing subclasses should call through to the super method first.
174     *
175     * @param a the typed array rom which properties should be read
176     */
177    private void updateStateFromTypedArray(@NonNull TypedArray a) {
178        final DrawableWrapperState state = mState;
179        if (state == null) {
180            return;
181        }
182
183        // Account for any configuration changes.
184        state.mChangingConfigurations |= a.getChangingConfigurations();
185
186        // Extract the theme attributes, if any.
187        state.mThemeAttrs = a.extractThemeAttrs();
188
189        if (a.hasValueOrEmpty(R.styleable.DrawableWrapper_drawable)) {
190            setDrawable(a.getDrawable(R.styleable.DrawableWrapper_drawable));
191        }
192    }
193
194    @Override
195    public boolean canApplyTheme() {
196        return (mState != null && mState.canApplyTheme()) || super.canApplyTheme();
197    }
198
199    @Override
200    public void invalidateDrawable(@NonNull Drawable who) {
201        final Callback callback = getCallback();
202        if (callback != null) {
203            callback.invalidateDrawable(this);
204        }
205    }
206
207    @Override
208    public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {
209        final Callback callback = getCallback();
210        if (callback != null) {
211            callback.scheduleDrawable(this, what, when);
212        }
213    }
214
215    @Override
216    public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {
217        final Callback callback = getCallback();
218        if (callback != null) {
219            callback.unscheduleDrawable(this, what);
220        }
221    }
222
223    @Override
224    public void draw(@NonNull Canvas canvas) {
225        if (mDrawable != null) {
226            mDrawable.draw(canvas);
227        }
228    }
229
230    @Override
231    public @Config int getChangingConfigurations() {
232        return super.getChangingConfigurations()
233                | (mState != null ? mState.getChangingConfigurations() : 0)
234                | mDrawable.getChangingConfigurations();
235    }
236
237    @Override
238    public boolean getPadding(@NonNull Rect padding) {
239        return mDrawable != null && mDrawable.getPadding(padding);
240    }
241
242    /** @hide */
243    @Override
244    public Insets getOpticalInsets() {
245        return mDrawable != null ? mDrawable.getOpticalInsets() : Insets.NONE;
246    }
247
248    @Override
249    public void setHotspot(float x, float y) {
250        if (mDrawable != null) {
251            mDrawable.setHotspot(x, y);
252        }
253    }
254
255    @Override
256    public void setHotspotBounds(int left, int top, int right, int bottom) {
257        if (mDrawable != null) {
258            mDrawable.setHotspotBounds(left, top, right, bottom);
259        }
260    }
261
262    @Override
263    public void getHotspotBounds(@NonNull Rect outRect) {
264        if (mDrawable != null) {
265            mDrawable.getHotspotBounds(outRect);
266        } else {
267            outRect.set(getBounds());
268        }
269    }
270
271    @Override
272    public boolean setVisible(boolean visible, boolean restart) {
273        final boolean superChanged = super.setVisible(visible, restart);
274        final boolean changed = mDrawable != null && mDrawable.setVisible(visible, restart);
275        return superChanged | changed;
276    }
277
278    @Override
279    public void setAlpha(int alpha) {
280        if (mDrawable != null) {
281            mDrawable.setAlpha(alpha);
282        }
283    }
284
285    @Override
286    public int getAlpha() {
287        return mDrawable != null ? mDrawable.getAlpha() : 255;
288    }
289
290    @Override
291    public void setColorFilter(@Nullable ColorFilter colorFilter) {
292        if (mDrawable != null) {
293            mDrawable.setColorFilter(colorFilter);
294        }
295    }
296
297    @Override
298    public void setTintList(@Nullable ColorStateList tint) {
299        if (mDrawable != null) {
300            mDrawable.setTintList(tint);
301        }
302    }
303
304    @Override
305    public void setTintMode(@Nullable PorterDuff.Mode tintMode) {
306        if (mDrawable != null) {
307            mDrawable.setTintMode(tintMode);
308        }
309    }
310
311    @Override
312    public boolean onLayoutDirectionChanged(@View.ResolvedLayoutDir int layoutDirection) {
313        return mDrawable != null && mDrawable.setLayoutDirection(layoutDirection);
314    }
315
316    @Override
317    public int getOpacity() {
318        return mDrawable != null ? mDrawable.getOpacity() : PixelFormat.TRANSPARENT;
319    }
320
321    @Override
322    public boolean isStateful() {
323        return mDrawable != null && mDrawable.isStateful();
324    }
325
326    @Override
327    protected boolean onStateChange(int[] state) {
328        if (mDrawable != null && mDrawable.isStateful()) {
329            final boolean changed = mDrawable.setState(state);
330            if (changed) {
331                onBoundsChange(getBounds());
332            }
333            return changed;
334        }
335        return false;
336    }
337
338    @Override
339    protected boolean onLevelChange(int level) {
340        return mDrawable != null && mDrawable.setLevel(level);
341    }
342
343    @Override
344    protected void onBoundsChange(@NonNull Rect bounds) {
345        if (mDrawable != null) {
346            mDrawable.setBounds(bounds);
347        }
348    }
349
350    @Override
351    public int getIntrinsicWidth() {
352        return mDrawable != null ? mDrawable.getIntrinsicWidth() : -1;
353    }
354
355    @Override
356    public int getIntrinsicHeight() {
357        return mDrawable != null ? mDrawable.getIntrinsicHeight() : -1;
358    }
359
360    @Override
361    public void getOutline(@NonNull Outline outline) {
362        if (mDrawable != null) {
363            mDrawable.getOutline(outline);
364        } else {
365            super.getOutline(outline);
366        }
367    }
368
369    @Override
370    @Nullable
371    public ConstantState getConstantState() {
372        if (mState != null && mState.canConstantState()) {
373            mState.mChangingConfigurations = getChangingConfigurations();
374            return mState;
375        }
376        return null;
377    }
378
379    @Override
380    @NonNull
381    public Drawable mutate() {
382        if (!mMutated && super.mutate() == this) {
383            mState = mutateConstantState();
384            if (mDrawable != null) {
385                mDrawable.mutate();
386            }
387            if (mState != null) {
388                mState.mDrawableState = mDrawable != null ? mDrawable.getConstantState() : null;
389            }
390            mMutated = true;
391        }
392        return this;
393    }
394
395    /**
396     * Mutates the constant state and returns the new state. Responsible for
397     * updating any local copy.
398     * <p>
399     * This method should never call the super implementation; it should always
400     * mutate and return its own constant state.
401     *
402     * @return the new state
403     */
404    DrawableWrapperState mutateConstantState() {
405        return mState;
406    }
407
408    /**
409     * @hide Only used by the framework for pre-loading resources.
410     */
411    public void clearMutated() {
412        super.clearMutated();
413        if (mDrawable != null) {
414            mDrawable.clearMutated();
415        }
416        mMutated = false;
417    }
418
419    /**
420     * Called during inflation to inflate the child element. The last valid
421     * child element will take precedence over any other child elements or
422     * explicit drawable attribute.
423     */
424    private void inflateChildDrawable(@NonNull Resources r, @NonNull XmlPullParser parser,
425            @NonNull AttributeSet attrs, @Nullable Theme theme)
426            throws XmlPullParserException, IOException {
427        // Seek to the first child element.
428        Drawable dr = null;
429        int type;
430        final int outerDepth = parser.getDepth();
431        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
432                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
433            if (type == XmlPullParser.START_TAG) {
434                dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
435            }
436        }
437
438        if (dr != null) {
439            setDrawable(dr);
440        }
441    }
442
443    abstract static class DrawableWrapperState extends Drawable.ConstantState {
444        private int[] mThemeAttrs;
445
446        @Config int mChangingConfigurations;
447        int mDensity = DisplayMetrics.DENSITY_DEFAULT;
448
449        Drawable.ConstantState mDrawableState;
450
451        DrawableWrapperState(@Nullable DrawableWrapperState orig, @Nullable Resources res) {
452            if (orig != null) {
453                mThemeAttrs = orig.mThemeAttrs;
454                mChangingConfigurations = orig.mChangingConfigurations;
455                mDrawableState = orig.mDrawableState;
456            }
457
458            final int density;
459            if (res != null) {
460                density = res.getDisplayMetrics().densityDpi;
461            } else if (orig != null) {
462                density = orig.mDensity;
463            } else {
464                density = 0;
465            }
466
467            mDensity = density == 0 ? DisplayMetrics.DENSITY_DEFAULT : density;
468        }
469
470        /**
471         * Sets the constant state density.
472         * <p>
473         * If the density has been previously set, dispatches the change to
474         * subclasses so that density-dependent properties may be scaled as
475         * necessary.
476         *
477         * @param targetDensity the new constant state density
478         */
479        public final void setDensity(int targetDensity) {
480            if (mDensity != targetDensity) {
481                final int sourceDensity = mDensity;
482                mDensity = targetDensity;
483
484                onDensityChanged(sourceDensity, targetDensity);
485            }
486        }
487
488        /**
489         * Called when the constant state density changes.
490         * <p>
491         * Subclasses with density-dependent constant state properties should
492         * override this method and scale their properties as necessary.
493         *
494         * @param sourceDensity the previous constant state density
495         * @param targetDensity the new constant state density
496         */
497        void onDensityChanged(int sourceDensity, int targetDensity) {
498            // Stub method.
499        }
500
501        @Override
502        public boolean canApplyTheme() {
503            return mThemeAttrs != null
504                    || (mDrawableState != null && mDrawableState.canApplyTheme())
505                    || super.canApplyTheme();
506        }
507
508        @Override
509        public Drawable newDrawable() {
510            return newDrawable(null);
511        }
512
513        @Override
514        public abstract Drawable newDrawable(@Nullable Resources res);
515
516        @Override
517        public @Config int getChangingConfigurations() {
518            return mChangingConfigurations
519                    | (mDrawableState != null ? mDrawableState.getChangingConfigurations() : 0);
520        }
521
522        public boolean canConstantState() {
523            return mDrawableState != null;
524        }
525    }
526}
527