From 53f4e1319f8f273306634e4354000338d3eeb560 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Tue, 10 Jul 2018 17:26:55 +0300 Subject: [PATCH] Use AIDL-API instead of Intents-API for opening OsmAnd --- .../net/osmand/aidl/IOsmAndAidlInterface.aidl | 4 ++ .../maplayer/ShowLayerPointOnMapParams.aidl | 3 + .../maplayer/ShowLayerPointOnMapParams.java | 51 +++++++++++++++ .../telegram/helpers/OsmandAidlHelper.kt | 16 +++-- .../osmand/telegram/helpers/OsmandHelper.kt | 65 ------------------- .../telegram/helpers/ShowLocationHelper.kt | 6 +- .../osmand/telegram/ui/LiveNowTabFragment.kt | 22 +++++-- 7 files changed, 90 insertions(+), 77 deletions(-) create mode 100644 OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.aidl create mode 100644 OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.java delete mode 100644 OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandHelper.kt diff --git a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl index 64410a3f9f..ea79b255bf 100644 --- a/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl +++ b/OsmAnd-telegram/src/net/osmand/aidl/IOsmAndAidlInterface.aidl @@ -51,6 +51,8 @@ import net.osmand.aidl.note.StopRecordingParams; import net.osmand.aidl.gpx.RemoveGpxParams; +import net.osmand.aidl.maplayer.ShowLayerPointOnMapParams; + // NOTE: Add new methods at the end of file!!! interface IOsmAndAidlInterface { @@ -101,4 +103,6 @@ interface IOsmAndAidlInterface { boolean navigateGpx(in NavigateGpxParams params); boolean removeGpx(in RemoveGpxParams params); + + boolean showLayerPointOnMap(in ShowLayerPointOnMapParams params); } \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.aidl b/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.aidl new file mode 100644 index 0000000000..7bb973b2ce --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.aidl @@ -0,0 +1,3 @@ +package net.osmand.aidl.maplayer; + +parcelable ShowLayerPointOnMapParams; \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.java b/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.java new file mode 100644 index 0000000000..f8be2f55dc --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/aidl/maplayer/ShowLayerPointOnMapParams.java @@ -0,0 +1,51 @@ +package net.osmand.aidl.maplayer; + +import android.os.Parcel; +import android.os.Parcelable; + +public class ShowLayerPointOnMapParams implements Parcelable { + + private String layerId; + private String pointId; + + public ShowLayerPointOnMapParams(String layerId, String pointId) { + this.layerId = layerId; + this.pointId = pointId; + } + + public ShowLayerPointOnMapParams(Parcel in) { + layerId = in.readString(); + pointId = in.readString(); + } + + public String getLayerId() { + return layerId; + } + + public String getPointId() { + return pointId; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(layerId); + dest.writeString(pointId); + } + + @Override + public int describeContents() { + return 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public ShowLayerPointOnMapParams createFromParcel(Parcel in) { + return new ShowLayerPointOnMapParams(in); + } + + @Override + public ShowLayerPointOnMapParams[] newArray(int size) { + return new ShowLayerPointOnMapParams[size]; + } + }; +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index 8025520dbb..ff52612fde 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -20,10 +20,7 @@ import net.osmand.aidl.favorite.group.UpdateFavoriteGroupParams import net.osmand.aidl.gpx.* import net.osmand.aidl.map.ALatLon import net.osmand.aidl.map.SetMapLocationParams -import net.osmand.aidl.maplayer.AMapLayer -import net.osmand.aidl.maplayer.AddMapLayerParams -import net.osmand.aidl.maplayer.RemoveMapLayerParams -import net.osmand.aidl.maplayer.UpdateMapLayerParams +import net.osmand.aidl.maplayer.* import net.osmand.aidl.maplayer.point.AMapPoint import net.osmand.aidl.maplayer.point.AddMapPointParams import net.osmand.aidl.maplayer.point.RemoveMapPointParams @@ -524,6 +521,17 @@ class OsmandAidlHelper(private val app: Application) { return false } + fun showLayerPointOnMap(layerId: String, pointId: String): Boolean { + if (mIOsmAndAidlInterface != null) { + try { + return mIOsmAndAidlInterface!!.showLayerPointOnMap(ShowLayerPointOnMapParams(layerId, pointId)) + } catch (e: RemoteException) { + e.printStackTrace() + } + } + return false + } + /** * Add point to user layer. * diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandHelper.kt deleted file mode 100644 index 11790e789a..0000000000 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandHelper.kt +++ /dev/null @@ -1,65 +0,0 @@ -package net.osmand.telegram.helpers - -import android.content.Intent -import android.net.Uri -import android.support.v4.app.FragmentActivity -import net.osmand.telegram.helpers.TelegramUiHelper.ListItem -import net.osmand.telegram.ui.MainActivity -import net.osmand.telegram.utils.AndroidUtils - -object OsmandHelper { - - private const val PREFIX = "osmand.api://" - - private const val API_CMD_SHOW_LOCATION = "show_location" - - private const val PARAM_LAT = "lat" - private const val PARAM_LON = "lon" - - private const val PARAM_AMAP_LAYER_ID = "amap_layer_id" - private const val PARAM_AMAP_POINT_ID = "amap_point_id" - - fun showUserOnMap(activity: FragmentActivity?, listItem: ListItem) { - if (listItem.canBeOpenedOnMap()) { - showLocationPointOnMap( - activity, - listItem.latLon?.latitude, - listItem.latLon?.longitude, - listItem.getMapPointId() - ) - } - } - - fun showLocationPointOnMap( - activity: FragmentActivity?, - lat: Double?, - lon: Double?, - pointId: String - ) { - if (activity == null || lat == null || lon == null) { - return - } - val params = mapOf( - PARAM_LAT to lat.toString(), - PARAM_LON to lon.toString(), - PARAM_AMAP_LAYER_ID to MAP_LAYER_ID, - PARAM_AMAP_POINT_ID to pointId - ) - val intent = Intent(Intent.ACTION_VIEW, createUri(API_CMD_SHOW_LOCATION, params)) - if (AndroidUtils.isIntentSafe(activity, intent)) { - activity.startActivity(intent) - } else { - MainActivity.OsmandMissingDialogFragment().show(activity.supportFragmentManager, null) - } - } - - private fun createUri(command: String, params: Map): Uri { - val sb = StringBuilder(PREFIX).append(command) - if (params.isNotEmpty()) { - sb.append("?") - params.forEach { (key, value) -> sb.append("$key=$value&") } - sb.delete(sb.length - 1, sb.length) - } - return Uri.parse(sb.toString()) - } -} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 35354e9547..f11a53699c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -12,10 +12,12 @@ import net.osmand.telegram.utils.AndroidUtils import org.drinkless.td.libcore.telegram.TdApi import java.io.File -const val MAP_LAYER_ID = "telegram_layer" - class ShowLocationHelper(private val app: TelegramApplication) { + companion object { + const val MAP_LAYER_ID = "telegram_layer" + } + private val telegramHelper = app.telegramHelper private val osmandAidlHelper = app.osmandAidlHelper diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 37536f8552..a9a59a7a91 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -18,7 +18,7 @@ import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener -import net.osmand.telegram.helpers.OsmandHelper +import net.osmand.telegram.helpers.ShowLocationHelper import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem @@ -202,6 +202,12 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage return res } + private fun showOsmAndMissingDialog() { + activity?.let { + MainActivity.OsmandMissingDialogFragment().show(it.supportFragmentManager, null) + } + } + inner class LiveNowListAdapter : RecyclerView.Adapter() { private val menuList = @@ -242,7 +248,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { - OsmandHelper.showUserOnMap(activity, item) + if (osmandAidlHelper.isOsmandNotInstalled()) { + showOsmAndMissingDialog() + } else { + osmandAidlHelper.showLayerPointOnMap( + ShowLocationHelper.MAP_LAYER_ID, + item.getMapPointId() + ) + } } } else { openOnMapView?.setOnClickListener(null) @@ -303,10 +316,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage if (settings.hasAnyChatToShowOnMap()) { if (osmandAidlHelper.isOsmandNotInstalled()) { if (allSelected) { - activity?.let { - MainActivity.OsmandMissingDialogFragment() - .show(it.supportFragmentManager, null) - } + showOsmAndMissingDialog() } } else { if (allSelected) {