add app icons at the widget list too

This commit is contained in:
tibbi 2022-09-03 21:35:47 +02:00
parent d8129e2e55
commit 92d0d8a958
5 changed files with 52 additions and 10 deletions

View File

@ -30,10 +30,12 @@ class WidgetsAdapter(
override fun getItemCount() = appWidgets.size
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(widget: AppWidget): View {
fun bindView(appWidget: AppWidget): View {
itemView.apply {
widget_app_title.text = widget.appTitle
widget_app_title.text = appWidget.appTitle
widget_app_title.setTextColor(textColor)
widget_app_icon.setImageDrawable(appWidget.appIcon)
}
return itemView

View File

@ -2,6 +2,8 @@ package com.simplemobiletools.launcher.fragments
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.pm.LauncherApps
import android.os.Process
import android.util.AttributeSet
import android.view.Surface
import android.view.WindowManager
@ -11,6 +13,7 @@ import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.adapters.WidgetsAdapter
import com.simplemobiletools.launcher.models.AppMetadata
import com.simplemobiletools.launcher.models.AppWidget
import kotlinx.android.synthetic.main.widgets_fragment.view.*
@ -37,11 +40,13 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
val infoList = manager.installedProviders
for (info in infoList) {
val appPackageName = info.provider.packageName
val appTitle = getAppNameFromPackage(appPackageName) ?: continue
val appMetadata = getAppMetadataFromPackage(appPackageName) ?: continue
val appTitle = appMetadata.appTitle
val appIcon = appMetadata.appIcon
val widgetTitle = info.loadLabel(activity?.packageManager)
val width = info.minWidth
val height = info.minHeight
val widget = AppWidget(appPackageName, appTitle, widgetTitle, width, height)
val widget = AppWidget(appPackageName, appTitle, appIcon, widgetTitle, width, height)
appWidgets.add(widget)
}
@ -95,11 +100,24 @@ class WidgetsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
widgets_fastscroller.setPadding(leftListPadding, 0, rightListPadding, 0)
}
private fun getAppNameFromPackage(packageName: String): String? {
private fun getAppMetadataFromPackage(packageName: String): AppMetadata? {
try {
val appInfo = activity!!.packageManager.getApplicationInfo(packageName, 0)
return activity!!.packageManager.getApplicationLabel(appInfo).toString()
val appTitle = activity!!.packageManager.getApplicationLabel(appInfo).toString()
val launcher = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityList = launcher.getActivityList(packageName, Process.myUserHandle())
var appIcon = activityList.firstOrNull()?.getBadgedIcon(0)
if (appIcon == null) {
appIcon = context.packageManager.getApplicationIcon(packageName)
}
if (appTitle.isNotEmpty()) {
return AppMetadata(appTitle, appIcon)
}
} catch (ignored: Exception) {
} catch (error: Error) {
}
return null

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.launcher.models
import android.graphics.drawable.Drawable
data class AppMetadata(var appTitle: String, var appIcon: Drawable)

View File

@ -1,3 +1,5 @@
package com.simplemobiletools.launcher.models
data class AppWidget(var appPackageName: String, var appTitle: String, val widgetTitle: String, var width: Int, val height: Int)
import android.graphics.drawable.Drawable
data class AppWidget(var appPackageName: String, var appTitle: String, val appIcon: Drawable, val widgetTitle: String, var width: Int, val height: Int)

View File

@ -3,15 +3,30 @@
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_app_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:padding="@dimen/medium_margin">
<com.simplemobiletools.commons.views.MySquareImageView
android:id="@+id/widget_app_icon"
android:layout_width="@dimen/launcher_icon_size"
android:layout_height="@dimen/launcher_icon_size"
android:layout_gravity="center_horizontal|bottom"
android:padding="@dimen/small_margin"
tools:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/widget_app_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@+id/widget_app_icon"
android:layout_alignBottom="@+id/widget_app_icon"
android:layout_toEndOf="@+id/widget_app_icon"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="@dimen/normal_margin"
android:paddingStart="@dimen/medium_margin"
android:paddingTop="@dimen/normal_margin"
android:paddingBottom="@dimen/normal_margin"
android:textSize="@dimen/bigger_text_size"
tools:text="@string/app_name" />