Răsfoiți Sursa

show geocoded location on map before sharing

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 4 ani în urmă
părinte
comite
d7a8935709

+ 1 - 0
app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt

@@ -267,6 +267,7 @@ class OutcomingLocationMessageViewHolder(incomingView: View) : MessageHolders
         if (!locationGeoLink.isNullOrEmpty()) {
             val geoLinkWithMarker = addMarkerToGeoLink(locationGeoLink!!)
             val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(geoLinkWithMarker))
+            browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
             context!!.startActivity(browserIntent)
         } else {
             Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()

+ 10 - 40
app/src/main/java/com/nextcloud/talk/controllers/GeocodingController.kt

@@ -16,7 +16,6 @@ import android.view.ViewGroup
 import android.view.inputmethod.EditorInfo
 import android.widget.AdapterView
 import android.widget.ListView
-import android.widget.Toast
 import androidx.appcompat.widget.SearchView
 import androidx.core.view.MenuItemCompat
 import androidx.preference.PreferenceManager
@@ -27,17 +26,11 @@ import com.nextcloud.talk.adapters.GeocodingAdapter
 import com.nextcloud.talk.api.NcApi
 import com.nextcloud.talk.application.NextcloudTalkApplication
 import com.nextcloud.talk.controllers.base.BaseController
-import com.nextcloud.talk.models.json.generic.GenericOverall
-import com.nextcloud.talk.utils.ApiUtils
 import com.nextcloud.talk.utils.bundle.BundleKeys
 import com.nextcloud.talk.utils.database.user.UserUtils
 import com.nextcloud.talk.utils.preferences.AppPreferences
 import fr.dudie.nominatim.client.JsonNominatimClient
 import fr.dudie.nominatim.model.Address
-import io.reactivex.Observer
-import io.reactivex.android.schedulers.AndroidSchedulers
-import io.reactivex.disposables.Disposable
-import io.reactivex.schedulers.Schedulers
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.Dispatchers.Main
@@ -84,6 +77,10 @@ class GeocodingController(args: Bundle) : BaseController(args), SearchView.OnQue
     lateinit var adapter: GeocodingAdapter
     private var geocodingResults: List<Address> = ArrayList()
 
+    constructor(args: Bundle, listener: LocationPickerController) : this(args) {
+        targetController = listener
+    }
+
     init {
         setHasOptionsMenu(true)
         NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
@@ -114,7 +111,10 @@ class GeocodingController(args: Bundle) : BaseController(args), SearchView.OnQue
 
         geocodingResultListView?.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
             val address: Address = adapter.getItem(position) as Address
-            shareLocation(address.latitude, address.longitude, address.displayName)
+
+            val listener: GeocodingResultListener? = targetController as GeocodingResultListener?
+            listener?.receiveChosenGeocodingResult(address.latitude, address.longitude, address.displayName)
+            router.popCurrentController()
         }
     }
 
@@ -216,38 +216,8 @@ class GeocodingController(args: Bundle) : BaseController(args), SearchView.OnQue
         }
     }
 
