Skip to content

Commit 048e8fc

Browse files
geavenxaechiaravdonnangelo
authored
v1.0.3
* added location request * make headers be optional in doGet * add capability of getting encrypted fingerprint fix #8 --------- Co-authored-by: Alex Eduardo Chiaranda <aechiara@gmail.com> Co-authored-by: Vitor Cardoso <vitor.cardoso@sec4you.com.br>
1 parent dec9035 commit 048e8fc

7 files changed

Lines changed: 251 additions & 89 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ root = true
88

99
# Change these settings to your own preference
1010
indent_style = space
11-
indent_size = 2
11+
indent_size = 4
1212

1313
# We recommend you to keep these unchanged
1414
end_of_line = lf

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ dependencies {
6666
debugImplementation("androidx.compose.ui:ui-test-manifest")
6767
implementation("androidx.navigation:navigation-compose:2.7.7")
6868

69-
implementation("com.google.android.gms:play-services-location:21.3.0")
69+
implementation("com.google.android.gms:play-services-location:20.0.0")
7070

7171
// corotines for http call
7272
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
xmlns:tools="http://schemas.android.com/tools">
44

55
<uses-permission android:name="android.permission.INTERNET" />
6+
<!-- Used by DNA -->
7+
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
8+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
9+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
10+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
611

712

813
<application

app/src/main/java/br/com/sec4you/authfy/app/AuthScreen.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package br.com.sec4you.authfy.app
22

3+
import android.Manifest
4+
import android.content.pm.PackageManager
35
import android.app.Activity
46
import android.content.Context
57
import android.net.Uri
68
import android.util.Base64
79
import android.util.Log
10+
import android.widget.Toast
811
import androidx.activity.compose.rememberLauncherForActivityResult
912
import androidx.activity.result.ActivityResult
1013
import androidx.activity.result.contract.ActivityResultContracts
@@ -37,6 +40,7 @@ import androidx.compose.material3.Scaffold
3740
import androidx.compose.material3.Surface
3841
import androidx.compose.material3.Text
3942
import androidx.compose.runtime.Composable
43+
import androidx.compose.runtime.LaunchedEffect
4044
import androidx.compose.runtime.getValue
4145
import androidx.compose.runtime.mutableStateOf
4246
import androidx.compose.runtime.remember
@@ -50,6 +54,7 @@ import androidx.compose.ui.text.font.FontWeight
5054
import androidx.compose.ui.tooling.preview.Preview
5155
import androidx.compose.ui.unit.dp
5256
import androidx.compose.ui.unit.sp
57+
import androidx.core.content.ContextCompat
5358
import androidx.navigation.NavController
5459
import androidx.navigation.compose.rememberNavController
5560
import br.com.sec4you.authfy.app.ui.theme.AuthfySampleTheme
@@ -84,6 +89,71 @@ fun AuthScreen(
8489
val authService = remember { AuthorizationService(context) }
8590
var errorMessage by remember { mutableStateOf<String?>(null) }
8691

92+
93+
/* LOCATION */
94+
95+
// State to track if location permission is granted
96+
val isLocationPermissionGranted = remember { mutableStateOf(false) }
97+
98+
// Function to get location (you'll implement this - same as in Activity example)
99+
fun getLocation(context: android.content.Context) {
100+
// Implement your location retrieval logic here using FusedLocationProviderClient etc.
101+
// Make sure to use the 'context' passed to this function
102+
Toast.makeText(context, "Getting Location... (AuthScreen)", Toast.LENGTH_SHORT).show()
103+
// ... Location retrieval code (FusedLocationProviderClient) ...
104+
// ... Remember to handle permission check *again* within getLocation() if necessary in more complex scenarios
105+
}
106+
107+
// Launcher for requesting location permission
108+
val locationPermissionLauncher = rememberLauncherForActivityResult(
109+
ActivityResultContracts.RequestPermission()
110+
) { isGranted: Boolean ->
111+
if (isGranted) {
112+
isLocationPermissionGranted.value = true
113+
Toast.makeText(context, "Location permission granted in AuthScreen", Toast.LENGTH_SHORT).show()
114+
getLocation(context) // Call function to get location after permission is granted
115+
} else {
116+
isLocationPermissionGranted.value = false
117+
Toast.makeText(context, "Location permission denied in AuthScreen", Toast.LENGTH_SHORT).show()
118+
// Handle permission denial scenario - maybe explain why location is needed for some features
119+
}
120+
}
121+
122+
// Function to check location permission
123+
fun checkLocationPermission() {
124+
if (ContextCompat.checkSelfPermission(
125+
context,
126+
Manifest.permission.ACCESS_FINE_LOCATION
127+
) == PackageManager.PERMISSION_GRANTED
128+
) {
129+
isLocationPermissionGranted.value = true
130+
Toast.makeText(context, "Location permission already granted in AuthScreen", Toast.LENGTH_SHORT).show()
131+
getLocation(context) // Permission already granted, get location directly
132+
} else {
133+
isLocationPermissionGranted.value = false // Not strictly needed, but for clarity
134+
}
135+
}
136+
137+
// Function to request location permission
138+
fun requestLocationPermission() {
139+
locationPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
140+
}
141+
142+
// LaunchedEffect to run code when the Composable is launched (similar to onCreate in Activity)
143+
LaunchedEffect(Unit) { // Unit means it runs only once on initial composition
144+
checkLocationPermission() // Check permission when AuthScreen is displayed
145+
if (!isLocationPermissionGranted.value) {
146+
requestLocationPermission() // Request permission if not already granted
147+
}
148+
// ... other initialization logic for AuthScreen if needed ...
149+
}
150+
151+
152+
/* LOCATION */
153+
154+
155+
156+
87157
if (authenticated) {
88158
navController.navigate(Screen.StartScreen.route)
89159
return

app/src/main/java/br/com/sec4you/authfy/app/NetworkUtils.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ object NetworkUtils {
2626
}
2727

2828
suspend fun doGet(
29-
urlString: String, headers: Map<String, String>
29+
urlString: String, headers: Map<String, String>?
3030
): NetworkResult<Map<String, Any>> {
3131
return withContext(Dispatchers.IO) {
3232
try {
3333
val url = URL(urlString)
3434
val connection = url.openConnection() as HttpURLConnection
3535

3636
// Configurar headers
37-
for ((key, value) in headers) {
38-
connection.setRequestProperty(key, value)
39-
}
37+
if (headers != null) {
38+
for ((key, value) in headers) {
39+
connection.setRequestProperty(key, value)
40+
}
41+
}
4042

4143
try {
4244
connection.requestMethod = "GET"

0 commit comments

Comments
 (0)