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 17 package android.support.v7.widget; 18 19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.graphics.drawable.Drawable; 24 import android.os.Build; 25 import android.support.annotation.NonNull; 26 import android.support.annotation.RestrictTo; 27 import android.support.v7.app.AppCompatDelegate; 28 29 import 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) 38 public class VectorEnabledTintResources extends Resources { 39 shouldBeUsed()40 public static boolean shouldBeUsed() { 41 return AppCompatDelegate.isCompatVectorFromResourcesEnabled() 42 && Build.VERSION.SDK_INT <= MAX_SDK_WHERE_REQUIRED; 43 } 44 45 /** 46 * The maximum API level where this class is needed. 47 */ 48 public static final int MAX_SDK_WHERE_REQUIRED = 20; 49 50 private final WeakReference<Context> mContextRef; 51 VectorEnabledTintResources(@onNull final Context context, @NonNull final Resources res)52 public VectorEnabledTintResources(@NonNull final Context context, 53 @NonNull final Resources res) { 54 super(res.getAssets(), res.getDisplayMetrics(), res.getConfiguration()); 55 mContextRef = new WeakReference<>(context); 56 } 57 58 /** 59 * We intercept this call so that we tint the result (if applicable). This is needed for 60 * things like {@link android.graphics.drawable.DrawableContainer}s which can retrieve 61 * their children via this method. 62 */ 63 @Override getDrawable(int id)64 public Drawable getDrawable(int id) throws NotFoundException { 65 final Context context = mContextRef.get(); 66 if (context != null) { 67 return AppCompatDrawableManager.get().onDrawableLoadedFromResources(context, this, id); 68 } else { 69 return super.getDrawable(id); 70 } 71 } 72 superGetDrawable(int id)73 final Drawable superGetDrawable(int id) { 74 return super.getDrawable(id); 75 } 76 }