Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/widget/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<application>
<receiver
android:name="dev.tokenteam.iwut.widget.ScheduleWidget"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class ScheduleWidget : AppWidgetProvider() {
for (id in appWidgetIds) {
updateWidget(context, appWidgetManager, id)
}
scheduleNextAlarm(context)
if (appWidgetIds.isNotEmpty()) {
scheduleNextAlarm(context)
}
}

override fun onReceive(context: Context, intent: Intent) {
Expand All @@ -37,6 +39,10 @@ class ScheduleWidget : AppWidgetProvider() {
companion object {
const val ACTION_AUTO_REFRESH = "dev.tokenteam.iwut.widget.AUTO_REFRESH"

// Do not use `0` for request code
const val BROADCAST_REQUEST_CODE = 1234
const val ACTIVITY_REQUEST_CODE = 1235

fun updateWidget(
context: Context,
appWidgetManager: AppWidgetManager,
Expand All @@ -48,6 +54,7 @@ class ScheduleWidget : AppWidgetProvider() {
if (data == null || data.termStart.isEmpty()) {
views.setViewVisibility(R.id.course_group, View.GONE)
views.setViewVisibility(R.id.all_done_group, View.VISIBLE)
setOnClickAction(context, views)
appWidgetManager.updateAppWidget(appWidgetId, views)
return
}
Expand Down Expand Up @@ -76,7 +83,8 @@ class ScheduleWidget : AppWidgetProvider() {
ScheduleData.parseTimeToMinutes(it.endTime) > nowMin
}

val combined = (upcomingToday.map { it to true } + tomorrowCourses.map { it to false }).take(2)
val combined =
(upcomingToday.map { it to true } + tomorrowCourses.map { it to false }).take(2)

if (combined.isEmpty()) {
views.setViewVisibility(R.id.course_group, View.GONE)
Expand Down Expand Up @@ -112,16 +120,34 @@ class ScheduleWidget : AppWidgetProvider() {
if (upcomingToday.isEmpty() && tomorrowCourses.isEmpty()) {
hintText = "今天和明天都没有课啦~"
} else {
val todayHint = if (upcomingToday.isEmpty()) "今天没有课啦," else "今天还有${upcomingToday.size}节课,"
val tomorrowHint = if (tomorrowCourses.isEmpty()) "明天没有课啦~" else "明天还有${tomorrowCourses.size}节课"
val todayHint =
if (upcomingToday.isEmpty()) "今天没有课啦," else "今天还有${upcomingToday.size}节课,"
val tomorrowHint =
if (tomorrowCourses.isEmpty()) "明天没有课啦~" else "明天还有${tomorrowCourses.size}节课"
hintText = todayHint + tomorrowHint
}
views.setViewVisibility(R.id.tv_course_hint, View.VISIBLE)
views.setTextViewText(R.id.tv_course_hint, hintText)

setOnClickAction(context, views)
appWidgetManager.updateAppWidget(appWidgetId, views)
}

fun setOnClickAction(context: Context, views: RemoteViews) {
context.packageManager
.getLaunchIntentForPackage(context.packageName)
?.let {
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK
views.setOnClickPendingIntent(
R.id.widget_root,
PendingIntent.getActivity(
context, ACTIVITY_REQUEST_CODE, it,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
)
)
}
}

fun scheduleNextAlarm(context: Context) {
val data = ScheduleData.load(context) ?: return
if (data.termStart.isEmpty()) return
Expand All @@ -148,12 +174,12 @@ class ScheduleWidget : AppWidgetProvider() {
action = ACTION_AUTO_REFRESH
}
val pendingIntent = PendingIntent.getBroadcast(
context, 0, intent,
context, BROADCAST_REQUEST_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setExactAndAllowWhileIdle(
alarmManager.set(
AlarmManager.RTC_WAKEUP,
alarmTime.timeInMillis,
pendingIntent
Expand Down
Loading