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
|
2019-05-23 16:26:12 +02:00
|
|
|
import android.os.Handler
|
|
|
|
|
import android.os.Looper
|
2019-05-22 20:33:07 +02:00
|
|
|
import android.preference.PreferenceManager
|
|
|
|
|
import android.view.View
|
2019-05-23 16:26:12 +02:00
|
|
|
import android.view.animation.Animation
|
|
|
|
|
import android.view.animation.AnimationUtils
|
|
|
|
|
import android.widget.ProgressBar
|
2019-05-22 20:33:07 +02:00
|
|
|
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
|
|
|
|
|
import java.net.URL
|
2019-05-23 16:26:12 +02:00
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
import java.util.*
|
|
|
|
|
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-23 21:42:56 +02:00
|
|
|
/* booleans to improve speed when fetching data
|
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-05-23 18:08:04 +02:00
|
|
|
private lateinit var pullToRefresh: SwipeRefreshLayout
|
|
|
|
|
private lateinit var progressBar: ProgressBar
|
|
|
|
|
private val oldDateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
|
|
|
|
|
private val newDateFormat = "yyyy-MM-dd, HH:mm:ss"
|
|
|
|
|
private var sdf = SimpleDateFormat(oldDateFormat)
|
|
|
|
|
private lateinit var d: Date
|
2019-05-26 14:59:00 +02:00
|
|
|
private val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
|
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
2019-05-22 20:33:07 +02:00
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void?): Void? {
|
|
|
|
|
try {
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let { v: View ->
|
|
|
|
|
pullToRefresh = v.findViewById(R.id.pullToRefresh)
|
|
|
|
|
progressBar = v.findViewById(R.id.progressBar)
|
|
|
|
|
progressBar.progress = 0
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
if (menu) {
|
2019-07-26 21:43:52 +02:00
|
|
|
val foodViewModel = FoodViewModel(mApplication)
|
|
|
|
|
val docFood = Jsoup.connect("https://djd4rkn355.github.io/food.html").get()
|
|
|
|
|
val foodElements = docFood.select("th")
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let {
|
|
|
|
|
progressBar.max = foodElements.size
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
var foodInt = 0
|
2019-07-06 14:18:59 +02:00
|
|
|
var priority = 0
|
|
|
|
|
foodViewModel.deleteAll()
|
2019-05-23 16:26:12 +02:00
|
|
|
while (foodInt in 0 until foodElements.size) {
|
|
|
|
|
try { // what a mess this is!
|
|
|
|
|
if (foodElements[foodInt].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt].text().contains("Freitag")) {
|
2019-05-23 14:28:41 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
if (foodElements[foodInt + 3].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("Freitag") ||
|
|
|
|
|
foodElements[foodInt + 3].text().contains("von")) {
|
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-05-23 16:26:12 +02:00
|
|
|
foodInt += 2
|
|
|
|
|
} else if (foodElements[foodInt + 2].text().contains("Montag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Dienstag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Mittwoch") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Donnerstag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("Freitag") ||
|
|
|
|
|
foodElements[foodInt + 2].text().contains("von")) {
|
2019-07-06 14:18:59 +02:00
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
|
|
|
|
+ foodElements[foodInt + 1].text(), priority))
|
2019-05-23 16:26:12 +02:00
|
|
|
foodInt += 1
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-05-23 14:28:41 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
} else {
|
2019-07-06 14:18:59 +02:00
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text(), priority))
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
|
|
|
|
} catch (e: IndexOutOfBoundsException) {
|
|
|
|
|
try {
|
2019-07-06 14:18:59 +02:00
|
|
|
foodViewModel.insert(Food(foodElements[foodInt].text() + "\n"
|
2019-05-23 16:26:12 +02:00
|
|
|
+ foodElements[foodInt + 1].text() + "\n"
|
2019-07-06 14:18:59 +02:00
|
|
|
+ foodElements[foodInt + 2].text(), priority))
|
2019-05-23 16:26:12 +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-05-23 16:26:12 +02:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foodInt++
|
2019-07-06 14:18:59 +02:00
|
|
|
priority++
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let {
|
2019-05-26 14:59:00 +02:00
|
|
|
progressBar.progress = foodInt
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-26 14:59:00 +02:00
|
|
|
mView?.let {
|
|
|
|
|
progressBar.progress = 100
|
|
|
|
|
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
|
|
|
|
fadeOut.setAnimationListener(object: Animation.AnimationListener {
|
|
|
|
|
override fun onAnimationStart(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationRepeat(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationEnd(arg0: Animation) {
|
|
|
|
|
progressBar.progress = 0
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
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)
|
|
|
|
|
val doc = Jsoup.connect("https://djd4rkn355.github.io/subst_test").get()
|
|
|
|
|
val connection = URL("https://djd4rkn355.github.io/subst").openConnection()
|
2019-05-23 16:26:12 +02:00
|
|
|
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
2019-07-23 21:42:56 +02:00
|
|
|
d = sdf.parse(connection.getHeaderField("Last-Modified"))
|
2019-07-26 21:43:52 +02:00
|
|
|
val rows = doc.select("tr")
|
|
|
|
|
val paragraphs = doc.select("p")
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-07-23 21:42:56 +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-05-23 16:26:12 +02:00
|
|
|
mView?.let {
|
|
|
|
|
progressBar.max = rows.size
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
for (i in 0 until paragraphs.size) {
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
informational = paragraphs[i].text()
|
|
|
|
|
} else {
|
|
|
|
|
informational += "\n\n" + paragraphs[i].text()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
edit.putString("informational", informational).apply()
|
|
|
|
|
|
|
|
|
|
substViewModel.deleteAllSubst()
|
|
|
|
|
|
|
|
|
|
for (i in 0 until rows.size) {
|
|
|
|
|
val row = rows[i]
|
|
|
|
|
val cols = row.select("th") as Elements
|
|
|
|
|
|
2019-07-23 21:42:56 +02:00
|
|
|
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())
|
2019-05-23 16:26:12 +02:00
|
|
|
if (!jobservice) {
|
|
|
|
|
mView?.let {
|
|
|
|
|
progressBar.incrementProgressBy(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 21:43:52 +02:00
|
|
|
val drawable = MiscData.getIcon(courseS[i])
|
2019-07-23 21:42:56 +02:00
|
|
|
val subst = Subst(drawable, groupS[i], dateS[i], timeS[i], courseS[i],
|
|
|
|
|
roomS[i], additionalS[i], priority)
|
2019-05-23 16:26:12 +02:00
|
|
|
priority--
|
2019-06-02 17:57:57 +02:00
|
|
|
substViewModel.insertSubst(subst)
|
2019-05-23 16:26:12 +02:00
|
|
|
|
|
|
|
|
if (jobservice) {
|
2019-07-23 21:42:56 +02:00
|
|
|
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-23 16:26:12 +02:00
|
|
|
if (notifText.isNotEmpty()) {
|
|
|
|
|
notifText += ", "
|
|
|
|
|
}
|
|
|
|
|
notifText += courseS[i] + ": " + additionalS[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-23 21:42:56 +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())) {
|
2019-05-22 20:33:07 +02:00
|
|
|
if (notifText.isNotEmpty()) {
|
|
|
|
|
notifText += ", "
|
|
|
|
|
}
|
|
|
|
|
notifText += courseS[i] + ": " + additionalS[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-23 14:28:41 +02:00
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
if (jobservice) {
|
|
|
|
|
val openApp = Intent(mContext, Main::class.java)
|
|
|
|
|
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
|
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
|
2019-07-26 21:43:52 +02:00
|
|
|
val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0)
|
2019-05-23 16:26:12 +02:00
|
|
|
|
|
|
|
|
val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification)
|
|
|
|
|
notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.subst))
|
|
|
|
|
notificationLayout.setTextViewText(R.id.notification_textview, notifText)
|
|
|
|
|
|
|
|
|
|
if (notifText.isNotEmpty()) {
|
2019-06-04 09:27:54 +02:00
|
|
|
val manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
val channelId = "general"
|
|
|
|
|
val channelName = mContext.getString(R.string.general)
|
|
|
|
|
|
2019-05-23 16:26:12 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2019-07-26 21:43:52 +02:00
|
|
|
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
|
2019-06-04 09:27:54 +02:00
|
|
|
channel.enableLights(true)
|
|
|
|
|
channel.lightColor = Color.BLUE
|
|
|
|
|
manager.createNotificationChannel(channel)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 13:31:31 +02:00
|
|
|
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()
|
2019-05-26 14:59:00 +02:00
|
|
|
manager.notify(1, notification)
|
2019-05-23 16:26:12 +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 ->
|
|
|
|
|
handler.postDelayed({ progressBar.startAnimation(fadeOut) }, 200)
|
|
|
|
|
fadeOut.setAnimationListener(object: Animation.AnimationListener {
|
|
|
|
|
override fun onAnimationStart(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationRepeat(arg0: Animation) {}
|
|
|
|
|
override fun onAnimationEnd(arg0: Animation) {
|
|
|
|
|
progressBar.progress = 0
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
pullToRefresh.isRefreshing = false
|
2019-07-06 14:18:59 +02:00
|
|
|
if (plan) {
|
2019-07-23 21:42:56 +02:00
|
|
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
2019-07-06 14:18:59 +02:00
|
|
|
sdf.applyPattern(newDateFormat)
|
|
|
|
|
val sb = StringBuilder()
|
2019-07-23 21:42:56 +02:00
|
|
|
val lastUpdated = sb.append(mContext.getText(R.string.lastupdatedK)).append(sdf.format(d)).toString()
|
|
|
|
|
Snackbar.make(snackBarView, lastUpdated, Snackbar.LENGTH_LONG).show()
|
2019-07-06 14:18:59 +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
|
|
|
|
2019-06-02 17:57:57 +02:00
|
|
|
} catch (e: Exception) {
|
2019-05-23 16:26:12 +02:00
|
|
|
mView?.let { v: View ->
|
2019-06-02 17:57:57 +02:00
|
|
|
try {
|
|
|
|
|
pullToRefresh.isRefreshing = false
|
|
|
|
|
} catch (ignored: Exception) {}
|
2019-07-23 21:42:56 +02:00
|
|
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
|
|
|
|
Snackbar.make(snackBarView, mContext.getText(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
|
|
|
|
|
}
|
|
|
|
|
}
|