18b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen/* 28b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * Copyright (C) 2015 The Android Open Source Project 38b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * 48b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * Licensed under the Apache License, Version 2.0 (the "License"); 58b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * you may not use this file except in compliance with the License. 68b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * You may obtain a copy of the License at 78b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * 88b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * http://www.apache.org/licenses/LICENSE-2.0 98b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * 108b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * Unless required by applicable law or agreed to in writing, software 118b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * distributed under the License is distributed on an "AS IS" BASIS, 128b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * See the License for the specific language governing permissions and 148b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen * limitations under the License. 158b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen */ 168b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenpackage android.support.car.ui; 178b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 188b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.content.Context; 198b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.content.pm.PackageManager.NameNotFoundException; 208b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.content.res.Resources; 218b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.graphics.Color; 228b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.graphics.drawable.ColorDrawable; 238b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.graphics.drawable.Drawable; 2456b49e068c199ff193e5e5dfa8432de7c5365000Yao Chenimport android.util.DisplayMetrics; 258b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenimport android.util.Log; 268b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 278b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 288b7af7188228ca88838e31e070f0fca0d1f90581Yao Chenpublic class CarUiResourceLoader { 298b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen private static final String TAG = "CarUiResourceLoader"; 3056b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen private static final String CAR_UI_PACKAGE = "android.car.ui.provider"; 3156b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen private static final String DRAWABLE = "drawable"; 3256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen private static final String BOOL = "bool"; 3356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen private static final String DIMEN = "dimen"; 348b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 3556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen public static synchronized Drawable getDrawable( 3656b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Context context, String drawableName) { 3756b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return getDrawable(context, drawableName, null); 388b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 398b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 4056b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen public static synchronized Drawable getDrawable( 4156b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Context context, String drawableName, DisplayMetrics metrics) { 4256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Resources res; 4356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen try { 4456b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE); 4556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen } catch (NameNotFoundException e) { 4656b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "CarUiProvider not installed, this class will return blank drawables."); 4756b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return new ColorDrawable(Color.TRANSPARENT); 488b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 498b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 5056b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen int id = res.getIdentifier(drawableName, DRAWABLE, CAR_UI_PACKAGE); 518b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen if (id == 0) { 5256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "Resource not found in CarUiProvider.apk: " + drawableName); 5356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return new ColorDrawable(Color.TRANSPARENT); 548b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 5556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen if (metrics == null) { 5656b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return res.getDrawable(id, null); 578b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } else { 5856b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return res.getDrawableForDensity(id, metrics.densityDpi, null); 598b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 608b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 618b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 628b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen public static synchronized boolean getBoolean( 6356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Context context, String boolName, boolean def) { 6456b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Resources res; 6556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen try { 6656b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE); 6756b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen } catch (NameNotFoundException e) { 6856b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "CarUiProvider not installed, returning default"); 698b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen return def; 708b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 718b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 7256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen int id = res.getIdentifier(boolName, BOOL, CAR_UI_PACKAGE); 738b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen if (id == 0) { 7456b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "Resource not found in CarUiProvider.apk: " + boolName); 758b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen return def; 768b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 7756b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return res.getBoolean(id); 788b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 798b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 8056b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen public static synchronized float getDimen( 8156b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Context context, String dimenName, float def) { 8256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Resources res; 8356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen try { 8456b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen res = context.getPackageManager().getResourcesForApplication(CAR_UI_PACKAGE); 8556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen } catch (NameNotFoundException e) { 8656b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "CarUiProvider not installed, returning default"); 8756b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return def; 888b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 898b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen 9056b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen int id = res.getIdentifier(dimenName, DIMEN, CAR_UI_PACKAGE); 918b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen if (id == 0) { 9256b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen Log.w(TAG, "Resource not found in CarUiProvider.apk: " + dimenName); 9356b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return def; 948b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 9556b49e068c199ff193e5e5dfa8432de7c5365000Yao Chen return res.getDimension(id); 968b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen } 978b7af7188228ca88838e31e070f0fca0d1f90581Yao Chen} 98