Skip to content

Commit

Permalink
add timetable widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Mar 29, 2022
1 parent 48bcbed commit 3deb038
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.ethosa.ktc"
minSdk 21
targetSdk 32
versionCode 8
versionName "0.5.1"
versionCode 9
versionName "0.6.0"
ndk {
abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64'
}
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ethosa.ktc">

<uses-permission android:name="android.permission.INTERNET" />
Expand All @@ -11,19 +12,32 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KTC"
android:usesCleartextTraffic="true">
android:usesCleartextTraffic="true"
tools:targetApi="m">
<receiver
android:name=".ui.widgets.TimetableWidget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/timetable_widget_info" />
</receiver>

<activity
android:name="com.ethosa.ktc.ui.activities.AlbumActivity"
android:name=".ui.activities.AlbumActivity"
android:exported="true"
android:label="@string/title_activity_album"
android:theme="@style/Theme.KTC.NoActionBar" />
<activity
android:name="com.ethosa.ktc.ui.activities.WallPostActivity"
android:name=".ui.activities.WallPostActivity"
android:exported="true"
android:label="@string/title_activity_wall_post"
android:theme="@style/Theme.KTC.NoActionBar" />
<activity
android:name="com.ethosa.ktc.ui.activities.MainActivity"
android:name=".ui.activities.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TimetableFragment : IOFragmentBackPressed() {
binding.timetable.addItemDecoration(itemDecoration)

// Load state
preferences = requireActivity().getPreferences(Context.MODE_PRIVATE)
preferences = requireActivity().getSharedPreferences("com.ethosa.ktc", Context.MODE_PRIVATE)
state = preferences.getInt(STATE, 0)
branch = Branch(preferences.getInt(BRANCH, 0), "")
group = Group(
Expand Down
116 changes: 116 additions & 0 deletions app/src/main/java/com/ethosa/ktc/ui/widgets/TimetableWidget.kt
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)
}
1 change: 1 addition & 0 deletions app/src/main/res/drawable-v21/ic_reload.xml
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>
47 changes: 47 additions & 0 deletions app/src/main/res/layout/timetable_widget.xml
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>
124 changes: 124 additions & 0 deletions app/src/main/res/layout/widget_lesson.xml
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>
Loading

0 comments on commit 3deb038

Please sign in to comment.