From 27dd23f04b77756321aab5a809b8e2242f678d53 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 10 Aug 2018 15:31:34 +0300 Subject: [PATCH] Add the ability to choose app to connect --- .../res/layout/fragment_live_now_tab.xml | 6 +-- .../net/osmand/telegram/TelegramSettings.kt | 11 ++++++ .../telegram/helpers/OsmandAidlHelper.kt | 39 ++++++++++--------- .../telegram/helpers/ShowLocationHelper.kt | 2 +- .../osmand/telegram/ui/LiveNowTabFragment.kt | 26 ++++++++++--- .../net/osmand/telegram/ui/MainActivity.kt | 4 +- .../telegram/ui/SettingsDialogFragment.kt | 33 +++++++++++++--- 7 files changed, 84 insertions(+), 37 deletions(-) diff --git a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml index f2a321e97e..9ae21e679d 100644 --- a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml +++ b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml @@ -79,15 +79,15 @@ android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="@drawable/img_shadow_fab" - android:drawableLeft="@drawable/ic_action_osmand_plus" android:drawablePadding="@dimen/content_padding_half" - android:drawableStart="@drawable/ic_action_osmand_plus" android:gravity="center" android:paddingLeft="32dp" android:paddingRight="32dp" android:text="@string/open_osmand" android:textColor="@color/white" - app:typeface="@string/font_roboto_medium"/> + app:typeface="@string/font_roboto_medium" + tools:drawableLeft="@drawable/ic_action_osmand_plus" + tools:drawableStart="@drawable/ic_action_osmand_plus"/> diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index a76ec64404..e9cbae6932 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -1,6 +1,7 @@ package net.osmand.telegram import android.content.Context +import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants @@ -38,6 +39,8 @@ private const val SEND_MY_LOC_INTERVAL_KEY = "send_my_loc_interval" private const val STALE_LOC_TIME_KEY = "stale_loc_time" private const val LOC_HISTORY_TIME_KEY = "loc_history_time" +private const val APP_TO_CONNECT_PACKAGE_KEY = "app_to_connect_package" + private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id" @@ -56,6 +59,8 @@ 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 + init { updatePrefs() read() @@ -152,6 +157,8 @@ class TelegramSettings(private val app: TelegramApplication) { edit.putLong(STALE_LOC_TIME_KEY, staleLocTime) edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime) + edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage) + edit.apply() } @@ -185,6 +192,10 @@ class TelegramSettings(private val app: TelegramApplication) { staleLocTime = prefs.getLong(STALE_LOC_TIME_KEY, staleLocDef) val locHistoryDef = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef) + + appToConnectPackage = prefs.getString( + APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME + ) } private fun updatePrefs() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index 577903debc..811b1bc1fb 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -1,6 +1,5 @@ package net.osmand.telegram.helpers -import android.app.Application import android.content.ComponentName import android.content.Context import android.content.Intent @@ -41,16 +40,15 @@ import net.osmand.aidl.note.StartAudioRecordingParams import net.osmand.aidl.note.StartVideoRecordingParams import net.osmand.aidl.note.StopRecordingParams import net.osmand.aidl.note.TakePhotoNoteParams +import net.osmand.telegram.TelegramApplication import java.io.File import java.util.* -class OsmandAidlHelper(private val app: Application) { +class OsmandAidlHelper(private val app: TelegramApplication) { companion object { const val OSMAND_FREE_PACKAGE_NAME = "net.osmand" const val OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus" - var OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME - private set } private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null @@ -58,6 +56,8 @@ class OsmandAidlHelper(private val app: Application) { private var initialized: Boolean = false private var bound: Boolean = false + private var boundPackage = "" + var listener: OsmandHelperListener? = null interface OsmandHelperListener { @@ -127,20 +127,21 @@ class OsmandAidlHelper(private val app: Application) { connectOsmand() } + fun reconnectOsmand() { + if (boundPackage != app.settings.appToConnectPackage) { + cleanupResources() + connectOsmand() + } + } + fun connectOsmand() { - when { - bindService(OSMAND_PLUS_PACKAGE_NAME) -> { - OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME - bound = true - } - bindService(OSMAND_FREE_PACKAGE_NAME) -> { - OSMAND_PACKAGE_NAME = OSMAND_FREE_PACKAGE_NAME - bound = true - } - else -> { - bound = false - initialized = true - } + if (bindService(app.settings.appToConnectPackage)) { + boundPackage = app.settings.appToConnectPackage + bound = true + } else { + boundPackage = "" + bound = false + initialized = true } } @@ -656,7 +657,7 @@ class OsmandAidlHelper(private val app: Application) { fun importGpxFromUri(gpxUri: Uri, fileName: String, color: String, show: Boolean): Boolean { if (mIOsmAndAidlInterface != null) { try { - app.grantUriPermission(OSMAND_PACKAGE_NAME, gpxUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + app.grantUriPermission(app.settings.appToConnectPackage, gpxUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) return mIOsmAndAidlInterface!!.importGpx(ImportGpxParams(gpxUri, fileName, color, show)) } catch (e: RemoteException) { e.printStackTrace() @@ -669,7 +670,7 @@ class OsmandAidlHelper(private val app: Application) { fun navigateGpxFromUri(gpxUri: Uri, force: Boolean): Boolean { if (mIOsmAndAidlInterface != null) { try { - app.grantUriPermission(OSMAND_PACKAGE_NAME, gpxUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + app.grantUriPermission(app.settings.appToConnectPackage, gpxUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) return mIOsmAndAidlInterface!!.navigateGpx(NavigateGpxParams(gpxUri, force)) } catch (e: RemoteException) { e.printStackTrace() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 37762b0488..7fc019a9d6 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -141,7 +141,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { } val photoUri = AndroidUtils.getUriForFile(app, File(photoPath)) app.grantUriPermission( - OsmandAidlHelper.OSMAND_PACKAGE_NAME, + app.settings.appToConnectPackage, photoUri, Intent.FLAG_GRANT_READ_URI_PERMISSION ) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 24fd869b07..80911b0b59 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -18,7 +18,6 @@ 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.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem @@ -46,7 +45,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage private lateinit var adapter: LiveNowListAdapter private lateinit var locationViewCache: UpdateLocationViewCache - private lateinit var openOsmAndBtn: View + private lateinit var openOsmAndBtn: TextView private var location: Location? = null private var heading: Float? = null @@ -78,9 +77,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage (activity as MainActivity).setupOptionsBtn(mainView.findViewById(R.id.options)) - openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { + openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { setOnClickListener { - activity?.packageManager?.getLaunchIntentForPackage(OsmandAidlHelper.OSMAND_PACKAGE_NAME) + activity?.packageManager?.getLaunchIntentForPackage(settings.appToConnectPackage) ?.also { intent -> startActivity(intent) } @@ -96,6 +95,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage telegramHelper.addIncomingMessagesListener(this) telegramHelper.addFullInfoUpdatesListener(this) startLocationUpdate() + updateOpenOsmAndIcon() } override fun onPause() { @@ -184,13 +184,27 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } } - fun startLocationUpdate() { + fun tabOpened() { + startLocationUpdate() + updateOpenOsmAndIcon() + } + + fun tabClosed() { + stopLocationUpdate() + } + + private fun updateOpenOsmAndIcon() { + val ic = SettingsDialogFragment.AppConnect.getWhiteIconId(settings.appToConnectPackage) + openOsmAndBtn.setCompoundDrawablesWithIntrinsicBounds(ic, 0, 0, 0) + } + + private fun startLocationUpdate() { app.locationProvider.addLocationListener(this) app.locationProvider.addCompassListener(this) updateLocationUi() } - fun stopLocationUpdate() { + private fun stopLocationUpdate() { app.locationProvider.removeLocationListener(this) app.locationProvider.removeCompassListener(this) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 9857eea09c..9487d8ab08 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -75,8 +75,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } if (pos != -1 && pos != viewPager.currentItem) { when (pos) { - MY_LOCATION_TAB_POS -> liveNowTabFragment?.stopLocationUpdate() - LIVE_NOW_TAB_POS -> liveNowTabFragment?.startLocationUpdate() + MY_LOCATION_TAB_POS -> liveNowTabFragment?.tabClosed() + LIVE_NOW_TAB_POS -> liveNowTabFragment?.tabOpened() } viewPager.currentItem = pos return@setOnNavigationItemSelectedListener true diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 4050faef84..98eb46bfbb 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -71,9 +71,13 @@ class SettingsDialogFragment : DialogFragment() { findViewById(R.id.title).text = appConn.title if (installed) { findViewById(R.id.primary_btn).visibility = View.GONE - findViewById(R.id.radio_button).visibility = View.VISIBLE + findViewById(R.id.radio_button).apply { + visibility = View.VISIBLE + isChecked = pack == settings.appToConnectPackage + } setOnClickListener { - // FIXME + settings.appToConnectPackage = appConn.appPackage + app.osmandAidlHelper.reconnectOsmand() updateSelectedAppConn() } } else { @@ -144,9 +148,10 @@ class SettingsDialogFragment : DialogFragment() { private fun updateSelectedAppConn() { view?.findViewById(R.id.osmand_connect_container)?.apply { - for (i in 0..childCount) { + for (i in 0 until childCount) { getChildAt(i).apply { - // FIXME + findViewById(R.id.radio_button).isChecked = + tag == settings.appToConnectPackage } } } @@ -221,21 +226,37 @@ class SettingsDialogFragment : DialogFragment() { fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) } } - private enum class AppConnect( + enum class AppConnect( @DrawableRes val iconId: Int, + @DrawableRes val whiteIconId: Int, val title: String, val appPackage: String ) { OSMAND_PLUS( R.drawable.ic_logo_osmand_plus, + R.drawable.ic_action_osmand_plus, "OsmAnd+", OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME ), OSMAND_FREE( R.drawable.ic_logo_osmand_free, + R.drawable.ic_action_osmand_free, "OsmAnd", OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME - ) + ); + + companion object { + + @DrawableRes + fun getWhiteIconId(appPackage: String): Int { + for (item in values()) { + if (item.appPackage == appPackage) { + return item.whiteIconId + } + } + return 0 + } + } } companion object {