mirror of https://github.com/Xe/xesite_android
44 lines
1.6 KiB
Kotlin
44 lines
1.6 KiB
Kotlin
package website.christine.xesite
|
|
|
|
import android.appwidget.AppWidgetManager
|
|
import android.appwidget.AppWidgetProvider
|
|
import android.content.Context
|
|
import android.widget.RemoteViews
|
|
|
|
/**
|
|
* Implementation of App Widget functionality.
|
|
* App Widget Configuration implemented in [CurrentPostWidgetConfigureActivity]
|
|
*/
|
|
class CurrentPostWidget : 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 onDeleted(context: Context, appWidgetIds: IntArray) {
|
|
// When the user deletes the widget, delete the preference associated with it.
|
|
for (appWidgetId in appWidgetIds) {
|
|
deleteTitlePref(context, 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 = loadTitlePref(context, appWidgetId)
|
|
// Construct the RemoteViews object
|
|
val views = RemoteViews(context.packageName, R.layout.current_post_widget)
|
|
views.setTextViewText(R.id.appwidget_text, widgetText)
|
|
|
|
// Instruct the widget manager to update the widget
|
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
} |