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 androidx.appcompat.widget;
18
19import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.drawable.Drawable;
24import android.os.Build;
25
26import androidx.annotation.NonNull;
27import androidx.annotation.RestrictTo;
28
29import java.lang.ref.WeakReference;
30
31/**
32 * This class allows us to intercept calls so that we can tint resources (if applicable), and
33 * inflate vector resources from within drawable containers pre-L.
34 *
35 * @hide
36 */
37@RestrictTo(LIBRARY_GROUP)
38public class VectorEnabledTintResources extends Resources {
39    private static boolean sCompatVectorFromResourcesEnabled = false;
40
41    public static boolean shouldBeUsed() {
42        return isCompatVectorFromResourcesEnabled()
43                && Build.VERSION.SDK_INT <= MAX_SDK_WHERE_REQUIRED;
44    }
45
46    /**
47     * The maximum API level where this class is needed.
48     */
49    public static final int MAX_SDK_WHERE_REQUIRED = 20;
50
51    private final WeakReference<Context> mContextRef;
52
53    public VectorEnabledTintResources(@NonNull final Context context,
54            @NonNull final Resources res) {
55        super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration());
56        mContextRef = new WeakReference<>(context);
57    }
58
59    /**
60     * We intercept this call so that we tint the result (if applicable). This is needed for
61     * things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve
62     * their children via this method.
63     */
64    @Override
65    public Drawable getDrawable(int id) throws NotFoundException {
66        final Context context = mContextRef.get();
67        if (context != null) {
68            return AppCompatDrawableManager.get().onDrawableLoadedFromResources(context, this, id);
69        } else {
70            return super.getDrawable(id);
71        }
72    }
73
74    final Drawable superGetDrawable(int id) {
75        return super.getDrawable(id);
76    }
77
78    /**
79     * Sets whether vector drawables on older platforms (< API 21) can be used within
80     * {@link android.graphics.drawable.DrawableContainer} resources.
81     */
82    public static void setCompatVectorFromResourcesEnabled(boolean enabled) {
83        sCompatVectorFromResourcesEnabled = enabled;
84    }
85
86    /**
87     * Returns whether vector drawables on older platforms (< API 21) can be accessed from within
88     * resources.
89     *
90     * @see #setCompatVectorFromResourcesEnabled(boolean)
91     */
92    public static boolean isCompatVectorFromResourcesEnabled() {
93        return sCompatVectorFromResourcesEnabled;
94    }
95}