Parcourir la source

add timetable widget

Ethosa il y a 3 ans
Parent
commit
3deb038c87

+ 6 - 1
.idea/misc.xml

@@ -3,6 +3,9 @@
   <component name="DesignSurface">
     <option name="filePathToZoomLevelMap">
       <map>
+        <entry key="..\:/android projects/KTC/app/src/main/res/drawable-v21/app_widget_background.xml" value="0.1175" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/drawable-v21/app_widget_inner_view_background.xml" value="0.1175" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/drawable-v21/rotate-right_24.xml" value="0.1175" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable-v24/ic_launcher_foreground.xml" value="0.194" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/background.xml" value="0.1185" />
         <entry key="..\:/android projects/KTC/app/src/main/res/drawable/background1.xml" value="0.1185" />
@@ -57,12 +60,14 @@
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_lesson.xml" value="0.5" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_news.xml" value="0.5" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_settings_button.xml" value="0.5" />
-        <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_timetable.xml" value="0.67" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_timetable.xml" value="0.6307053941908713" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/layout_wall.xml" value="0.5" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/lesson.xml" value="0.75" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/scroll_content.xml" value="0.20471014492753623" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/timetable.xml" value="0.5314109165808445" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/layout/timetable_widget.xml" value="0.33" />
         <entry key="..\:/android projects/KTC/app/src/main/res/layout/wall.xml" value="0.67" />
+        <entry key="..\:/android projects/KTC/app/src/main/res/layout/widget_lesson.xml" value="0.43803680981595094" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/bottom_nav_menu.xml" value="0.371875" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/menu_album.xml" value="0.371875" />
         <entry key="..\:/android projects/KTC/app/src/main/res/menu/menu_wall_post.xml" value="0.371875" />

+ 6 - 0
.idea/other.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ScreenshotViewer">
+    <option name="frameScreenshot" value="true" />
+  </component>
+</project>

+ 2 - 2
app/build.gradle

@@ -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'
         }

+ 18 - 4
app/src/main/AndroidManifest.xml

@@ -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" />
@@ -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" />

+ 1 - 1
app/src/main/java/com/ethosa/ktc/ui/fragments/TimetableFragment.kt

@@ -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(

+ 116 - 0
app/src/main/java/com/ethosa/ktc/ui/widgets/TimetableWidget.kt

@@ -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)
+}

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
app/src/main/res/drawable-v21/ic_reload.xml


+ 47 - 0
app/src/main/res/layout/timetable_widget.xml

@@ -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 - 0
app/src/main/res/layout/widget_lesson.xml

@@ -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>

+ 7 - 0
app/src/main/res/values/attrs.xml

@@ -0,0 +1,7 @@
+<resources>
+    <declare-styleable name="AppWidgetAttrs">
+        <attr name="appWidgetPadding" format="dimension" />
+        <attr name="appWidgetInnerRadius" format="dimension" />
+        <attr name="appWidgetRadius" format="dimension" />
+    </declare-styleable>
+</resources>

+ 4 - 0
app/src/main/res/values/colors.xml

@@ -15,4 +15,8 @@
     <color name="btn_text">#DADCE3</color>
     <color name="inactive">#232427</color>
     <color name="active">#0026AE</color>
+    <color name="light_blue_50">#FFE1F5FE</color>
+    <color name="light_blue_200">#FF81D4FA</color>
+    <color name="light_blue_600">#FF039BE5</color>
+    <color name="light_blue_900">#FF01579B</color>
 </resources>

+ 6 - 0
app/src/main/res/values/dimens.xml

@@ -6,4 +6,10 @@
     <dimen name="fab_margin">16dp</dimen>
     <dimen name="text_margin">16dp</dimen>
     <dimen name="default_img_width">512dp</dimen>
+
+    <!--
+Refer to App Widget Documentation for margin information
+http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
+    -->
+    <dimen name="widget_margin">0dp</dimen>
 </resources>

+ 3 - 0
app/src/main/res/values/strings.xml

@@ -4,6 +4,9 @@
     <string name="author">Ethosa</string>
     <string name="github">Github</string>
 
+    <string name="add_widget">Расписание</string>
+    <string name="app_widget_description">Отображается расписание на сегодня</string>
+
     <string name="title_news">Новости</string>
     <string name="title_timetable">Расписание</string>
     <string name="title_gallery">Галерея</string>

+ 10 - 0
app/src/main/res/values/styles.xml

@@ -14,4 +14,14 @@
         <item name="android:windowBackground">@color/background</item>
         <item name="actionMenuTextColor">?attr/colorAccent</item>
     </style>
+
+    <style name="Widget.KTC.AppWidget.Container" parent="android:Widget">
+        <item name="android:id">@android:id/background</item>
+        <item name="android:background">@color/background</item>
+    </style>
+
+    <style name="Widget.KTC.AppWidget.InnerView" parent="android:Widget">
+        <item name="android:background">@color/background</item>
+        <item name="android:textColor">@color/primary</item>
+    </style>
 </resources>

+ 15 - 0
app/src/main/res/values/themes.xml

@@ -30,4 +30,19 @@
     </style>
 
     <style name="Theme.KTC.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
+
+    <style name="Theme.KTC.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault">
+        <!-- Radius of the outer bound of widgets to make the rounded corners -->
+        <item name="appWidgetRadius">16dp</item>
+        <!--
+        Radius of the inner view's bound of widgets to make the rounded corners.
+        It needs to be 8dp or less than the value of appWidgetRadius
+        -->
+        <item name="appWidgetInnerRadius">8dp</item>
+    </style>
+
+    <style name="Theme.KTC.AppWidgetContainer" parent="Theme.KTC.AppWidgetContainerParent">
+        <!-- Apply padding to avoid the content of the widget colliding with the rounded corners -->
+        <item name="appWidgetPadding">16dp</item>
+    </style>
 </resources>

+ 16 - 0
app/src/main/res/xml/timetable_widget_info.xml

@@ -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_description"
+    android:initialKeyguardLayout="@layout/timetable_widget"
+    android:initialLayout="@layout/timetable_widget"
+    android:minWidth="256dp"
+    android:minHeight="80dp"
+    android:previewImage="@drawable/ic_calendar"
+    android:previewLayout="@layout/timetable_widget"
+    android:resizeMode="horizontal|vertical"
+    android:targetCellWidth="6"
+    android:targetCellHeight="2"
+    android:updatePeriodMillis="1800000"
+    android:widgetCategory="home_screen"
+    tools:targetApi="s" />

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff