1 /* 2 * Copyright (C) 2020 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 com.android.car.settings.common; 18 19 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON_TINTABLE; 20 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT; 21 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.content.res.Resources; 26 import android.graphics.drawable.Drawable; 27 import android.net.Uri; 28 import android.os.Bundle; 29 import android.text.TextUtils; 30 31 import java.util.List; 32 33 /** Contains utility functions for injected settings. */ 34 public class ExtraSettingsUtil { 35 private static final Logger LOG = new Logger(ExtraSettingsUtil.class); 36 37 /** 38 * Returns whether or not an icon is tintable given the injected setting metadata. 39 */ isIconTintable(Bundle metaData)40 public static boolean isIconTintable(Bundle metaData) { 41 if (metaData.containsKey(META_DATA_PREFERENCE_ICON_TINTABLE)) { 42 return metaData.getBoolean(META_DATA_PREFERENCE_ICON_TINTABLE); 43 } 44 return false; 45 } 46 47 /** 48 * Returns a drawable from an resource's package context. 49 */ loadDrawableFromPackage(Context context, String resPackage, int resId)50 public static Drawable loadDrawableFromPackage(Context context, String resPackage, int resId) { 51 try { 52 return context.createPackageContext(resPackage, /* flags= */ 0) 53 .getDrawable(resId); 54 } catch (PackageManager.NameNotFoundException ex) { 55 LOG.e("loadDrawableFromPackage: package name not found: ", ex); 56 } catch (Resources.NotFoundException ex) { 57 LOG.w("loadDrawableFromPackage: resource not found with id " + resId, ex); 58 } 59 return null; 60 } 61 62 /** 63 * Returns the complete uri from the meta data key of the injected setting metadata. 64 * 65 * A complete uri should contain at least one path segment and be one of the following types: 66 * content://authority/method 67 * content://authority/method/key 68 * 69 * If the uri from the tile is not complete, build a uri by the default method and the 70 * preference key. 71 */ getCompleteUri(Bundle metaData, String metaDataKey, String defaultMethod)72 public static Uri getCompleteUri(Bundle metaData, String metaDataKey, String defaultMethod) { 73 String uriString = metaData.getString(metaDataKey); 74 if (TextUtils.isEmpty(uriString)) { 75 return null; 76 } 77 78 Uri uri = Uri.parse(uriString); 79 List<String> pathSegments = uri.getPathSegments(); 80 if (pathSegments != null && !pathSegments.isEmpty()) { 81 return uri; 82 } 83 84 String key = metaData.getString(META_DATA_PREFERENCE_KEYHINT); 85 if (TextUtils.isEmpty(key)) { 86 LOG.w("Please specify the meta-data " + META_DATA_PREFERENCE_KEYHINT 87 + " in AndroidManifest.xml for " + uriString); 88 return buildUri(uri.getAuthority(), defaultMethod); 89 } 90 return buildUri(uri.getAuthority(), defaultMethod, key); 91 } 92 buildUri(String authority, String method, String key)93 private static Uri buildUri(String authority, String method, String key) { 94 return new Uri.Builder() 95 .scheme(ContentResolver.SCHEME_CONTENT) 96 .authority(authority) 97 .appendPath(method) 98 .appendPath(key) 99 .build(); 100 } 101 buildUri(String authority, String method)102 private static Uri buildUri(String authority, String method) { 103 return new Uri.Builder() 104 .scheme(ContentResolver.SCHEME_CONTENT) 105 .authority(authority) 106 .appendPath(method) 107 .build(); 108 } 109 } 110