Archived
"Last updated" time is now compared to a string embedded in the HTML instead of the "Last-Updated" header field to prevent duplicate notifications when the food menu refreshes
This commit is contained in:
@@ -33,7 +33,6 @@ class ColourAdapter(private var mColours: List<Colour>, onClickListener: OnClick
|
|||||||
|
|
||||||
override fun onBindViewHolder(holder: ColourViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: ColourViewHolder, position: Int) {
|
||||||
val currentItem = mColours[position]
|
val currentItem = mColours[position]
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(holder.image.context)
|
|
||||||
|
|
||||||
holder.title.text = currentItem.title
|
holder.title.text = currentItem.title
|
||||||
holder.titleNoLang.text = currentItem.titleNoLang
|
holder.titleNoLang.text = currentItem.titleNoLang
|
||||||
@@ -54,6 +53,6 @@ class ColourAdapter(private var mColours: List<Colour>, onClickListener: OnClick
|
|||||||
}
|
}
|
||||||
|
|
||||||
public interface OnClickListener {
|
public interface OnClickListener {
|
||||||
fun onClick(position: Int, title: String, titleNolang: String)
|
fun onClick(position: Int, title: String, titleNoLang: String)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,12 +49,9 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
private val edit = prefs.edit()
|
private val edit = prefs.edit()
|
||||||
private lateinit var pullToRefresh: SwipeRefreshLayout
|
private lateinit var pullToRefresh: SwipeRefreshLayout
|
||||||
private lateinit var progressBar: ProgressBar
|
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
|
|
||||||
private val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
private val fadeOut = AnimationUtils.loadAnimation(mContext, R.anim.fade_out)
|
||||||
private val handler = Handler(Looper.getMainLooper())
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
var currentTime = ""
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void?): Void? {
|
override fun doInBackground(vararg params: Void?): Void? {
|
||||||
try {
|
try {
|
||||||
@@ -142,111 +139,120 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
if (plan) {
|
if (plan) {
|
||||||
val substViewModel = SubstViewModel(mApplication)
|
val substViewModel = SubstViewModel(mApplication)
|
||||||
val doc = Jsoup.connect("https://djd4rkn355.github.io/subst_test").get()
|
val doc = Jsoup.connect("https://djd4rkn355.github.io/subst_test").get()
|
||||||
val connection = URL("https://djd4rkn355.github.io/subst").openConnection()
|
currentTime = doc.select("h1").text()
|
||||||
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
if (currentTime != prefs.getString("time", "")) {
|
||||||
d = sdf.parse(connection.getHeaderField("Last-Modified"))
|
val rows = doc.select("tr")
|
||||||
val rows = doc.select("tr")
|
val paragraphs = doc.select("p")
|
||||||
val paragraphs = doc.select("p")
|
|
||||||
|
|
||||||
val groupS = ArrayList<String>()
|
val groupS = ArrayList<String>()
|
||||||
val dateS = ArrayList<String>()
|
val dateS = ArrayList<String>()
|
||||||
val timeS = ArrayList<String>()
|
val timeS = ArrayList<String>()
|
||||||
val courseS = ArrayList<String>()
|
val courseS = ArrayList<String>()
|
||||||
val roomS = ArrayList<String>()
|
val roomS = ArrayList<String>()
|
||||||
val additionalS = ArrayList<String>()
|
val additionalS = ArrayList<String>()
|
||||||
|
|
||||||
mView?.let {
|
mView?.let {
|
||||||
progressBar.max = rows.size
|
progressBar.max = rows.size
|
||||||
}
|
|
||||||
|
|
||||||
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 paragraphs.size) {
|
||||||
|
if (i == 0) {
|
||||||
for (i in 0 until rows.size) {
|
informational = paragraphs[i].text()
|
||||||
val row = rows[i]
|
} else {
|
||||||
val cols = row.select("th") as Elements
|
informational += "\n\n" + paragraphs[i].text()
|
||||||
|
|
||||||
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())
|
|
||||||
if (!jobservice) {
|
|
||||||
mView?.let {
|
|
||||||
progressBar.incrementProgressBy(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
edit.putString("informational", informational).apply()
|
||||||
|
|
||||||
val drawable = MiscData.getIcon(courseS[i])
|
substViewModel.deleteAllSubst()
|
||||||
val subst = Subst(drawable, groupS[i], dateS[i], timeS[i], courseS[i],
|
|
||||||
roomS[i], additionalS[i], priority)
|
|
||||||
priority--
|
|
||||||
substViewModel.insertSubst(subst)
|
|
||||||
|
|
||||||
if (jobservice) {
|
for (i in 0 until rows.size) {
|
||||||
if ((prefs.getString("courses", "") ?: "").isEmpty() && (prefs.getString("classes", "") ?: "").isNotEmpty()) {
|
val row = rows[i]
|
||||||
if (groupS[i].isNotEmpty() && groupS[i] != "") {
|
val cols = row.select("th") as Elements
|
||||||
if ((prefs.getString("classes", "") ?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "") ?: "").toString())) {
|
|
||||||
if (notifText.isNotEmpty()) {
|
groupS.add(cols[0].text())
|
||||||
notifText += ", "
|
dateS.add(cols[1].text())
|
||||||
}
|
timeS.add(cols[2].text())
|
||||||
notifText += courseS[i] + ": " + additionalS[i]
|
courseS.add(cols[3].text())
|
||||||
}
|
roomS.add(cols[4].text())
|
||||||
|
additionalS.add(cols[5].text())
|
||||||
|
if (!jobservice) {
|
||||||
|
mView?.let {
|
||||||
|
progressBar.incrementProgressBy(1)
|
||||||
}
|
}
|
||||||
} else if ((prefs.getString("classes", "") ?: "").isNotEmpty() && (prefs.getString("courses", "") ?: "").isNotEmpty()) {
|
}
|
||||||
if (groupS[i] != "" && courseS[i] != "") {
|
|
||||||
if ((prefs.getString("courses", "") ?: "").contains(courseS[i])) {
|
val drawable = MiscData.getIcon(courseS[i])
|
||||||
if ((prefs.getString("classes", "") ?: "").contains(groupS[i]) || groupS[i].contains((prefs.getString("classes", "") ?: "").toString())) {
|
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())) {
|
||||||
if (notifText.isNotEmpty()) {
|
if (notifText.isNotEmpty()) {
|
||||||
notifText += ", "
|
notifText += ", "
|
||||||
}
|
}
|
||||||
notifText += courseS[i] + ": " + additionalS[i]
|
notifText += courseS[i] + ": " + additionalS[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} 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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
edit.putString("time", currentTime).apply()
|
||||||
}
|
}
|
||||||
}
|
if (jobservice && prefs.getBoolean("notif", true)) {
|
||||||
if (jobservice) {
|
val openApp = Intent(mContext, Main::class.java)
|
||||||
val openApp = Intent(mContext, Main::class.java)
|
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
openApp.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||||
openApp.flags += Intent.FLAG_ACTIVITY_CLEAR_TASK
|
val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0)
|
||||||
val openAppPending = PendingIntent.getActivity(mContext, 0, openApp, 0)
|
|
||||||
|
|
||||||
val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification)
|
val notificationLayout = RemoteViews(mContext.packageName, R.layout.notification)
|
||||||
notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.subst))
|
notificationLayout.setTextViewText(R.id.notification_title, mContext.getString(R.string.subst))
|
||||||
notificationLayout.setTextViewText(R.id.notification_textview, notifText)
|
notificationLayout.setTextViewText(R.id.notification_textview, notifText)
|
||||||
|
|
||||||
if (notifText.isNotEmpty()) {
|
if (notifText.isNotEmpty()) {
|
||||||
val manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
val manager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
val channelId = "general"
|
val channelId = "general"
|
||||||
val channelName = mContext.getString(R.string.general)
|
val channelName = mContext.getString(R.string.general)
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
|
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
|
||||||
channel.enableLights(true)
|
channel.enableLights(true)
|
||||||
channel.lightColor = Color.BLUE
|
channel.lightColor = Color.BLUE
|
||||||
manager.createNotificationChannel(channel)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,9 +269,8 @@ class DataFetcher(isplan: Boolean, ismenu: Boolean, isjobservice: Boolean, conte
|
|||||||
pullToRefresh.isRefreshing = false
|
pullToRefresh.isRefreshing = false
|
||||||
if (plan) {
|
if (plan) {
|
||||||
val snackBarView = v.findViewById<View>(R.id.coordination)
|
val snackBarView = v.findViewById<View>(R.id.coordination)
|
||||||
sdf.applyPattern(newDateFormat)
|
|
||||||
val sb = StringBuilder()
|
val sb = StringBuilder()
|
||||||
val lastUpdated = sb.append(mContext.getText(R.string.lastupdatedK)).append(sdf.format(d)).toString()
|
val lastUpdated = sb.append(mContext.getText(R.string.lastupdatedK)).append(currentTime)
|
||||||
Snackbar.make(snackBarView, lastUpdated, Snackbar.LENGTH_LONG).show()
|
Snackbar.make(snackBarView, lastUpdated, Snackbar.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import android.app.job.JobService
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
|
import org.jsoup.Jsoup
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
class NotificationService : JobService() {
|
class NotificationService : JobService() {
|
||||||
@@ -29,21 +30,20 @@ class NotificationService : JobService() {
|
|||||||
val networkStatus = connectivityManager.activeNetworkInfo
|
val networkStatus = connectivityManager.activeNetworkInfo
|
||||||
|
|
||||||
Thread(Runnable {
|
Thread(Runnable {
|
||||||
if (jobCancelled || networkStatus == null) {
|
if (!prefs.getBoolean("notif", false)) {
|
||||||
return@Runnable
|
return@Runnable
|
||||||
}
|
} else {
|
||||||
if (prefs.getBoolean("notif", false)) {
|
if (jobCancelled || networkStatus == null) {
|
||||||
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
|
return@Runnable
|
||||||
try {
|
}
|
||||||
val url = URL("https://djd4rkn355.github.io/subst.html")
|
if (prefs.getBoolean("notif", false)) {
|
||||||
val connection = url.openConnection()
|
edit.putInt("notificationTestNumberDev", prefs.getInt("notificationTestNumberDev", 0) + 1).apply()
|
||||||
if (connection.getHeaderField("Last-Modified") != prefs.getString("time", "")) {
|
try {
|
||||||
DataFetcher(true, true, true, context, application, null).execute()
|
DataFetcher(true, true, true, context, application, null).execute()
|
||||||
edit.putString("time", connection.getHeaderField("Last-Modified")).apply()
|
} catch (ignored: Exception) {}
|
||||||
}
|
}
|
||||||
} catch (ignored: Exception) {}
|
jobFinished(params, false)
|
||||||
}
|
}
|
||||||
jobFinished(params, false)
|
|
||||||
}).start()
|
}).start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -350,7 +350,7 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
|
|||||||
dialogView.findViewById<TextView>(R.id.dialogtext).text = getString(R.string.fortest)
|
dialogView.findViewById<TextView>(R.id.dialogtext).text = getString(R.string.fortest)
|
||||||
dialogButton.setOnClickListener {
|
dialogButton.setOnClickListener {
|
||||||
when (dialogEditText.text.toString()) {
|
when (dialogEditText.text.toString()) {
|
||||||
"@DIAGNOSTICS" -> {
|
"_DIAGNOSTICS" -> {
|
||||||
val alertDialogDev = AlertDialog.Builder(mContext, R.style.AlertDialog)
|
val alertDialogDev = AlertDialog.Builder(mContext, R.style.AlertDialog)
|
||||||
val devDialogView = LayoutInflater.from(mContext).inflate(R.layout.diagnostics_dialog, null)
|
val devDialogView = LayoutInflater.from(mContext).inflate(R.layout.diagnostics_dialog, null)
|
||||||
val devDialogText = devDialogView.findViewById<TextView>(R.id.dialogtext)
|
val devDialogText = devDialogView.findViewById<TextView>(R.id.dialogtext)
|
||||||
@@ -382,11 +382,11 @@ class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListen
|
|||||||
Toast.makeText(mContext, R.string.noyoutube, Toast.LENGTH_LONG).show()
|
Toast.makeText(mContext, R.string.noyoutube, Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"@NOTIFICATION" -> {
|
"_NOTIFICATION" -> {
|
||||||
edit.putString("time", "").apply()
|
edit.putString("time", "").apply()
|
||||||
Toast.makeText(mContext, "Notification time cleared", Toast.LENGTH_LONG).show()
|
Toast.makeText(mContext, "Notification time cleared", Toast.LENGTH_LONG).show()
|
||||||
} // TODO add option to clear database
|
} // TODO add option to clear database
|
||||||
"@FIRSTTIME" -> {
|
"_FIRSTTIME" -> {
|
||||||
edit.putBoolean("firstTime", true).apply()
|
edit.putBoolean("firstTime", true).apply()
|
||||||
Toast.makeText(mContext, "First time flag cleared", Toast.LENGTH_LONG).show()
|
Toast.makeText(mContext, "First time flag cleared", Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user