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.content.Context;
20import android.content.res.Resources;
21import android.graphics.drawable.Drawable;
22import android.support.annotation.NonNull;
23
24import java.lang.ref.WeakReference;
25
26/**
27 * This class allows us to intercept calls so that we can tint resources (if applicable).
28 */
29class TintResources extends ResourcesWrapper {
30
31    private final WeakReference<Context> mContextRef;
32
33    public TintResources(@NonNull Context context, @NonNull final Resources res) {
34        super(res);
35        mContextRef = new WeakReference<>(context);
36    }
37
38    /**
39     * We intercept this call so that we tint the result (if applicable). This is needed for
40     * things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve
41     * their children via this method.
42     */
43    @Override
44    public Drawable getDrawable(int id) throws NotFoundException {
45        Drawable d = super.getDrawable(id);
46        Context context = mContextRef.get();
47        if (d != null && context != null) {
48            AppCompatDrawableManager.get().tintDrawableUsingColorFilter(context, id, d);
49        }
50        return d;
51    }
52}