From f154b444cc2b6c22e9bf4ad5392658e7997c0a5a Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 1 Sep 2022 15:48:24 +0200 Subject: [PATCH] adding some widget fetching code --- .../launcher/fragments/AllAppsFragment.kt | 2 +- .../launcher/fragments/WidgetsFragment.kt | 52 +++++++++++++++++++ .../launcher/models/AppWidget.kt | 3 ++ app/src/main/res/layout/widgets_fragment.xml | 23 ++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt create mode 100644 app/src/main/res/layout/widgets_fragment.xml 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 5a1f7a6..c88125d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -30,9 +30,9 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : RelativeLa @SuppressLint("ClickableViewAccessibility") fun setupFragment(activity: MainActivity) { this.activity = activity - getLaunchers() background.applyColorFilter(activity.getProperBackgroundColor()) setPadding(0, activity.statusBarHeight, 0, 0) + getLaunchers() all_apps_grid.setOnTouchListener { v, event -> if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL) { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt new file mode 100644 index 0000000..983625c --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/WidgetsFragment.kt @@ -0,0 +1,52 @@ +package com.simplemobiletools.launcher.fragments + +import android.appwidget.AppWidgetManager +import android.content.Context +import android.util.AttributeSet +import android.widget.RelativeLayout +import com.simplemobiletools.commons.extensions.applyColorFilter +import com.simplemobiletools.commons.extensions.getProperBackgroundColor +import com.simplemobiletools.commons.extensions.statusBarHeight +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.launcher.activities.MainActivity +import com.simplemobiletools.launcher.models.AppWidget + +class WidgetsFragment(context: Context, attributeSet: AttributeSet) : RelativeLayout(context, attributeSet) { + private var activity: MainActivity? = null + + fun setupFragment(activity: MainActivity) { + this.activity = activity + background.applyColorFilter(activity.getProperBackgroundColor()) + setPadding(0, activity.statusBarHeight, 0, 0) + getWidgets() + } + + private fun getWidgets() { + ensureBackgroundThread { + var widgets = ArrayList() + val manager = AppWidgetManager.getInstance(context) + val infoList = manager.installedProviders + for (info in infoList) { + val appPackageName = info.provider.packageName + val appTitle = getAppNameFromPackage(appPackageName) ?: continue + val widgetTitle = info.loadLabel(activity?.packageManager) + val width = info.minWidth + val height = info.minHeight + val widget = AppWidget(appPackageName, appTitle, widgetTitle, width, height) + widgets.add(widget) + } + + widgets = widgets.sortedWith(compareBy({ it.appTitle }, { it.widgetTitle })).toMutableList() as ArrayList + } + } + + private fun getAppNameFromPackage(packageName: String): String? { + try { + val appInfo = activity!!.packageManager.getApplicationInfo(packageName, 0) + return activity!!.packageManager.getApplicationLabel(appInfo).toString() + } catch (ignored: Exception) { + } + + return null + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt new file mode 100644 index 0000000..cf9d03e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppWidget.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.launcher.models + +data class AppWidget(var appPackageName: String, var appTitle: String, val widgetTitle: String, var width: Int, val height: Int) diff --git a/app/src/main/res/layout/widgets_fragment.xml b/app/src/main/res/layout/widgets_fragment.xml new file mode 100644 index 0000000..eb450f3 --- /dev/null +++ b/app/src/main/res/layout/widgets_fragment.xml @@ -0,0 +1,23 @@ + + + + + + + + +