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 androidx.lifecycle.ViewModelProvider
|
|
|
|
|
import com.denizk0461.studip.R
|
2023-04-25 15:38:30 +02:00
|
|
|
import com.denizk0461.studip.data.getThemedColor
|
2023-04-12 18:58:10 +02:00
|
|
|
import com.denizk0461.studip.databinding.ActivityFetcherBinding
|
|
|
|
|
import com.denizk0461.studip.viewmodel.FetcherViewModel
|
2023-04-25 12:02:25 +02:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
|
|
|
|
import java.io.IOException
|
2023-04-12 18:58:10 +02:00
|
|
|
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
|
|
|
|
|
|
2023-04-27 20:27:42 +02:00
|
|
|
/**
|
2023-04-12 18:58:10 +02:00
|
|
|
* 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-25 12:02:25 +02:00
|
|
|
try {
|
|
|
|
|
// Decode HTML and parse it into a list of StudIPEvent.kt
|
2023-04-28 15:14:50 +02:00
|
|
|
// TODO this exception handling and finish() is broken
|
|
|
|
|
viewModel.parse(URLDecoder.decode(p0, "UTF-8"))
|
|
|
|
|
// StudIPParser(application).parse(URLDecoder.decode(p0, "UTF-8")) { events ->
|
|
|
|
|
//
|
|
|
|
|
// // Delete all previously fetched elements
|
|
|
|
|
//// viewModel.nukeEvents()
|
|
|
|
|
//// viewModel.replaceEvents(events)
|
|
|
|
|
//
|
|
|
|
|
// // Insert the list into the database
|
|
|
|
|
//// viewModel.insertEvents(events)
|
|
|
|
|
//
|
|
|
|
|
// // Notify the user that the fetch was successful
|
|
|
|
|
// Toast.makeText(
|
|
|
|
|
// this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
|
|
|
|
|
// ).show()
|
|
|
|
|
//
|
|
|
|
|
// // Close the activity
|
|
|
|
|
// finish()
|
|
|
|
|
// }
|
2023-04-25 12:02:25 +02:00
|
|
|
} catch (e: IOException) {
|
|
|
|
|
|
|
|
|
|
// Let the user know that an error occurred
|
|
|
|
|
Snackbar
|
|
|
|
|
.make(
|
|
|
|
|
binding.rootView,
|
|
|
|
|
getString(R.string.fetch_error_snack),
|
|
|
|
|
Snackbar.LENGTH_SHORT
|
|
|
|
|
)
|
|
|
|
|
// Set colours to signify an error
|
2023-04-25 15:38:30 +02:00
|
|
|
.setBackgroundTint(theme.getThemedColor(R.attr.colorErrorContainer))
|
|
|
|
|
.setTextColor(theme.getThemedColor(R.attr.colorOnErrorContainer))
|
2023-04-25 12:02:25 +02:00
|
|
|
.show()
|
2023-04-12 18:58:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|