From 7f9090ecdfaae0c2f2592642ea834f6a6e6f151a Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 22 May 2018 19:22:11 +0200 Subject: [PATCH] try getting the properly colored app launcher icon --- .../applauncher/extensions/Context.kt | 27 +++++++++++-- .../applauncher/helpers/DBHelper.kt | 39 ++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt index 67400f6..a9ed1b1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/extensions/Context.kt @@ -1,16 +1,22 @@ package com.simplemobiletools.applauncher.extensions +import android.annotation.TargetApi import android.content.Context import android.content.Intent +import android.content.pm.LauncherApps import android.content.pm.PackageManager +import android.graphics.drawable.Drawable +import android.os.Build import com.simplemobiletools.applauncher.helpers.Config import com.simplemobiletools.applauncher.helpers.DBHelper import com.simplemobiletools.applauncher.models.AppLauncher +import com.simplemobiletools.commons.helpers.isLollipopPlus val Context.config: Config get() = Config.newInstance(applicationContext) val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext) +@TargetApi(Build.VERSION_CODES.LOLLIPOP) fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList): ArrayList { val allApps = ArrayList() val intent = Intent(Intent.ACTION_MAIN, null) @@ -21,10 +27,23 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList) val label = componentInfo.loadLabel(packageManager).toString() val packageName = componentInfo.packageName - val drawable = if (packageName.isAPredefinedApp()) { - resources.getLauncherDrawable(packageName) - } else { - packageManager.getApplicationIcon(packageName) + var drawable: Drawable? = null + if (isLollipopPlus()) { + try { + // try getting the properly colored launcher icons + val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0] + drawable = activityList.getBadgedIcon(0) + } catch (e: Exception) { + } + } + + if (drawable == null) { + drawable = if (packageName.isAPredefinedApp()) { + resources.getLauncherDrawable(packageName) + } else { + packageManager.getApplicationIcon(packageName) + } } allApps.add(AppLauncher(0, label, packageName, drawable)) diff --git a/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt index 92aa404..823c59d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/applauncher/helpers/DBHelper.kt @@ -1,10 +1,14 @@ package com.simplemobiletools.applauncher.helpers +import android.annotation.TargetApi import android.content.ContentValues import android.content.Context +import android.content.pm.LauncherApps import android.content.pm.PackageManager import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper +import android.graphics.drawable.Drawable +import android.os.Build import android.text.TextUtils import com.simplemobiletools.applauncher.R import com.simplemobiletools.applauncher.extensions.getLauncherDrawable @@ -12,6 +16,7 @@ import com.simplemobiletools.applauncher.extensions.isAPredefinedApp import com.simplemobiletools.applauncher.models.AppLauncher import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue +import com.simplemobiletools.commons.helpers.isLollipopPlus import java.util.* class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { @@ -109,6 +114,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) == 1 } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) fun getLaunchers(): ArrayList { val resources = context.resources val packageManager = context.packageManager @@ -122,18 +128,31 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont val name = cursor.getStringValue(COL_NAME) val packageName = cursor.getStringValue(COL_PKG_NAME) - val drawable = if (packageName.isAPredefinedApp()) { + var drawable: Drawable? = null + if (isLollipopPlus()) { try { - packageManager.getApplicationIcon(packageName) - } catch (e: PackageManager.NameNotFoundException) { - resources.getLauncherDrawable(packageName) + // try getting the properly colored launcher icons + val launcher = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle())[0] + drawable = activityList.getBadgedIcon(0) + } catch (e: Exception) { } - } else { - try { - packageManager.getApplicationIcon(packageName) - } catch (e: PackageManager.NameNotFoundException) { - IDsToDelete.add(id.toString()) - null + } + + if (drawable == null) { + drawable = if (packageName.isAPredefinedApp()) { + try { + packageManager.getApplicationIcon(packageName) + } catch (e: PackageManager.NameNotFoundException) { + resources.getLauncherDrawable(packageName) + } + } else { + try { + packageManager.getApplicationIcon(packageName) + } catch (e: PackageManager.NameNotFoundException) { + IDsToDelete.add(id.toString()) + null + } } }