From f8d00342220388df3e4d8660042e2223928bd1bf Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 14 Oct 2022 10:43:39 +0200 Subject: [PATCH] fix #24, add support for apps creating multiple icons, not just launcher --- .../launcher/activities/MainActivity.kt | 31 +++++++++---------- .../launcher/databases/AppsDatabase.kt | 10 +++++- .../launcher/extensions/Activity.kt | 14 +++++++-- .../launcher/fragments/AllAppsFragment.kt | 20 ++++++++++-- .../launcher/models/AppLauncher.kt | 3 +- .../launcher/models/HomeScreenGridItem.kt | 3 +- .../launcher/views/HomeScreenGrid.kt | 1 + 7 files changed, 57 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 6d559db..2f60f3e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -107,6 +107,7 @@ class MainActivity : SimpleActivity(), FlingListener { rect.right, rect.bottom, item.shortcutInfo!!.`package`, + "", label, ITEM_TYPE_SHORTCUT, "", @@ -396,7 +397,7 @@ class MainActivity : SimpleActivity(), FlingListener { val clickedGridItem = home_screen_grid.isClickingGridItem(x.toInt(), y.toInt()) if (clickedGridItem != null) { if (clickedGridItem.type == ITEM_TYPE_ICON) { - launchApp(clickedGridItem.packageName) + launchApp(clickedGridItem.packageName, clickedGridItem.activityName) } else if (clickedGridItem.type == ITEM_TYPE_SHORTCUT) { if (clickedGridItem.intent.isNotEmpty()) { launchShortcutIntent(clickedGridItem) @@ -567,7 +568,6 @@ class MainActivity : SimpleActivity(), FlingListener { @SuppressLint("WrongConstant") fun getAllAppLaunchers(): ArrayList { val allApps = ArrayList() - val allPackageNames = ArrayList() val intent = Intent(Intent.ACTION_MAIN, null) intent.addCategory(Intent.CATEGORY_LAUNCHER) @@ -576,28 +576,25 @@ class MainActivity : SimpleActivity(), FlingListener { val list = packageManager.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED) for (info in list) { val componentInfo = info.activityInfo.applicationInfo - val packageName = componentInfo.packageName + var packageName = componentInfo.packageName if (packageName == simpleLauncher || packageName == microG) { continue } val label = info.loadLabel(packageManager).toString() - val drawable = getDrawableForPackageName(packageName) ?: continue + val drawable = info.loadIcon(packageManager) ?: getDrawableForPackageName(packageName) ?: continue val placeholderColor = calculateAverageColor(drawable.toBitmap()) - - allPackageNames.add(packageName) - allApps.add(AppLauncher(null, label, packageName, 0, placeholderColor, drawable)) + val activityName = info.activityInfo.name + allApps.add(AppLauncher(null, label, packageName, activityName, 0, placeholderColor, drawable)) } // add Simple Launchers settings as an app val drawable = getDrawableForPackageName(packageName) val placeholderColor = calculateAverageColor(drawable!!.toBitmap()) - val launcherSettings = AppLauncher(null, getString(R.string.launcher_settings), packageName, 0, placeholderColor, drawable) + val launcherSettings = AppLauncher(null, getString(R.string.launcher_settings), packageName, "", 0, placeholderColor, drawable) allApps.add(launcherSettings) - - val launchers = allApps.distinctBy { it.packageName } as ArrayList - launchersDB.insertAll(launchers) - return launchers + launchersDB.insertAll(allApps) + return allApps } private fun getDefaultAppPackages(appLaunchers: ArrayList) { @@ -605,7 +602,7 @@ class MainActivity : SimpleActivity(), FlingListener { try { val defaultDialerPackage = (getSystemService(Context.TELECOM_SERVICE) as TelecomManager).defaultDialerPackage appLaunchers.firstOrNull { it.packageName == defaultDialerPackage }?.apply { - val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 0, ROW_COUNT - 1, defaultDialerPackage, title, ITEM_TYPE_ICON, "", -1, "", "", null) + val dialerIcon = HomeScreenGridItem(null, 0, ROW_COUNT - 1, 0, ROW_COUNT - 1, defaultDialerPackage, "", title, ITEM_TYPE_ICON, "", -1, "", "", null) homeScreenGridItems.add(dialerIcon) } } catch (e: Exception) { @@ -615,7 +612,7 @@ class MainActivity : SimpleActivity(), FlingListener { val defaultSMSMessengerPackage = Telephony.Sms.getDefaultSmsPackage(this) appLaunchers.firstOrNull { it.packageName == defaultSMSMessengerPackage }?.apply { val SMSMessengerIcon = - HomeScreenGridItem(null, 1, ROW_COUNT - 1, 1, ROW_COUNT - 1, defaultSMSMessengerPackage, title, ITEM_TYPE_ICON, "", -1, "", "", null) + HomeScreenGridItem(null, 1, ROW_COUNT - 1, 1, ROW_COUNT - 1, defaultSMSMessengerPackage, "", title, ITEM_TYPE_ICON, "", -1, "", "", null) homeScreenGridItems.add(SMSMessengerIcon) } } catch (e: Exception) { @@ -627,7 +624,7 @@ class MainActivity : SimpleActivity(), FlingListener { val defaultBrowserPackage = resolveInfo!!.activityInfo.packageName appLaunchers.firstOrNull { it.packageName == defaultBrowserPackage }?.apply { val browserIcon = - HomeScreenGridItem(null, 2, ROW_COUNT - 1, 2, ROW_COUNT - 1, defaultBrowserPackage, title, ITEM_TYPE_ICON, "", -1, "", "", null) + HomeScreenGridItem(null, 2, ROW_COUNT - 1, 2, ROW_COUNT - 1, defaultBrowserPackage, "", title, ITEM_TYPE_ICON, "", -1, "", "", null) homeScreenGridItems.add(browserIcon) } } catch (e: Exception) { @@ -638,7 +635,7 @@ class MainActivity : SimpleActivity(), FlingListener { val storePackage = potentialStores.firstOrNull { isPackageInstalled(it) && appLaunchers.map { it.packageName }.contains(it) } if (storePackage != null) { appLaunchers.firstOrNull { it.packageName == storePackage }?.apply { - val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 3, ROW_COUNT - 1, storePackage, title, ITEM_TYPE_ICON, "", -1, "", "", null) + val storeIcon = HomeScreenGridItem(null, 3, ROW_COUNT - 1, 3, ROW_COUNT - 1, storePackage, "", title, ITEM_TYPE_ICON, "", -1, "", "", null) homeScreenGridItems.add(storeIcon) } } @@ -650,7 +647,7 @@ class MainActivity : SimpleActivity(), FlingListener { val resolveInfo = packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY) val defaultCameraPackage = resolveInfo!!.activityInfo.packageName appLaunchers.firstOrNull { it.packageName == defaultCameraPackage }?.apply { - val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 4, ROW_COUNT - 1, defaultCameraPackage, title, ITEM_TYPE_ICON, "", -1, "", "", null) + val cameraIcon = HomeScreenGridItem(null, 4, ROW_COUNT - 1, 4, ROW_COUNT - 1, defaultCameraPackage, "", title, ITEM_TYPE_ICON, "", -1, "", "", null) homeScreenGridItems.add(cameraIcon) } } catch (e: Exception) { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt index bf0baf1..b32ea36 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt @@ -13,7 +13,7 @@ import com.simplemobiletools.launcher.interfaces.HomeScreenGridItemsDao import com.simplemobiletools.launcher.models.AppLauncher import com.simplemobiletools.launcher.models.HomeScreenGridItem -@Database(entities = [AppLauncher::class, HomeScreenGridItem::class], version = 2) +@Database(entities = [AppLauncher::class, HomeScreenGridItem::class], version = 3) @TypeConverters(Converters::class) abstract class AppsDatabase : RoomDatabase() { @@ -30,6 +30,7 @@ abstract class AppsDatabase : RoomDatabase() { if (db == null) { db = Room.databaseBuilder(context.applicationContext, AppsDatabase::class.java, "apps.db") .addMigrations(MIGRATION_1_2) + .addMigrations(MIGRATION_2_3) .build() } } @@ -44,5 +45,12 @@ abstract class AppsDatabase : RoomDatabase() { database.execSQL("ALTER TABLE home_screen_grid_items ADD COLUMN icon BLOB") } } + + private val MIGRATION_2_3 = object : Migration(2, 3) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE apps ADD COLUMN activity_name TEXT default '' NOT NULL") + database.execSQL("ALTER TABLE home_screen_grid_items ADD COLUMN activity_name TEXT default '' NOT NULL") + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt index 3e8a7bf..5d76958 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt @@ -1,6 +1,8 @@ package com.simplemobiletools.launcher.extensions import android.app.Activity +import android.content.ActivityNotFoundException +import android.content.ComponentName import android.content.Intent import android.net.Uri import android.provider.Settings @@ -9,16 +11,22 @@ import com.simplemobiletools.launcher.activities.SettingsActivity import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE import com.simplemobiletools.launcher.models.HomeScreenGridItem -fun Activity.launchApp(packageName: String) { +fun Activity.launchApp(packageName: String, activityName: String) { // if this is true, launch the app settings if (packageName == this.packageName) { startActivity(Intent(applicationContext, SettingsActivity::class.java)) return } - val launchIntent = packageManager.getLaunchIntentForPackage(packageName) try { - startActivity(launchIntent) + Intent(Intent.ACTION_MAIN).apply { + addCategory(Intent.CATEGORY_LAUNCHER) + `package` = packageName + component = ComponentName.unflattenFromString("$packageName/$activityName") + startActivity(this) + } + } catch (e: ActivityNotFoundException) { + showErrorToast(e) } catch (e: Exception) { showErrorToast(e) } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index 6848711..0bb525f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -98,7 +98,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment val currAdapter = all_apps_grid.adapter if (currAdapter == null) { LaunchersAdapter(activity!!, launchers, this) { - activity?.launchApp((it as AppLauncher).packageName) + activity?.launchApp((it as AppLauncher).packageName, it.activityName) }.apply { all_apps_grid.adapter = this } @@ -151,7 +151,23 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) { val gridItem = - HomeScreenGridItem(null, -1, -1, -1, -1, appLauncher.packageName, appLauncher.title, ITEM_TYPE_ICON, "", -1, "", "", null, appLauncher.drawable) + HomeScreenGridItem( + null, + -1, + -1, + -1, + -1, + appLauncher.packageName, + appLauncher.activityName, + appLauncher.title, + ITEM_TYPE_ICON, + "", + -1, + "", + "", + null, + appLauncher.drawable + ) activity?.showHomeIconMenu(x, y, gridItem, true) ignoreTouches = true } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt index e901ed9..2bb67b1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt @@ -11,13 +11,14 @@ data class AppLauncher( @PrimaryKey(autoGenerate = true) var id: Long?, @ColumnInfo(name = "title") var title: String, @ColumnInfo(name = "package_name") var packageName: String, + @ColumnInfo(name = "activity_name") var activityName: String, // some apps create multiple icons, this is needed at clicking them @ColumnInfo(name = "order") var order: Int, @ColumnInfo(name = "thumbnail_color") var thumbnailColor: Int, @Ignore var drawable: Drawable? ) : Comparable { - constructor() : this(null, "", "", 0, 0, null) + constructor() : this(null, "", "", "", 0, 0, null) companion object { var sorting = 0 diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt index 92f62e9..f82ee3b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt @@ -16,6 +16,7 @@ data class HomeScreenGridItem( @ColumnInfo(name = "right") var right: Int, @ColumnInfo(name = "bottom") var bottom: Int, @ColumnInfo(name = "package_name") var packageName: String, + @ColumnInfo(name = "activity_name") var activityName: String, // needed at apps that create multiple icons at install, not just the launcher @ColumnInfo(name = "title") var title: String, @ColumnInfo(name = "type") var type: Int, @ColumnInfo(name = "class_name") var className: String, @@ -30,7 +31,7 @@ data class HomeScreenGridItem( @Ignore var widthCells: Int = 1, @Ignore var heightCells: Int = 1 ) { - constructor() : this(null, -1, -1, -1, -1, "", "", ITEM_TYPE_ICON, "", -1, "", "", null, null, null, null, 1, 1) + constructor() : this(null, -1, -1, -1, -1, "", "", "", ITEM_TYPE_ICON, "", -1, "", "", null, null, null, null, 1, 1) fun getWidthInCells() = if (right == -1 || left == -1) { widthCells diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index a12eb87..f544109 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -278,6 +278,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel xIndex, yIndex, draggedItem!!.packageName, + draggedItem!!.activityName, draggedItem!!.title, draggedItem!!.type, "",