1 /* 2 * Copyright (C) 2022 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.quicksearchbox.util 18 19 import android.content.ContentResolver 20 import android.content.Context 21 import android.content.pm.ApplicationInfo 22 import android.content.pm.PackageManager 23 import android.content.res.Resources 24 import android.net.Uri 25 import android.util.Log 26 27 /** General utilities. */ 28 object Util { 29 30 private const val TAG = "QSB.Util" 31 setOfFirstNnull32 fun <A> setOfFirstN(list: List<A>, n: Int): Set<A> { 33 val end: Int = Math.min(list.size, n) 34 val set: HashSet<A> = hashSetOf() 35 for (i in 0 until end) { 36 set.add(list[i]) 37 } 38 return set 39 } 40 getResourceUrinull41 fun getResourceUri(packageContext: Context?, res: Int): Uri? { 42 return try { 43 val resources: Resources? = packageContext?.getResources() 44 getResourceUri(resources, packageContext?.getPackageName(), res) 45 } catch (e: Resources.NotFoundException) { 46 Log.e(TAG, "Resource not found: " + res + " in " + packageContext?.getPackageName()) 47 null 48 } 49 } 50 getResourceUrinull51 fun getResourceUri(context: Context?, appInfo: ApplicationInfo?, res: Int): Uri? { 52 return try { 53 val resources: Resources? = 54 context?.getPackageManager()?.getResourcesForApplication(appInfo!!) 55 getResourceUri(resources, appInfo?.packageName, res) 56 } catch (e: PackageManager.NameNotFoundException) { 57 Log.e(TAG, "Resources not found for " + appInfo?.packageName) 58 null 59 } catch (e: Resources.NotFoundException) { 60 Log.e(TAG, "Resource not found: " + res + " in " + appInfo?.packageName) 61 null 62 } 63 } 64 65 @Throws(Resources.NotFoundException::class) getResourceUrinull66 private fun getResourceUri(resources: Resources?, appPkg: String?, res: Int): Uri { 67 val resPkg: String? = resources?.getResourcePackageName(res) 68 val type: String? = resources?.getResourceTypeName(res) 69 val name: String? = resources?.getResourceEntryName(res) 70 return makeResourceUri(appPkg, resPkg, type, name) 71 } 72 makeResourceUrinull73 private fun makeResourceUri(appPkg: String?, resPkg: String?, type: String?, name: String?): Uri { 74 val uriBuilder: Uri.Builder = Uri.Builder() 75 uriBuilder.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) 76 uriBuilder.encodedAuthority(appPkg) 77 uriBuilder.appendEncodedPath(type) 78 if (appPkg != resPkg) { 79 uriBuilder.appendEncodedPath("$resPkg:$name") 80 } else { 81 uriBuilder.appendEncodedPath(name) 82 } 83 return uriBuilder.build() 84 } 85 } 86