package com.deeplinkingwidget import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.widget.RemoteViews import android.app.PendingIntent import android.content.Intent import android.net.Uri /** * Implementation of App Widget functionality. */ class NewAppWidget : AppWidgetProvider() { override fun onUpdate( context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray ) { // There may be multiple widgets active, so update all of them for (appWidgetId in appWidgetIds) { updateAppWidget(context, appWidgetManager, appWidgetId) } } override fun onEnabled(context: Context) { // Enter relevant functionality for when the first widget is created } override fun onDisabled(context: Context) { // Enter relevant functionality for when the last widget is disabled } } internal fun updateAppWidget( context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int ) { // val widgetText = context.getString(R.string.appwidget_text) // Construct the RemoteViews object val views = RemoteViews(context.packageName, R.layout.new_app_widget) // views.setTextViewText(R.id.appwidget_text, widgetText) views.setImageViewResource(R.id.widget_image, R.drawable.thumb_bubble_icon) val prefs = context.getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE) val widgetDataString = prefs.getString("widgetData", "{}") val widgetData = widgetDataString?.let { // Simple parsing - you might want to use proper JSON parsing if (it.contains("text=")) { it.substringAfter("text=").substringBefore(",") } else { "Default Text" } } ?: "Default Text" // val deepLinkUrl = "https://google.com" // val deepLinkUrl = "myapp://home" val deepLinkUrl = "flow-magic://record-speech" // val deepLinkUrl = prefs.getString("widgetUrl", "https://www.google.com") ?: "https://www.google.com" val intent = Intent(Intent.ACTION_VIEW, Uri.parse(deepLinkUrl)) // val intent = Intent(Intent.ACTION_VIEW).apply { // // Use your app's deep link scheme // data = Uri.parse("flow-magic://record-speech") // // Ensure it opens your app specifically // `package` = context.packageName // } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.`package` = null // Allow any browser to handle it // 3. Create pending intent val pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) // 4. Set click handler on the entire widget or specific view views.setOnClickPendingIntent(R.id.widget_layout_root, pendingIntent) // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views) }