diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 63ad2eb250..caf1d09fc8 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -5,6 +5,7 @@ import android.support.annotation.DrawableRes import android.support.annotation.StringRes import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper +import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandFormatter import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants @@ -63,7 +64,7 @@ class TelegramSettings(private val app: TelegramApplication) { var staleLocTime = STALE_LOC_VALUES_SEC[STALE_LOC_DEFAULT_INDEX] var locHistoryTime = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] - var appToConnectPackage = OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME + var appToConnectPackage = "" val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref()) @@ -348,6 +349,9 @@ class TelegramSettings(private val app: TelegramApplication) { } return 0 } + + fun getInstalledApps(context: Context) = + values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) } } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index c931ad3ba4..2b05e4b27c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -81,10 +81,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { setOnClickListener { - activity?.packageManager?.getLaunchIntentForPackage(settings.appToConnectPackage) - ?.also { intent -> + val pack = settings.appToConnectPackage + if (AndroidUtils.isAppInstalled(context, pack)) { + activity?.packageManager?.getLaunchIntentForPackage(pack)?.also { intent -> startActivity(intent) } + } else { + chooseOsmAnd() + } } } return mainView @@ -195,6 +199,22 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage stopLocationUpdate() } + private fun chooseOsmAnd() { + val ctx = context ?: return + val installedApps = TelegramSettings.AppConnect.getInstalledApps(ctx) + when { + installedApps.isEmpty() -> showOsmAndMissingDialog() + installedApps.size == 1 -> { + settings.appToConnectPackage = installedApps.first().appPackage + osmandAidlHelper.reconnectOsmand() + updateOpenOsmAndIcon() + } + installedApps.size > 1 -> { + // TODO: open "Choose OsmAnd" dialog + } + } + } + private fun updateOpenOsmAndIcon() { val ic = TelegramSettings.AppConnect.getWhiteIconId(settings.appToConnectPackage) openOsmAndBtn.setCompoundDrawablesWithIntrinsicBounds(ic, 0, 0, 0) @@ -275,7 +295,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } inner class LiveNowListAdapter : RecyclerView.Adapter() { - + private var lastResponseStr = getString(R.string.last_response) + ": " private val menuList = diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index 7c9d308596..b7ccf92ba0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -141,6 +141,9 @@ object AndroidUtils { } fun isAppInstalled(ctx: Context, appPackage: String): Boolean { + if (appPackage.isEmpty()) { + return false + } try { ctx.packageManager.getPackageInfo(appPackage, 0) } catch (e: PackageManager.NameNotFoundException) {