diff --git a/android/app/src/main/java/org/sparcs/otlplus/TodayWidget.kt b/android/app/src/main/java/org/sparcs/otlplus/TodayWidget.kt index ee892ab1..ae53be2a 100644 --- a/android/app/src/main/java/org/sparcs/otlplus/TodayWidget.kt +++ b/android/app/src/main/java/org/sparcs/otlplus/TodayWidget.kt @@ -7,6 +7,7 @@ import android.appwidget.AppWidgetProvider import android.content.ComponentName import android.content.Context import android.content.Intent +import android.util.Log import android.view.View import java.util.Calendar import android.widget.RemoteViews @@ -60,8 +61,9 @@ class TodayWidget : AppWidgetProvider() { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(context, TodayWidget::class.java) intent.action = ACTION_UPDATE_WIDGET - val pendingIntent = - PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent = PendingIntent.getBroadcast( + context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE + ) // Schedule alarm to update widget every minute alarmManager.setRepeating( @@ -74,12 +76,11 @@ class TodayWidget : AppWidgetProvider() { val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val intent = Intent(context, TodayWidget::class.java) intent.action = ACTION_UPDATE_WIDGET - val pendingIntent = - PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + val pendingIntent = PendingIntent.getBroadcast( + context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE + ) alarmManager.cancel(pendingIntent) } - - } internal fun updateTodayWidget( @@ -94,8 +95,9 @@ internal fun updateTodayWidget( val dottedWidth = context.resources.getDimensionPixelSize(R.dimen.today_dotted_time_width) val indicatorWidth = context.resources.getDimensionPixelSize(R.dimen.time_indicator_width) val itemMargin = context.resources.getDimensionPixelSize(R.dimen.today_time_margin) + val blockMargin = context.resources.getDimensionPixelSize(R.dimen.block_margin) val hourMargin = solidWidth + dottedWidth + itemMargin * 2 - val startMargin = itemMargin + solidWidth / 2.0 - indicatorWidth / 2.0 + val startMargin = itemMargin + solidWidth / 2.0 // Construct the RemoteViews object val views = RemoteViews(context.packageName, R.layout.today_widget) @@ -126,10 +128,12 @@ internal fun updateTodayWidget( val hour = calendar.get(Calendar.HOUR_OF_DAY) val minute = calendar.get(Calendar.MINUTE) val visibility = isVisible(hour, minute) + val offset = getOffset(hour, minute) + + Log.d("TodayWidget", "updated at $hour:$minute") if (visibility) { - val margin = getMargin(hour, minute) - val offset = getOffset(hour, minute) + val margin = getMargin(hour, minute) - indicatorWidth / 2.0 views.setViewPadding(R.id.todayTimetable, (itemMargin - offset).roundToInt(), 0, 0, 0) views.setViewVisibility(R.id.timeIndicator, View.VISIBLE) @@ -139,6 +143,39 @@ internal fun updateTodayWidget( views.setViewVisibility(R.id.timeIndicator, View.INVISIBLE) } + // Add lecture blocks + views.removeAllViews(R.id.todayLectureBlockContainer) + + fun getLayoutId(minute: Int): Int? = when (minute) { + 30 -> R.layout.today_lecture_block_30 + 50 -> R.layout.today_lecture_block_50 + 60 -> R.layout.today_lecture_block_60 + 70 -> R.layout.today_lecture_block_70 + 75 -> R.layout.today_lecture_block_75 + 90 -> R.layout.today_lecture_block_90 + 110 -> R.layout.today_lecture_block_110 + 120 -> R.layout.today_lecture_block_120 + 150 -> R.layout.today_lecture_block_150 + 165 -> R.layout.today_lecture_block_165 + 170 -> R.layout.today_lecture_block_170 + 180 -> R.layout.today_lecture_block_180 + 210 -> R.layout.today_lecture_block_210 + 240 -> R.layout.today_lecture_block_240 + 300 -> R.layout.today_lecture_block_300 + 360 -> R.layout.today_lecture_block_360 + 420 -> R.layout.today_lecture_block_420 + 480 -> R.layout.today_lecture_block_480 + 540 -> R.layout.today_lecture_block_540 + else -> null + } + + (getLayoutId(110))?.let { layoutId -> + val margin = getMargin(10, 30) + blockMargin + val lectureBlock = RemoteViews(context.packageName, layoutId) + lectureBlock.setViewPadding(R.id.todayLectureBlock, (margin - offset).roundToInt(), 0, 0, 0) + views.addView(R.id.todayLectureBlockContainer, lectureBlock) + } + // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views) } \ No newline at end of file diff --git a/android/app/src/main/res/drawable/lecture_block_background.xml b/android/app/src/main/res/drawable/lecture_block_background.xml new file mode 100644 index 00000000..4af3d56b --- /dev/null +++ b/android/app/src/main/res/drawable/lecture_block_background.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_110.xml b/android/app/src/main/res/layout/today_lecture_block_110.xml new file mode 100644 index 00000000..5b9cd000 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_110.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_120.xml b/android/app/src/main/res/layout/today_lecture_block_120.xml new file mode 100644 index 00000000..4bfea405 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_120.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_150.xml b/android/app/src/main/res/layout/today_lecture_block_150.xml new file mode 100644 index 00000000..f8f6dabc --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_150.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_165.xml b/android/app/src/main/res/layout/today_lecture_block_165.xml new file mode 100644 index 00000000..4b0c0137 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_165.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_170.xml b/android/app/src/main/res/layout/today_lecture_block_170.xml new file mode 100644 index 00000000..b79d3ce8 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_170.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_180.xml b/android/app/src/main/res/layout/today_lecture_block_180.xml new file mode 100644 index 00000000..6c968581 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_180.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_210.xml b/android/app/src/main/res/layout/today_lecture_block_210.xml new file mode 100644 index 00000000..a7ce93f6 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_210.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_240.xml b/android/app/src/main/res/layout/today_lecture_block_240.xml new file mode 100644 index 00000000..9c284a6a --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_240.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_30.xml b/android/app/src/main/res/layout/today_lecture_block_30.xml new file mode 100644 index 00000000..c9cdaf8f --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_30.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_300.xml b/android/app/src/main/res/layout/today_lecture_block_300.xml new file mode 100644 index 00000000..25db4ff8 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_300.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_360.xml b/android/app/src/main/res/layout/today_lecture_block_360.xml new file mode 100644 index 00000000..c903004a --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_360.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_420.xml b/android/app/src/main/res/layout/today_lecture_block_420.xml new file mode 100644 index 00000000..75055603 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_420.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_480.xml b/android/app/src/main/res/layout/today_lecture_block_480.xml new file mode 100644 index 00000000..b9c980f4 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_480.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_50.xml b/android/app/src/main/res/layout/today_lecture_block_50.xml new file mode 100644 index 00000000..8e165e51 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_50.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_540.xml b/android/app/src/main/res/layout/today_lecture_block_540.xml new file mode 100644 index 00000000..1f790134 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_540.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_60.xml b/android/app/src/main/res/layout/today_lecture_block_60.xml new file mode 100644 index 00000000..396950cf --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_60.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_70.xml b/android/app/src/main/res/layout/today_lecture_block_70.xml new file mode 100644 index 00000000..0fa0d19f --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_70.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_75.xml b/android/app/src/main/res/layout/today_lecture_block_75.xml new file mode 100644 index 00000000..96360d00 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_75.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_lecture_block_90.xml b/android/app/src/main/res/layout/today_lecture_block_90.xml new file mode 100644 index 00000000..b7223330 --- /dev/null +++ b/android/app/src/main/res/layout/today_lecture_block_90.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/res/layout/today_widget.xml b/android/app/src/main/res/layout/today_widget.xml index 810b7cc1..f68dd47f 100644 --- a/android/app/src/main/res/layout/today_widget.xml +++ b/android/app/src/main/res/layout/today_widget.xml @@ -12,6 +12,12 @@ android:orientation="horizontal" android:paddingHorizontal="@dimen/today_time_margin" /> + + 10dp 12dp 10dp + 1.5dp + + 20.5dp + 36.16dp + 44dp + 51.83dp + 55.75dp + 67.5dp, + 83.16dp + 91dp + 114.5dp + 126.25dp + 130.16dp + 138dp + 161.5dp + 185dp + 232dp + 279dp + 326dp + 373dp + 420dp \ No newline at end of file