diff --git a/app/build.gradle b/app/build.gradle index 409f8d7..be179a0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ android { targetSdk target_sdk versionCode 1 - versionName '0.1.0' + versionName '0.1.0-alpha.01' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 481bb43..e391cf1 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -18,4 +18,10 @@ # If you keep the line number information, uncomment this to # hide the original source file name. -#-renamesourcefileattribute SourceFile \ No newline at end of file +#-renamesourcefileattribute SourceFile + +# Keep default constuctors of wiget classes +-keepnames class com.zjutjh.ijh.widget.* extends androidx.glance.appwidget.GlanceAppWidget +-keepclassmembers class com.zjutjh.ijh.widget.* extends androidx.glance.appwidget.GlanceAppWidget { + public (); +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/zjutjh/ijh/MainActivity.kt b/app/src/main/kotlin/com/zjutjh/ijh/MainActivity.kt index 1ccbd6a..6a9b048 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/MainActivity.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/MainActivity.kt @@ -12,9 +12,12 @@ import androidx.core.view.WindowCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleOwner +import androidx.work.WorkManager import com.zjutjh.ijh.ui.IJhApp import com.zjutjh.ijh.ui.theme.IJhTheme import com.zjutjh.ijh.util.ViewModelStoreMappingOwner +import com.zjutjh.ijh.widget.ScheduleWidget +import com.zjutjh.ijh.work.enqueueWidgetRefresh import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -60,6 +63,9 @@ class MainActivity : ComponentActivity() { // Make system bars inside the application's layout scope WindowCompat.setDecorFitsSystemWindows(window, false) + // Update widget + WorkManager.getInstance(this).enqueueWidgetRefresh() + setContent { IJhTheme { IJhApp( diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt index 68e61cb..a418f84 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt @@ -178,8 +178,6 @@ class ScheduleWidget : GlanceAppWidget() { } } } - } else { - GText(context.getString(R.string.loading)) } } } diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt index 773c694..09b4133 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt @@ -10,6 +10,7 @@ import androidx.work.WorkManager import com.zjutjh.ijh.data.repository.CourseRepository import com.zjutjh.ijh.data.repository.WeJhInfoRepository import com.zjutjh.ijh.work.ScheduleWidgetUpdater +import com.zjutjh.ijh.work.enqueueWidgetRefresh import dagger.hilt.EntryPoint import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent @@ -31,7 +32,6 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { override fun onEnabled(context: Context) { super.onEnabled(context) - // Enter relevant functionality for when the first widget is created val manager = WorkManager.getInstance(context) val request = PeriodicWorkRequestBuilder( @@ -46,6 +46,8 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { request ) + manager.enqueueWidgetRefresh() + Log.i("ScheduleWidget", "Updater enqueued.") } diff --git a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt index a4dc95a..0f6ef55 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt @@ -12,6 +12,11 @@ import com.zjutjh.ijh.widget.ScheduleWidget import com.zjutjh.ijh.widget.ScheduleWidgetReceiver import dagger.hilt.android.EntryPointAccessors +/** + * Worker that syncs data and updates widgets. + * + * **Note** On some devices, periodic update will work only when auto launch is enabled. + */ class ScheduleWidgetUpdater( private val context: Context, workerParameters: WorkerParameters diff --git a/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt b/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt index a2011ae..8b6cdcb 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/work/WidgetRefreshWorker.kt @@ -7,11 +7,14 @@ import androidx.glance.appwidget.updateAll import androidx.work.Constraints import androidx.work.CoroutineWorker import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters import androidx.work.workDataOf import java.time.Duration +import java.util.concurrent.TimeUnit /** * This worker is used to refresh the widget periodically. @@ -27,33 +30,6 @@ class WidgetRefreshWorker( inline fun getUniqueWorkName() = WidgetRefreshWorker::class.java.simpleName + "_" + T::class.java.simpleName - /** - * Enqueue a [WidgetRefreshWorker] for the given [GlanceAppWidget]. - * Do nothing if the worker is already enqueued. - * Default refresh interval is 15 minutes. - */ - inline fun enqueue(context: Context) { - val manager = WorkManager.getInstance(context) - val request = PeriodicWorkRequestBuilder( - Duration.ofMinutes(15), Duration.ofMinutes(5) - ).setConstraints( - Constraints(requiresBatteryNotLow = true) - ).setInputData( - workDataOf(INPUT_CLASS_NAME to T::class.java.name) - ) - .build() - - manager.enqueueUniquePeriodicWork( - getUniqueWorkName(), - ExistingPeriodicWorkPolicy.KEEP, - request - ) - } - - inline fun cancel(context: Context) { - val manager = WorkManager.getInstance(context) - manager.cancelUniqueWork(getUniqueWorkName()) - } } /** @@ -78,5 +54,43 @@ class WidgetRefreshWorker( Result.failure() } } +} + +inline fun WorkManager.enqueueWidgetRefresh(force: Boolean = false) { + enqueueUniqueWork( + WidgetRefreshWorker.getUniqueWorkName(), + if (force) ExistingWorkPolicy.REPLACE else ExistingWorkPolicy.KEEP, + OneTimeWorkRequestBuilder() + .setInitialDelay(1, TimeUnit.SECONDS) + .setInputData( + workDataOf(WidgetRefreshWorker.INPUT_CLASS_NAME to T::class.java.name) + ) + .build() + ) +} + +/** + * Enqueue a [WidgetRefreshWorker] for the given [GlanceAppWidget]. + * Do nothing if the worker is already enqueued. + * Default refresh interval is 15 minutes. + */ +inline fun WorkManager.enqueuePeriodicWidgetRefresh() { + val request = PeriodicWorkRequestBuilder( + Duration.ofMinutes(15), Duration.ofMinutes(5) + ).setConstraints( + Constraints(requiresBatteryNotLow = true) + ).setInputData( + workDataOf(WidgetRefreshWorker.INPUT_CLASS_NAME to T::class.java.name) + ) + .build() + + enqueueUniquePeriodicWork( + WidgetRefreshWorker.getUniqueWorkName(), + ExistingPeriodicWorkPolicy.KEEP, + request + ) +} +inline fun WorkManager.cancelPeriodicWidgetRefresh() { + cancelUniqueWork(WidgetRefreshWorker.getUniqueWorkName()) } \ No newline at end of file diff --git a/app/src/main/res/drawable/baseline_autorenew_24.xml b/app/src/main/res/drawable/baseline_autorenew_24.xml index 1f497e9..60e8594 100644 --- a/app/src/main/res/drawable/baseline_autorenew_24.xml +++ b/app/src/main/res/drawable/baseline_autorenew_24.xml @@ -1,7 +1,6 @@ - - - \ No newline at end of file diff --git a/app/src/main/res/layout/widget_schedule_layout.xml b/app/src/main/res/layout/widget_schedule_layout.xml new file mode 100644 index 0000000..09f3b28 --- /dev/null +++ b/app/src/main/res/layout/widget_schedule_layout.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/app_widget_schedule.xml b/app/src/main/res/xml/app_widget_schedule.xml index 51191a0..4db57f2 100644 --- a/app/src/main/res/xml/app_widget_schedule.xml +++ b/app/src/main/res/xml/app_widget_schedule.xml @@ -2,13 +2,13 @@