2023-04-12 18:58:10 +02:00
|
|
|
package com.denizk0461.studip.activity
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
|
import android.app.Activity
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.webkit.WebResourceRequest
|
|
|
|
|
import android.webkit.WebView
|
|
|
|
|
import android.webkit.WebViewClient
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
|
|
|
|
import com.denizk0461.studip.R
|
|
|
|
|
import com.denizk0461.studip.data.StudIPParser
|
|
|
|
|
import com.denizk0461.studip.databinding.ActivityFetcherBinding
|
|
|
|
|
import com.denizk0461.studip.viewmodel.FetcherViewModel
|
|
|
|
|
import java.net.URLDecoder
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/**
|
|
|
|
|
* Activity for allowing the user to log in to access and fetch their schedule from their Stud.IP
|
|
|
|
|
* profile. Launched through a button in the app's settings.
|
|
|
|
|
*/
|
2023-04-12 18:58:10 +02:00
|
|
|
class FetcherActivity : Activity() {
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// View binding
|
2023-04-12 18:58:10 +02:00
|
|
|
private lateinit var binding: ActivityFetcherBinding
|
2023-04-21 15:42:21 +02:00
|
|
|
// View model
|
2023-04-12 18:58:10 +02:00
|
|
|
private lateinit var viewModel: FetcherViewModel
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Enabling JavaScript is necessary to get the HTML source in this context. As the HTML can only
|
|
|
|
|
* be accessed once the user has logged in, a simple HTML request would not suffice here.
|
|
|
|
|
*/
|
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Instantiate view model
|
2023-04-12 18:58:10 +02:00
|
|
|
viewModel = ViewModelProvider.AndroidViewModelFactory(this.application).create(FetcherViewModel::class.java)
|
2023-04-21 15:42:21 +02:00
|
|
|
// Inflate view binding and bind to this activity
|
2023-04-12 18:58:10 +02:00
|
|
|
binding = ActivityFetcherBinding.inflate(layoutInflater)
|
|
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
// Enabling JavaScript. See comment above lint suppression for reason why this is done.
|
2023-04-12 18:58:10 +02:00
|
|
|
binding.webview.settings.apply {
|
|
|
|
|
javaScriptEnabled = true
|
|
|
|
|
}
|
|
|
|
|
binding.webview.webViewClient = object : WebViewClient() {
|
2023-04-21 15:42:21 +02:00
|
|
|
// Disallow redirecting to prevent the app from launching Chrome after the user logs in
|
2023-04-12 18:58:10 +02:00
|
|
|
override fun shouldOverrideUrlLoading(
|
|
|
|
|
view: WebView,
|
|
|
|
|
request: WebResourceRequest
|
|
|
|
|
): Boolean {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:42:21 +02:00
|
|
|
/*
|
|
|
|
|
* Attempt to load the user-set Stud.IP front page. Redirects to login page if user is
|
|
|
|
|
* not logged in.
|
|
|
|
|
*/
|
2023-04-12 18:58:10 +02:00
|
|
|
binding.webview.loadUrl("https://elearning.uni-bremen.de/index.php?again=yes")
|
|
|
|
|
|
|
|
|
|
binding.fab.setOnClickListener { view ->
|
2023-04-21 15:42:21 +02:00
|
|
|
/*
|
|
|
|
|
* Retrieve encoded HTML via JavaScript function. Encoding is done as otherwise not all
|
|
|
|
|
* symbols will be accurately fetched.
|
|
|
|
|
*/
|
2023-04-12 18:58:10 +02:00
|
|
|
binding.webview.evaluateJavascript(
|
|
|
|
|
"(function(){return encodeURI(document.getElementsByTagName('html')[0].innerHTML)})();"
|
|
|
|
|
) { p0 ->
|
2023-04-21 15:42:21 +02:00
|
|
|
// Delete all previously fetched elements
|
2023-04-12 18:58:10 +02:00
|
|
|
viewModel.nukeEvents()
|
2023-04-21 15:42:21 +02:00
|
|
|
// Decode HTML and parse it into a list of StudIPEvent.kt
|
2023-04-12 18:58:10 +02:00
|
|
|
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
|
2023-04-21 15:42:21 +02:00
|
|
|
// Insert the list into the database
|
2023-04-12 18:58:10 +02:00
|
|
|
viewModel.insertEvents(events)
|
2023-04-21 15:42:21 +02:00
|
|
|
// Notify the user that the fetch was successful. TODO: notify on error
|
|
|
|
|
Toast.makeText(
|
|
|
|
|
this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
|
|
|
|
|
).show()
|
|
|
|
|
// Close the activity
|
2023-04-12 18:58:10 +02:00
|
|
|
finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|