-    private fun shareLocation(selectedLat: Double?, selectedLon: Double?, name : String?) {
-        val objectId = "geo:$selectedLat,$selectedLon"
-        val metaData: String =
-            "{\"type\":\"geo-location\",\"id\":\"geo:$selectedLat,$selectedLon\",\"latitude\":\"$selectedLat\"," +
-                "\"longitude\":\"$selectedLon\",\"name\":\"$name\"}"
-
-        ncApi.sendLocation(
-            ApiUtils.getCredentials(userUtils.currentUser?.username, userUtils.currentUser?.token),
-            ApiUtils.getUrlToSendLocation(userUtils.currentUser?.baseUrl, roomToken),
-            "geo-location",
-            objectId,
-            metaData
-        )
-            .subscribeOn(Schedulers.io())
-            .observeOn(AndroidSchedulers.mainThread())
-            .subscribe(object : Observer<GenericOverall> {
-                override fun onSubscribe(d: Disposable) {
-                }
-
-                override fun onNext(t: GenericOverall) {
-                    router.popCurrentController()
-                }
-
-                override fun onError(e: Throwable) {
-                    Log.e(TAG, "error when trying to share location", e)
-                    Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()
-                    router.popCurrentController()
-                }
-
-                override fun onComplete() {
-                }
-            })
+    interface GeocodingResultListener {
+        fun receiveChosenGeocodingResult(lat: Double, lon: Double, name: String)
     }
 
     companion object {

+ 65 - 23
app/src/main/java/com/nextcloud/talk/controllers/LocationPickerController.kt

@@ -56,7 +56,8 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
 import javax.inject.Inject
 
 @AutoInjector(NextcloudTalkApplication::class)
-class LocationPickerController(args: Bundle) : BaseController(args), SearchView.OnQueryTextListener {
+class LocationPickerController(args: Bundle) : BaseController(args), SearchView.OnQueryTextListener,
+    GeocodingController.GeocodingResultListener {
 
     @Inject
     lateinit var ncApi: NcApi
@@ -84,9 +85,9 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
     @JvmField
     var shareLocation: LinearLayout? = null
 
-    @BindView(R.id.gps_accuracy)
+    @BindView(R.id.place_name)
     @JvmField
-    var gpsAccuracy: TextView? = null
+    var placeName: TextView? = null
 
     @BindView(R.id.share_location_description)
     @JvmField
@@ -99,6 +100,11 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
     var searchItem: MenuItem? = null
     var searchView: SearchView? = null
 
+    var receivedChosenGeocodingResult: Boolean = false
+    var geocodedLat: Double = 0.0
+    var geocodedLon: Double = 0.0
+    var geocodedName: String = ""
+
     init {
         setHasOptionsMenu(true)
         NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
@@ -132,11 +138,15 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
     }
 
     override fun onViewBound(view: View) {
-        setCurrentLocationDescription()
+        setLocationDescription(false, receivedChosenGeocodingResult)
         shareLocation?.isClickable = false
         shareLocation?.setOnClickListener {
             if (readyToShareLocation) {
-                shareLocation()
+                shareLocation(
+                    map?.mapCenter?.latitude,
+                    map?.mapCenter?.longitude,
+                    placeName?.text.toString()
+                )
             } else {
                 Log.d(TAG, "readyToShareLocation was false while user tried to share location.")
             }
@@ -170,7 +180,7 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
             bundle.putString(BundleKeys.KEY_GEOCODING_QUERY, query)
             bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)
             router.pushController(
-                RouterTransaction.with(GeocodingController(bundle))
+                RouterTransaction.with(GeocodingController(bundle, this))
                     .pushChangeHandler(HorizontalChangeHandler())
                     .popChangeHandler(HorizontalChangeHandler())
             )
@@ -205,32 +215,48 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
         map?.overlays?.add(locationOverlay)
 
         val mapController = map?.controller
-        mapController?.setZoom(12.0)
+
+        if (receivedChosenGeocodingResult) {
+            mapController?.setZoom(14.0)
+        } else {
+            mapController?.setZoom(12.0)
+        }
 
         var myLocation: GeoPoint
-        myLocation = GeoPoint(52.0, 13.0)
+        myLocation = GeoPoint(13.0, 52.0)
 
+        var zoomToCurrentPositionAllowed = !receivedChosenGeocodingResult
         locationOverlay.runOnFirstFix(Runnable {
-            activity!!.runOnUiThread {
-                myLocation = locationOverlay.myLocation
-                mapController?.setCenter(myLocation)
+            myLocation = locationOverlay.myLocation
+            if (zoomToCurrentPositionAllowed) {
+                activity!!.runOnUiThread {
+                    mapController?.setZoom(12.0)
+                    mapController?.setCenter(myLocation)
+                }
             }
         })
 
+        if (receivedChosenGeocodingResult && geocodedLat != 0.0 && geocodedLon != 0.0) {
+            mapController?.setCenter(GeoPoint(geocodedLat, geocodedLon))
+        }
+
         btCenterMap?.setOnClickListener(View.OnClickListener {
-            map?.controller?.animateTo(myLocation)
-            setCurrentLocationDescription()
+            mapController?.animateTo(myLocation)
             moveToCurrentLocationWasClicked = true
         })
 
         map?.addMapListener(DelayedMapListener(object : MapListener {
             override fun onScroll(paramScrollEvent: ScrollEvent): Boolean {
                 if (moveToCurrentLocationWasClicked) {
+                    setLocationDescription(true, false)
                     moveToCurrentLocationWasClicked = false
+                } else if (receivedChosenGeocodingResult) {
+                    shareLocation?.isClickable = true
+                    setLocationDescription(false, true)
+                    receivedChosenGeocodingResult = false
                 } else {
                     shareLocation?.isClickable = true
-                    shareLocationDescription?.text = "Share this location"
-                    gpsAccuracy?.text = ""
+                    setLocationDescription(false, false)
                 }
                 readyToShareLocation = true
                 return true
@@ -242,15 +268,24 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
         }))
     }
 
-    private fun setCurrentLocationDescription() {
-        shareLocationDescription?.text = "Share current location"
-        gpsAccuracy?.text = "Accuracy: xx m"
+    private fun setLocationDescription(isGpsLocation: Boolean, isGeocodedResult: Boolean) {
+        when {
+            isGpsLocation -> {
+                shareLocationDescription?.text = "Share current location"
+                placeName?.text = ""
+            }
+            isGeocodedResult -> {
+                shareLocationDescription?.text = "Share this location"
+                placeName?.text = geocodedName
+            }
+            else -> {
+                shareLocationDescription?.text = "Share this location"
+                placeName?.text = ""
+            }
+        }
     }
 
-    private fun shareLocation() {
-        val selectedLat: Double? = map?.mapCenter?.latitude
-        val selectedLon: Double? = map?.mapCenter?.longitude
-        val name = ""
+    private fun shareLocation(selectedLat: Double?, selectedLon: Double?, name: String?) {
         val objectId = "geo:$selectedLat,$selectedLon"
         val metaData: String =
             "{\"type\":\"geo-location\",\"id\":\"geo:$selectedLat,$selectedLon\",\"latitude\":\"$selectedLat\"," +
@@ -297,7 +332,7 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
                 Log.d(TAG, "Permission is revoked")
                 return false
             }
-        } else { //permission is automatically granted on sdk<23 upon installation
+        } else {
             Log.d(TAG, "Permission is granted")
             return true
         }
@@ -320,6 +355,13 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
         }
     }
 
+    override fun receiveChosenGeocodingResult(lat: Double, lon: Double, name: String) {
+        receivedChosenGeocodingResult = true
+        geocodedLat = lat
+        geocodedLon = lon
+        geocodedName = name
+    }
+
     companion object {
         private val TAG = "LocationPickerController"
         private val REQUEST_PERMISSIONS_REQUEST_CODE = 1

+ 1 - 1
app/src/main/res/layout/controller_location.xml

@@ -81,7 +81,7 @@
                 android:gravity="center_vertical">
             </TextView>
             <TextView
-                android:id="@+id/gps_accuracy"
+                android:id="@+id/place_name"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:gravity="center_vertical">