some more progress on android app

This commit is contained in:
2023-10-08 11:07:25 +02:00
parent ff87b8646a
commit 5644272d5e
5 changed files with 73 additions and 14 deletions

View File

@@ -29,4 +29,5 @@
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View File

@@ -26,7 +26,7 @@ class ApiClient {
connection.doOutput = true
// Create the JSON request body
val jsonRequest = "{\"username\":\"$username\",\"password\":\"$password\"}"
val jsonRequest = "{\"email\":\"$username\",\"password\":\"$password\"}"
// Write the JSON data to the output stream
val outputStream = DataOutputStream(connection.outputStream)
@@ -50,8 +50,7 @@ class ApiClient {
// Return the response as a String
return response.toString()
} else {
// Handle the error (e.g., authentication failed)
// You can also throw an exception here if needed
println(responseCode)
return "status-code-non-ok"
}
} catch (e: Exception) {
@@ -61,7 +60,7 @@ class ApiClient {
}
fun checkTicket(apiUrl: String, username: String, password: String, ticket: String): String {
val url = URL("$apiUrl/app/ticketLookup")
val url = URL("https://$apiUrl/app/ticketLookup")
val connection = url.openConnection() as HttpURLConnection
// Set the request method to POST
@@ -76,7 +75,7 @@ class ApiClient {
connection.doOutput = true
// Create the JSON request body
val jsonRequest = "{\"username\":\"$username\",\"password\":\"$password\",\"ticketID\":$ticket}"
val jsonRequest = "{\"email\":\"$username\",\"password\":\"$password\",\"ticketID\":$ticket}"
// Write the JSON data to the output stream
val outputStream = DataOutputStream(connection.outputStream)

View File

@@ -1,23 +1,43 @@
package com.janishutz.libreevent
import android.app.AlertDialog
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.widget.Button
import android.widget.EditText
import android.app.AlertDialog
import com.janishutz.libreevent.ApiClient
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val sharedPref = getPreferences( MODE_PRIVATE )
val hasSwitched = intent.hasExtra("hasSwitched")
val loginButton = findViewById<Button>(R.id.loginButton)
val urlEditText = findViewById<EditText>(R.id.url)
val usernameEditText = findViewById<EditText>(R.id.username)
val passwordEditText = findViewById<EditText>(R.id.password)
if (sharedPref.getString( "url", null ).toString() != "null" && sharedPref.getString( "username", null ).toString() != "null" ) {
urlEditText.setText(sharedPref.getString( "url", null ).toString())
usernameEditText.setText(sharedPref.getString( "username", null ).toString())
}
if (sharedPref.getString( "loginOk", null ).toString() != "null" && !hasSwitched) {
println(sharedPref.getString( "loginOk", null ).toString())
val switchIntent = Intent(this, ScannerActivity::class.java)
startActivity(switchIntent)
}
loginButton.setOnClickListener {
val url = urlEditText.text.toString()
val username = usernameEditText.text.toString()
@@ -30,6 +50,13 @@ class MainActivity : AppCompatActivity() {
val res = ApiClient().authenticateUser( url, username, password )
println( res )
if ( res == "authOk" ) {
val sharedPref = getPreferences( MODE_PRIVATE )
val editor = sharedPref.edit()
editor.putString( "username", username )
editor.putString( "password", password )
editor.putString( "url", url )
editor.putString( "loginOk", "true" )
editor.apply()
val switchIntent = Intent(this, ScannerActivity::class.java)
startActivity(switchIntent)
} else if ( res == "status-code-non-ok" ) {

View File

@@ -1,9 +1,12 @@
package com.janishutz.libreevent
import android.Manifest
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.core.app.ActivityCompat
import com.journeyapps.barcodescanner.BarcodeCallback
import com.journeyapps.barcodescanner.BarcodeResult
@@ -23,6 +26,19 @@ class ScannerActivity : CaptureActivity() {
barcodeView = findViewById(R.id.barcodeScannerView)
val logoutButton = findViewById<Button>(R.id.logoutButton)
logoutButton.setOnClickListener {
val sharedPref = getPreferences( MODE_PRIVATE )
val editor = sharedPref.edit()
editor.remove( "loginOk" )
editor.remove( "username" )
editor.remove( "url" )
editor.apply()
val switchIntent = Intent(this, MainActivity::class.java)
switchIntent.putExtra("hasSwitched", true)
startActivity(switchIntent)
}
// Check for camera permission and request if not granted
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_REQUEST)
@@ -43,16 +59,17 @@ class ScannerActivity : CaptureActivity() {
handleScanResult(scannedData)
}
}
override fun possibleResultPoints(resultPoints: List<com.google.zxing.ResultPoint>?) {
// Optional: Handle possible result points
}
})
}
private fun handleScanResult(result: String) {
if ( lastScanned != result ) {
println(result)
val sharedPref = getPreferences( MODE_PRIVATE )
ApiClient().checkTicket( sharedPref.getString( "url", null ).toString(),
sharedPref.getString( "username", null ).toString(),
sharedPref.getString( "password", null ).toString(), result )
lastScanned = result
}
}
@@ -79,7 +96,15 @@ class ScannerActivity : CaptureActivity() {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setupScanner()
} else {
// Handle permission denied
val alertDialogBuilder = AlertDialog.Builder(this)
alertDialogBuilder.setTitle("Camera access required!")
alertDialogBuilder.setMessage("Please ensure that camera access is enabled in settings")
alertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert)
alertDialogBuilder.setPositiveButton("OK") { dialog, _ ->
dialog.dismiss()
}
alertDialogBuilder.show()
}
}
}

View File

@@ -10,6 +10,13 @@
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/barcodeScannerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent" >
<Button
android:id="@+id/logoutButton"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:text="Log out" />
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
</FrameLayout>