fix Account Spotlight widget
This commit is contained in:
2 files changed
+59
-3
No files matched your search
@@ -1,7 +1,10 @@
|
||||
package net.rodakot.remaining.ui.screens
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
@@ -43,6 +46,7 @@ import net.rodakot.remaining.widget.RemainingWidgetRenderer
|
||||
import net.rodakot.remaining.widget.RemainingWidgetStats
|
||||
import net.rodakot.remaining.widget.RemainingWidgetType
|
||||
import net.rodakot.remaining.widget.AccountSpotlightWidgetProvider
|
||||
import net.rodakot.remaining.widget.AccountWidgetConfigureActivity
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -114,7 +118,7 @@ fun WidgetGalleryScreen(
|
||||
AppWidgetManager.getInstance(context).requestPinAppWidget(
|
||||
ComponentName(context, AccountSpotlightWidgetProvider::class.java),
|
||||
null,
|
||||
null
|
||||
accountWidgetConfigureCallback(context)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -234,3 +238,22 @@ private fun previewHeight(type: RemainingWidgetType) =
|
||||
RemainingWidgetType.ACCOUNT_MATRIX -> 184.dp
|
||||
else -> 156.dp
|
||||
}
|
||||
|
||||
private fun accountWidgetConfigureCallback(context: Context): PendingIntent {
|
||||
val intent = Intent(context, AccountWidgetConfigureActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
val mutabilityFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
PendingIntent.FLAG_MUTABLE
|
||||
} else {
|
||||
0
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
context,
|
||||
ACCOUNT_WIDGET_CONFIGURE_REQUEST,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or mutabilityFlag
|
||||
)
|
||||
}
|
||||
|
||||
private const val ACCOUNT_WIDGET_CONFIGURE_REQUEST = 45_901
|
||||
@@ -5,6 +5,7 @@ import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@@ -154,14 +155,24 @@ object RemainingWidgetUpdater {
|
||||
R.id.remaining_widget_image,
|
||||
data.account?.let { "Account Spotlight: ${it.name}" } ?: "Account Spotlight"
|
||||
)
|
||||
views.setOnClickPendingIntent(R.id.remaining_widget_root, openAppIntent(context, widgetId))
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
if (data.account == null) {
|
||||
openConfigureIntent(context, widgetId, mutable = false)
|
||||
} else {
|
||||
openAppIntent(context, widgetId)
|
||||
}
|
||||
)
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}.onFailure {
|
||||
val views = RemoteViews(context.packageName, R.layout.remaining_widget_chart)
|
||||
views.setViewVisibility(R.id.remaining_widget_image, View.GONE)
|
||||
views.setViewVisibility(R.id.remaining_widget_fallback, View.VISIBLE)
|
||||
views.setTextViewText(R.id.remaining_widget_fallback, "Choose account")
|
||||
views.setOnClickPendingIntent(R.id.remaining_widget_root, openAppIntent(context, widgetId))
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
openConfigureIntent(context, widgetId, mutable = false)
|
||||
)
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}
|
||||
}
|
||||
@@ -186,4 +197,26 @@ object RemainingWidgetUpdater {
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
fun openConfigureIntent(
|
||||
context: Context,
|
||||
requestCode: Int,
|
||||
mutable: Boolean
|
||||
): PendingIntent {
|
||||
val intent = Intent(context, AccountWidgetConfigureActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, requestCode)
|
||||
}
|
||||
val mutabilityFlag = if (mutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
PendingIntent.FLAG_MUTABLE
|
||||
} else {
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
context,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or mutabilityFlag
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user