-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
260 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
app/src/main/java/com/ethosa/ktc/ui/widgets/TeacherTimetableWidget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
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.os.Build | ||
import android.widget.RemoteViews | ||
import com.ethosa.ktc.Preferences | ||
import com.ethosa.ktc.R | ||
import com.ethosa.ktc.college.CollegeApi | ||
import com.ethosa.ktc.college.CollegeCallback | ||
import com.ethosa.ktc.college.teacher.TeacherTimetable | ||
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 TeacherTimetableWidget : AppWidgetProvider() { | ||
private val college = CollegeApi() | ||
private var preferences: Preferences? = null | ||
|
||
override fun onUpdate( | ||
context: Context, | ||
appWidgetManager: AppWidgetManager, | ||
appWidgetIds: IntArray | ||
) { | ||
preferences = Preferences(context) | ||
// 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.widget_timetable) | ||
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 teacherId = Preferences.teacherId | ||
val branchId = Preferences.branch!!.id | ||
val calendar = Calendar.getInstance() | ||
val weekday = calendar.get(Calendar.DAY_OF_WEEK) | ||
|
||
college.fetchTeacherTimetable(branchId, teacherId, object : CollegeCallback { | ||
@SuppressLint("SetTextI18n") | ||
override fun onResponse(call: Call, response: Response) { | ||
// Parse JSON | ||
val json = response.body?.string() | ||
val timetable = Gson().fromJson(json, TeacherTimetable::class.java) | ||
println(weekday) | ||
// Get current day timetable | ||
val day = | ||
when { | ||
weekday >= 2 -> timetable.week[weekday-2] | ||
weekday > 1 -> timetable.week[1] | ||
else -> timetable.week[0] | ||
} | ||
|
||
// Setup widget | ||
views.setTextViewText(R.id.timetable_widget_title, "${timetable.teacher} - ${day.title}") | ||
views.removeAllViews(R.id.timetable_widget_lessons) | ||
|
||
// Setup views | ||
for (l in day.lessons) { | ||
println(l) | ||
// Load lesson data | ||
if (l.group == "") continue | ||
val lesson = RemoteViews(context.packageName, R.layout.widget_tlesson) | ||
lesson.setTextViewText(R.id.widget_tlesson_title, l.title) | ||
lesson.setTextViewText(R.id.widget_tlesson_classroom, l.classroom) | ||
lesson.setTextViewText(R.id.widget_tlesson_group, l.group) | ||
lesson.setTextViewText(R.id.widget_tlesson_number, l.number) | ||
views.addView(R.id.timetable_widget_lessons, lesson) | ||
} | ||
// Update widget | ||
appWidgetManager.updateAppWidget(appWidgetId, views) | ||
} | ||
}) | ||
appWidgetManager.updateAppWidget(appWidgetId, views) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:paddingTop="4dp" | ||
android:paddingBottom="4dp"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:orientation="horizontal" | ||
tools:ignore="UselessParent"> | ||
|
||
<TextView | ||
android:id="@+id/widget_tlesson_number" | ||
android:layout_width="wrap_content" | ||
android:layout_height="match_parent" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginEnd="4dp" | ||
android:gravity="center" | ||
android:text="@string/lesson_number" | ||
android:textStyle="bold" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/widget_tlesson_title" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:lineSpacingExtra="-4sp" | ||
android:text="@string/lesson_title" | ||
android:textStyle="bold" /> | ||
|
||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="match_parent" | ||
android:gravity="center" | ||
android:orientation="horizontal"> | ||
|
||
<TextView | ||
android:id="@+id/widget_tlesson_group" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="4dp" | ||
android:text="@string/lesson_teacher" /> | ||
|
||
<TextView | ||
android:id="@+id/widget_tlesson_classroom" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="4dp" | ||
android:layout_weight="1" | ||
android:text="@string/lesson_classroom" /> | ||
</LinearLayout> | ||
|
||
</LinearLayout> | ||
</LinearLayout> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:description="@string/app_widget_teacher_description" | ||
android:initialKeyguardLayout="@layout/widget_timetable" | ||
android:initialLayout="@layout/widget_timetable" | ||
android:minWidth="300dp" | ||
android:minHeight="140dp" | ||
android:previewImage="@drawable/ic_graduation_cap" | ||
android:previewLayout="@layout/widget_timetable" | ||
android:resizeMode="horizontal|vertical" | ||
android:targetCellWidth="4" | ||
android:targetCellHeight="3" | ||
android:updatePeriodMillis="1800000" | ||
android:widgetCategory="home_screen" | ||
tools:targetApi="s" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.