blob: f70fc4d864933c761ab5a9706899454748ce036c [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurkaa805e1a2013-08-22 15:00:33 +020019import android.app.Activity;
20import android.content.ActivityNotFoundException;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070021import android.content.ComponentName;
Winson Chungaafa03c2010-06-11 17:34:16 -070022import android.content.Context;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020023import android.content.Intent;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070024import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.PackageManager.NameNotFoundException;
28import android.content.pm.ResolveInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Canvas;
Sunny Goyal95abbb32014-08-04 10:53:22 -070032import android.graphics.Color;
Winson Chungc763c4e2013-07-19 13:49:06 -070033import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070035import android.graphics.PaintFlagsDrawFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.graphics.Rect;
Winson Chungaafa03c2010-06-11 17:34:16 -070037import android.graphics.drawable.BitmapDrawable;
38import android.graphics.drawable.Drawable;
39import android.graphics.drawable.PaintDrawable;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070040import android.os.Build;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020041import android.util.Log;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070042import android.util.Pair;
Sunny Goyal95abbb32014-08-04 10:53:22 -070043import android.util.SparseArray;
Winson Chungc763c4e2013-07-19 13:49:06 -070044import android.view.View;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020045import android.widget.Toast;
Winson Chungc763c4e2013-07-19 13:49:06 -070046
47import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
49/**
50 * Various utilities shared amongst the Launcher's classes.
51 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000052public final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040053 private static final String TAG = "Launcher.Utilities";
54
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private static int sIconWidth = -1;
56 private static int sIconHeight = -1;
Adam Cohen61f560d2013-09-30 15:58:20 -070057 public static int sIconTextureWidth = -1;
58 public static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070061 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 static {
64 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
65 Paint.FILTER_BITMAP_FLAG));
66 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070067 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
68 static int sColorIndex = 0;
69
Adam Cohen63f1ec02014-08-12 09:23:13 -070070 static int[] sLoc0 = new int[2];
71 static int[] sLoc1 = new int[2];
Michael Jurka7ad868b2013-12-12 15:04:25 +010072
73 // To turn on these properties, type
74 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
75 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
76 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
77
Joe Onorato6665c0f2009-09-02 15:27:24 -070078 /**
Winson Chung0dbd7342013-10-13 22:46:20 -070079 * Returns a FastBitmapDrawable with the icon, accurately sized.
80 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070081 static FastBitmapDrawable createIconDrawable(Bitmap icon) {
Winson Chung0dbd7342013-10-13 22:46:20 -070082 FastBitmapDrawable d = new FastBitmapDrawable(icon);
Winson Chung54000492013-10-14 16:29:29 -070083 d.setFilterBitmap(true);
Winson Chung0dbd7342013-10-13 22:46:20 -070084 resizeIconDrawable(d);
85 return d;
86 }
87
88 /**
89 * Resizes an icon drawable to the correct icon size.
90 */
91 static void resizeIconDrawable(Drawable icon) {
92 icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight);
93 }
94
Michael Jurka7ad868b2013-12-12 15:04:25 +010095 private static boolean isPropertyEnabled(String propertyName) {
96 return Log.isLoggable(propertyName, Log.VERBOSE);
97 }
98
99 public static boolean isRotationEnabled(Context c) {
100 boolean enableRotation = sForceEnableRotation ||
101 c.getResources().getBoolean(R.bool.allow_rotation);
102 return enableRotation;
103 }
104
Winson Chung0dbd7342013-10-13 22:46:20 -0700105 /**
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700106 * Indicates if the device is running LMP or not.
107 * TODO(sansid): Change the check to a VERSION_CODES code check once we have a version for L.
108 */
109 public static boolean isLmp() {
110 return "L".equals(Build.VERSION.CODENAME);
111 }
112
113 /**
Michael Jurka931dc972011-08-05 15:08:15 -0700114 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
115 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
116 * to the proper size (48dp)
117 */
118 static Bitmap createIconBitmap(Bitmap icon, Context context) {
119 int textureWidth = sIconTextureWidth;
120 int textureHeight = sIconTextureHeight;
121 int sourceWidth = icon.getWidth();
122 int sourceHeight = icon.getHeight();
123 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
124 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
125 return Bitmap.createBitmap(icon,
126 (sourceWidth - textureWidth) / 2,
127 (sourceHeight - textureHeight) / 2,
128 textureWidth, textureHeight);
129 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
130 // Icon is the right size, no need to change it
131 return icon;
132 } else {
133 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -0700134 final Resources resources = context.getResources();
135 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700136 }
137 }
138
139 /**
140 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700141 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000142 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700143 synchronized (sCanvas) { // we share the statics :-(
144 if (sIconWidth == -1) {
145 initStatics(context);
146 }
147
148 int width = sIconWidth;
149 int height = sIconHeight;
150
Joe Onorato6665c0f2009-09-02 15:27:24 -0700151 if (icon instanceof PaintDrawable) {
152 PaintDrawable painter = (PaintDrawable) icon;
153 painter.setIntrinsicWidth(width);
154 painter.setIntrinsicHeight(height);
155 } else if (icon instanceof BitmapDrawable) {
156 // Ensure the bitmap has a density.
157 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
158 Bitmap bitmap = bitmapDrawable.getBitmap();
159 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
160 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
161 }
162 }
163 int sourceWidth = icon.getIntrinsicWidth();
164 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700165 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700166 // Scale the icon proportionally to the icon dimensions
167 final float ratio = (float) sourceWidth / sourceHeight;
168 if (sourceWidth > sourceHeight) {
169 height = (int) (width / ratio);
170 } else if (sourceHeight > sourceWidth) {
171 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700172 }
173 }
174
175 // no intrinsic size --> use default size
176 int textureWidth = sIconTextureWidth;
177 int textureHeight = sIconTextureHeight;
178
179 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
180 Bitmap.Config.ARGB_8888);
181 final Canvas canvas = sCanvas;
182 canvas.setBitmap(bitmap);
183
184 final int left = (textureWidth-width) / 2;
185 final int top = (textureHeight-height) / 2;
186
Michael Jurka3a9fced2012-04-13 14:44:29 -0700187 @SuppressWarnings("all") // suppress dead code warning
188 final boolean debug = false;
189 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700190 // draw a big box for the icon for debugging
191 canvas.drawColor(sColors[sColorIndex]);
192 if (++sColorIndex >= sColors.length) sColorIndex = 0;
193 Paint debugPaint = new Paint();
194 debugPaint.setColor(0xffcccc00);
195 canvas.drawRect(left, top, left+width, top+height, debugPaint);
196 }
197
198 sOldBounds.set(icon.getBounds());
199 icon.setBounds(left, top, left+width, top+height);
200 icon.draw(canvas);
201 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700202 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700203
204 return bitmap;
205 }
206 }
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 /**
209 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 * @param bitmap The bitmap to get a thumbnail of.
212 * @param context The application's context.
213 *
214 * @return A thumbnail for the specified bitmap or the bitmap itself if the
215 * thumbnail could not be created.
216 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 synchronized (sCanvas) { // we share the statics :-(
219 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700220 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 }
222
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
224 return bitmap;
225 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700226 final Resources resources = context.getResources();
227 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700231
Winson Chungc763c4e2013-07-19 13:49:06 -0700232 /**
233 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
234 * coordinates.
235 *
236 * @param descendant The descendant to which the passed coordinate is relative.
237 * @param root The root view to make the coordinates relative to.
238 * @param coord The coordinate that we want mapped.
239 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
240 * sometimes this is relevant as in a child's coordinates within the descendant.
241 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
242 * this scale factor is assumed to be equal in X and Y, and so if at any point this
243 * assumption fails, we will need to return a pair of scale factors.
244 */
245 public static float getDescendantCoordRelativeToParent(View descendant, View root,
246 int[] coord, boolean includeRootScroll) {
247 ArrayList<View> ancestorChain = new ArrayList<View>();
248
249 float[] pt = {coord[0], coord[1]};
250
251 View v = descendant;
252 while(v != root && v != null) {
253 ancestorChain.add(v);
254 v = (View) v.getParent();
255 }
256 ancestorChain.add(root);
257
258 float scale = 1.0f;
259 int count = ancestorChain.size();
260 for (int i = 0; i < count; i++) {
261 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700262 // For TextViews, scroll has a meaning which relates to the text position
263 // which is very strange... ignore the scroll.
264 if (v0 != descendant || includeRootScroll) {
265 pt[0] -= v0.getScrollX();
266 pt[1] -= v0.getScrollY();
267 }
268
269 v0.getMatrix().mapPoints(pt);
270 pt[0] += v0.getLeft();
271 pt[1] += v0.getTop();
272 scale *= v0.getScaleX();
273 }
274
275 coord[0] = (int) Math.round(pt[0]);
276 coord[1] = (int) Math.round(pt[1]);
277 return scale;
278 }
279
280 /**
281 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
282 */
283 public static float mapCoordInSelfToDescendent(View descendant, View root,
284 int[] coord) {
285 ArrayList<View> ancestorChain = new ArrayList<View>();
286
287 float[] pt = {coord[0], coord[1]};
288
289 View v = descendant;
290 while(v != root) {
291 ancestorChain.add(v);
292 v = (View) v.getParent();
293 }
294 ancestorChain.add(root);
295
296 float scale = 1.0f;
297 Matrix inverse = new Matrix();
298 int count = ancestorChain.size();
299 for (int i = count - 1; i >= 0; i--) {
300 View ancestor = ancestorChain.get(i);
301 View next = i > 0 ? ancestorChain.get(i-1) : null;
302
303 pt[0] += ancestor.getScrollX();
304 pt[1] += ancestor.getScrollY();
305
306 if (next != null) {
307 pt[0] -= next.getLeft();
308 pt[1] -= next.getTop();
309 next.getMatrix().invert(inverse);
310 inverse.mapPoints(pt);
311 scale *= next.getScaleX();
312 }
313 }
314
315 coord[0] = (int) Math.round(pt[0]);
316 coord[1] = (int) Math.round(pt[1]);
317 return scale;
318 }
319
Jason Monk02dd7ae2014-04-15 15:23:31 -0400320 /**
321 * Utility method to determine whether the given point, in local coordinates,
322 * is inside the view, where the area of the view is expanded by the slop factor.
323 * This method is called while processing touch-move events to determine if the event
324 * is still within the view.
325 */
326 public static boolean pointInView(View v, float localX, float localY, float slop) {
327 return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
328 localY < (v.getHeight() + slop);
329 }
330
Joe Onorato6665c0f2009-09-02 15:27:24 -0700331 private static void initStatics(Context context) {
332 final Resources resources = context.getResources();
Michael Jurkac9a96192010-11-01 11:52:08 -0700333 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700334 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700335 }
336
Winson Chung5f8afe62013-08-12 16:19:28 -0700337 public static void setIconSize(int widthPx) {
338 sIconWidth = sIconHeight = widthPx;
339 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700340 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200341
Winson Chung3a6e7f32013-10-09 15:50:52 -0700342 public static void scaleRect(Rect r, float scale) {
343 if (scale != 1.0f) {
344 r.left = (int) (r.left * scale + 0.5f);
345 r.top = (int) (r.top * scale + 0.5f);
346 r.right = (int) (r.right * scale + 0.5f);
347 r.bottom = (int) (r.bottom * scale + 0.5f);
348 }
349 }
350
Adam Cohen63f1ec02014-08-12 09:23:13 -0700351 public static int[] getCenterDeltaInScreenSpace(View v0, View v1, int[] delta) {
352 v0.getLocationInWindow(sLoc0);
353 v1.getLocationInWindow(sLoc1);
354
355 sLoc0[0] += (v0.getMeasuredWidth() * v0.getScaleX()) / 2;
356 sLoc0[1] += (v0.getMeasuredHeight() * v0.getScaleY()) / 2;
357 sLoc1[0] += (v1.getMeasuredWidth() * v1.getScaleX()) / 2;
358 sLoc1[1] += (v1.getMeasuredHeight() * v1.getScaleY()) / 2;
359
360 if (delta == null) {
361 delta = new int[2];
362 }
363
364 delta[0] = sLoc1[0] - sLoc0[0];
365 delta[1] = sLoc1[1] - sLoc0[1];
366
367 return delta;
368 }
369
Winson Chung3a6e7f32013-10-09 15:50:52 -0700370 public static void scaleRectAboutCenter(Rect r, float scale) {
371 int cx = r.centerX();
372 int cy = r.centerY();
373 r.offset(-cx, -cy);
374 Utilities.scaleRect(r, scale);
375 r.offset(cx, cy);
376 }
377
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200378 public static void startActivityForResultSafely(
379 Activity activity, Intent intent, int requestCode) {
380 try {
381 activity.startActivityForResult(intent, requestCode);
382 } catch (ActivityNotFoundException e) {
383 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
384 } catch (SecurityException e) {
385 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
386 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
387 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
388 "or use the exported attribute for this activity.", e);
389 }
390 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700391
392 static boolean isSystemApp(Context context, Intent intent) {
393 PackageManager pm = context.getPackageManager();
394 ComponentName cn = intent.getComponent();
395 String packageName = null;
396 if (cn == null) {
397 ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
398 if ((info != null) && (info.activityInfo != null)) {
399 packageName = info.activityInfo.packageName;
400 }
401 } else {
402 packageName = cn.getPackageName();
403 }
404 if (packageName != null) {
405 try {
406 PackageInfo info = pm.getPackageInfo(packageName, 0);
407 return (info != null) && (info.applicationInfo != null) &&
408 ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
409 } catch (NameNotFoundException e) {
410 return false;
411 }
412 } else {
413 return false;
414 }
415 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700416
417 /**
418 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
419 * @param bitmap The bitmap to scan
420 * @param samples The approximate max number of samples to use.
421 */
422 static int findDominantColorByHue(Bitmap bitmap, int samples) {
423 final int height = bitmap.getHeight();
424 final int width = bitmap.getWidth();
425 int sampleStride = (int) Math.sqrt((height * width) / samples);
426 if (sampleStride < 1) {
427 sampleStride = 1;
428 }
429
430 // This is an out-param, for getting the hsv values for an rgb
431 float[] hsv = new float[3];
432
433 // First get the best hue, by creating a histogram over 360 hue buckets,
434 // where each pixel contributes a score weighted by saturation, value, and alpha.
435 float[] hueScoreHistogram = new float[360];
436 float highScore = -1;
437 int bestHue = -1;
438
439 for (int y = 0; y < height; y += sampleStride) {
440 for (int x = 0; x < width; x += sampleStride) {
441 int argb = bitmap.getPixel(x, y);
442 int alpha = 0xFF & (argb >> 24);
443 if (alpha < 0x80) {
444 // Drop mostly-transparent pixels.
445 continue;
446 }
447 // Remove the alpha channel.
448 int rgb = argb | 0xFF000000;
449 Color.colorToHSV(rgb, hsv);
450 // Bucket colors by the 360 integer hues.
451 int hue = (int) hsv[0];
452 if (hue < 0 || hue >= hueScoreHistogram.length) {
453 // Defensively avoid array bounds violations.
454 continue;
455 }
456 float score = hsv[1] * hsv[2];
457 hueScoreHistogram[hue] += score;
458 if (hueScoreHistogram[hue] > highScore) {
459 highScore = hueScoreHistogram[hue];
460 bestHue = hue;
461 }
462 }
463 }
464
465 SparseArray<Float> rgbScores = new SparseArray<Float>();
466 int bestColor = 0xff000000;
467 highScore = -1;
468 // Go back over the RGB colors that match the winning hue,
469 // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
470 // The highest-scoring RGB color wins.
471 for (int y = 0; y < height; y += sampleStride) {
472 for (int x = 0; x < width; x += sampleStride) {
473 int rgb = bitmap.getPixel(x, y) | 0xff000000;
474 Color.colorToHSV(rgb, hsv);
475 int hue = (int) hsv[0];
476 if (hue == bestHue) {
477 float s = hsv[1];
478 float v = hsv[2];
479 int bucket = (int) (s * 100) + (int) (v * 10000);
480 // Score by cumulative saturation * value.
481 float score = s * v;
482 Float oldTotal = rgbScores.get(bucket);
483 float newTotal = oldTotal == null ? score : oldTotal + score;
484 rgbScores.put(bucket, newTotal);
485 if (newTotal > highScore) {
486 highScore = newTotal;
487 // All the colors in the winning bucket are very similar. Last in wins.
488 bestColor = rgb;
489 }
490 }
491 }
492 }
493 return bestColor;
494 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700495
496 /*
497 * Finds a system apk which had a broadcast receiver listening to a particular action.
498 * @param action intent action used to find the apk
499 * @return a pair of apk package name and the resources.
500 */
501 static Pair<String, Resources> findSystemApk(String action, PackageManager pm) {
502 final Intent intent = new Intent(action);
503 for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
504 if (info.activityInfo != null &&
505 (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
506 final String packageName = info.activityInfo.packageName;
507 try {
508 final Resources res = pm.getResourcesForApplication(packageName);
509 return Pair.create(packageName, res);
510 } catch (NameNotFoundException e) {
511 Log.w(TAG, "Failed to find resources for " + packageName);
512 }
513 }
514 }
515 return null;
516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517}