mirror of
https://github.com/nextcloud/talk-android
synced 2025-07-19 18:55:05 +01:00
show geocoded location on map before sharing
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
96e5e544ea
commit
d7a8935709
@ -267,6 +267,7 @@ class OutcomingLocationMessageViewHolder(incomingView: View) : MessageHolders
|
|||||||
if (!locationGeoLink.isNullOrEmpty()) {
|
if (!locationGeoLink.isNullOrEmpty()) {
|
||||||
val geoLinkWithMarker = addMarkerToGeoLink(locationGeoLink!!)
|
val geoLinkWithMarker = addMarkerToGeoLink(locationGeoLink!!)
|
||||||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(geoLinkWithMarker))
|
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(geoLinkWithMarker))
|
||||||
|
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
context!!.startActivity(browserIntent)
|
context!!.startActivity(browserIntent)
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()
|
Toast.makeText(context, R.string.nc_common_error_sorry, Toast.LENGTH_LONG).show()
|
||||||
|
@ -16,7 +16,6 @@ import android.view.ViewGroup
|
|||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
import android.widget.AdapterView
|
import android.widget.AdapterView
|
||||||
import android.widget.ListView
|
import android.widget.ListView
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.appcompat.widget.SearchView
|
import androidx.appcompat.widget.SearchView
|
||||||
import androidx.core.view.MenuItemCompat
|
import androidx.core.view.MenuItemCompat
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
@ -27,17 +26,11 @@ import com.nextcloud.talk.adapters.GeocodingAdapter
|
|||||||
import com.nextcloud.talk.api.NcApi
|
import com.nextcloud.talk.api.NcApi
|
||||||
import com.nextcloud.talk.application.NextcloudTalkApplication
|
import com.nextcloud.talk.application.NextcloudTalkApplication
|
||||||
import com.nextcloud.talk.controllers.base.BaseController
|
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.bundle.BundleKeys
|
||||||
import com.nextcloud.talk.utils.database.user.UserUtils
|
import com.nextcloud.talk.utils.database.user.UserUtils
|
||||||
import com.nextcloud.talk.utils.preferences.AppPreferences
|
import com.nextcloud.talk.utils.preferences.AppPreferences
|
||||||
import fr.dudie.nominatim.client.JsonNominatimClient
|
import fr.dudie.nominatim.client.JsonNominatimClient
|
||||||
import fr.dudie.nominatim.model.Address
|
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.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Dispatchers.Main
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
@ -84,6 +77,10 @@ class GeocodingController(args: Bundle) : BaseController(args), SearchView.OnQue
|
|||||||
lateinit var adapter: GeocodingAdapter
|
lateinit var adapter: GeocodingAdapter
|
||||||
private var geocodingResults: List<Address> = ArrayList()
|
private var geocodingResults: List<Address> = ArrayList()
|
||||||
|
|
||||||
|
constructor(args: Bundle, listener: LocationPickerController) : this(args) {
|
||||||
|
targetController = listener
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setHasOptionsMenu(true)
|
setHasOptionsMenu(true)
|
||||||
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
|
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 ->
|
geocodingResultListView?.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
|
||||||
val address: Address = adapter.getItem(position) as Address
|
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?) {
|
interface GeocodingResultListener {
|
||||||
val objectId = "geo:$selectedLat,$selectedLon"
|
fun receiveChosenGeocodingResult(lat: Double, lon: Double, name: String)
|
||||||
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() {
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -56,7 +56,8 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AutoInjector(NextcloudTalkApplication::class)
|
@AutoInjector(NextcloudTalkApplication::class)
|
||||||
class LocationPickerController(args: Bundle) : BaseController(args), SearchView.OnQueryTextListener {
|
class LocationPickerController(args: Bundle) : BaseController(args), SearchView.OnQueryTextListener,
|
||||||
|
GeocodingController.GeocodingResultListener {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var ncApi: NcApi
|
lateinit var ncApi: NcApi
|
||||||
@ -84,9 +85,9 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
|
|||||||
@JvmField
|
@JvmField
|
||||||
var shareLocation: LinearLayout? = null
|
var shareLocation: LinearLayout? = null
|
||||||
|
|
||||||
@BindView(R.id.gps_accuracy)
|
@BindView(R.id.place_name)
|
||||||
@JvmField
|
@JvmField
|
||||||
var gpsAccuracy: TextView? = null
|
var placeName: TextView? = null
|
||||||
|
|
||||||
@BindView(R.id.share_location_description)
|
@BindView(R.id.share_location_description)
|
||||||
@JvmField
|
@JvmField
|
||||||
@ -99,6 +100,11 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
|
|||||||
var searchItem: MenuItem? = null
|
var searchItem: MenuItem? = null
|
||||||
var searchView: SearchView? = null
|
var searchView: SearchView? = null
|
||||||
|
|
||||||
|
var receivedChosenGeocodingResult: Boolean = false
|
||||||
|
var geocodedLat: Double = 0.0
|
||||||
|
var geocodedLon: Double = 0.0
|
||||||
|
var geocodedName: String = ""
|
||||||
|
|
||||||
init {
|
init {
|
||||||
setHasOptionsMenu(true)
|
setHasOptionsMenu(true)
|
||||||
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
|
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
|
||||||
@ -132,11 +138,15 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onViewBound(view: View) {
|
override fun onViewBound(view: View) {
|
||||||
setCurrentLocationDescription()
|
setLocationDescription(false, receivedChosenGeocodingResult)
|
||||||
shareLocation?.isClickable = false
|
shareLocation?.isClickable = false
|
||||||
shareLocation?.setOnClickListener {
|
shareLocation?.setOnClickListener {
|
||||||
if (readyToShareLocation) {
|
if (readyToShareLocation) {
|
||||||
shareLocation()
|
shareLocation(
|
||||||
|
map?.mapCenter?.latitude,
|
||||||
|
map?.mapCenter?.longitude,
|
||||||
|
placeName?.text.toString()
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "readyToShareLocation was false while user tried to share location.")
|
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_GEOCODING_QUERY, query)
|
||||||
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)
|
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)
|
||||||
router.pushController(
|
router.pushController(
|
||||||
RouterTransaction.with(GeocodingController(bundle))
|
RouterTransaction.with(GeocodingController(bundle, this))
|
||||||
.pushChangeHandler(HorizontalChangeHandler())
|
.pushChangeHandler(HorizontalChangeHandler())
|
||||||
.popChangeHandler(HorizontalChangeHandler())
|
.popChangeHandler(HorizontalChangeHandler())
|
||||||
)
|
)
|
||||||
@ -205,32 +215,48 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
|
|||||||
map?.overlays?.add(locationOverlay)
|
map?.overlays?.add(locationOverlay)
|
||||||
|
|
||||||
val mapController = map?.controller
|
val mapController = map?.controller
|
||||||
mapController?.setZoom(12.0)
|
|
||||||
|
if (receivedChosenGeocodingResult) {
|
||||||
|
mapController?.setZoom(14.0)
|
||||||
|
} else {
|
||||||
|
mapController?.setZoom(12.0)
|
||||||
|
}
|
||||||
|
|
||||||
var myLocation: GeoPoint
|
var myLocation: GeoPoint
|
||||||
myLocation = GeoPoint(52.0, 13.0)
|
myLocation = GeoPoint(13.0, 52.0)
|
||||||
|
|
||||||
|
var zoomToCurrentPositionAllowed = !receivedChosenGeocodingResult
|
||||||
locationOverlay.runOnFirstFix(Runnable {
|
locationOverlay.runOnFirstFix(Runnable {
|
||||||
activity!!.runOnUiThread {
|
myLocation = locationOverlay.myLocation
|
||||||
myLocation = locationOverlay.myLocation
|
if (zoomToCurrentPositionAllowed) {
|
||||||
mapController?.setCenter(myLocation)
|
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 {
|
btCenterMap?.setOnClickListener(View.OnClickListener {
|
||||||
map?.controller?.animateTo(myLocation)
|
mapController?.animateTo(myLocation)
|
||||||
setCurrentLocationDescription()
|
|
||||||
moveToCurrentLocationWasClicked = true
|
moveToCurrentLocationWasClicked = true
|
||||||
})
|
})
|
||||||
|
|
||||||
map?.addMapListener(DelayedMapListener(object : MapListener {
|
map?.addMapListener(DelayedMapListener(object : MapListener {
|
||||||
override fun onScroll(paramScrollEvent: ScrollEvent): Boolean {
|
override fun onScroll(paramScrollEvent: ScrollEvent): Boolean {
|
||||||
if (moveToCurrentLocationWasClicked) {
|
if (moveToCurrentLocationWasClicked) {
|
||||||
|
setLocationDescription(true, false)
|
||||||
moveToCurrentLocationWasClicked = false
|
moveToCurrentLocationWasClicked = false
|
||||||
|
} else if (receivedChosenGeocodingResult) {
|
||||||
|
shareLocation?.isClickable = true
|
||||||
|
setLocationDescription(false, true)
|
||||||
|
receivedChosenGeocodingResult = false
|
||||||
} else {
|
} else {
|
||||||
shareLocation?.isClickable = true
|
shareLocation?.isClickable = true
|
||||||
shareLocationDescription?.text = "Share this location"
|
setLocationDescription(false, false)
|
||||||
gpsAccuracy?.text = ""
|
|
||||||
}
|
}
|
||||||
readyToShareLocation = true
|
readyToShareLocation = true
|
||||||
return true
|
return true
|
||||||
@ -242,15 +268,24 @@ class LocationPickerController(args: Bundle) : BaseController(args), SearchView.
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setCurrentLocationDescription() {
|
private fun setLocationDescription(isGpsLocation: Boolean, isGeocodedResult: Boolean) {
|
||||||
shareLocationDescription?.text = "Share current location"
|
when {
|
||||||
gpsAccuracy?.text = "Accuracy: xx m"
|
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() {
|
private fun shareLocation(selectedLat: Double?, selectedLon: Double?, name: String?) {
|
||||||
val selectedLat: Double? = map?.mapCenter?.latitude
|
|
||||||
val selectedLon: Double? = map?.mapCenter?.longitude
|
|
||||||
val name = ""
|
|
||||||
val objectId = "geo:$selectedLat,$selectedLon"
|
val objectId = "geo:$selectedLat,$selectedLon"
|
||||||
val metaData: String =
|
val metaData: String =
|
||||||
"{\"type\":\"geo-location\",\"id\":\"geo:$selectedLat,$selectedLon\",\"latitude\":\"$selectedLat\"," +
|
"{\"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")
|
Log.d(TAG, "Permission is revoked")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
} else { //permission is automatically granted on sdk<23 upon installation
|
} else {
|
||||||
Log.d(TAG, "Permission is granted")
|
Log.d(TAG, "Permission is granted")
|
||||||
return true
|
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 {
|
companion object {
|
||||||
private val TAG = "LocationPickerController"
|
private val TAG = "LocationPickerController"
|
||||||
private val REQUEST_PERMISSIONS_REQUEST_CODE = 1
|
private val REQUEST_PERMISSIONS_REQUEST_CODE = 1
|
||||||
|
@ -81,7 +81,7 @@
|
|||||||
android:gravity="center_vertical">
|
android:gravity="center_vertical">
|
||||||
</TextView>
|
</TextView>
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/gps_accuracy"
|
android:id="@+id/place_name"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center_vertical">
|
android:gravity="center_vertical">
|
||||||
|
Loading…
Reference in New Issue
Block a user