-
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
16 changed files
with
382 additions
and
8 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.
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
116 changes: 116 additions & 0 deletions
116
app/src/main/java/com/ethosa/ktc/ui/widgets/TimetableWidget.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,116 @@ | ||
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.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.google.gson.Gson | ||
import okhttp3.Call | ||
import okhttp3.Response | ||
import java.util.* | ||
|
||
|
||
val college = CollegeApi() | ||
var preferences: SharedPreferences? = null | ||
|
||
|
||
/** | ||
* Implementation of App Widget functionality. | ||
*/ | ||
class TimetableWidget : AppWidgetProvider() { | ||
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") | ||
internal 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, | ||
PendingIntent.FLAG_UPDATE_CURRENT) | ||
} | ||
|
||
|
||
@SuppressLint("RemoteViewLayout") | ||
internal 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) | ||
) | ||
// Load last group ID | ||
val groupId = preferences?.getInt("group", 0) | ||
val calendar = Calendar.getInstance() | ||
val weekday = calendar.get(Calendar.DAY_OF_WEEK) | ||
println(weekday) | ||
|
||
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 = | ||
if (weekday > 1) | ||
timetable.days[weekday-2] | ||
else | ||
timetable.days[0] | ||
|
||
views.setTextViewText(R.id.timetable_widget_title, day.title) | ||
views.removeAllViews(R.id.timetable_widget_lessons) | ||
|
||
// Setup view | ||
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) | ||
} |
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 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorControlNormal"><path android:fillColor="@android:color/white" android:pathData="M21.962,12.875A10.03,10.03,0,1,1,19.122,5H16a1,1,0,0,0-1,1h0a1,1,0,0,0,1,1h4.143A1.858,1.858,0,0,0,22,5.143V1a1,1,0,0,0-1-1h0a1,1,0,0,0-1,1V3.078A11.985,11.985,0,1,0,23.95,13.1a1.007,1.007,0,0,0-1-1.1h0A0.982 0.982,0,0,0,21.962,12.875Z"/></vector> |
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,47 @@ | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/timetable_widget_background" | ||
style="@style/Widget.KTC.AppWidget.Container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@drawable/shape_background_primary" | ||
android:gravity="center_horizontal" | ||
android:orientation="vertical" | ||
android:padding="8dp" | ||
android:theme="@style/Theme.KTC.AppWidgetContainer"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="horizontal"> | ||
|
||
<TextView | ||
android:id="@+id/timetable_widget_title" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="10" | ||
android:text="@string/add_widget" | ||
android:textAlignment="center" | ||
android:textColor="@color/text" | ||
android:textSize="20sp" | ||
android:textStyle="bold" /> | ||
|
||
<ImageView | ||
android:id="@+id/timetable_widget_reload" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_weight="2" | ||
android:src="@drawable/ic_reload" | ||
app:srcCompat="@drawable/ic_reload" | ||
app:tint="@color/primary" | ||
tools:ignore="ContentDescription,ImageContrastCheck" /> | ||
</LinearLayout> | ||
|
||
<LinearLayout | ||
android:id="@+id/timetable_widget_lessons" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"></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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
android:paddingTop="4dp" | ||
android:paddingBottom="4dp" | ||
tools:layout_editor_absoluteX="8dp" | ||
tools:layout_editor_absoluteY="9dp"> | ||
|
||
<LinearLayout | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center_vertical" | ||
android:orientation="horizontal" | ||
tools:ignore="UselessParent"> | ||
|
||
<TextView | ||
android:id="@+id/lesson_number" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:text="@string/lesson_number" | ||
android:textColor="@color/text" | ||
android:textStyle="bold" | ||
app:layout_constraintBottom_toBottomOf="@+id/lesson_teacher" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="@+id/lesson_title" /> | ||
|
||
<LinearLayout | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/lesson_from" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/lesson_time" | ||
android:textColor="@color/text" | ||
android:textSize="12sp" | ||
app:layout_constraintBottom_toTopOf="@+id/hr_time" | ||
app:layout_constraintEnd_toEndOf="@+id/hr_time" | ||
app:layout_constraintStart_toStartOf="@+id/hr_time" /> | ||
|
||
<LinearLayout | ||
android:id="@+id/hr_time" | ||
android:layout_width="40dp" | ||
android:layout_height="1dp" | ||
android:background="@color/text" | ||
android:orientation="horizontal" | ||
android:textColor="@color/text"> | ||
|
||
</LinearLayout> | ||
|
||
<TextView | ||
android:id="@+id/lesson_to" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/lesson_time" | ||
android:textColor="@color/text" | ||
android:textSize="12sp" | ||
app:layout_constraintEnd_toEndOf="@+id/hr_time" | ||
app:layout_constraintStart_toStartOf="@+id/hr_time" | ||
app:layout_constraintTop_toBottomOf="@+id/lesson_from" /> | ||
|
||
</LinearLayout> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:orientation="vertical"> | ||
|
||
<TextView | ||
android:id="@+id/lesson_title" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:gravity="center" | ||
android:lineSpacingExtra="-4sp" | ||
android:padding="0dp" | ||
android:text="@string/lesson_title" | ||
android:textColor="@color/text" | ||
android:textSize="14sp" | ||
app:layout_constraintTop_toTopOf="parent" | ||
tools:textStyle="bold" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:orientation="horizontal"> | ||
|
||
<TextView | ||
android:id="@+id/lesson_teacher" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:padding="0dp" | ||
android:text="@string/lesson_teacher" | ||
android:textColor="@color/text" /> | ||
|
||
<TextView | ||
android:id="@+id/lesson_classroom" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="8dp" | ||
android:text="@string/lesson_classroom" | ||
android:textColor="@color/text" | ||
app:layout_constraintBottom_toBottomOf="@+id/lesson_from" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="@+id/lesson_to" /> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> |
Oops, something went wrong.