mirror of
https://github.com/SimpleMobileTools/Simple-App-Launcher.git
synced 2025-02-08 07:48:39 +01:00
try getting the properly colored app launcher icon
This commit is contained in:
parent
5d57bcf7c6
commit
7f9090ecdf
@ -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,10 +27,23 @@ fun Context.getNotDisplayedLaunchers(displayedLaunchers: ArrayList<AppLauncher>)
|
||||
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))
|
||||
|
@ -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,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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user