blob: 497b43874e962a810f3a29558c74066080ac24d7 [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
Sunny Goyalfafca522014-11-03 11:30:01 -080019import android.annotation.TargetApi;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020020import android.app.Activity;
Sunny Goyal594d76d2014-11-06 10:12:54 -080021import android.app.SearchManager;
22import android.appwidget.AppWidgetManager;
23import android.appwidget.AppWidgetProviderInfo;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020024import android.content.ActivityNotFoundException;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070025import android.content.ComponentName;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.content.Context;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020027import android.content.Intent;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070028import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.PackageManager.NameNotFoundException;
32import android.content.pm.ResolveInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.content.res.Resources;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080034import android.database.Cursor;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Bitmap;
Sunny Goyal4fbc3822015-02-18 16:46:50 -080036import android.graphics.BitmapFactory;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.graphics.Canvas;
Sunny Goyal95abbb32014-08-04 10:53:22 -070038import android.graphics.Color;
Winson Chungc763c4e2013-07-19 13:49:06 -070039import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070041import android.graphics.PaintFlagsDrawFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.graphics.Rect;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.graphics.drawable.BitmapDrawable;
44import android.graphics.drawable.Drawable;
45import android.graphics.drawable.PaintDrawable;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070046import android.os.Build;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020047import android.util.Log;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070048import android.util.Pair;
Sunny Goyal95abbb32014-08-04 10:53:22 -070049import android.util.SparseArray;
Winson Chungc763c4e2013-07-19 13:49:06 -070050import android.view.View;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020051import android.widget.Toast;
Winson Chungc763c4e2013-07-19 13:49:06 -070052
53import java.util.ArrayList;
Sunny Goyal08f72612015-01-05 13:41:43 -080054import java.util.Comparator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
56/**
57 * Various utilities shared amongst the Launcher's classes.
58 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000059public final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040060 private static final String TAG = "Launcher.Utilities";
61
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062 private static int sIconWidth = -1;
63 private static int sIconHeight = -1;
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070066 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067
68 static {
69 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
70 Paint.FILTER_BITMAP_FLAG));
71 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070072 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
73 static int sColorIndex = 0;
74
Adam Cohen63f1ec02014-08-12 09:23:13 -070075 static int[] sLoc0 = new int[2];
76 static int[] sLoc1 = new int[2];
Michael Jurka7ad868b2013-12-12 15:04:25 +010077
78 // To turn on these properties, type
79 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
80 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
81 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
82
Joe Onorato6665c0f2009-09-02 15:27:24 -070083 /**
Winson Chung0dbd7342013-10-13 22:46:20 -070084 * Returns a FastBitmapDrawable with the icon, accurately sized.
85 */
Sunny Goyalffe83f12014-08-14 17:39:34 -070086 public static FastBitmapDrawable createIconDrawable(Bitmap icon) {
Winson Chung0dbd7342013-10-13 22:46:20 -070087 FastBitmapDrawable d = new FastBitmapDrawable(icon);
Winson Chung54000492013-10-14 16:29:29 -070088 d.setFilterBitmap(true);
Winson Chung0dbd7342013-10-13 22:46:20 -070089 resizeIconDrawable(d);
90 return d;
91 }
92
93 /**
94 * Resizes an icon drawable to the correct icon size.
95 */
96 static void resizeIconDrawable(Drawable icon) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -070097 icon.setBounds(0, 0, sIconWidth, sIconHeight);
Winson Chung0dbd7342013-10-13 22:46:20 -070098 }
99
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800100 public static boolean isPropertyEnabled(String propertyName) {
Michael Jurka7ad868b2013-12-12 15:04:25 +0100101 return Log.isLoggable(propertyName, Log.VERBOSE);
102 }
103
104 public static boolean isRotationEnabled(Context c) {
105 boolean enableRotation = sForceEnableRotation ||
106 c.getResources().getBoolean(R.bool.allow_rotation);
107 return enableRotation;
108 }
109
Winson Chung0dbd7342013-10-13 22:46:20 -0700110 /**
Kenny Guyd794a3f2014-09-16 15:17:58 +0100111 * Indicates if the device is running LMP or higher.
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700112 */
Kenny Guyd794a3f2014-09-16 15:17:58 +0100113 public static boolean isLmpOrAbove() {
Brian Parfett1bc1ae62014-12-12 12:30:00 -0800114 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700115 }
116
Sunny Goyal4fbc3822015-02-18 16:46:50 -0800117 static Bitmap createIconBitmap(Cursor c, int iconIndex, Context context) {
118 byte[] data = c.getBlob(iconIndex);
119 try {
120 return createIconBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), context);
121 } catch (Exception e) {
122 return null;
123 }
124 }
125
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700126 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -0700127 * Returns a bitmap suitable for the all apps view. If the package or the resource do not
128 * exist, it returns null.
129 */
130 static Bitmap createIconBitmap(String packageName, String resourceName, IconCache cache,
131 Context context) {
132 PackageManager packageManager = context.getPackageManager();
133 // the resource
134 try {
135 Resources resources = packageManager.getResourcesForApplication(packageName);
136 if (resources != null) {
137 final int id = resources.getIdentifier(resourceName, null, null);
138 return createIconBitmap(
139 resources.getDrawableForDensity(id, cache.getFullResIconDpi()), context);
140 }
141 } catch (Exception e) {
142 // Icon not found.
143 }
144 return null;
145 }
146
147 /**
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700148 * Returns a bitmap which is of the appropriate size to be displayed as an icon
Michael Jurka931dc972011-08-05 15:08:15 -0700149 */
150 static Bitmap createIconBitmap(Bitmap icon, Context context) {
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700151 synchronized (sCanvas) { // we share the statics :-(
152 if (sIconWidth == -1) {
153 initStatics(context);
154 }
Michael Jurka931dc972011-08-05 15:08:15 -0700155 }
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700156 if (sIconWidth == icon.getWidth() && sIconHeight == icon.getHeight()) {
157 return icon;
158 }
159 return createIconBitmap(new BitmapDrawable(context.getResources(), icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700160 }
161
162 /**
163 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700164 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000165 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700166 synchronized (sCanvas) { // we share the statics :-(
167 if (sIconWidth == -1) {
168 initStatics(context);
169 }
170
171 int width = sIconWidth;
172 int height = sIconHeight;
173
Joe Onorato6665c0f2009-09-02 15:27:24 -0700174 if (icon instanceof PaintDrawable) {
175 PaintDrawable painter = (PaintDrawable) icon;
176 painter.setIntrinsicWidth(width);
177 painter.setIntrinsicHeight(height);
178 } else if (icon instanceof BitmapDrawable) {
179 // Ensure the bitmap has a density.
180 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
181 Bitmap bitmap = bitmapDrawable.getBitmap();
182 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
183 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
184 }
185 }
186 int sourceWidth = icon.getIntrinsicWidth();
187 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700188 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700189 // Scale the icon proportionally to the icon dimensions
190 final float ratio = (float) sourceWidth / sourceHeight;
191 if (sourceWidth > sourceHeight) {
192 height = (int) (width / ratio);
193 } else if (sourceHeight > sourceWidth) {
194 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700195 }
196 }
197
198 // no intrinsic size --> use default size
Sunny Goyal2fce90c2014-10-07 12:01:58 -0700199 int textureWidth = sIconWidth;
200 int textureHeight = sIconHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700201
202 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
203 Bitmap.Config.ARGB_8888);
204 final Canvas canvas = sCanvas;
205 canvas.setBitmap(bitmap);
206
207 final int left = (textureWidth-width) / 2;
208 final int top = (textureHeight-height) / 2;
209
Michael Jurka3a9fced2012-04-13 14:44:29 -0700210 @SuppressWarnings("all") // suppress dead code warning
211 final boolean debug = false;
212 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700213 // draw a big box for the icon for debugging
214 canvas.drawColor(sColors[sColorIndex]);
215 if (++sColorIndex >= sColors.length) sColorIndex = 0;
216 Paint debugPaint = new Paint();
217 debugPaint.setColor(0xffcccc00);
218 canvas.drawRect(left, top, left+width, top+height, debugPaint);
219 }
220
221 sOldBounds.set(icon.getBounds());
222 icon.setBounds(left, top, left+width, top+height);
223 icon.draw(canvas);
224 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700225 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700226
227 return bitmap;
228 }
229 }
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 /**
Winson Chungc763c4e2013-07-19 13:49:06 -0700232 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
233 * coordinates.
234 *
235 * @param descendant The descendant to which the passed coordinate is relative.
236 * @param root The root view to make the coordinates relative to.
237 * @param coord The coordinate that we want mapped.
238 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
239 * sometimes this is relevant as in a child's coordinates within the descendant.
240 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
241 * this scale factor is assumed to be equal in X and Y, and so if at any point this
242 * assumption fails, we will need to return a pair of scale factors.
243 */
244 public static float getDescendantCoordRelativeToParent(View descendant, View root,
245 int[] coord, boolean includeRootScroll) {
246 ArrayList<View> ancestorChain = new ArrayList<View>();
247
248 float[] pt = {coord[0], coord[1]};
249
250 View v = descendant;
251 while(v != root && v != null) {
252 ancestorChain.add(v);
253 v = (View) v.getParent();
254 }
255 ancestorChain.add(root);
256
257 float scale = 1.0f;
258 int count = ancestorChain.size();
259 for (int i = 0; i < count; i++) {
260 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700261 // For TextViews, scroll has a meaning which relates to the text position
262 // which is very strange... ignore the scroll.
263 if (v0 != descendant || includeRootScroll) {
264 pt[0] -= v0.getScrollX();
265 pt[1] -= v0.getScrollY();
266 }
267
268 v0.getMatrix().mapPoints(pt);
269 pt[0] += v0.getLeft();
270 pt[1] += v0.getTop();
271 scale *= v0.getScaleX();
272 }
273
274 coord[0] = (int) Math.round(pt[0]);
275 coord[1] = (int) Math.round(pt[1]);
276 return scale;
277 }
278
279 /**
280 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
281 */
282 public static float mapCoordInSelfToDescendent(View descendant, View root,
283 int[] coord) {
284 ArrayList<View> ancestorChain = new ArrayList<View>();
285
286 float[] pt = {coord[0], coord[1]};
287
288 View v = descendant;
289 while(v != root) {
290 ancestorChain.add(v);
291 v = (View) v.getParent();
292 }
293 ancestorChain.add(root);
294
295 float scale = 1.0f;
296 Matrix inverse = new Matrix();
297 int count = ancestorChain.size();
298 for (int i = count - 1; i >= 0; i--) {
299 View ancestor = ancestorChain.get(i);
300 View next = i > 0 ? ancestorChain.get(i-1) : null;
301
302 pt[0] += ancestor.getScrollX();
303 pt[1] += ancestor.getScrollY();
304
305 if (next != null) {
306 pt[0] -= next.getLeft();
307 pt[1] -= next.getTop();
308 next.getMatrix().invert(inverse);
309 inverse.mapPoints(pt);
310 scale *= next.getScaleX();
311 }
312 }
313
314 coord[0] = (int) Math.round(pt[0]);
315 coord[1] = (int) Math.round(pt[1]);
316 return scale;
317 }
318
Jason Monk02dd7ae2014-04-15 15:23:31 -0400319 /**
320 * Utility method to determine whether the given point, in local coordinates,
321 * is inside the view, where the area of the view is expanded by the slop factor.
322 * This method is called while processing touch-move events to determine if the event
323 * is still within the view.
324 */
325 public static boolean pointInView(View v, float localX, float localY, float slop) {
326 return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
327 localY < (v.getHeight() + slop);
328 }
329
Joe Onorato6665c0f2009-09-02 15:27:24 -0700330 private static void initStatics(Context context) {
331 final Resources resources = context.getResources();
Michael Jurkac9a96192010-11-01 11:52:08 -0700332 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 }
334
Winson Chung5f8afe62013-08-12 16:19:28 -0700335 public static void setIconSize(int widthPx) {
336 sIconWidth = sIconHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700337 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200338
Winson Chung3a6e7f32013-10-09 15:50:52 -0700339 public static void scaleRect(Rect r, float scale) {
340 if (scale != 1.0f) {
341 r.left = (int) (r.left * scale + 0.5f);
342 r.top = (int) (r.top * scale + 0.5f);
343 r.right = (int) (r.right * scale + 0.5f);
344 r.bottom = (int) (r.bottom * scale + 0.5f);
345 }
346 }
347
Adam Cohen63f1ec02014-08-12 09:23:13 -0700348 public static int[] getCenterDeltaInScreenSpace(View v0, View v1, int[] delta) {
349 v0.getLocationInWindow(sLoc0);
350 v1.getLocationInWindow(sLoc1);
351
352 sLoc0[0] += (v0.getMeasuredWidth() * v0.getScaleX()) / 2;
353 sLoc0[1] += (v0.getMeasuredHeight() * v0.getScaleY()) / 2;
354 sLoc1[0] += (v1.getMeasuredWidth() * v1.getScaleX()) / 2;
355 sLoc1[1] += (v1.getMeasuredHeight() * v1.getScaleY()) / 2;
356
357 if (delta == null) {
358 delta = new int[2];
359 }
360
361 delta[0] = sLoc1[0] - sLoc0[0];
362 delta[1] = sLoc1[1] - sLoc0[1];
363
364 return delta;
365 }
366
Winson Chung3a6e7f32013-10-09 15:50:52 -0700367 public static void scaleRectAboutCenter(Rect r, float scale) {
368 int cx = r.centerX();
369 int cy = r.centerY();
370 r.offset(-cx, -cy);
371 Utilities.scaleRect(r, scale);
372 r.offset(cx, cy);
373 }
374
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200375 public static void startActivityForResultSafely(
376 Activity activity, Intent intent, int requestCode) {
377 try {
378 activity.startActivityForResult(intent, requestCode);
379 } catch (ActivityNotFoundException e) {
380 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
381 } catch (SecurityException e) {
382 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
383 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
384 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
385 "or use the exported attribute for this activity.", e);
386 }
387 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700388
389 static boolean isSystemApp(Context context, Intent intent) {
390 PackageManager pm = context.getPackageManager();
391 ComponentName cn = intent.getComponent();
392 String packageName = null;
393 if (cn == null) {
394 ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
395 if ((info != null) && (info.activityInfo != null)) {
396 packageName = info.activityInfo.packageName;
397 }
398 } else {
399 packageName = cn.getPackageName();
400 }
401 if (packageName != null) {
402 try {
403 PackageInfo info = pm.getPackageInfo(packageName, 0);
404 return (info != null) && (info.applicationInfo != null) &&
405 ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
406 } catch (NameNotFoundException e) {
407 return false;
408 }
409 } else {
410 return false;
411 }
412 }
Sunny Goyal95abbb32014-08-04 10:53:22 -0700413
414 /**
415 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
416 * @param bitmap The bitmap to scan
417 * @param samples The approximate max number of samples to use.
418 */
419 static int findDominantColorByHue(Bitmap bitmap, int samples) {
420 final int height = bitmap.getHeight();
421 final int width = bitmap.getWidth();
422 int sampleStride = (int) Math.sqrt((height * width) / samples);
423 if (sampleStride < 1) {
424 sampleStride = 1;
425 }
426
427 // This is an out-param, for getting the hsv values for an rgb
428 float[] hsv = new float[3];
429
430 // First get the best hue, by creating a histogram over 360 hue buckets,
431 // where each pixel contributes a score weighted by saturation, value, and alpha.
432 float[] hueScoreHistogram = new float[360];
433 float highScore = -1;
434 int bestHue = -1;
435
436 for (int y = 0; y < height; y += sampleStride) {
437 for (int x = 0; x < width; x += sampleStride) {
438 int argb = bitmap.getPixel(x, y);
439 int alpha = 0xFF & (argb >> 24);
440 if (alpha < 0x80) {
441 // Drop mostly-transparent pixels.
442 continue;
443 }
444 // Remove the alpha channel.
445 int rgb = argb | 0xFF000000;
446 Color.colorToHSV(rgb, hsv);
447 // Bucket colors by the 360 integer hues.
448 int hue = (int) hsv[0];
449 if (hue < 0 || hue >= hueScoreHistogram.length) {
450 // Defensively avoid array bounds violations.
451 continue;
452 }
453 float score = hsv[1] * hsv[2];
454 hueScoreHistogram[hue] += score;
455 if (hueScoreHistogram[hue] > highScore) {
456 highScore = hueScoreHistogram[hue];
457 bestHue = hue;
458 }
459 }
460 }
461
462 SparseArray<Float> rgbScores = new SparseArray<Float>();
463 int bestColor = 0xff000000;
464 highScore = -1;
465 // Go back over the RGB colors that match the winning hue,
466 // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
467 // The highest-scoring RGB color wins.
468 for (int y = 0; y < height; y += sampleStride) {
469 for (int x = 0; x < width; x += sampleStride) {
470 int rgb = bitmap.getPixel(x, y) | 0xff000000;
471 Color.colorToHSV(rgb, hsv);
472 int hue = (int) hsv[0];
473 if (hue == bestHue) {
474 float s = hsv[1];
475 float v = hsv[2];
476 int bucket = (int) (s * 100) + (int) (v * 10000);
477 // Score by cumulative saturation * value.
478 float score = s * v;
479 Float oldTotal = rgbScores.get(bucket);
480 float newTotal = oldTotal == null ? score : oldTotal + score;
481 rgbScores.put(bucket, newTotal);
482 if (newTotal > highScore) {
483 highScore = newTotal;
484 // All the colors in the winning bucket are very similar. Last in wins.
485 bestColor = rgb;
486 }
487 }
488 }
489 }
490 return bestColor;
491 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700492
493 /*
494 * Finds a system apk which had a broadcast receiver listening to a particular action.
495 * @param action intent action used to find the apk
496 * @return a pair of apk package name and the resources.
497 */
498 static Pair<String, Resources> findSystemApk(String action, PackageManager pm) {
499 final Intent intent = new Intent(action);
500 for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
501 if (info.activityInfo != null &&
502 (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
503 final String packageName = info.activityInfo.packageName;
504 try {
505 final Resources res = pm.getResourcesForApplication(packageName);
506 return Pair.create(packageName, res);
507 } catch (NameNotFoundException e) {
508 Log.w(TAG, "Failed to find resources for " + packageName);
509 }
510 }
511 }
512 return null;
513 }
Sunny Goyalfafca522014-11-03 11:30:01 -0800514
515 @TargetApi(Build.VERSION_CODES.KITKAT)
516 public static boolean isViewAttachedToWindow(View v) {
517 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
518 return v.isAttachedToWindow();
519 } else {
520 // A proxy call which returns null, if the view is not attached to the window.
521 return v.getKeyDispatcherState() != null;
522 }
523 }
Sunny Goyal594d76d2014-11-06 10:12:54 -0800524
525 /**
526 * Returns a widget with category {@link AppWidgetProviderInfo#WIDGET_CATEGORY_SEARCHBOX}
527 * provided by the same package which is set to be global search activity.
528 * If widgetCategory is not supported, or no such widget is found, returns the first widget
529 * provided by the package.
530 */
531 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
532 public static AppWidgetProviderInfo getSearchWidgetProvider(Context context) {
533 SearchManager searchManager =
534 (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
535 ComponentName searchComponent = searchManager.getGlobalSearchActivity();
536 if (searchComponent == null) return null;
537 String providerPkg = searchComponent.getPackageName();
538
539 AppWidgetProviderInfo defaultWidgetForSearchPackage = null;
540
541 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
542 for (AppWidgetProviderInfo info : appWidgetManager.getInstalledProviders()) {
543 if (info.provider.getPackageName().equals(providerPkg)) {
544 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
545 if ((info.widgetCategory & AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX) != 0) {
546 return info;
547 } else if (defaultWidgetForSearchPackage == null) {
548 defaultWidgetForSearchPackage = info;
549 }
550 } else {
551 return info;
552 }
553 }
554 }
555 return defaultWidgetForSearchPackage;
556 }
Sunny Goyal08f72612015-01-05 13:41:43 -0800557
558 public static final Comparator<ItemInfo> RANK_COMPARATOR = new Comparator<ItemInfo>() {
559
560 @Override
561 public int compare(ItemInfo lhs, ItemInfo rhs) {
562 return lhs.rank - rhs.rank;
563 }
564 };
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565}