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 package android.support.car.ui;
17 
18 import android.content.Context;
19 import android.content.pm.PackageManager.NameNotFoundException;
20 import android.content.res.Resources;
21 import android.graphics.Color;
22 import android.graphics.drawable.ColorDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.util.DisplayMetrics;
25 import android.util.Log;
26 
27 
28 public class CarUiResourceLoader {
29     private static final String TAG = "CarUiResourceLoader";
30     private static final String CAR_UI_PACKAGE = "android.car.ui.provider";
31     private static final String DRAWABLE = "drawable";
32     private static final String BOOL = "bool";
33     private static final String DIMEN = "dimen";
34 
getDrawable( Context context, String drawableName)35     public static synchronized Drawable getDrawable(
36             Context context, String drawableName) {
37         return getDrawable(context, drawableName, null);
38     }
39 
getDrawable( Context context, String drawableName, DisplayMetrics metrics)40     public static synchronized Drawable getDrawable(
41             Context context, String drawableName, DisplayMetrics metrics) {
42         Resources res;
43         try {
44             res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE);
45         } catch (NameNotFoundException e) {
46             Log.w(TAG, "CarUiProvider not installed, this class will return blank drawables.");
47             return new ColorDrawable(Color.TRANSPARENT);
48         }
49 
50         int id = res.getIdentifier(drawableName, DRAWABLE, CAR_UI_PACKAGE);
51         if (id == 0) {
52             Log.w(TAG, "Resource not found in CarUiProvider.apk: " + drawableName);
53             return new ColorDrawable(Color.TRANSPARENT);
54         }
55         if (metrics == null) {
56             return res.getDrawable(id, null);
57         } else {
58             return res.getDrawableForDensity(id, metrics.densityDpi, null);
59         }
60     }
61 
getBoolean( Context context, String boolName, boolean def)62     public static synchronized boolean getBoolean(
63             Context context, String boolName, boolean def) {
64         Resources res;
65         try {
66             res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE);
67         } catch (NameNotFoundException e) {
68             Log.w(TAG, "CarUiProvider not installed, returning default");
69             return def;
70         }
71 
72         int id = res.getIdentifier(boolName, BOOL, CAR_UI_PACKAGE);
73         if (id == 0) {
74             Log.w(TAG, "Resource not found in CarUiProvider.apk: " + boolName);
75             return def;
76         }
77         return res.getBoolean(id);
78     }
79 
getDimen( Context context, String dimenName, float def)80     public static synchronized float getDimen(
81             Context context, String dimenName, float def) {
82         Resources res;
83         try {
84             res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE);
85         } catch (NameNotFoundException e) {
86             Log.w(TAG, "CarUiProvider not installed, returning default");
87             return def;
88         }
89 
90         int id = res.getIdentifier(dimenName, DIMEN, CAR_UI_PACKAGE);
91         if (id == 0) {
92             Log.w(TAG, "Resource not found in CarUiProvider.apk: " + dimenName);
93             return def;
94         }
95         return res.getDimension(id);
96     }
97 }
98