/* * 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.android.launcher3; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP; import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT; import android.app.ActivityManager; import android.app.WallpaperManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.LauncherActivityInfo; 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.Matrix; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.os.DeadObjectException; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.os.TransactionTooLargeException; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.TtsSpan; import android.util.DisplayMetrics; import android.util.Log; import android.util.Pair; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.animation.Interpolator; import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.ShortcutConfigActivityInfo; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.dragndrop.DragLayer; import com.android.launcher3.dragndrop.FolderAdaptiveIcon; import com.android.launcher3.folder.FolderIcon; import com.android.launcher3.shortcuts.DeepShortcutManager; import com.android.launcher3.shortcuts.DeepShortcutView; import com.android.launcher3.shortcuts.ShortcutInfoCompat; import com.android.launcher3.shortcuts.ShortcutKey; import com.android.launcher3.util.IntArray; import com.android.launcher3.widget.PendingAddShortcutInfo; import java.io.Closeable; import java.io.IOException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Various utilities shared amongst the Launcher's classes. */ public final class Utilities { private static final String TAG = "Launcher.Utilities"; private static final Pattern sTrimPattern = Pattern.compile("^[\\s|\\p{javaSpaceChar}]*(.*)[\\s|\\p{javaSpaceChar}]*$"); private static final int[] sLoc0 = new int[2]; private static final int[] sLoc1 = new int[2]; private static final float[] sPoint = new float[2]; private static final Matrix sMatrix = new Matrix(); private static final Matrix sInverseMatrix = new Matrix(); public static final boolean ATLEAST_Q = Build.VERSION.CODENAME.length() == 1 && Build.VERSION.CODENAME.charAt(0) >= 'Q' && Build.VERSION.CODENAME.charAt(0) <= 'Z'; public static final boolean ATLEAST_P = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P; public static final boolean ATLEAST_OREO_MR1 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1; public static final boolean ATLEAST_OREO = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; public static final int SINGLE_FRAME_MS = 16; /** * Set on a motion event dispatched from the nav bar. See {@link MotionEvent#setEdgeFlags(int)}. */ public static final int EDGE_NAV_BAR = 1 << 8; /** * Indicates if the device has a debug build. Should only be used to store additional info or * add extra logging and not for changing the app behavior. */ public static final boolean IS_DEBUG_DEVICE = Build.TYPE.toLowerCase(Locale.ROOT).contains("debug") || Build.TYPE.toLowerCase(Locale.ROOT).equals("eng"); // An intent extra to indicate the horizontal scroll of the wallpaper. public static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET"; public static final String EXTRA_WALLPAPER_FLAVOR = "com.android.launcher3.WALLPAPER_FLAVOR"; // These values are same as that in {@link AsyncTask}. private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CORE_POOL_SIZE = CPU_COUNT + 1; private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; private static final int KEEP_ALIVE = 1; /** * An {@link Executor} to be used with async task with no limit on the queue size. */ public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor( CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue()); public static boolean IS_RUNNING_IN_TEST_HARNESS = ActivityManager.isRunningInTestHarness(); public static void enableRunningInTestHarnessForTests() { IS_RUNNING_IN_TEST_HARNESS = true; } public static boolean isPropertyEnabled(String propertyName) { return Log.isLoggable(propertyName, Log.VERBOSE); } /** * Given a coordinate relative to the descendant, find the coordinate in a parent view's * coordinates. * * @param descendant The descendant to which the passed coordinate is relative. * @param ancestor The root view to make the coordinates relative to. * @param coord The coordinate that we want mapped. * @param includeRootScroll Whether or not to account for the scroll of the descendant: * sometimes this is relevant as in a child's coordinates within the descendant. * @return The factor by which this descendant is scaled relative to this DragLayer. Caution * this scale factor is assumed to be equal in X and Y, and so if at any point this * assumption fails, we will need to return a pair of scale factors. */ public static float getDescendantCoordRelativeToAncestor( View descendant, View ancestor, int[] coord, boolean includeRootScroll) { sPoint[0] = coord[0]; sPoint[1] = coord[1]; float scale = 1.0f; View v = descendant; while(v != ancestor && v != null) { // For TextViews, scroll has a meaning which relates to the text position // which is very strange... ignore the scroll. if (v != descendant || includeRootScroll) { sPoint[0] -= v.getScrollX(); sPoint[1] -= v.getScrollY(); } v.getMatrix().mapPoints(sPoint); sPoint[0] += v.getLeft(); sPoint[1] += v.getTop(); scale *= v.getScaleX(); v = (View) v.getParent(); } coord[0] = Math.round(sPoint[0]); coord[1] = Math.round(sPoint[1]); return scale; } /** * Inverse of {@link #getDescendantCoordRelativeToAncestor(View, View, int[], boolean)}. */ public static void mapCoordInSelfToDescendant(View descendant, View root, int[] coord) { sMatrix.reset(); View v = descendant; while(v != root) { sMatrix.postTranslate(-v.getScrollX(), -v.getScrollY()); sMatrix.postConcat(v.getMatrix()); sMatrix.postTranslate(v.getLeft(), v.getTop()); v = (View) v.getParent(); } sMatrix.postTranslate(-v.getScrollX(), -v.getScrollY()); sMatrix.invert(sInverseMatrix); sPoint[0] = coord[0]; sPoint[1] = coord[1]; sInverseMatrix.mapPoints(sPoint); coord[0] = Math.round(sPoint[0]); coord[1] = Math.round(sPoint[1]); } /** * Utility method to determine whether the given point, in local coordinates, * is inside the view, where the area of the view is expanded by the slop factor. * This method is called while processing touch-move events to determine if the event * is still within the view. */ public static boolean pointInView(View v, float localX, float localY, float slop) { return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) && localY < (v.getHeight() + slop); } public static int[] getCenterDeltaInScreenSpace(View v0, View v1) { v0.getLocationInWindow(sLoc0); v1.getLocationInWindow(sLoc1); sLoc0[0] += (v0.getMeasuredWidth() * v0.getScaleX()) / 2; sLoc0[1] += (v0.getMeasuredHeight() * v0.getScaleY()) / 2; sLoc1[0] += (v1.getMeasuredWidth() * v1.getScaleX()) / 2; sLoc1[1] += (v1.getMeasuredHeight() * v1.getScaleY()) / 2; return new int[] {sLoc1[0] - sLoc0[0], sLoc1[1] - sLoc0[1]}; } public static void scaleRectFAboutCenter(RectF r, float scale) { if (scale != 1.0f) { float cx = r.centerX(); float cy = r.centerY(); r.offset(-cx, -cy); r.left = r.left * scale; r.top = r.top * scale ; r.right = r.right * scale; r.bottom = r.bottom * scale; r.offset(cx, cy); } } public static void scaleRectAboutCenter(Rect r, float scale) { if (scale != 1.0f) { int cx = r.centerX(); int cy = r.centerY(); r.offset(-cx, -cy); scaleRect(r, scale); r.offset(cx, cy); } } public static void scaleRect(Rect r, float scale) { if (scale != 1.0f) { r.left = (int) (r.left * scale + 0.5f); r.top = (int) (r.top * scale + 0.5f); r.right = (int) (r.right * scale + 0.5f); r.bottom = (int) (r.bottom * scale + 0.5f); } } public static void insetRect(Rect r, Rect insets) { r.left = Math.min(r.right, r.left + insets.left); r.top = Math.min(r.bottom, r.top + insets.top); r.right = Math.max(r.left, r.right - insets.right); r.bottom = Math.max(r.top, r.bottom - insets.bottom); } public static float shrinkRect(Rect r, float scaleX, float scaleY) { float scale = Math.min(Math.min(scaleX, scaleY), 1.0f); if (scale < 1.0f) { int deltaX = (int) (r.width() * (scaleX - scale) * 0.5f); r.left += deltaX; r.right -= deltaX; int deltaY = (int) (r.height() * (scaleY - scale) * 0.5f); r.top += deltaY; r.bottom -= deltaY; } return scale; } /** * Maps t from one range to another range. * @param t The value to map. * @param fromMin The lower bound of the range that t is being mapped from. * @param fromMax The upper bound of the range that t is being mapped from. * @param toMin The lower bound of the range that t is being mapped to. * @param toMax The upper bound of the range that t is being mapped to. * @return The mapped value of t. */ public static float mapToRange(float t, float fromMin, float fromMax, float toMin, float toMax, Interpolator interpolator) { if (fromMin == fromMax || toMin == toMax) { Log.e(TAG, "mapToRange: range has 0 length"); return toMin; } float progress = getProgress(t, fromMin, fromMax); return mapRange(interpolator.getInterpolation(progress), toMin, toMax); } public static float getProgress(float current, float min, float max) { return Math.abs(current - min) / Math.abs(max - min); } public static float mapRange(float value, float min, float max) { return min + (value * (max - min)); } public 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; } } /* * 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 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; } /** * Trims the string, removing all whitespace at the beginning and end of the string. * Non-breaking whitespaces are also removed. */ public static String trim(CharSequence s) { if (s == null) { return null; } // Just strip any sequence of whitespace or java space characters from the beginning and end Matcher m = sTrimPattern.matcher(s); return m.replaceAll("$1"); } /** * Calculates the height of a given string at a specific text size. */ public static int calculateTextHeight(float textSizePx) { Paint p = new Paint(); p.setTextSize(textSizePx); Paint.FontMetrics fm = p.getFontMetrics(); return (int) Math.ceil(fm.bottom - fm.top); } /** * Convenience println with multiple args. */ public static void println(String key, Object... args) { StringBuilder b = new StringBuilder(); b.append(key); b.append(": "); boolean isFirstArgument = true; for (Object arg : args) { if (isFirstArgument) { isFirstArgument = false; } else { b.append(", "); } b.append(arg); } System.out.println(b.toString()); } public static boolean isRtl(Resources res) { return res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; } /** * Returns true if the intent is a valid launch intent for a launcher activity of an app. * This is used to identify shortcuts which are different from the ones exposed by the * applications' manifest file. * * @param launchIntent The intent that will be launched when the shortcut is clicked. */ public static boolean isLauncherAppTarget(Intent launchIntent) { if (launchIntent != null && Intent.ACTION_MAIN.equals(launchIntent.getAction()) && launchIntent.getComponent() != null && launchIntent.getCategories() != null && launchIntent.getCategories().size() == 1 && launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && TextUtils.isEmpty(launchIntent.getDataString())) { // An app target can either have no extra or have ItemInfo.EXTRA_PROFILE. Bundle extras = launchIntent.getExtras(); return extras == null || extras.keySet().isEmpty(); } return false; } public static float dpiFromPx(int size, DisplayMetrics metrics){ float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT; return (size / densityRatio); } public static int pxFromDp(float size, DisplayMetrics metrics) { return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, metrics)); } public static int pxFromSp(float size, DisplayMetrics metrics) { return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, metrics)); } public static String createDbSelectionQuery(String columnName, IntArray values) { return String.format(Locale.ENGLISH, "%s IN (%s)", columnName, values.toConcatString()); } public static boolean isBootCompleted() { return "1".equals(getSystemProperty("sys.boot_completed", "1")); } public static String getSystemProperty(String property, String defaultValue) { try { Class clazz = Class.forName("android.os.SystemProperties"); Method getter = clazz.getDeclaredMethod("get", String.class); String value = (String) getter.invoke(null, property); if (!TextUtils.isEmpty(value)) { return value; } } catch (Exception e) { Log.d(TAG, "Unable to read system properties"); } return defaultValue; } /** * Ensures that a value is within given bounds. Specifically: * If value is less than lowerBound, return lowerBound; else if value is greater than upperBound, * return upperBound; else return value unchanged. */ public static int boundToRange(int value, int lowerBound, int upperBound) { return Math.max(lowerBound, Math.min(value, upperBound)); } /** * @see #boundToRange(int, int, int). */ public static float boundToRange(float value, float lowerBound, float upperBound) { return Math.max(lowerBound, Math.min(value, upperBound)); } /** * @see #boundToRange(int, int, int). */ public static long boundToRange(long value, long lowerBound, long upperBound) { return Math.max(lowerBound, Math.min(value, upperBound)); } /** * Wraps a message with a TTS span, so that a different message is spoken than * what is getting displayed. * @param msg original message * @param ttsMsg message to be spoken */ public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) { SpannableString spanned = new SpannableString(msg); spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(), 0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); return spanned; } public static SharedPreferences getPrefs(Context context) { return context.getSharedPreferences( LauncherFiles.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE); } public static SharedPreferences getDevicePrefs(Context context) { return context.getSharedPreferences( LauncherFiles.DEVICE_PREFERENCES_KEY, Context.MODE_PRIVATE); } public static boolean isPowerSaverPreventingAnimation(Context context) { if (ATLEAST_P) { // Battery saver mode no longer prevents animations. return false; } return context.getSystemService(PowerManager.class).isPowerSaveMode(); } public static boolean isWallpaperAllowed(Context context) { return context.getSystemService(WallpaperManager.class).isSetWallpaperAllowed(); } public static void closeSilently(Closeable c) { if (c != null) { try { c.close(); } catch (IOException e) { if (FeatureFlags.IS_DOGFOOD_BUILD) { Log.d(TAG, "Error closing", e); } } } } public static boolean isBinderSizeError(Exception e) { return e.getCause() instanceof TransactionTooLargeException || e.getCause() instanceof DeadObjectException; } /** * Utility method to post a runnable on the handler, skipping the synchronization barriers. */ public static void postAsyncCallback(Handler handler, Runnable callback) { Message msg = Message.obtain(handler, callback); msg.setAsynchronous(true); handler.sendMessage(msg); } /** * Parses a string encoded using {@link #getPointString(int, int)} */ public static Point parsePoint(String point) { String[] split = point.split(","); return new Point(Integer.parseInt(split[0]), Integer.parseInt(split[1])); } /** * Encodes a point to string to that it can be persisted atomically. */ public static String getPointString(int x, int y) { return String.format(Locale.ENGLISH, "%d,%d", x, y); } /** * Returns the location bounds of a view. * - For DeepShortcutView, we return the bounds of the icon view. * - For BubbleTextView, we return the icon bounds. */ public static void getLocationBoundsForView(Launcher launcher, View v, Rect outRect) { final DragLayer dragLayer = launcher.getDragLayer(); final boolean isBubbleTextView = v instanceof BubbleTextView; final boolean isFolderIcon = v instanceof FolderIcon; final Rect rect = new Rect(); // Deep shortcut views have their icon drawn in a separate view. final boolean fromDeepShortcutView = v.getParent() instanceof DeepShortcutView; if (v instanceof DeepShortcutView) { dragLayer.getDescendantRectRelativeToSelf(((DeepShortcutView) v).getIconView(), rect); } else if (fromDeepShortcutView) { DeepShortcutView view = (DeepShortcutView) v.getParent(); dragLayer.getDescendantRectRelativeToSelf(view.getIconView(), rect); } else if ((isBubbleTextView || isFolderIcon) && v.getTag() instanceof ItemInfo && (((ItemInfo) v.getTag()).container == CONTAINER_DESKTOP || ((ItemInfo) v.getTag()).container == CONTAINER_HOTSEAT)) { CellLayout pageViewIsOn = ((CellLayout) v.getParent().getParent()); int pageNum = launcher.getWorkspace().indexOfChild(pageViewIsOn); DeviceProfile dp = launcher.getDeviceProfile(); ItemInfo info = ((ItemInfo) v.getTag()); dp.getItemLocation(info.cellX, info.cellY, info.spanX, info.spanY, info.container, pageNum - launcher.getCurrentWorkspaceScreen(), rect); } else { dragLayer.getDescendantRectRelativeToSelf(v, rect); } int viewLocationLeft = rect.left; int viewLocationTop = rect.top; if (isBubbleTextView && !fromDeepShortcutView) { ((BubbleTextView) v).getIconBounds(rect); } else if (isFolderIcon) { ((FolderIcon) v).getPreviewBounds(rect); } else { rect.set(0, 0, rect.width(), rect.height()); } viewLocationLeft += rect.left; viewLocationTop += rect.top; outRect.set(viewLocationLeft, viewLocationTop, viewLocationLeft + rect.width(), viewLocationTop + rect.height()); } public static void unregisterReceiverSafely(Context context, BroadcastReceiver receiver) { try { context.unregisterReceiver(receiver); } catch (IllegalArgumentException e) {} } /** * Returns the full drawable for {@param info}. * @param outObj this is set to the internal data associated with {@param info}, * eg {@link LauncherActivityInfo} or {@link ShortcutInfoCompat}. */ public static Drawable getFullDrawable(Launcher launcher, ItemInfo info, int width, int height, boolean flattenDrawable, Object[] outObj) { LauncherAppState appState = LauncherAppState.getInstance(launcher); if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { LauncherActivityInfo activityInfo = LauncherAppsCompat.getInstance(launcher) .resolveActivity(info.getIntent(), info.user); outObj[0] = activityInfo; return (activityInfo != null) ? appState.getIconCache() .getFullResIcon(activityInfo, flattenDrawable) : null; } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { if (info instanceof PendingAddShortcutInfo) { ShortcutConfigActivityInfo activityInfo = ((PendingAddShortcutInfo) info).activityInfo; outObj[0] = activityInfo; return activityInfo.getFullResIcon(appState.getIconCache()); } ShortcutKey key = ShortcutKey.fromItemInfo(info); DeepShortcutManager sm = DeepShortcutManager.getInstance(launcher); List si = sm.queryForFullDetails( key.componentName.getPackageName(), Arrays.asList(key.getId()), key.user); if (si.isEmpty()) { return null; } else { outObj[0] = si.get(0); return sm.getShortcutIconDrawable(si.get(0), appState.getInvariantDeviceProfile().fillResIconDpi); } } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { FolderAdaptiveIcon icon = FolderAdaptiveIcon.createFolderAdaptiveIcon( launcher, info.id, new Point(width, height)); if (icon == null) { return null; } outObj[0] = icon; return icon; } else { return null; } } }