1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package android.content.res;
16 
17 import android.annotation.ColorRes;
18 import android.annotation.DrawableRes;
19 import android.content.Context;
20 import android.graphics.drawable.Drawable;
21 
22 import java.lang.ref.WeakReference;
23 
24 /**
25  * Version of resources generated for apps targeting <26.
26  * @hide
27  */
28 public class CompatResources extends Resources {
29 
30     private WeakReference<Context> mContext;
31 
CompatResources(ClassLoader cls)32     public CompatResources(ClassLoader cls) {
33         super(cls);
34         mContext = new WeakReference<>(null);
35     }
36 
37     /**
38      * @hide
39      */
setContext(Context context)40     public void setContext(Context context) {
41         mContext = new WeakReference<>(context);
42     }
43 
44     @Override
getDrawable(@rawableRes int id)45     public Drawable getDrawable(@DrawableRes int id) throws NotFoundException {
46         return getDrawable(id, getTheme());
47     }
48 
49     @Override
getDrawableForDensity(@rawableRes int id, int density)50     public Drawable getDrawableForDensity(@DrawableRes int id, int density)
51             throws NotFoundException {
52         return getDrawableForDensity(id, density, getTheme());
53     }
54 
55     @Override
getColor(@olorRes int id)56     public int getColor(@ColorRes int id) throws NotFoundException {
57         return getColor(id, getTheme());
58     }
59 
60     @Override
getColorStateList(@olorRes int id)61     public ColorStateList getColorStateList(@ColorRes int id) throws NotFoundException {
62         return getColorStateList(id, getTheme());
63     }
64 
getTheme()65     private Theme getTheme() {
66         Context c = mContext.get();
67         return c != null ? c.getTheme() : null;
68     }
69 }
70