commit 99b0b9fd2ae7cdcd36b7155451e0d4b20085a804 Author: David Neuhauss Date: Wed Oct 22 14:37:06 2025 +0200 initial commit - after task of week 2 was completed diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/copilot.data.migration.agent.xml b/.idea/copilot.data.migration.agent.xml new file mode 100644 index 0000000..4ea72a9 --- /dev/null +++ b/.idea/copilot.data.migration.agent.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/copilot.data.migration.ask.xml b/.idea/copilot.data.migration.ask.xml new file mode 100644 index 0000000..7ef04e2 --- /dev/null +++ b/.idea/copilot.data.migration.ask.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/copilot.data.migration.ask2agent.xml b/.idea/copilot.data.migration.ask2agent.xml new file mode 100644 index 0000000..1f2ea11 --- /dev/null +++ b/.idea/copilot.data.migration.ask2agent.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/copilot.data.migration.edit.xml b/.idea/copilot.data.migration.edit.xml new file mode 100644 index 0000000..8648f94 --- /dev/null +++ b/.idea/copilot.data.migration.edit.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/copilotDiffState.xml b/.idea/copilotDiffState.xml new file mode 100644 index 0000000..61b67bd --- /dev/null +++ b/.idea/copilotDiffState.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xml new file mode 100644 index 0000000..91f9558 --- /dev/null +++ b/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..7061a0d --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,61 @@ + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/studiobot.xml b/.idea/studiobot.xml new file mode 100644 index 0000000..539e3b8 --- /dev/null +++ b/.idea/studiobot.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..48e787f --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,69 @@ +plugins { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.kotlin.compose) + id("com.google.devtools.ksp") version "2.0.21-1.0.28" +} + +android { + namespace = "com.example.sumoc_demo" + compileSdk { + version = release(36) + } + + defaultConfig { + applicationId = "com.example.sumoc_demo" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + kotlinOptions { + jvmTarget = "11" + } + buildFeatures { + compose = true + } +} + +dependencies { + implementation(libs.androidx.core.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + implementation(libs.androidx.activity.compose) + implementation(platform(libs.androidx.compose.bom)) + implementation(libs.androidx.compose.ui) + implementation(libs.androidx.compose.ui.graphics) + implementation(libs.androidx.compose.ui.tooling.preview) + implementation(libs.androidx.compose.material3) + implementation("androidx.work:work-runtime-ktx:2.10.5") + implementation("com.google.android.gms:play-services-location:21.2.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3") + implementation("androidx.room:room-runtime:${rootProject.extra["room_version"]}") + ksp("androidx.room:room-compiler:${rootProject.extra["room_version"]}") + implementation("androidx.room:room-ktx:${rootProject.extra["room_version"]}") + implementation("androidx.compose.runtime:runtime-livedata") + implementation("com.google.android.gms:play-services-location:21.3.0") + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.junit) + androidTestImplementation(libs.androidx.espresso.core) + androidTestImplementation(platform(libs.androidx.compose.bom)) + androidTestImplementation(libs.androidx.compose.ui.test.junit4) + debugImplementation(libs.androidx.compose.ui.tooling) + debugImplementation(libs.androidx.compose.ui.test.manifest) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/sumoc_demo/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/example/sumoc_demo/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..4cd5f36 --- /dev/null +++ b/app/src/androidTest/java/com/example/sumoc_demo/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.example.sumoc_demo + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.example.sumoc_demo", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2878c60 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationDao.kt b/app/src/main/java/com/example/sumoc_demo/LocationDao.kt new file mode 100644 index 0000000..1473d2f --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationDao.kt @@ -0,0 +1,19 @@ +package com.example.sumoc_demo + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import kotlinx.coroutines.flow.Flow + +@Dao +interface LocationDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + suspend fun insert(location: LocationEntity) + + @Query("SELECT COUNT(*) FROM location_history") + fun getCount(): Flow + + @Query("SELECT * FROM location_history") + fun getAll(): Flow> +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationDatabase.kt b/app/src/main/java/com/example/sumoc_demo/LocationDatabase.kt new file mode 100644 index 0000000..f447268 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationDatabase.kt @@ -0,0 +1,40 @@ +package com.example.sumoc_demo + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +@Database(entities = [LocationEntity::class], version = 2) +abstract class LocationDatabase : RoomDatabase() { + + abstract fun locationDao(): LocationDao + + companion object { + @Volatile + private var INSTANCE: LocationDatabase? = null + + fun getDatabase(context: Context): LocationDatabase { + return INSTANCE ?: synchronized(this) { + val instance = Room.databaseBuilder( + context.applicationContext, + LocationDatabase::class.java, + "location_database" + ) + .addMigrations(MIGRATION_1_2) + .allowMainThreadQueries() + .build() + INSTANCE = instance + instance + } + } + + private val MIGRATION_1_2 = object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + // Since we are just adding new queries to the DAO, there is no need to alter the table. + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationEntity.kt b/app/src/main/java/com/example/sumoc_demo/LocationEntity.kt new file mode 100644 index 0000000..a7428f0 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationEntity.kt @@ -0,0 +1,13 @@ +package com.example.sumoc_demo + +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "location_history") +data class LocationEntity( + @PrimaryKey(autoGenerate = true) + val id: Int = 0, + val latitude: Double, + val longitude: Double, + val timestamp: Long +) \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationService.kt b/app/src/main/java/com/example/sumoc_demo/LocationService.kt new file mode 100644 index 0000000..33bf983 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationService.kt @@ -0,0 +1,142 @@ +package com.example.sumoc_demo + +import android.Manifest +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Intent +import android.content.pm.PackageManager +import android.location.Location +import android.os.Binder +import android.os.Build +import android.os.IBinder +import android.os.Looper +import android.util.Log +import androidx.core.app.ActivityCompat +import androidx.core.app.NotificationCompat +import com.example.sumoc_demo.LocationDatabase +import com.example.sumoc_demo.LocationEntity +import com.example.sumoc_demo.R +import com.google.android.gms.location.FusedLocationProviderClient +import com.google.android.gms.location.LocationCallback +import com.google.android.gms.location.LocationRequest +import com.google.android.gms.location.LocationResult +import com.google.android.gms.location.LocationServices +import com.google.android.gms.location.Priority +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch + +class LocationService : Service() { + private lateinit var locationClient: FusedLocationProviderClient + private lateinit var locationCallback: LocationCallback + private val binder = LocationBinder() + private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) + + inner class LocationBinder : Binder() { + fun getService(): LocationService = this@LocationService + } + + override fun onBind(intent: Intent): IBinder = binder + + override fun onCreate() { + super.onCreate() + Log.d("LocationService", "onCreate called") + locationClient = LocationServices.getFusedLocationProviderClient(this) + setupLocationCallback() + } + + private fun setupLocationCallback() { + locationCallback = object : LocationCallback() { + override fun onLocationResult(result: LocationResult) { + result.lastLocation?.let { location -> + Log.d("LocationService", "Location received: ${location.latitude}, ${location.longitude}") + saveLocation(location) + } + } + } + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + Log.d("LocationService", "Service starting...") + try { + startForeground(1, createNotification()) + startLocationUpdates() + Log.d("LocationService", "Service started") + } catch (e: Exception) { + Log.e("LocationService", "Error starting service", e) + } + return START_STICKY + } + + private fun startLocationUpdates() { + Log.d("LocationService", "startLocationUpdates called") + val locationRequest = LocationRequest.Builder( + Priority.PRIORITY_HIGH_ACCURACY, + 15000L // 15 seconds + ).setMinUpdateIntervalMillis(15000L).build() + + if (ActivityCompat.checkSelfPermission( + this, Manifest.permission.ACCESS_FINE_LOCATION + ) == PackageManager.PERMISSION_GRANTED + ) { + Log.d("LocationService", "Permission granted, requesting location updates") + try { + locationClient.requestLocationUpdates( + locationRequest, + locationCallback, + Looper.getMainLooper() + ) + } catch (e: Exception) { + Log.e("LocationService", "Error requesting location updates", e) + } + } else { + Log.e("LocationService", "Location permission not granted") + } + } + + private fun saveLocation(location: Location) { + Log.d("LocationService", "Saving location to database") + coroutineScope.launch { + val database = LocationDatabase.getDatabase(applicationContext) + database.locationDao().insert( + LocationEntity( + latitude = location.latitude, + longitude = location.longitude, + timestamp = System.currentTimeMillis() + ) + ) + Log.d("LocationService", "Location saved") + } + } + + private fun createNotification(): Notification { + Log.d("LocationService", "Creating notification") + val channelId = "location_service_channel" + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + channelId, + "Location Service", + NotificationManager.IMPORTANCE_LOW + ) + val manager = getSystemService(NotificationManager::class.java) + manager.createNotificationChannel(channel) + } + + return NotificationCompat.Builder(this, channelId) + .setContentTitle("Location Tracking") + .setContentText("Tracking your location") + .setSmallIcon(R.drawable.ic_launcher_foreground) + .build() + } + + override fun onDestroy() { + super.onDestroy() + Log.d("LocationService", "Service destroyed") + locationClient.removeLocationUpdates(locationCallback) + coroutineScope.cancel() + } +} diff --git a/app/src/main/java/com/example/sumoc_demo/LocationViewModel.kt b/app/src/main/java/com/example/sumoc_demo/LocationViewModel.kt new file mode 100644 index 0000000..78cf893 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationViewModel.kt @@ -0,0 +1,38 @@ +package com.example.sumoc_demo + +import android.app.ActivityManager +import android.app.Application +import android.content.Context +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.asLiveData +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers + +class LocationViewModel(application: Application) : AndroidViewModel(application) { + + private val locationDao = LocationDatabase.getDatabase(application).locationDao() + private val _serviceRunning = MutableLiveData() + val serviceRunning: LiveData = _serviceRunning + + // Corrected: Use viewModelScope.coroutineContext to provide the necessary scope + val locationCount: LiveData = locationDao.getCount().asLiveData(viewModelScope.coroutineContext + Dispatchers.IO) + + fun updateServiceStatus(isRunning: Boolean) { + _serviceRunning.value = isRunning + } + + fun checkServiceStatus(): Boolean { + val manager = getApplication().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + @Suppress("DEPRECATION") + for (service in manager.getRunningServices(Integer.MAX_VALUE)) { + if (LocationService::class.java.name == service.service.className) { + _serviceRunning.value = true + return true + } + } + _serviceRunning.value = false + return false + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt b/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt new file mode 100644 index 0000000..664a5a6 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt @@ -0,0 +1,75 @@ +package com.example.sumoc_demo + + +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import android.util.Log +import androidx.core.content.ContextCompat +import androidx.work.CoroutineWorker +import androidx.work.WorkerParameters +import com.google.android.gms.location.LocationServices +import com.google.android.gms.location.Priority +import kotlinx.coroutines.tasks.await +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +class LocationWorker( + private val context: Context, + workerParameters: WorkerParameters +) : CoroutineWorker(context, workerParameters) { + // Get an instance of the Room database + private val locationDao = LocationDatabase.getDatabase(context).locationDao() + + companion object { + const val TAG = "LocationWorker" + } + + override suspend fun doWork(): Result { + Log.d(TAG, "Work starting...") + + // Check for permissions before proceeding + if (ContextCompat.checkSelfPermission( + context, + Manifest.permission.ACCESS_FINE_LOCATION + ) != PackageManager.PERMISSION_GRANTED + ) { + Log.e(TAG, "Location permission not granted. Cannot perform work.") + return Result.failure() + } + + val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) + + return try { + // Use the modern getCurrentLocation() for a single, fresh location update + val location = fusedLocationClient.getCurrentLocation( + Priority.PRIORITY_BALANCED_POWER_ACCURACY, + null + ).await() // This requires the kotlinx-coroutines-play-services library + + location?.let { + val simpleDate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", + Locale.getDefault()) + val formattedDate = simpleDate.format(Date(it.time)) + Log.d(TAG, "Location found: Lat ${it.latitude}, Lon ${it.longitude} at $formattedDate") + + // Save the location to the Room database + val locationEntity = LocationEntity( + latitude = it.latitude, + longitude = it.longitude, + timestamp = it.time + ) + locationDao.insert(locationEntity) + Log.d(TAG, "Location saved to database.") + + } ?: Log.d(TAG, "Location was null.") + + Log.d(TAG, "Work finished successfully.") + Result.success() + } catch (e: Exception) { + Log.e(TAG, "Error getting location", e) + Result.failure() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/MainActivity.kt b/app/src/main/java/com/example/sumoc_demo/MainActivity.kt new file mode 100644 index 0000000..e2291bf --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/MainActivity.kt @@ -0,0 +1,176 @@ +package com.example.sumoc_demo + +import android.Manifest +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.setContent +import androidx.activity.viewModels +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.core.app.ActivityCompat +import androidx.work.ExistingPeriodicWorkPolicy +import androidx.work.PeriodicWorkRequestBuilder +import androidx.work.WorkInfo +import androidx.work.WorkManager +import com.example.sumoc_demo.ui.theme.SuMoC_DemoTheme +import java.util.concurrent.TimeUnit +import kotlin.text.compareTo + +class MainActivity : ComponentActivity() { + + private val viewModel: LocationViewModel by viewModels() + + // Modern way to handle permission requests + private val requestPermissionLauncher = + registerForActivityResult( + ActivityResultContracts.RequestMultiplePermissions() + ) { permissions -> + when { + permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> { + // Fine location access granted. + scheduleLocationWork() + } + permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> { + // Only coarse location access granted. + scheduleLocationWork() + } else -> { + // No location access granted. + // You can show a message to the user here. + } + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + SuMoC_DemoTheme { + val locationCount by viewModel.locationCount.observeAsState(0) + val serviceRunning by viewModel.serviceRunning.observeAsState(false) + Surface( + modifier = Modifier.fillMaxSize(), + color = MaterialTheme.colorScheme.background + ) { + Control( + onStartClick = { requestLocationPermission() }, + onStopClick = { stopLocationWork() }, + locationCount = locationCount, + serviceRunning = serviceRunning + ) + } + } + } + } + + override fun onResume() { + super.onResume() + viewModel.checkServiceStatus() + } + + private fun requestLocationPermission() { + Log.d("MainActivity", "requestLocationPermission called") + val permissionsToRequest = mutableListOf( + Manifest.permission.ACCESS_COARSE_LOCATION, + Manifest.permission.ACCESS_FINE_LOCATION + ) + // Add background location permission for Android 10 (Q) and above + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + permissionsToRequest.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION) + } + requestPermissionLauncher.launch(permissionsToRequest.toTypedArray()) + } + + private fun scheduleLocationWork() { + Log.d("MainActivity", "scheduleLocationWork called") + + // Request notification permission on Android 13+ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (ActivityCompat.checkSelfPermission( + this, + Manifest.permission.POST_NOTIFICATIONS + ) != PackageManager.PERMISSION_GRANTED + ) { + Log.d("MainActivity", "Requesting notification permission") + ActivityCompat.requestPermissions( + this, + arrayOf(Manifest.permission.POST_NOTIFICATIONS), + 101 // Request code + ) + return + } + } + + Log.d("MainActivity", "Starting LocationService") + val intent = Intent(this, LocationService::class.java) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startForegroundService(intent) + } else { + startService(intent) + } + viewModel.updateServiceStatus(true) + } + + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + Log.d("MainActivity", "onRequestPermissionsResult: requestCode=$requestCode, granted=${grantResults.firstOrNull() == PackageManager.PERMISSION_GRANTED}") + if (requestCode == 101 && grantResults.isNotEmpty() && + grantResults[0] == PackageManager.PERMISSION_GRANTED) { + scheduleLocationWork() + } + } + + private fun stopLocationWork() { + Log.d("MainActivity", "Stopping LocationService") + stopService(Intent(this, LocationService::class.java)) + viewModel.updateServiceStatus(false) + } +} + +@Composable +fun Control( + modifier: Modifier = Modifier, onStartClick: () -> Unit, onStopClick: () -> Unit, locationCount: Int, serviceRunning: Boolean ) { + Column( + modifier = modifier.fillMaxSize().padding(16.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Button(onClick = onStartClick) { + Text(stringResource(R.string.start_tracking)) + } + Button(onClick = onStopClick) { + Text(stringResource(R.string.stop_tracking)) + } + Text(text = "Database entries: $locationCount") + Text(text = "Service status: ${if (serviceRunning) "Running" else "Stopped"}") + } +} + +@Preview(showBackground = true) +@Composable +fun DemoAppPreview() { + SuMoC_DemoTheme { + Control(onStartClick = {}, onStopClick = {}, locationCount = 0, serviceRunning = false) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/ui/theme/Color.kt b/app/src/main/java/com/example/sumoc_demo/ui/theme/Color.kt new file mode 100644 index 0000000..270f5fd --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.example.sumoc_demo.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/ui/theme/Theme.kt b/app/src/main/java/com/example/sumoc_demo/ui/theme/Theme.kt new file mode 100644 index 0000000..3ffd964 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/ui/theme/Theme.kt @@ -0,0 +1,58 @@ +package com.example.sumoc_demo.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun SuMoC_DemoTheme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/ui/theme/Type.kt b/app/src/main/java/com/example/sumoc_demo/ui/theme/Type.kt new file mode 100644 index 0000000..6c5b531 --- /dev/null +++ b/app/src/main/java/com/example/sumoc_demo/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.example.sumoc_demo.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.webp b/app/src/main/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/app/src/main/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..915362e --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,5 @@ + + SuMoC_Demo + Start + Stop + \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml new file mode 100644 index 0000000..d8526e8 --- /dev/null +++ b/app/src/main/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +