/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.appsimobile.appsii.appwidget; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PaintFlagsDrawFilter; import android.os.Build; import android.util.Log; import android.util.Pair; import android.util.SparseArray; import android.widget.Toast; import com.appsimobile.appsii.R; /** * Various utilities shared amongst the Launcher's classes. */ public final class Utilities { // To turn on these properties, type // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS] static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate"; private static final String TAG = "Launcher.Utilities"; private static final Canvas sCanvas = new Canvas(); static { sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG)); } /** * Indicates if the device is running LMP or higher. */ public static boolean isLmpOrAbove() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; } public static void startActivityForResultSafely( Activity activity, Intent intent, int requestCode) { try { activity.startActivityForResult(intent, requestCode); } catch (ActivityNotFoundException e) { Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); } catch (SecurityException e) { Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); Log.e(TAG, "Launcher does not have the permission to launch " + intent + ". Make sure to create a MAIN intent-filter for the corresponding activity " + "or use the exported attribute for this activity.", e); } } static boolean isSystemApp(Context context, Intent intent) { PackageManager pm = context.getPackageManager(); ComponentName cn = intent.getComponent(); String packageName = null; if (cn == null) { ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); if ((info != null) && (info.activityInfo != null)) { packageName = info.activityInfo.packageName; } } else { packageName = cn.getPackageName(); } if (packageName != null) { try { PackageInfo info = pm.getPackageInfo(packageName, 0); return (info != null) && (info.applicationInfo != null) && ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); } catch (NameNotFoundException e) { return false; } } else { return false; } } /** * This picks a dominant color, looking for high-saturation, high-value, repeated hues. * * @param bitmap The bitmap to scan * @param samples The approximate max number of samples to use. */ static int findDominantColorByHue(Bitmap bitmap, int samples) { final int height = bitmap.getHeight(); final int width = bitmap.getWidth(); int sampleStride = (int) Math.sqrt((height * width) / samples); if (sampleStride < 1) { sampleStride = 1; } // This is an out-param, for getting the hsv values for an rgb float[] hsv = new float[3]; // First get the best hue, by creating a histogram over 360 hue buckets, // where each pixel contributes a score weighted by saturation, value, and alpha. float[] hueScoreHistogram = new float[360]; float highScore = -1; int bestHue = -1; for (int y = 0; y < height; y += sampleStride) { for (int x = 0; x < width; x += sampleStride) { int argb = bitmap.getPixel(x, y); int alpha = 0xFF & (argb >> 24); if (alpha < 0x80) { // Drop mostly-transparent pixels. continue; } // Remove the alpha channel. int rgb = argb | 0xFF000000; Color.colorToHSV(rgb, hsv); // Bucket colors by the 360 integer hues. int hue = (int) hsv[0]; if (hue < 0 || hue >= hueScoreHistogram.length) { // Defensively avoid array bounds violations. continue; } float score = hsv[1] * hsv[2]; hueScoreHistogram[hue] += score; if (hueScoreHistogram[hue] > highScore) { highScore = hueScoreHistogram[hue]; bestHue = hue; } } } SparseArray<Float> rgbScores = new SparseArray<Float>(); int bestColor = 0xff000000; highScore = -1; // Go back over the RGB colors that match the winning hue, // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets. // The highest-scoring RGB color wins. for (int y = 0; y < height; y += sampleStride) { for (int x = 0; x < width; x += sampleStride) { int rgb = bitmap.getPixel(x, y) | 0xff000000; Color.colorToHSV(rgb, hsv); int hue = (int) hsv[0]; if (hue == bestHue) { float s = hsv[1]; float v = hsv[2]; int bucket = (int) (s * 100) + (int) (v * 10000); // Score by cumulative saturation * value. float score = s * v; Float oldTotal = rgbScores.get(bucket); float newTotal = oldTotal == null ? score : oldTotal + score; rgbScores.put(bucket, newTotal); if (newTotal > highScore) { highScore = newTotal; // All the colors in the winning bucket are very similar. Last in wins. bestColor = rgb; } } } } return bestColor; } /* * Finds a system apk which had a broadcast receiver listening to a particular action. * @param action intent action used to find the apk * @return a pair of apk package name and the resources. */ static Pair<String, Resources> findSystemApk(String action, PackageManager pm) { final Intent intent = new Intent(action); for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { if (info.activityInfo != null && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { final String packageName = info.activityInfo.packageName; try { final Resources res = pm.getResourcesForApplication(packageName); return Pair.create(packageName, res); } catch (NameNotFoundException e) { Log.w(TAG, "Failed to find resources for " + packageName); } } } return null; } }