|
| 1 | +package de.tum.`in`.tumcampus.widgets.calendar |
| 2 | + |
| 3 | +import android.appwidget.AppWidgetManager |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.SharedPreferences |
| 7 | +import android.view.View |
| 8 | +import android.widget.RemoteViews |
| 9 | +import de.tum.`in`.tumcampus.MainActivity |
| 10 | +import de.tum.`in`.tumcampus.R |
| 11 | +import de.tum.`in`.tumcampus.util.DateTimeSerializer |
| 12 | +import de.tum.`in`.tumcampus.util.timeAgo |
| 13 | +import es.antonborri.home_widget.HomeWidgetLaunchIntent |
| 14 | +import es.antonborri.home_widget.HomeWidgetProvider |
| 15 | +import java.time.LocalDate |
| 16 | +import java.time.LocalDateTime |
| 17 | +import java.time.Period |
| 18 | +import java.time.format.DateTimeFormatter |
| 19 | +import java.time.format.FormatStyle |
| 20 | +import java.time.format.TextStyle |
| 21 | +import java.util.Locale |
| 22 | +import androidx.core.net.toUri |
| 23 | + |
| 24 | +class CalendarWidgetProvider : HomeWidgetProvider() { |
| 25 | + override fun onUpdate( |
| 26 | + context: Context, |
| 27 | + appWidgetManager: AppWidgetManager, |
| 28 | + appWidgetIds: IntArray, |
| 29 | + widgetData: SharedPreferences |
| 30 | + ) { |
| 31 | + // There may be multiple widgets active, so update all of them |
| 32 | + for (appWidgetId in appWidgetIds) { |
| 33 | + updateAppWidget(context, appWidgetManager, appWidgetId, widgetData) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private fun updateAppWidget( |
| 38 | + context: Context, |
| 39 | + appWidgetManager: AppWidgetManager, |
| 40 | + appWidgetId: Int, |
| 41 | + widgetData: SharedPreferences |
| 42 | + ) { |
| 43 | + // Instantiate the RemoteViews object for the app widget layout. |
| 44 | + val remoteViews = RemoteViews(context.packageName, R.layout.calendar_widget) |
| 45 | + |
| 46 | + // Set formatted date in the header |
| 47 | + val localDate = LocalDate.now() |
| 48 | + val dateFormatter = |
| 49 | + DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(Locale.getDefault()) |
| 50 | + val date = localDate.format(dateFormatter) |
| 51 | + val weekday = localDate.dayOfWeek.getDisplayName(TextStyle.FULL, Locale.getDefault()) |
| 52 | + remoteViews.setTextViewText(R.id.calendar_widget_date, "$weekday, $date") |
| 53 | + |
| 54 | + // Set last saved date in the header |
| 55 | + val lastSaved = widgetData.getString("calendar_save", null) |
| 56 | + val lastSavedDate = DateTimeSerializer.deserializeStringToDate(lastSaved) |
| 57 | + val lastSavedDateString = (lastSavedDate ?: LocalDateTime.now()).timeAgo(context) |
| 58 | + remoteViews.setTextViewText(R.id.calendar_widget_updated_on, lastSavedDateString) |
| 59 | + |
| 60 | + if (lastSavedDate != null && Period.between( |
| 61 | + LocalDate.now(), |
| 62 | + lastSavedDate.toLocalDate(), |
| 63 | + ).days < 14 |
| 64 | + ) { |
| 65 | + // Set up the intent that starts the calendarWidgetService |
| 66 | + val intent = Intent(context, CalendarWidgetService::class.java) |
| 67 | + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 68 | + intent.data = intent.toUri(Intent.URI_INTENT_SCHEME).toUri() |
| 69 | + remoteViews.setRemoteAdapter(R.id.calendar_widget_listview, intent) |
| 70 | + |
| 71 | + // The empty view is displayed when the collection has no items. |
| 72 | + // It should be in the same layout used to instantiate the RemoteViews |
| 73 | + // object above. |
| 74 | + remoteViews.setEmptyView(R.id.calendar_widget_listview, R.id.empty_list_item) |
| 75 | + remoteViews.setViewVisibility(R.id.old_data_item, View.INVISIBLE) |
| 76 | + } else { |
| 77 | + remoteViews.setViewVisibility(R.id.old_data_item, View.VISIBLE) |
| 78 | + remoteViews.setViewVisibility(R.id.calendar_widget_listview, View.INVISIBLE) |
| 79 | + } |
| 80 | + |
| 81 | + val pendingIntentWithData = HomeWidgetLaunchIntent.getActivity( |
| 82 | + context, |
| 83 | + MainActivity::class.java, |
| 84 | + "tumCampusApp://message?homeWidget=calendar".toUri() |
| 85 | + ) |
| 86 | + remoteViews.setOnClickPendingIntent(R.id.calendar_widget, pendingIntentWithData) |
| 87 | + |
| 88 | + // Instruct the widget manager to update the widget |
| 89 | + appWidgetManager.updateAppWidget(appWidgetId, remoteViews) |
| 90 | + appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.calendar_widget_listview) |
| 91 | + } |
| 92 | +} |
0 commit comments