From b4fe7cfdf76692eb2b59b6e6b0ea214e4013b16f Mon Sep 17 00:00:00 2001 From: kapmaurya Date: Sat, 9 Nov 2024 00:02:56 +0530 Subject: [PATCH 1/2] Clean PathTracking --- mifosng-android/src/main/AndroidManifest.xml | 9 +- .../pathtracking/PathTrackingActivity.kt | 53 --- .../pathtracking/PathTrackingService.kt | 308 ------------------ .../adapters/PathTrackingAdapter.kt | 143 -------- .../main/res/navigation/home_nav_graph.xml | 5 +- 5 files changed, 3 insertions(+), 515 deletions(-) delete mode 100644 mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/pathtracking/PathTrackingActivity.kt delete mode 100644 mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/pathtracking/PathTrackingService.kt delete mode 100644 mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/PathTrackingAdapter.kt diff --git a/mifosng-android/src/main/AndroidManifest.xml b/mifosng-android/src/main/AndroidManifest.xml index 3c3233bb295..79cee3283c7 100755 --- a/mifosng-android/src/main/AndroidManifest.xml +++ b/mifosng-android/src/main/AndroidManifest.xml @@ -98,10 +98,7 @@ - + - + -// onUserLocationClick(userLatLngs) -// } - ) - } - } - - private fun onUserLocationClick(userLatLngs: List) { - val uri = if (userLatLngs.isNotEmpty()) { - val originLatLng = userLatLngs[0] - val destinationLatLng = userLatLngs[userLatLngs.size - 1] - "http://maps.google.com/maps?f=d&hl=en&saddr=${originLatLng.lat},${originLatLng.lng}&daddr=${destinationLatLng.lat},${destinationLatLng.lng}" - } else { - // Handle the case when userLatLngs is empty - "" - } - - val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri)) - intent.setClassName( - "com.google.android.apps.maps", "com.google.android.maps.MapsActivity" - ) - startActivity(Intent.createChooser(intent, getString(R.string.start_tracking))) - } -} \ No newline at end of file diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/pathtracking/PathTrackingService.kt b/mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/pathtracking/PathTrackingService.kt deleted file mode 100644 index 9b2b797af48..00000000000 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/pathtracking/PathTrackingService.kt +++ /dev/null @@ -1,308 +0,0 @@ -/* - * This project is licensed under the open source MPL V2. - * See https://github.com/openMF/android-client/blob/master/LICENSE.md - */ -package com.mifos.mifosxdroid.activity.pathtracking - -import android.Manifest -import android.app.NotificationManager -import android.app.PendingIntent -import android.app.Service -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter -import android.content.pm.PackageManager -import android.location.Location -import android.os.Bundle -import android.os.IBinder -import android.util.Log -import android.widget.Toast -import androidx.core.app.ActivityCompat -import androidx.core.app.NotificationCompat -import com.google.android.gms.common.ConnectionResult -import com.google.android.gms.common.api.GoogleApiClient -import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks -import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener -import com.google.android.gms.location.LocationListener -import com.google.android.gms.location.LocationRequest -import com.google.android.gms.location.LocationServices -import com.mifos.core.network.GenericResponse -import com.mifos.core.network.datamanager.DataManagerDataTable -import com.mifos.core.objects.user.UserLatLng -import com.mifos.core.objects.user.UserLocation -import com.mifos.mifosxdroid.R -import com.mifos.utils.Constants -import com.mifos.utils.DateHelper -import com.mifos.utils.DateHelper.getCurrentDateTime -import com.mifos.utils.PrefManager -import rx.Subscriber -import rx.Subscription -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers -import javax.inject.Inject - -/** - * @author fomenkoo - */ -class PathTrackingService : Service(), ConnectionCallbacks, OnConnectionFailedListener, - LocationListener { - private val TAG = PathTrackingService::class.java.simpleName - private val NOTIFICATION = 0 - private var googleApiClient: GoogleApiClient? = null - private var locationRequest: LocationRequest? = null - private lateinit var currentLocation: Location - private var notificationManager: NotificationManager? = null - private var notification: NotificationCompat.Builder? = null - private var notificationReceiver: BroadcastReceiver? = null - private var latLngs: MutableList? = null - private var startTime: String? = null - private var stopTime: String? = null - private var date: String? = null - - @Inject - lateinit var dataManagerDataTable: DataManagerDataTable - private var subscription: Subscription? = null - override fun onCreate() { - super.onCreate() - buildGoogleApiClient() - } - - /** - * Builds a GoogleApiClient. Uses the `#addApi` method to request the - * LocationServices API. - */ - @Synchronized - protected fun buildGoogleApiClient() { - googleApiClient = GoogleApiClient.Builder(this) - .addConnectionCallbacks(this) - .addOnConnectionFailedListener(this) - .addApi(LocationServices.API) - .build() - createLocationRequest() - } - - protected fun createLocationRequest() { - locationRequest = LocationRequest() - locationRequest!!.interval = UPDATE_INTERVAL_IN_MILLISECONDS - locationRequest!!.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS - locationRequest!!.priority = - LocationRequest.PRIORITY_HIGH_ACCURACY - } - - protected fun startLocationUpdates() { - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return - } - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED && ActivityCompat - .checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return - } - googleApiClient?.let { - locationRequest?.let { it1 -> - LocationServices.FusedLocationApi.requestLocationUpdates( - it, it1, this - ) - } - } - } - - private fun stopLocationUpdates() { - googleApiClient?.let { LocationServices.FusedLocationApi.removeLocationUpdates(it, this) } - } - - override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - latLngs = ArrayList() - startTime = getCurrentDateTime(DateHelper.TIME_FORMAT_VALUE) - date = getCurrentDateTime(DateHelper.DATE_FORMAT_VALUE) - googleApiClient!!.connect() - startNotification() - createNotificationReceiver() - return START_STICKY - } - - override fun onConnected(bundle: Bundle?) { - if (currentLocation == null) { - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED && ActivityCompat - .checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return - } - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED && ActivityCompat - .checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - // TODO: Consider calling - // ActivityCompat#requestPermissions - // here to request the missing permissions, and then overriding - // public void onRequestPermissionsResult(int requestCode, String[] permissions, - // int[] grantResults) - // to handle the case where the user grants the permission. See the documentation - // for ActivityCompat#requestPermissions for more details. - return - } - currentLocation = googleApiClient?.let { - LocationServices.FusedLocationApi.getLastLocation( - it - ) - }!! - latLngs?.add( - UserLatLng( - currentLocation.latitude, - currentLocation.longitude - ) - ) - } - startLocationUpdates() - } - - override fun onConnectionSuspended(i: Int) { - googleApiClient!!.connect() - } - - override fun onLocationChanged(location: Location) { - currentLocation = location - latLngs!!.add(UserLatLng(currentLocation.latitude, currentLocation.longitude)) - } - - override fun onConnectionFailed(connectionResult: ConnectionResult) { - Log.i(TAG, "Connection to location services failed" + connectionResult.errorCode) - } - - override fun onBind(intent: Intent): IBinder? { - return null - } - - fun startNotification() { - notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - notification = NotificationCompat.Builder(this) - .setContentTitle(getString(R.string.mifos_path_tracker)) - .setAutoCancel(false) - .setOngoing(true) - .setContentText(getString(R.string.description_location_tracking)) - .setSmallIcon(R.drawable.ic_launcher) - val resultIntent = Intent() - resultIntent.action = Constants.STOP_TRACKING - val intentBroadCast = PendingIntent.getBroadcast( - this, 0, resultIntent, - PendingIntent.FLAG_IMMUTABLE - ) - notification?.addAction( - R.drawable.ic_assignment_turned_in_black_24dp, - getString(R.string.stop_tracking), intentBroadCast - ) - notification?.setContentIntent(intentBroadCast) - notificationManager!!.notify(NOTIFICATION, notification?.build()) - } - - private fun stopNotification() { - notificationManager!!.cancel(NOTIFICATION) - } - - override fun onDestroy() { - if (subscription != null) subscription!!.unsubscribe() - stopLocationUpdates() - googleApiClient!!.disconnect() - stopNotification() - unregisterReceiver(notificationReceiver) - PrefManager.userStatus = false - stopTime = getCurrentDateTime(DateHelper.TIME_FORMAT_VALUE) - addPathTracking(PrefManager.getUserId(), buildUserLocation()) - super.onDestroy() - } - - private fun createNotificationReceiver() { - notificationReceiver = object : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - val action = intent.action - if (Constants.STOP_TRACKING == action) { - onDestroy() - } - } - } - registerReceiver(notificationReceiver, IntentFilter(Constants.STOP_TRACKING)) - } - - private fun buildUserLocation(): UserLocation { - val userLocation = UserLocation() - userLocation.latlng = latLngs.toString() - userLocation.start_time = startTime - userLocation.stop_time = stopTime - userLocation.date = date - userLocation.user_id = PrefManager.getUserId() - return userLocation - } - - private fun addPathTracking(userId: Int, userLocation: UserLocation?) { - if (subscription != null && !subscription!!.isUnsubscribed) { - subscription!!.unsubscribe() - } - subscription = dataManagerDataTable - .addUserPathTracking(userId, userLocation) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeOn(Schedulers.io()).subscribe( - object : Subscriber() { - override fun onCompleted() {} - override fun onError(e: Throwable) {} - override fun onNext(genericResponse: GenericResponse) { - Toast.makeText( - applicationContext, - getString(R.string.tracks_submitted), - Toast.LENGTH_SHORT - ).show() - } - } - ) - } - - companion object { - const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 10000 - const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2 - } -} \ No newline at end of file diff --git a/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/PathTrackingAdapter.kt b/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/PathTrackingAdapter.kt deleted file mode 100644 index eb9d4d6c205..00000000000 --- a/mifosng-android/src/main/java/com/mifos/mifosxdroid/adapters/PathTrackingAdapter.kt +++ /dev/null @@ -1,143 +0,0 @@ -package com.mifos.mifosxdroid.adapters - -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.TextView -import androidx.cardview.widget.CardView -import androidx.recyclerview.widget.RecyclerView -import com.google.android.gms.maps.CameraUpdateFactory -import com.google.android.gms.maps.GoogleMap -import com.google.android.gms.maps.MapView -import com.google.android.gms.maps.MapsInitializer -import com.google.android.gms.maps.OnMapReadyCallback -import com.google.android.gms.maps.model.LatLng -import com.google.android.gms.maps.model.MarkerOptions -import com.google.android.gms.maps.model.PolylineOptions -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken -import com.mifos.core.objects.user.UserLatLng -import com.mifos.core.objects.user.UserLocation -import com.mifos.mifosxdroid.R - - -class PathTrackingAdapter( - val onUserLocationClick: (UserLocation) -> Unit -) : RecyclerView.Adapter() { - - private var userLocations: List = ArrayList() - private var userLatLngs: List = ArrayList() - - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val viewHolder = ViewHolder( - LayoutInflater.from(parent.context) - .inflate(R.layout.item_pinpoint_location, parent, false) - ) - viewHolder.itemView.setOnClickListener { - if (viewHolder.adapterPosition != RecyclerView.NO_POSITION) - onUserLocationClick(userLocations[viewHolder.adapterPosition]) - } - return viewHolder - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val location = userLocations[position] - userLatLngs = getLatLngList(location.latlng) - holder.tvAddress.text = - location.date + " from " + location.start_time + " to " + location.stop_time - try { - holder.mvUserLocation.tag = userLatLngs[0] - } catch (e: IndexOutOfBoundsException) { - /* Prevents crashing upon calling an item not in the list */ - } - - // Ensure the map has been initialised by the on map ready callback in ViewHolder. - // If it is not ready yet, it will be initialised with the NamedLocation set as its tag - // when the callback is received. - if (holder.map != null) { - // The map is already ready to be used - try { - setMapLocation(holder.map, userLatLngs[0]) - } catch (e: IndexOutOfBoundsException) { - /* Prevents crashing upon calling an item not in the list */ - } - } - } - - fun setPathTracker(userLocations: List) { - this.userLocations = userLocations - notifyDataSetChanged() - } - - fun getItem(position: Int) = userLocations[position] - - - override fun getItemId(i: Int) = 0L - - override fun getItemCount() = userLocations.size - - override fun onViewRecycled(holder: ViewHolder) { - // Cleanup MapView here - if (holder.map != null) { - holder.map!!.clear() - holder.map!!.mapType = GoogleMap.MAP_TYPE_NONE - } - } - - private fun setMapLocation(map: GoogleMap?, location: UserLatLng) { - // Add a marker for this item and set the camera - val polylineOptions = PolylineOptions() - for (userLatLng in userLatLngs) { - polylineOptions.add(LatLng(userLatLng.lat, userLatLng.lng)) - } - map!!.addPolyline(polylineOptions) - val startLatLng = LatLng(location.lat, location.lng) - val stopLatLng = LatLng( - userLatLngs[userLatLngs.size - 1].lat, - userLatLngs[userLatLngs.size - 1].lng - ) - map.moveCamera(CameraUpdateFactory.newLatLngZoom(startLatLng, 13f)) - map.addMarker(MarkerOptions().position(startLatLng)) - map.addMarker(MarkerOptions().position(stopLatLng)) - // Set the map type back to normal. - map.mapType = GoogleMap.MAP_TYPE_NORMAL - } - - inner class ViewHolder(val v: View) : RecyclerView.ViewHolder(v), OnMapReadyCallback { - val mvUserLocation: MapView = v.findViewById(R.id.mv_client_location) - val tvAddress: TextView = v.findViewById(R.id.tv_address) - val cardView: CardView = v.findViewById(R.id.card_view) - var map: GoogleMap? = null - - init { - initializeMapView() - } - - override fun onMapReady(googleMap: GoogleMap) { - MapsInitializer.initialize(v.context) - map = googleMap - val data = mvUserLocation.tag as UserLatLng - setMapLocation(map, data) - } - - /** - * Initialises the MapView by calling its lifecycle methods. - */ - fun initializeMapView() { - // Initialise the MapView - mvUserLocation.onCreate(null) - mvUserLocation.onResume() - // Set the map ready callback to receive the GoogleMap object - mvUserLocation.getMapAsync(this) - } - } - - fun getLatLngList(latLngString: String?): List { - val gson = Gson() - return gson.fromJson( - latLngString, - object : TypeToken?>() {}.type - ) - } -} \ No newline at end of file diff --git a/mifosng-android/src/main/res/navigation/home_nav_graph.xml b/mifosng-android/src/main/res/navigation/home_nav_graph.xml index f2f70fb66eb..cfdf9a1e08e 100644 --- a/mifosng-android/src/main/res/navigation/home_nav_graph.xml +++ b/mifosng-android/src/main/res/navigation/home_nav_graph.xml @@ -95,10 +95,7 @@ android:label="CheckerInboxPendingTasksActivity" /> - + Date: Sat, 9 Nov 2024 23:19:46 +0530 Subject: [PATCH 2/2] Clean Path Tracking file with delete xml --- .../drawable-xhdpi/ic_path_tracker_white.xml | 9 ---- .../src/main/res/drawable/ic_path_tracker.xml | 9 ---- .../main/res/layout/activity_path_tracker.xml | 47 ------------------- .../src/main/res/menu/menu_path_track.xml | 18 ------- 4 files changed, 83 deletions(-) delete mode 100644 mifosng-android/src/main/res/drawable-xhdpi/ic_path_tracker_white.xml delete mode 100644 mifosng-android/src/main/res/drawable/ic_path_tracker.xml delete mode 100755 mifosng-android/src/main/res/layout/activity_path_tracker.xml delete mode 100644 mifosng-android/src/main/res/menu/menu_path_track.xml diff --git a/mifosng-android/src/main/res/drawable-xhdpi/ic_path_tracker_white.xml b/mifosng-android/src/main/res/drawable-xhdpi/ic_path_tracker_white.xml deleted file mode 100644 index a56a7786a0b..00000000000 --- a/mifosng-android/src/main/res/drawable-xhdpi/ic_path_tracker_white.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/mifosng-android/src/main/res/drawable/ic_path_tracker.xml b/mifosng-android/src/main/res/drawable/ic_path_tracker.xml deleted file mode 100644 index d579724b28e..00000000000 --- a/mifosng-android/src/main/res/drawable/ic_path_tracker.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/mifosng-android/src/main/res/layout/activity_path_tracker.xml b/mifosng-android/src/main/res/layout/activity_path_tracker.xml deleted file mode 100755 index b8ed9dfcd3c..00000000000 --- a/mifosng-android/src/main/res/layout/activity_path_tracker.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/mifosng-android/src/main/res/menu/menu_path_track.xml b/mifosng-android/src/main/res/menu/menu_path_track.xml deleted file mode 100644 index c060e187832..00000000000 --- a/mifosng-android/src/main/res/menu/menu_path_track.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - \ No newline at end of file