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-08-06 11:56:44 +02:00
|
|
|
import androidx.core.content.ContextCompat
|
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
|
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-09 09:52:05 +02:00
|
|
|
private var substUrl = "https://djd4rkn355.github.io/subst.html"
|
|
|
|
|
private var foodUrl = "https://djd4rkn355.github.io/food.html"
|
2019-05-22 20:33:07 +02:00
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void?): Void? {
|
2019-08-07 07:38:50 +02:00
|
|
|
try {
|
2019-08-09 09:52:05 +02:00
|
|
|
if (prefs.getBoolean("testUrls", false)) {
|
|
|
|
|
substUrl = "https://djd4rkn355.github.io/subst_test.html"
|
|
|
|
|
foodUrl = "https://djd4rkn355.github.io/food_test.html"
|
|
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
if (menu) {
|
2019-07-30 22:48:13 +02:00
|
|
|
val docFood = Jsoup.connect(foodUrl).get()
|
2019-07-31 11:27:46 +02:00
|
|
|
currentFoodTime = docFood.select("h1")[0].text()
|
2019-08-18 17:53:22 +02:00
|
|
|
if (currentFoodTime != prefs.getString("timeFoodNew", "")) {
|
2019-08-14 18:53:56 +02:00
|
|
|
val foodRepository = FoodRepository(mApplication)
|
2019-08-07 07:25:06 +02:00
|
|
|
val foodElements = docFood.select("th")
|
2019-08-14 18:53:56 +02:00
|
|
|
foodRepository.deleteAll()
|
2019-05-23 14:28:41 +02:00
|
|
|
|
2019-08-16 07:57:13 +02:00
|
|
|
val indices = ArrayList<Int>()
|
|
|
|
|
val daysAndVon = arrayOf("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "von")
|
|
|
|
|
for (i in 0 until foodElements.size) {
|
|
|
|
|
if (checkStringForArray(foodElements[i].text(), daysAndVon)) indices.add(i)
|
|
|
|
|
}
|
|
|
|
|
indices.add(foodElements.size)
|
|
|
|
|
|
|
|
|
|
for ((priority, l) in (0 until indices.size - 1).withIndex()) {
|
|
|
|
|
var s = ""
|
|
|
|
|
for (i2 in indices[l] until indices[l + 1]) {
|
|
|
|
|
if (s.isEmpty()) {
|
|
|
|
|
s = foodElements[i2].text()
|
|
|
|
|
} else {
|
|
|
|
|
s += "\n${foodElements[i2].text()}"
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-08-16 07:57:13 +02:00
|
|
|
foodRepository.insert(Food(s, priority))
|
2019-05-23 16:26:12 +02:00
|
|
|
}
|
2019-08-18 17:53:22 +02:00
|
|
|
edit.putString("timeFoodNew", 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-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-08-18 17:53:22 +02:00
|
|
|
if (currentTime != prefs.getString("timeNew", "")) {
|
2019-08-14 18:53:56 +02:00
|
|
|
val substRepo = SubstRepository(mApplication)
|
2019-07-30 11:18:01 +02:00
|
|
|
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-08-14 18:53:56 +02:00
|
|
|
// substViewModel.deleteAllSubst()
|
|
|
|
|
substRepo.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]
|
2019-08-07 07:25:06 +02:00
|
|
|
val cols = row.select("th")
|
2019-07-30 11:18:01 +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-08-18 17:53:22 +02:00
|
|
|
val subst = Subst(groupS[i], dateS[i], timeS[i], courseS[i],
|
2019-07-30 11:18:01 +02:00
|
|
|
roomS[i], additionalS[i], priority)
|
|
|
|
|
priority--
|
2019-08-14 18:53:56 +02:00
|
|
|
// substViewModel.insertSubst(subst)
|
|
|
|
|
substRepo.insert(subst)
|
2019-07-30 11:18:01 +02:00
|
|
|
|
|
|
|
|
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-05-23 14:28:41 +02:00
|
|
|
}
|
2019-08-07 07:25:06 +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-08-18 17:53:22 +02:00
|
|
|
edit.putString("timeNew", currentTime).apply()
|
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-08-07 07:38:50 +02:00
|
|
|
} catch (e: Exception) {
|
|
|
|
|
mView?.let { v: View ->
|
|
|
|
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
|
|
|
|
Snackbar.make(snackBarView, mContext.getString(R.string.noInternet), Snackbar.LENGTH_LONG).setBackgroundTint(ContextCompat.getColor(mContext, R.color.colorError)).show()
|
|
|
|
|
}
|
|
|
|
|
}
|
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-08-16 07:57:13 +02:00
|
|
|
|
|
|
|
|
private fun checkStringForArray(s: String, checking: Array<String>): Boolean {
|
|
|
|
|
for (i in checking.indices) {
|
|
|
|
|
if (s.contains(checking[i])) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-05-22 20:33:07 +02:00
|
|
|
}
|