2019-05-22 20:33:07 +02:00
|
|
|
package com.denizd.substitutionplan
|
|
|
|
|
|
|
|
|
|
import android.app.*
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.graphics.Color
|
|
|
|
|
import android.media.RingtoneManager
|
|
|
|
|
import android.os.AsyncTask
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import android.preference.PreferenceManager
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.widget.RemoteViews
|
|
|
|
|
import androidx.core.app.NotificationCompat
|
2019-05-23 16:26:12 +02:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2019-05-22 20:33:07 +02:00
|
|
|
import org.jsoup.Jsoup
|
|
|
|
|
import org.jsoup.select.Elements
|
|
|
|
|
import java.lang.IndexOutOfBoundsException
|
2019-05-23 16:26:12 +02:00
|
|
|
import kotlin.collections.ArrayList
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, context: Context, application: Application, parentview: View?) : AsyncTask<Void, Void, Void>() {
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 18:08:04 +02:00
|
|
|
private var jobservice = isjobservice
|
|
|
|
|
private var plan = isplan
|
|
|
|
|
private var menu = ismenu
|
2019-05-23 14:28:41 +02:00
|
|
|
|
2019-07-31 19:48:21 +02:00
|
|
|
/* booleans to improve speed and decrease network usage when fetching data by only grabbing the required HTML pages
|
2019-06-02 17:57:57 +02:00
|
|
|
"plan" = true is required to fetch the substitution data
|
|
|
|
|
"menu" = true is required to fetch the food menu
|
|
|
|
|
"jobservice" = true is required to send a notification
|
2019-05-23 14:28:41 +02:00
|
|
|
"jobservice" = true has no effect unless "plan" is also true
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-23 18:08:04 +02:00
|
|
|
private var mContext = context
|
|
|
|
|
private var mApplication = application
|
|
|
|
|
private var mView = parentview
|
|
|
|
|
private var priority = 200
|
|
|
|
|
private var notifText = ""
|
|
|
|
|
private var informational = ""
|
2019-07-26 21:43:52 +02:00
|
|
|
private val prefs = PreferenceManager.getDefaultSharedPreferences(mContext)
|
|
|
|
|
private val edit = prefs.edit()
|
2019-07-31 19:48:21 +02:00
|
|
|
private var currentTime = ""
|
|
|
|
|
private var currentFoodTime = ""
|
2019-08-01 15:36:21 +02:00
|
|
|
private val substUrl = "https://djd4rkn355.github.io/subst.html"
|
|
|
|
|
private val foodUrl = "https://djd4rkn355.github.io/food.html"
|
2019-05-22 20:33:07 +02:00
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void?): Void? {
|
|
|
|
|
try {
|
2019-05-23 16:26:12 +02:00
|
|
|
if (menu) {
|
2019-07-26 21:43:52 +02:00
|
|
|
val foodViewModel = FoodViewModel(mApplication)
|
2019-07-30 22:48:13 +02:00
|
|
|
val docFood = Jsoup.connect(foodUrl).get()
|
2019-07-26 21:43:52 +02:00
|
|
|
val foodElements = docFood.select("th")
|
2019-07-31 11:27:46 +02:00
|
|
|
currentFoodTime = docFood.select("h1")[0].text()
|
|
|
|
|
if (currentFoodTime != prefs.getString("timeFood", "")) {
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-07-31 11:27:46 +02:00
|
|
|
var foodInt = 0
|
|
|
|
|
var priority = 0
|
|
|
|
|
foodViewModel.deleteAll()
|
|
|
|
|
while (foodInt in 0 until foodElements.size) {
|
|
|
|
|
try { // what a mess this is!
|
|
|
|
|
with(foodElements[foodInt].text()) {
|
|
|
|
|
if (contains("Montag") || contains("Dienstag") || contains("Mittwoch") ||
|
|
|
|
|
contains("Donnerstag") || contains("Freitag")) {
|
|
|
|
|
val three = foodElements[foodInt + 3].text()
|
|
|
|
|
val two = foodElements[foodInt + 2].text()
|
|
|
|
|
if (three.contains("Montag") || three.contains("Dienstag") || three.contains("Mittwoch") ||
|
|
|
|
|
three.contains("Donnerstag") || three.contains("Freitag") || three.contains("von")) {
|
|
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 2].text(), priority))
|
|
|
|
|
foodInt += 2
|
|
|
|
|
} else if (two.contains("Montag") || two.contains("Dienstag") || two.contains("Mittwoch") ||
|
|
|
|
|
two.contains("Donnerstag") || two.contains("Freitag") || two.contains("von")) {
|
|
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text(), priority))
|
|
|
|
|
foodInt += 1
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text(), priority))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-23 14:28:41 +02:00
|
|
|
|
2019-07-31 11:27:46 +02:00
|
|
|
} catch (e: IndexOutOfBoundsException) {
|
|
|
|
|
try {
|
2019-07-06 14:18:59 +02:00
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
2019-05-22 20:33:07 +02:00
|
|
|
+ foodElements[foodInt + 1].text() + "\n"
|
2019-07-06 14:18:59 +02:00
|
|
|
+ foodElements[foodInt + 2].text(), priority))
|
2019-07-31 11:27:46 +02:00
|
|
|
break
|
|
|
|
|
} catch (e1: IndexOutOfBoundsException) {
|
2019-07-06 14:18:59 +02:00
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text(), priority))
|
2019-07-31 11:27:46 +02:00
|
|
|
break
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-07-31 11:27:46 +02:00
|
|
|
foodInt++
|
|
|
|
|
priority++
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-07-31 11:27:46 +02:00
|
|
|
edit.putString("timeFood", currentFoodTime).apply()
|
2019-05-26 14:59:00 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
if (plan) {
|
2019-07-26 21:43:52 +02:00
|
|
|
val substViewModel = SubstViewModel(mApplication)
|
2019-07-30 22:48:13 +02:00
|
|
|
val doc = Jsoup.connect(substUrl).get()
|
2019-07-30 16:58:51 +02:00
|
|
|
currentTime = doc.select("h1")[0].text()
|
2019-07-30 11:18:01 +02:00
|
|
|
if (currentTime != prefs.getString("time", "")) {
|
|
|
|
|
val rows = doc.select("tr")
|
|
|
|
|
val paragraphs = doc.select("p")
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
val groupS = ArrayList<String>()
|
|
|
|
|
val dateS = ArrayList<String>()
|
|
|
|
|
val timeS = ArrayList<String>()
|
|
|
|
|
val courseS = ArrayList<String>()
|
|
|
|
|
val roomS = ArrayList<String>()
|
|
|
|
|
val additionalS = ArrayList<String>()
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
for (i in 0 until paragraphs.size) {
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
informational = paragraphs[i].text()
|
|
|
|
|
} else {
|
|
|
|
|
informational += "\n\n" + paragraphs[i].text()
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-30 11:18:01 +02:00
|
|
|
edit.putString("informational", informational).apply()
|
2019-05-23 16:26:12 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
substViewModel.deleteAllSubst()
|
2019-05-23 16:26:12 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
for (i in 0 until rows.size) {
|
|
|
|
|
val row = rows[i]
|
|
|
|
|
val cols = row.select("th") as Elements
|
|
|
|
|
|
|
|
|
|
groupS.add(cols[0].text())
|
|
|
|
|
dateS.add(cols[1].text())
|
|
|
|
|
timeS.add(cols[2].text())
|
|
|
|
|
courseS.add(cols[3].text())
|
|
|
|
|
roomS.add(cols[4].text())
|
|
|
|
|
additionalS.add(cols[5].text())
|
|
|
|
|
|
|
|
|
|
val drawable = MiscData.getIcon(courseS[i])
|
|
|
|
|
val subst = Subst(drawable, groupS[i], dateS[i], timeS[i], courseS[i],
|
|
|
|
|
roomS[i], additionalS[i], priority)
|
|
|
|
|
priority--
|
|
|
|
|
substViewModel.insertSubst(subst)
|
|
|
|
|
|
|
|
|
|
if (jobservice && prefs.getBoolean("notif", true)) {
|
|
|
|
|
if ((prefs.getString("courses", "")
|
|
|
|
|
?: "").isEmpty() && (prefs.getString("classes", "")
|
|
|
|
|
?: "").isNotEmpty()) {
|
|
|
|
|
if (groupS[i].isNotEmpty() && groupS[i] != "") {
|
|
|
|
|
if ((prefs.getString("classes", "")
|
|
|
|
|
?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "")
|
|
|
|
|
?: "").toString())) {
|
2019-05-22 20:33:07 +02:00
|
|
|
if (notifText.isNotEmpty()) {
|
|
|
|
|
notifText += ", "
|
|
|
|
|
}
|
|
|
|
|
notifText += courseS[i] + ": " + additionalS[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-30 11:18:01 +02:00
|
|
|
} else if ((prefs.getString("classes", "")
|
|
|
|
|
?: "").isNotEmpty() && (prefs.getString("courses", "")
|
|
|
|
|
?: "").isNotEmpty()) {
|
|
|
|
|
if (groupS[i] != "" && courseS[i] != "") {
|
|
|
|
|
if ((prefs.getString("courses", "") ?: "").contains(courseS[i])) {
|
|
|
|
|
if ((prefs.getString("classes", "")
|
|
|
|
|
?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "")
|
|
|
|
|
?: "").toString())) {
|
|
|
|
|
if (notifText.isNotEmpty()) {
|
|
|
|
|
notifText += ", "
|
|
|
|
|
}
|
|
|
|
|
notifText += courseS[i] + ": " + additionalS[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-30 11:18:01 +02:00
|
|
|
edit.putString("time", currentTime).apply()
|
2019-05-23 14:28:41 +02:00
|
|
|
}
|
2019-07-30 11:18:01 +02:00
|
|
|
if (jobservice && prefs.getBoolean("notif", true)) {
|
|
|
|
|
val openApp = Intent(mContext, Main::class.java)
|
|
|
|
|
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
|
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
|
|
|
|
|
val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0)
|
2019-05-23 16:26:12 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification)
|
2019-07-31 19:48:21 +02:00
|
|
|
notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.substitutionPlan))
|
2019-07-30 11:18:01 +02:00
|
|
|
notificationLayout.setTextViewText(R.id.notification_textview, notifText)
|
2019-05-23 16:26:12 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
if (notifText.isNotEmpty()) {
|
|
|
|
|
val manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
val channelId = "general"
|
|
|
|
|
val channelName = mContext.getString(R.string.general)
|
2019-06-04 09:27:54 +02:00
|
|
|
|
2019-07-30 11:18:01 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
|
channel.enableLights(true)
|
|
|
|
|
channel.lightColor = Color.BLUE
|
|
|
|
|
manager.createNotificationChannel(channel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val notification = NotificationCompat.Builder(mContext, channelId)
|
|
|
|
|
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
|
|
|
|
|
.setCustomContentView(notificationLayout)
|
|
|
|
|
.setSmallIcon(R.drawable.ic_avh)
|
|
|
|
|
.setContentIntent(openAppPending)
|
|
|
|
|
.setAutoCancel(true)
|
|
|
|
|
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
|
|
|
|
|
.build()
|
|
|
|
|
manager.notify(1, notification)
|
2019-06-04 09:27:54 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-06 14:18:59 +02:00
|
|
|
}
|
2019-08-01 15:36:21 +02:00
|
|
|
|
2019-07-06 14:18:59 +02:00
|
|
|
if (plan || menu) {
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let { v: View ->
|
2019-08-01 15:36:21 +02:00
|
|
|
val snackText = mContext.getText(R.string.lastUpdated).toString() + when {
|
2019-07-31 11:27:46 +02:00
|
|
|
menu -> currentFoodTime
|
|
|
|
|
else -> currentTime
|
2019-07-06 14:18:59 +02:00
|
|
|
}
|
2019-08-01 15:36:21 +02:00
|
|
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
|
|
|
|
Snackbar.make(snackBarView, snackText, Snackbar.LENGTH_LONG).show()
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-06-02 17:57:57 +02:00
|
|
|
} catch (e: Exception) {
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let { v: View ->
|
2019-07-23 21:42:56 +02:00
|
|
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
2019-08-01 15:36:21 +02:00
|
|
|
Snackbar.make(snackBarView, mContext.getString(R.string.noInternet), Snackbar.LENGTH_LONG).show()
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
return null
|
|
|
|
|
}
|
2019-08-01 15:36:21 +02:00
|
|
|
|
|
|
|
|
override fun onPostExecute(result: Void?) {
|
|
|
|
|
mView?.let {
|
|
|
|
|
try {
|
|
|
|
|
it.findViewById<SwipeRefreshLayout>(R.id.pullToRefresh).isRefreshing = false
|
|
|
|
|
} catch (ignored: Exception) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|