Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f64f6aec authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Nishith Khanna
Browse files

feat(db): add widget migration

parent 668e602f
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
 */
package foundation.e.bliss.utils

import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.ContentValues
import android.content.Context
@@ -240,6 +241,66 @@ object BlissDbUtils {
        return true
    }

    fun getWidgetDetails(context: Context): MutableList<WidgetItems> {
        val widgetsInfoList = mutableListOf<WidgetItems>()

        // Check if old database exists
        val oldFile = context.getDatabasePath(oldDbName)
        if (!oldFile.exists()) {
            // Check if old database with "_old" suffix exists
            val oldFileWithSuffix = context.getDatabasePath(oldDbName + "_old")
            if (!oldFileWithSuffix.exists()) {
                return widgetsInfoList
            }
        }

        // Determine the correct database file name to use
        val dbName = if (oldFile.exists()) oldDbName else oldDbName + "_old"

        // Initialize database helper class
        val oldDbHelper = BlissDbHelper(context, dbName)

        val appWidgetManager = AppWidgetManager.getInstance(context)
        oldDbHelper.readableDatabase.use { database ->
            database.rawQuery("SELECT * FROM widget_items", null).use { cursor ->
                while (cursor.moveToNext()) {
                    try {
                        val id = cursor.getInt(cursor.getColumnIndexOrThrow("id"))
                        val height = cursor.getInt(cursor.getColumnIndexOrThrow("height"))
                        val order = cursor.getInt(cursor.getColumnIndexOrThrow("order"))

                        // Get the AppWidgetInfo for the current widget ID
                        val widgetInfo = appWidgetManager.getAppWidgetInfo(id)
                        if (widgetInfo != null) {
                            widgetsInfoList.add(
                                WidgetItems(
                                    id,
                                    height,
                                    order,
                                    widgetInfo.provider,
                                )
                            )
                        }
                    } catch (e: URISyntaxException) {
                        Logger.e(TAG, "getWidgetDetails: ", e)
                    }
                }
            }
        }

        // Close oldDbHelper
        oldDbHelper.close()

        return widgetsInfoList
    }

    data class WidgetItems(
        val id: Int,
        val height: Int,
        val order: Int,
        val componentName: ComponentName
    )

    private fun getBaseContentValues(favorite: Favorite): ContentValues {
        return ContentValues().apply {
            put("appWidgetId", -1)
+2 −2
Original line number Diff line number Diff line
@@ -30,11 +30,11 @@ class BlissAppWidgetHost(val context: Context) : AppWidgetHost(context, WIDGET_H

    @SuppressLint("NewApi")
    override fun onCreateView(
        context: Context?,
        context: Context,
        appWidgetId: Int,
        appWidget: AppWidgetProviderInfo?
    ): AppWidgetHostView {
        val blur = DefaultWidgets.widgets.contains(appWidget?.provider)
        val blur = DefaultWidgets.getWidgetsList(context).contains(appWidget?.provider)
        return RoundedWidgetView(context, blur)
    }

+24 −1
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
package foundation.e.bliss.widgets

import android.content.ComponentName
import android.content.Context
import foundation.e.bliss.utils.BlissDbUtils

object DefaultWidgets {
    private val ecloudWidget =
@@ -30,5 +32,26 @@ object DefaultWidgets {
            "foundation.e.blissweather.widget.WeatherAppWidgetProvider"
        )

    @JvmStatic val widgets = listOf(ecloudWidget, privacyWidget, weatherWidget)
    private val widgets = listOf(ecloudWidget, privacyWidget, weatherWidget)

    @JvmStatic
    fun getWidgetsList(context: Context): List<ComponentName> {
        val providerList: MutableList<ComponentName> = mutableListOf()

        // Get widget details from old database
        val widgetItemsList: MutableList<BlissDbUtils.WidgetItems> =
            BlissDbUtils.getWidgetDetails(context)

        for (widgetItem in widgetItemsList) {
            val provider = widgetItem.componentName
            provider.let { providerList.add(it) }
        }

        // Return default widgets if the providerList is empty
        return if (providerList.isEmpty()) {
            widgets
        } else {
            providerList
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) : FrameLayout(cont
                mWidgetHost.deleteHost()
                Logger.e(TAG, "default not added ${mWidgetHost.appWidgetIds.size}")

                DefaultWidgets.widgets.forEach {
                DefaultWidgets.getWidgetsList(context).forEach {
                    try {
                        bindWidget(it)
                    } catch (e: Exception) {