1
0

Compare commits

..

No commits in common. "8a54db266f874f5ddc48515899aec4212a749274" and "5d81e98b02ffc6bad0d11ee7b2500475cfd47a0e" have entirely different histories.

7 changed files with 17 additions and 284 deletions

View File

@ -67,7 +67,6 @@ dependencies {
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.okhttp)
implementation(libs.ktor.client.logging)
implementation(libs.ktor.client.json)
implementation(libs.logback.classic)
implementation(libs.splitties.base)

View File

@ -6,14 +6,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<application
android:allowBackup="true"
@ -24,7 +17,6 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GeoTracker"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
@ -37,12 +29,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".LocationForegroundService"
android:foregroundServiceType="location"
android:enabled="true"
android:exported="false" />
</application>
</manifest>

View File

@ -9,12 +9,7 @@ import io.ktor.client.*
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import org.json.JSONObject
val client = HttpClient(OkHttp) {
install(Logging) {
@ -43,22 +38,6 @@ suspend fun health(baseurl: String): Boolean {
/**
* Send data to a server
* @return true if sent successfully
*/
suspend fun sendGeo(baseurl: String = SettingsPreferences.url, data: GeoData): Boolean {
try {
val json = JSONObject()
json.put("ln", data.ln)
json.put("lt", data.lt)
json.put("nick", data.nick)
client.post("$baseurl/map") {
contentType(ContentType.Application.Json)
setBody(json.toString(2))
}
return true
} catch (e: Exception) {
println("Error: ${e.message}")
return false
}
fun sendGeo(baseurl: String, data: GeoData) {
}

View File

@ -1,76 +0,0 @@
package ru.risdeveau.geotracker
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
class LocationForegroundService : Service() {
private lateinit var locationTracker: LocationTracker
private val serviceScope = CoroutineScope(Dispatchers.Main + Job())
override fun onCreate() {
Log.d("Service", "onCreate")
super.onCreate()
locationTracker = LocationTracker(this) { location ->
serviceScope.launch {
sendGeo(
data = GeoData(
lt = location.latitude,
ln = location.longitude,
nick = SettingsPreferences.username
)
)
}
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("Service", "onStartCommand")
createNotificationChannel()
val notification = createNotification()
startForeground(1, notification)
locationTracker.startTracking(5000)
return START_STICKY
}
override fun onDestroy() {
locationTracker.stopTracking()
super.onDestroy()
}
private fun createNotificationChannel() {
Log.d("Service", "createNotificationChannel")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"location_channel",
"Location Tracking",
NotificationManager.IMPORTANCE_LOW
)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
}
}
private fun createNotification(): Notification {
return NotificationCompat.Builder(this, "location_channel")
.setContentTitle("Отслеживание местоположения")
.setContentText("Обновление каждые 5 секунд")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
}
override fun onBind(intent: Intent?): IBinder? = null
}

View File

@ -1,62 +0,0 @@
package ru.risdeveau.geotracker
import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.os.Looper
import android.util.Log
import androidx.core.content.ContextCompat
class LocationTracker(
private val context: Context,
private val onLocationUpdate: (Location) -> Unit
) : LocationListener {
private val locationManager =
context.getSystemService(LOCATION_SERVICE) as LocationManager
@SuppressLint("MissingPermission")
fun startTracking(interval: Long = 5000, minDistance: Float = 0f) {
Log.d("Tracker", "perms: ${hasPermissions()}")
if (!hasPermissions()) return
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
interval,
minDistance,
this,
Looper.getMainLooper()
)
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)?.let {
onLocationUpdate(it)
}
}
fun stopTracking() {
locationManager.removeUpdates(this)
}
override fun onLocationChanged(location: Location) {
onLocationUpdate(location)
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
private fun hasPermissions(): Boolean {
return ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
}

View File

@ -5,27 +5,18 @@
package ru.risdeveau.geotracker
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Done
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
@ -38,21 +29,21 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.core.content.ContextCompat
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.intellij.lang.annotations.JdkConstants
import ru.risdeveau.geotracker.ui.theme.GeoTrackerTheme
import splitties.experimental.ExperimentalSplittiesApi
import splitties.init.appCtx
import splitties.resources.appColor
import splitties.resources.appStr
import kotlin.apply
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
GeoTrackerTheme {
@ -64,20 +55,10 @@ class MainActivity : ComponentActivity() {
var screen by remember { mutableStateOf<Screen>(Screen.Loading) }
when (screen) {
Screen.Main -> {
LaunchedEffect(Unit) {
Log.d("Thread", "Starting...")
startLocationService()
Log.d("Thread", "Started")
}
Text("Hello world")
}
Screen.Main -> TODO()
Screen.Settings -> {
Settings(Modifier.align(Alignment.Center)) {
screen = Screen.Main
}
Settings(Modifier.align(Alignment.Center))
}
Screen.Loading -> {
@ -109,11 +90,10 @@ sealed class Screen {
object Loading : Screen()
}
@OptIn(ExperimentalSplittiesApi::class)
@Composable
fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
var username by remember { mutableStateOf(SettingsPreferences.username) }
var url by remember { mutableStateOf(SettingsPreferences.url) }
fun Settings(modifier: Modifier = Modifier) {
var username by remember { mutableStateOf("") }
var url by remember { mutableStateOf("") }
var urlIsValid by remember { mutableStateOf(false) }
var loading by remember { mutableStateOf(false) }
@ -136,85 +116,13 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
OutlinedTextField(
value = url,
onValueChange = { url = it },
placeholder = {
Text("https://geo.example.com",
style = TextStyle(color = MaterialTheme.colorScheme.onSurfaceVariant)
)
},
placeholder = { Text("https://geo.example.com", style = TextStyle(color = MaterialTheme.colorScheme.onSurfaceVariant)) },
label = { Text(appStr(R.string.server_url)) },
trailingIcon = {
if (loading) CircularProgressIndicator()
else if (urlIsValid) Icon(Icons.Outlined.Done, "Done")
}
trailingIcon = { if (loading) CircularProgressIndicator() }
)
LocationPermissionScreen()
Button({
SettingsPreferences.username = username.trim()
SettingsPreferences.url = url
onConfirm()
}, enabled = urlIsValid
&& !loading
&& username.trim().isNotEmpty()
&& hasLocationPermissions(appCtx)
) {
Button({}, enabled = urlIsValid) {
Text(appStr(R.string.apply))
}
}
}
@Composable
fun LocationPermissionScreen() {
val context = LocalContext.current
val locationPermissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION
)
val hasLocationPermissions = remember {
mutableStateOf(
locationPermissions.all {
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
)
}
val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
hasLocationPermissions.value = permissions.all { it.value }
}
if (hasLocationPermissions.value) {
Button({}, enabled = false) {
Icon(Icons.Outlined.Done, "Done")
Text("Разрешения получены")
}
} else {
Button(onClick = {
permissionLauncher.launch(locationPermissions)
}) {
Text("Получить разрешения")
}
}
}
fun hasLocationPermissions(context: Context): Boolean {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
fun startLocationService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
appCtx.startForegroundService(Intent(appCtx, LocationForegroundService::class.java))
Log.d("startLocationService", "startForegroundService")
} else {
appCtx.startService(Intent(appCtx, LocationForegroundService::class.java))
Log.d("startLocationService", "startService")
}
}

View File

@ -31,7 +31,6 @@ androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-man
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-json = { module = "io.ktor:ktor-client-json", version.ref = "ktor" }
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logbackClassic" }