-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into registration-deliv…
…ery-component
- Loading branch information
Showing
423 changed files
with
19,063 additions
and
4,740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,7 @@ app.*.map.json | |
/android/app/debug | ||
/android/app/profile | ||
/android/app/release | ||
|
||
# FVM Version Cache | ||
.fvm/ | ||
.fvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 93 additions & 10 deletions
103
...ealth_campaign_field_worker_app/android/app/src/main/kotlin/com/digit/hcm/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,100 @@ | ||
package com.digit.hcm | ||
import android.app.NotificationManager | ||
import android.content.Context | ||
|
||
import io.flutter.embedding.android.FlutterActivity | ||
import android.os.Bundle | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import io.flutter.plugin.common.MethodChannel | ||
import android.content.Intent | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.IntentFilter | ||
import android.os.Environment | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import android.widget.Toast | ||
|
||
class MainActivity : FlutterActivity() { | ||
private val CHANNEL = "com.digit.location_tracker" | ||
private val locationReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
val latitude = intent?.getDoubleExtra("latitude", 0.0) | ||
val longitude = intent?.getDoubleExtra("longitude", 0.0) | ||
// Handle the location data here | ||
Toast.makeText(context, "Latitude: $latitude, Longitude: $longitude", Toast.LENGTH_LONG).show() | ||
// Optionally, you can send this data to Flutter via MethodChannel | ||
flutterEngine?.dartExecutor?.binaryMessenger?.let { | ||
MethodChannel(it, CHANNEL).invokeMethod("locationUpdate", mapOf("latitude" to latitude, "longitude" to longitude)) | ||
} | ||
} | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onResume() { | ||
super.onResume() | ||
closeAllNotifications(); | ||
MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> | ||
when (call.method) { | ||
"startLocationUpdates" -> { | ||
val interval = (call.argument<Number>("interval")?.toLong()) ?: 60000L | ||
val stopAfterTimestamp = (call.argument<Number>("stopAfterTimestamp")?.toLong()) ?: (System.currentTimeMillis() + 60000L) | ||
if (!isMyServiceRunning(LocationService::class.java)) { | ||
startService(interval, stopAfterTimestamp) | ||
} else { | ||
Toast.makeText(this, "Location service is already running", Toast.LENGTH_SHORT).show() | ||
} | ||
result.success(null) | ||
} | ||
"stopLocationUpdates" -> { | ||
stopService() | ||
result.success(null) | ||
} | ||
else -> result.notImplemented() | ||
} | ||
} | ||
|
||
// Register the receiver for location updates | ||
val filter = IntentFilter("LocationUpdate") | ||
registerReceiver(locationReceiver, filter) | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
// Unregister the receiver | ||
unregisterReceiver(locationReceiver) | ||
} | ||
|
||
private fun closeAllNotifications() { | ||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
notificationManager.cancelAll() | ||
private fun startService(locationUpdateInterval: Long, stopAfterTimestamp: Long) { | ||
try { | ||
val serviceIntent = Intent(this, LocationService::class.java).apply { | ||
putExtra("interval", locationUpdateInterval) // Pass the interval to the service | ||
putExtra("stopAfterTimestamp", stopAfterTimestamp) | ||
} | ||
startService(serviceIntent) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
} | ||
private fun stopService() { | ||
try { | ||
val serviceIntent = Intent(this, LocationService::class.java) | ||
Toast.makeText(this, "Stopping location service", Toast.LENGTH_SHORT).show() | ||
stopService(serviceIntent) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
// Check if service is running | ||
private fun isMyServiceRunning(serviceClass: Class<*>): Boolean { | ||
val manager = getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager | ||
for (service in manager.getRunningServices(Int.MAX_VALUE)) { | ||
if (serviceClass.name == service.service.className) { | ||
Toast.makeText(this, "Location service is already running", Toast.LENGTH_SHORT).show() | ||
return true | ||
} | ||
} | ||
Toast.makeText(this, "Location service starting", Toast.LENGTH_SHORT).show() | ||
return false | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
...h_campaign_field_worker_app/android/app/src/main/kotlin/com/digit/hcm/location_service.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package com.digit.hcm | ||
|
||
import android.annotation.TargetApi | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.location.Location | ||
import android.os.Build | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import android.os.IBinder | ||
import android.os.Environment | ||
import android.os.Looper | ||
import android.util.Log | ||
import androidx.core.app.NotificationCompat | ||
import com.google.android.gms.location.* | ||
import java.io.IOException | ||
|
||
class LocationService : Service() { | ||
|
||
private lateinit var fusedLocationClient: FusedLocationProviderClient | ||
private lateinit var locationCallback: LocationCallback | ||
private var locationUpdateInterval: Long = 60000L | ||
private var stopAfterTimestamp: Long = 0L | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) | ||
createNotificationChannel() | ||
startForegroundService() | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
locationUpdateInterval = intent?.getLongExtra("interval", 60000L) ?: 60000L | ||
stopAfterTimestamp = intent?.getLongExtra("stopAfterTimestamp", System.currentTimeMillis() + 60000L) ?: System.currentTimeMillis() + 60000L | ||
startLocationUpdates() | ||
return START_STICKY | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.ECLAIR) | ||
private fun startForegroundService() { | ||
val notificationIntent = Intent(this, MainActivity::class.java) | ||
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) | ||
|
||
val notification = NotificationCompat.Builder(this, CHANNEL_ID) | ||
.setContentTitle("Location Service") | ||
.setContentText("Tracking location in background") | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
.setContentIntent(pendingIntent) | ||
.build() | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
startForeground(1, notification) | ||
} else { | ||
startForeground(1, notification) | ||
} | ||
} | ||
|
||
private fun startLocationUpdates() { | ||
val locationRequest = LocationRequest.create().apply { | ||
interval = locationUpdateInterval | ||
fastestInterval = locationUpdateInterval | ||
priority = LocationRequest.PRIORITY_HIGH_ACCURACY | ||
} | ||
|
||
locationCallback = object : LocationCallback() { | ||
override fun onLocationResult(locationResult: LocationResult) { | ||
locationResult ?: return | ||
for (location in locationResult.locations) { | ||
updateNotification(location) | ||
sendLocationToMainActivity(location) | ||
|
||
// Check if the stop time has been reached | ||
if (System.currentTimeMillis() >= stopAfterTimestamp) { | ||
Log.d("LocationService", "Stop after timestamp reached") | ||
stopSelf() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fusedLocationClient.requestLocationUpdates( | ||
locationRequest, | ||
locationCallback, | ||
Looper.getMainLooper() | ||
) | ||
} | ||
|
||
private fun sendLocationToMainActivity(location: Location) { | ||
val intent = Intent("LocationUpdate") | ||
intent.putExtra("latitude", location.latitude) | ||
intent.putExtra("longitude", location.longitude) | ||
|
||
sendBroadcast(intent) | ||
|
||
Log.d("LocationSharing", "Location sent to MainActivity: Latitude ${location.latitude}, Longitude ${location.longitude}") | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
fusedLocationClient.removeLocationUpdates(locationCallback) | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
return null | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val serviceChannel = NotificationChannel( | ||
CHANNEL_ID, | ||
"Location Service Channel", | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
) | ||
val manager = getSystemService(NotificationManager::class.java) | ||
manager.createNotificationChannel(serviceChannel) | ||
} | ||
} | ||
|
||
fun stopService() { | ||
stopForeground(true) | ||
stopSelf() | ||
Log.d("LocationService", "Service stopped") | ||
} | ||
|
||
private fun updateNotification(location: Location) { | ||
val notificationIntent = Intent(this, MainActivity::class.java) | ||
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE) | ||
val timestamp = System.currentTimeMillis() | ||
val notificationContent = "Latitude: ${location.latitude}, Longitude: ${location.longitude}," | ||
writeToFile("Latitude: ${location.latitude}, Longitude: ${location.longitude}, isSync: false, timestamp: $timestamp") | ||
|
||
val notification = NotificationCompat.Builder(this, CHANNEL_ID) | ||
.setContentTitle("Location Service") | ||
.setContentText(notificationContent) | ||
.setSmallIcon(R.mipmap.ic_launcher) | ||
.setContentIntent(pendingIntent) | ||
.build() | ||
|
||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.notify(1, notification) | ||
} | ||
|
||
private fun writeToFile(data: String) { | ||
try { | ||
val fileName = "location_data.txt" | ||
val file: File | ||
|
||
// Access Downloads folder | ||
val downloadsFolder = File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "") | ||
file = File(downloadsFolder, fileName) | ||
|
||
val fileOutputStream = FileOutputStream(file, true) // Append mode | ||
fileOutputStream.write((data + "\n").toByteArray()) | ||
fileOutputStream.close() | ||
|
||
// Print the file path | ||
println("File saved at: ${file.absolutePath}") | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val CHANNEL_ID = "LocationServiceChannel" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
apps/health_campaign_field_worker_app/lib/blocs/localization/app_localizations_delegate.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.