try getting the properly colored app launcher icon

This commit is contained in:
tibbi 2018-05-22 19:22:11 +02:00
parent 5d57bcf7c6
commit 7f9090ecdf
2 changed files with 52 additions and 14 deletions

View File

@ -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<AppLauncher>): ArrayList<AppLauncher> {
val allApps = ArrayList<AppLauncher>()
val intent = Intent(Intent.ACTION_MAIN, null)
@ -21,11 +27,24 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>)
val label = componentInfo.loadLabel(packageManager).toString()
val packageName = componentInfo.packageName
val drawable = if (packageName.isAPredefinedApp()) {
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))
}

View File

@ -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<AppLauncher> {
val resources = context.resources
val packageManager = context.packageManager
@ -122,7 +128,19 @@ 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 {
// 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) {
}
}
if (drawable == null) {
drawable = if (packageName.isAPredefinedApp()) {
try {
packageManager.getApplicationIcon(packageName)
} catch (e: PackageManager.NameNotFoundException) {
@ -136,6 +154,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
null
}
}
}
if (drawable != null) {
val launcher = AppLauncher(id, name, packageName, drawable)