/* * 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.model.data.ItemInfoWithIcon.FLAG_ICON_BADGED; import android.Manifest.permission; import static com.android.launcher3.model.data.ItemInfoWithIcon.FLAG_ICON_BADGED; import android.animation.ValueAnimator; import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; import android.app.Person; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.WallpaperManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.pm.ShortcutInfo; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION_CODES; import android.os.DeadObjectException; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.os.Process; import android.os.TransactionTooLargeException; import android.os.UserHandle; import android.provider.Settings; import android.text.Spannable; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.TtsSpan; import android.util.DisplayMetrics; import android.util.FloatProperty; import android.util.Log; import android.util.Property; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.Window; import android.view.animation.Interpolator; import androidx.core.os.BuildCompat; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.core.content.pm.ShortcutInfoCompat; import androidx.core.content.pm.ShortcutManagerCompat; import androidx.core.graphics.drawable.IconCompat; import ch.deletescape.lawnchair.HiddenApiCompat; import ch.deletescape.lawnchair.LawnchairAppKt; import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.ShortcutConfigActivityInfo; import com.android.launcher3.compat.UserManagerCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.dragndrop.FolderAdaptiveIcon; import com.android.launcher3.graphics.GridOptionsProvider; import com.android.launcher3.graphics.TintedDrawableSpan; import com.android.launcher3.icons.IconProvider; import com.android.launcher3.icons.BitmapInfo; import com.android.launcher3.icons.IconCache; import com.android.launcher3.icons.LauncherIcons; import com.android.launcher3.icons.ShortcutCachingLogic; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.ItemInfoWithIcon; import com.android.launcher3.pm.ShortcutConfigActivityInfo; import com.android.launcher3.shortcuts.ShortcutKey; import com.android.launcher3.shortcuts.ShortcutRequest; import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.IntArray; import com.android.launcher3.util.PackageManagerHelper; import com.android.launcher3.widget.PendingAddShortcutInfo; import com.android.systemui.shared.system.QuickStepContract; 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.regex.Matcher; import java.util.regex.Pattern; import ch.deletescape.lawnchair.LawnchairLauncher; import ch.deletescape.lawnchair.LawnchairPreferences; import ch.deletescape.lawnchair.backup.RestoreBackupActivity; import ch.deletescape.lawnchair.settings.ui.SettingsActivity; /** * 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 Matrix sMatrix = new Matrix(); private static final Matrix sInverseMatrix = new Matrix(); public static final String[] EMPTY_STRING_ARRAY = new String[0]; public static final Person[] EMPTY_PERSON_ARRAY = new Person[0]; public static final boolean ATLEAST_R = BuildCompat.isAtLeastR(); public static final boolean ATLEAST_Q = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; public static final boolean IS_NEW_Q = ATLEAST_Q && HiddenApiCompat.isNewQ(); 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; /** * Set on a motion event dispatched from the nav bar. See {@link MotionEvent#setEdgeFlags(int)}. */ public static final int EDGE_NAV_BAR = 1 << 8; public static boolean HIDDEN_APIS_ALLOWED = !ATLEAST_P || HiddenApiCompat.checkIfAllowed(); /** * 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"); public static boolean isDevelopersOptionsEnabled(Context context) { return Settings.Global.getInt(context.getApplicationContext().getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; } // 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"; public static boolean IS_RUNNING_IN_TEST_HARNESS = ActivityManager.isRunningInTestHarness(); public static boolean IS_ONEPLUS_STOCK = isOnePlusStock(); public static void enableRunningInTestHarnessForTests() { IS_RUNNING_IN_TEST_HARNESS = true; } public static boolean isPropertyEnabled(String propertyName) { return Log.isLoggable(propertyName, Log.VERBOSE); } public static boolean existsStyleWallpapers(Context context) { ResolveInfo ri = context.getPackageManager().resolveActivity( PackageManagerHelper.getStyleWallpapersIntent(context), 0); return ri != null; } /** * 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, float[] coord, boolean includeRootScroll) { return getDescendantCoordRelativeToAncestor(descendant, ancestor, coord, includeRootScroll, false); } /** * 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. * @param ignoreTransform If true, view transform is ignored * @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, float[] coord, boolean includeRootScroll, boolean ignoreTransform) { 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) { offsetPoints(coord, -v.getScrollX(), -v.getScrollY()); } if (!ignoreTransform) { v.getMatrix().mapPoints(coord); } offsetPoints(coord, v.getLeft(), v.getTop()); scale *= v.getScaleX(); v = (View) v.getParent(); } return scale; } /** * Inverse of {@link #getDescendantCoordRelativeToAncestor(View, View, float[], boolean)}. */ public static void mapCoordInSelfToDescendant(View descendant, View root, float[] 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); sInverseMatrix.mapPoints(coord); } /** * Sets {@param out} to be same as {@param in} by rounding individual values */ public static void roundArray(float[] in, int[] out) { for (int i = 0; i < in.length; i++) { out[i] = Math.round(in[i]); } } public static void offsetPoints(float[] points, float offsetX, float offsetY) { for (int i = 0; i < points.length; i += 2) { points[i] += offsetX; points[i + 1] += offsetY; } } /** * 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)); } /** * Bounds parameter to the range [0, 1] */ public static float saturate(float a) { return boundToRange(a, 0, 1.0f); } /** * Returns the compliment (1 - a) of the parameter. */ public static float comp(float a) { return 1 - a; } /** * Returns the "probabilistic or" of a and b. (a + b - ab). * Useful beyond probability, can be used to combine two unit progresses for example. */ public static float or(float a, float b) { float satA = saturate(a); float satB = saturate(b); return satA + satB - (satA * satB); } /** * 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); } public static boolean isRtl(Resources res) { return res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; } public static float dpiFromPx(float size, DisplayMetrics metrics) { float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT; return (size / densityRatio); } 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; } /** * Prefixes a text with the provided icon */ public static CharSequence prefixTextWithIcon(Context context, int iconRes, CharSequence msg) { // Update the hint to contain the icon. // Prefix the original hint with two spaces. The first space gets replaced by the icon // using span. The second space is used for a singe space character between the hint // and the icon. SpannableString spanned = new SpannableString(" " + msg); spanned.setSpan(new TintedDrawableSpan(context, iconRes), 0, 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); return spanned; } public static SharedPreferences getPrefs(Context context) { return getLawnchairPrefs(context).getSharedPrefs(); } public static SharedPreferences getDevicePrefs(Context context) { // Use application context for shared preferences, so that we use a single cached instance return context.getApplicationContext().getSharedPreferences( LauncherFiles.DEVICE_PREFERENCES_KEY, Context.MODE_PRIVATE); } /** * @return {@link SharedPreferences} that backs {@link FeatureFlags} */ public static SharedPreferences getFeatureFlagsPrefs(Context context) { // Use application context for shared preferences, so that we use a single cached instance return context.getApplicationContext().getSharedPreferences( FeatureFlags.FLAGS_PREF_NAME, Context.MODE_PRIVATE); } public static SharedPreferences getReflectionPrefs(Context context) { return context.getSharedPreferences( LauncherFiles.REFLECTION_PREFERENCES_KEY, Context.MODE_PRIVATE); } public static boolean areAnimationsEnabled(Context context) { return ATLEAST_OREO ? ValueAnimator.areAnimatorsEnabled() : !context.getSystemService(PowerManager.class).isPowerSaveMode(); } public static boolean isWallpaperAllowed(Context context) { return context.getSystemService(WallpaperManager.class).isSetWallpaperAllowed(); } public static boolean isBinderSizeError(Exception e) { return e.getCause() instanceof TransactionTooLargeException || e.getCause() instanceof DeadObjectException; } public static boolean isGridOptionsEnabled(Context context) { return isComponentEnabled(context.getPackageManager(), context.getPackageName(), GridOptionsProvider.class.getName()); } private static boolean isComponentEnabled(PackageManager pm, String pkgName, String clsName) { ComponentName componentName = new ComponentName(pkgName, clsName); int componentEnabledSetting = pm.getComponentEnabledSetting(componentName); switch (componentEnabledSetting) { case PackageManager.COMPONENT_ENABLED_STATE_DISABLED: return false; case PackageManager.COMPONENT_ENABLED_STATE_ENABLED: return true; case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT: default: // We need to get the application info to get the component's default state try { PackageInfo packageInfo = pm.getPackageInfo(pkgName, PackageManager.GET_PROVIDERS | PackageManager.GET_DISABLED_COMPONENTS); if (packageInfo.providers != null) { return Arrays.stream(packageInfo.providers).anyMatch( pi -> pi.name.equals(clsName) && pi.isEnabled()); } // the component is not declared in the AndroidManifest return false; } catch (PackageManager.NameNotFoundException e) { // the package isn't installed on the device return false; } } } /** * 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); } public static void unregisterReceiverSafely(Context context, BroadcastReceiver receiver) { try { context.unregisterReceiver(receiver); } catch (IllegalArgumentException e) {} } /** * Returns the full drawable for info without any flattening or pre-processing. * * @param outObj this is set to the internal data associated with {@param info}, * eg {@link LauncherActivityInfo} or {@link ShortcutInfo}. */ public static Drawable getFullDrawable(Launcher launcher, ItemInfo info, int width, int height, Object[] outObj) { LauncherAppState appState = LauncherAppState.getInstance(launcher); if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { LauncherActivityInfo activityInfo = launcher.getSystemService(LauncherApps.class) .resolveActivity(info.getIntent(), info.user); outObj[0] = activityInfo; return activityInfo == null ? null : new IconProvider(launcher).getIconForUI( activityInfo, launcher.getDeviceProfile().inv.fillResIconDpi); } 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()); } if (info.getIntent() == null || info.getIntent().getPackage() == null) return null; List si = ShortcutKey.fromItemInfo(info) .buildRequest(launcher) .query(ShortcutRequest.ALL); if (si.isEmpty()) { return null; } else { outObj[0] = si.get(0); return ShortcutCachingLogic.getIcon(launcher, si.get(0), appState.getInvariantDeviceProfile().fillResIconDpi); } } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { FolderInfo folderInfo = (FolderInfo) info; if (folderInfo.isCoverMode()) { return getFullDrawable(launcher, folderInfo.getCoverInfo(), width, height, flattenDrawable, outObj); } FolderAdaptiveIcon icon = FolderAdaptiveIcon.createFolderAdaptiveIcon( launcher, info.id, new Point(width, height)); if (icon == null) { return null; } outObj[0] = icon; return icon; } else { return null; } } @NonNull public static LawnchairPreferences getLawnchairPrefs(Context context) { return LawnchairPreferences.Companion.getInstance(context); } public static void goToHome(Context context){ PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); ComponentName componentName = intent.resolveActivity(pm); if (!context.getPackageName().equals(componentName.getPackageName())) { intent = pm.getLaunchIntentForPackage(context.getPackageName()); } context.startActivity(intent); } public static void restartLauncher(Context context) { PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName componentName = intent.resolveActivity(pm); if (!context.getPackageName().equals(componentName.getPackageName())) { intent = pm.getLaunchIntentForPackage(context.getPackageName()); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } restartLauncher(context, intent); } public static void restartLauncher(Context context, Intent intent) { context.startActivity(intent); // Create a pending intent so the application is restarted after System.exit(0) was called. // We use an AlarmManager to call this intent in 100ms PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); // Kill the application killLauncher(); } public static void killLauncher() { System.exit(0); } public static void checkRestoreSuccess(Context context) { LawnchairPreferences prefs = Utilities.getLawnchairPrefs(context); if (prefs.getRestoreSuccess()) { prefs.setRestoreSuccess(false); context.startActivity(new Intent(context, RestoreBackupActivity.class) .putExtra(RestoreBackupActivity.EXTRA_SUCCESS, true)); } } @Nullable public static Bitmap drawableToBitmap(Drawable drawable) { return Utilities.drawableToBitmap(drawable, true); } public static Bitmap drawableToBitmap(Drawable drawable, boolean forceCreate) { return drawableToBitmap(drawable, forceCreate, 0); } public static Bitmap drawableToBitmap(Drawable drawable, boolean forceCreate, int fallbackSize) { if (!forceCreate && drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); if (width <= 0 || height <= 0) { if (fallbackSize > 0) { width = height = fallbackSize; } else { return null; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } public static int setFlag(int flags, int flag, boolean value) { if (value) { return flags | flag; } else { return flags & ~flag; } } public static String upperCaseFirstLetter(String str) { if (TextUtils.isEmpty(str)) { return str; } return str.substring(0, 1).toUpperCase(Locale.US) + str.substring(1); } public static void pinSettingsShortcut(Context context) { if (!ShortcutManagerCompat.isRequestPinShortcutSupported(context)) return; ShortcutManagerCompat.requestPinShortcut(context, new ShortcutInfoCompat.Builder(context, "settings") .setIntent(new Intent(context, SettingsActivity.class).setAction(Intent.ACTION_MAIN)) .setIcon(IconCompat.createWithResource(context, R.drawable.ic_setting)) .setShortLabel(context.getString(R.string.settings_button_text)) .setAlwaysBadged() .build(), null); } public static boolean hasStoragePermission(Context context) { return hasPermission(context, android.Manifest.permission.READ_EXTERNAL_STORAGE); } public static boolean hasPermission(Context context, String permission) { return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; } public static void requestStoragePermission(Activity activity) { ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, LawnchairLauncher.REQUEST_PERMISSION_STORAGE_ACCESS); } public static void requestLocationPermission(Activity activity) { ActivityCompat.requestPermissions(activity, new String[]{permission.ACCESS_COARSE_LOCATION}, LawnchairLauncher.REQUEST_PERMISSION_LOCATION_ACCESS); } public static int parseResourceIdentifier(Resources res, String identifier, String packageName) { try { return Integer.parseInt(identifier.substring(1)); } catch (NumberFormatException e) { return res.getIdentifier(identifier.substring(1), null, packageName); } } public static T notNullOrDefault(T value, T defValue) { return (value == null) ? defValue : value; } public static Boolean isEmui(){ return !TextUtils.isEmpty(getSystemProperty("ro.build.version.emui", "")); } public static Boolean isOnePlusStock() { return !TextUtils.isEmpty(getSystemProperty("ro.oxygen.version", "")) || !TextUtils.isEmpty(getSystemProperty("ro.hydrogen.version", "")) // ro.{oxygen|hydrogen}.version has been removed from the 7 series onwards || getSystemProperty("ro.rom.version", "").contains("Oxygen OS") || getSystemProperty("ro.rom.version", "").contains("Hydrogen OS"); } public static Boolean isMiui(){ return !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.code", "")) || !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name", "")); } public static Boolean hasKnoxSecureFolder(Context context) { return PackageManagerHelper.isAppInstalled(context.getPackageManager(), "com.samsung.knox.securefolder", 0); } public static void openURLinBrowser(Context context, String url) { openURLinBrowser(context, url, null, null); } public static void openURLinBrowser(Context context, String url, Rect sourceBounds, Bundle options) { try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); if (!(context instanceof Activity)){ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } intent.setSourceBounds(sourceBounds); if(options == null){ context.startActivity(intent); } else { context.startActivity(intent, options); } } catch (ActivityNotFoundException exc) { // Believe me, this actually happens. Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT).show(); } } /** * @param bitmap the Bitmap to be scaled * @param threshold the maxium dimension (either width or height) of the scaled bitmap * @param isNecessaryToKeepOrig is it necessary to keep the original bitmap? If not recycle the original bitmap to prevent memory leak. * * Credit: https://gist.github.com/vxhviet/873d142b41217739a1302d337b7285ba * */ public static Bitmap getScaledDownBitmap(Bitmap bitmap, int threshold, boolean isNecessaryToKeepOrig) { if (bitmap == null) return null; int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = width; int newHeight = height; if(width > height && width > threshold){ newWidth = threshold; newHeight = (int)(height * (float)newWidth/width); } if(width > height && width <= threshold){ //the bitmap is already smaller than our required dimension, no need to resize it return bitmap; } if(width < height && height > threshold){ newHeight = threshold; newWidth = (int)(width * (float)newHeight/height); } if(width < height && height <= threshold){ //the bitmap is already smaller than our required dimension, no need to resize it return bitmap; } if(width == height && width > threshold){ newWidth = threshold; newHeight = newWidth; } if(width == height && width <= threshold){ //the bitmap is already smaller than our required dimension, no need to resize it return bitmap; } return getResizedBitmap(bitmap, newWidth, newHeight, isNecessaryToKeepOrig); } private static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, boolean isNecessaryToKeepOrig) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPULATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); if (!isNecessaryToKeepOrig) { bm.recycle(); } return resizedBitmap; } /** * For apps icons and shortcut icons that have badges, this method creates a drawable that can * later on be rendered on top of the layers for the badges. For app icons, work profile badges * can only be applied. For deep shortcuts, when dragged from the pop up container, there's no * badge. When dragged from workspace or folder, it may contain app AND/OR work profile badge **/ @TargetApi(Build.VERSION_CODES.O) public static Drawable getBadge(Launcher launcher, ItemInfo info, Object obj) { LauncherAppState appState = LauncherAppState.getInstance(launcher); int iconSize = appState.getInvariantDeviceProfile().iconBitmapSize; if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { boolean iconBadged = (info instanceof ItemInfoWithIcon) && (((ItemInfoWithIcon) info).runtimeStatusFlags & FLAG_ICON_BADGED) > 0; if ((info.id == ItemInfo.NO_ID && !iconBadged) || !(obj instanceof ShortcutInfo)) { // The item is not yet added on home screen. return new FixedSizeEmptyDrawable(iconSize); } ShortcutInfo si = (ShortcutInfo) obj; Bitmap badge = LauncherAppState.getInstance(appState.getContext()) .getIconCache().getShortcutInfoBadge(si).icon; float badgeSize = LauncherIcons.getBadgeSizeForIconSize(iconSize); float insetFraction = (iconSize - badgeSize) / iconSize; return new InsetDrawable(new FastBitmapDrawable(badge), insetFraction, insetFraction, 0, 0); } else if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_FOLDER) { FolderInfo folderInfo = (FolderInfo) info; if (folderInfo.isCoverMode()) { return getBadge(launcher, folderInfo.getCoverInfo(), obj); } return ((FolderAdaptiveIcon) obj).getBadge(); } else { return launcher.getPackageManager() .getUserBadgedIcon(new FixedSizeEmptyDrawable(iconSize), info.user); } } public static float squaredHypot(float x, float y) { return x * x + y * y; } public static float squaredTouchSlop(Context context) { float slop = ViewConfiguration.get(context).getScaledTouchSlop(); return slop * slop; } private static class FixedSizeEmptyDrawable extends ColorDrawable { private final int mSize; public FixedSizeEmptyDrawable(int size) { super(Color.TRANSPARENT); mSize = size; } @Override public int getIntrinsicHeight() { return mSize; } @Override public int getIntrinsicWidth() { return mSize; } } public static boolean isRecentsEnabled() { LauncherAppState las = LauncherAppState.getInstanceNoCreate(); if (las != null) { Context context = las.getContext(); return LawnchairAppKt.getLawnchairApp(context).getRecentsEnabled(); } return false; } public static void startAssistant(Context context) { context.startActivity(new Intent(Intent.ACTION_VOICE_COMMAND)); } public static Drawable getIconForTask(Context context, int userId, String packageName) { IconCache ic = LauncherAppState.getInstanceNoCreate().getIconCache(); LauncherAppsCompat lac = LauncherAppsCompat.getInstance(context); UserHandle user = UserHandle.of(userId); List al = lac.getActivityList(packageName, user); if (!al.isEmpty()) { Drawable fullResIcon = ic.getFullResIcon(al.get(0)); if (user == Process.myUserHandle()) { return fullResIcon; } else { LauncherIcons li = LauncherIcons.obtain(context); BitmapInfo bitmapInfo = li.createBadgedIconBitmap(fullResIcon, user, 24); li.recycle(); return new BitmapDrawable(context.getResources(), bitmapInfo.icon); } } else { return null; } } public static int getUserId() { return UserHandle.myUserId(); } @RequiresApi(VERSION_CODES.O) public static final Property VIEW_SCALE = new FloatProperty("scale") { @Override public void setValue(View object, float value) { object.setScaleX(value); object.setScaleY(value); } @Override public Float get(View object) { return object.getScaleX(); } }; public static boolean hasWriteSecureSettingsPermission(Context context) { return ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_SECURE_SETTINGS) == PackageManager.PERMISSION_GRANTED; } /** * Creates a new component key from an encoded component key string in the form of * [flattenedComponentString#userId]. If the userId is not present, then it defaults * to the current user. */ public static ComponentKey makeComponentKey(Context context, String componentKeyStr) { ComponentName componentName; UserHandle user; int userDelimiterIndex = componentKeyStr.indexOf("#"); if (userDelimiterIndex != -1) { String componentStr = componentKeyStr.substring(0, userDelimiterIndex); Long componentUser = Long.valueOf(componentKeyStr.substring(userDelimiterIndex + 1)); componentName = ComponentName.unflattenFromString(componentStr); user = Utilities.notNullOrDefault(UserManagerCompat.getInstance(context) .getUserForSerialNumber(componentUser.longValue()), Process.myUserHandle()); } else { // No user provided, default to the current user componentName = ComponentName.unflattenFromString(componentKeyStr); user = Process.myUserHandle(); } try { return new ComponentKey(componentName, user); } catch (NullPointerException e) { throw new NullPointerException("Trying to create invalid component key: " + componentKeyStr); } } public static float ONEPLUS_WINDOW_CORNER_RADIUS = -1; public static float getWindowCornerRadius(Context context) { if (IS_ONEPLUS_STOCK) { if (ONEPLUS_WINDOW_CORNER_RADIUS == -1) { try { String sysui = "com.android.systemui"; Context sysuiContext = context.createPackageContext(sysui, 0); Resources res = sysuiContext.getResources(); int topId = res.getIdentifier("rounded_corner_radius_top", "dimen", sysui); int bottomId = res.getIdentifier("rounded_corner_radius_bottom", "dimen", sysui); float top = res.getDimension(topId); float bottom = res.getDimension(bottomId); ONEPLUS_WINDOW_CORNER_RADIUS = Math.min(top, bottom) * 0.8f; } catch (Exception e) { e.printStackTrace(); ONEPLUS_WINDOW_CORNER_RADIUS = Utilities.getWindowCornerRadius(context); } } return ONEPLUS_WINDOW_CORNER_RADIUS; } else { return QuickStepContract.getWindowCornerRadius(context.getResources()); } } public static boolean supportsRoundedCornersOnWindows(Resources resources) { if (IS_ONEPLUS_STOCK) { return true; } else { return QuickStepContract.supportsRoundedCornersOnWindows(resources); } } }