• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 
31 /**
32  * General utilities.
33  */
34 public class Util {
35 
36     private static final String TAG = "QSB.Util";
37 
setOfFirstN(List<A> list, int n)38     public static <A> Set<A> setOfFirstN(List<A> list, int n) {
39         int end = Math.min(list.size(), n);
40         HashSet<A> set = new HashSet<A>(end);
41         for (int i = 0; i < end; i++) {
42             set.add(list.get(i));
43         }
44         return set;
45     }
46 
getResourceUri(Context packageContext, int res)47     public static Uri getResourceUri(Context packageContext, int res) {
48         try {
49             Resources resources = packageContext.getResources();
50             return getResourceUri(resources, packageContext.getPackageName(), res);
51         } catch (Resources.NotFoundException e) {
52             Log.e(TAG, "Resource not found: " + res + " in " + packageContext.getPackageName());
53             return null;
54         }
55     }
56 
getResourceUri(Context context, ApplicationInfo appInfo, int res)57     public static Uri getResourceUri(Context context, ApplicationInfo appInfo, int res) {
58         try {
59             Resources resources = context.getPackageManager().getResourcesForApplication(appInfo);
60             return getResourceUri(resources, appInfo.packageName, res);
61         } catch (PackageManager.NameNotFoundException e) {
62             Log.e(TAG, "Resources not found for " + appInfo.packageName);
63             return null;
64         } catch (Resources.NotFoundException e) {
65             Log.e(TAG, "Resource not found: " + res + " in " + appInfo.packageName);
66             return null;
67         }
68     }
69 
getResourceUri(Resources resources, String appPkg, int res)70     private static Uri getResourceUri(Resources resources, String appPkg, int res)
71             throws Resources.NotFoundException {
72         String resPkg = resources.getResourcePackageName(res);
73         String type = resources.getResourceTypeName(res);
74         String name = resources.getResourceEntryName(res);
75         return makeResourceUri(appPkg, resPkg, type, name);
76     }
77 
makeResourceUri(String appPkg, String resPkg, String type, String name)78     private static Uri makeResourceUri(String appPkg, String resPkg, String type, String name) {
79         Uri.Builder uriBuilder = new Uri.Builder();
80         uriBuilder.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE);
81         uriBuilder.encodedAuthority(appPkg);
82         uriBuilder.appendEncodedPath(type);
83         if (!appPkg.equals(resPkg)) {
84             uriBuilder.appendEncodedPath(resPkg + ":" + name);
85         } else {
86             uriBuilder.appendEncodedPath(name);
87         }
88         return uriBuilder.build();
89     }
90 }
91