123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package com.ethosa.ktc.ui.widgets
- import android.annotation.SuppressLint
- import android.app.PendingIntent
- import android.appwidget.AppWidgetManager
- import android.appwidget.AppWidgetProvider
- import android.content.ComponentName
- import android.content.Context
- import android.content.Intent
- import android.content.SharedPreferences
- import android.os.Build
- import android.widget.RemoteViews
- import com.ethosa.ktc.R
- import com.ethosa.ktc.college.CollegeApi
- import com.ethosa.ktc.college.CollegeCallback
- import com.ethosa.ktc.college.timetable.Week
- import com.ethosa.ktc.ui.activities.MainActivity
- import com.google.gson.Gson
- import okhttp3.Call
- import okhttp3.Response
- import java.util.*
- /**
- * Implementation of App Widget functionality.
- */
- class TimetableWidget : AppWidgetProvider() {
- private val college = CollegeApi()
- private var preferences: SharedPreferences? = null
- override fun onUpdate(
- context: Context,
- appWidgetManager: AppWidgetManager,
- appWidgetIds: IntArray
- ) {
- preferences = context.getSharedPreferences("com.ethosa.ktc", Context.MODE_PRIVATE)
- // There may be multiple widgets active, so update all of them
- for (appWidgetId in appWidgetIds) {
- updateAppWidget(context, appWidgetManager, appWidgetId)
- }
- }
- /**
- * Update timetable for current widget
- */
- @SuppressLint("UnspecifiedImmutableFlag")
- private fun updateWidgetPendingIntent(
- context: Context?,
- appWidgetId: Int
- ): PendingIntent {
- val intent = Intent(context, TimetableWidget::class.java)
- val ids = AppWidgetManager.getInstance(context)
- .getAppWidgetIds(ComponentName(context!!, TimetableWidget::class.java))
- intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
- intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
- return PendingIntent.getBroadcast(
- context,
- appWidgetId,
- intent,
- when {
- Build.VERSION.SDK_INT >= 31 -> PendingIntent.FLAG_MUTABLE
- else -> PendingIntent.FLAG_UPDATE_CURRENT
- })
- }
- /**
- * Open app with pending intent
- */
- @SuppressLint("UnspecifiedImmutableFlag")
- private fun openAppPendingIntent(
- context: Context?,
- appWidgetId: Int
- ): PendingIntent {
- val intent = Intent(context, MainActivity::class.java)
- return PendingIntent.getActivity(
- context,
- appWidgetId,
- intent,
- when {
- Build.VERSION.SDK_INT >= 31 -> PendingIntent.FLAG_MUTABLE
- else -> PendingIntent.FLAG_UPDATE_CURRENT
- })
- }
- @SuppressLint("RemoteViewLayout")
- private fun updateAppWidget(
- context: Context,
- appWidgetManager: AppWidgetManager,
- appWidgetId: Int
- ) {
- // Construct the RemoteViews object
- val views = RemoteViews(context.packageName, R.layout.timetable_widget)
- views.setOnClickPendingIntent(
- R.id.timetable_widget_reload,
- updateWidgetPendingIntent(context, appWidgetId)
- )
- views.setOnClickPendingIntent(
- R.id.timetable_widget_background,
- openAppPendingIntent(context, appWidgetId)
- )
- // Load last group ID
- val groupId = preferences?.getInt("group", 0)
- val calendar = Calendar.getInstance()
- val weekday = calendar.get(Calendar.DAY_OF_WEEK)
- college.fetchTimetable(groupId!!, object : CollegeCallback {
- @SuppressLint("SetTextI18n")
- override fun onResponse(call: Call, response: Response) {
- // Parse JSON
- val json = response.body?.string()
- val timetable = Gson().fromJson(json, Week::class.java)
- // Get current day timetable
- val day =
- when {
- weekday > 1 -> timetable.days[weekday-2]
- weekday > 0 -> timetable.days[1]
- else -> timetable.days[0]
- }
- // Setup widget
- views.setTextViewText(R.id.timetable_widget_title, day.title)
- views.removeAllViews(R.id.timetable_widget_lessons)
- // Setup views
- for (l in day.lessons) {
- println(l)
- // Load lesson data
- val lesson = RemoteViews(context.packageName, R.layout.widget_lesson)
- lesson.setTextViewText(R.id.lesson_title, l.title)
- lesson.setTextViewText(R.id.lesson_classroom, l.classroom)
- lesson.setTextViewText(R.id.lesson_number, l.time[0])
- lesson.setTextViewText(R.id.lesson_from, l.time[1])
- lesson.setTextViewText(R.id.lesson_to, l.time[2])
- lesson.setTextViewText(R.id.lesson_teacher, l.teacher)
- views.addView(R.id.timetable_widget_lessons, lesson)
- }
- // Update widget
- appWidgetManager.updateAppWidget(appWidgetId, views)
- }
- }, null)
- appWidgetManager.updateAppWidget(appWidgetId, views)
- }
- }
|