Choose OsmAnd on "Open OsmAnd" button click in progress

This commit is contained in:
Alex Sytnyk 2018-08-31 16:19:28 +03:00
parent 6a9a30c955
commit ca48340164
3 changed files with 31 additions and 4 deletions

View file

@ -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) }
}
}
}

View file

@ -81,10 +81,14 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
openOsmAndBtn = mainView.findViewById<TextView>(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<BaseViewHolder>() {
private var lastResponseStr = getString(R.string.last_response) + ": "
private val menuList =

View file

@ -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) {