Add the ability to choose app to connect

This commit is contained in:
Alex Sytnyk 2018-08-10 15:31:34 +03:00
parent df682bc6f7
commit 27dd23f04b
7 changed files with 84 additions and 37 deletions

View file

@ -79,15 +79,15 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" android:layout_gravity="bottom|center_horizontal"
android:background="@drawable/img_shadow_fab" android:background="@drawable/img_shadow_fab"
android:drawableLeft="@drawable/ic_action_osmand_plus"
android:drawablePadding="@dimen/content_padding_half" android:drawablePadding="@dimen/content_padding_half"
android:drawableStart="@drawable/ic_action_osmand_plus"
android:gravity="center" android:gravity="center"
android:paddingLeft="32dp" android:paddingLeft="32dp"
android:paddingRight="32dp" android:paddingRight="32dp"
android:text="@string/open_osmand" android:text="@string/open_osmand"
android:textColor="@color/white" 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"/>
</FrameLayout> </FrameLayout>

View file

@ -1,6 +1,7 @@
package net.osmand.telegram package net.osmand.telegram
import android.content.Context import android.content.Context
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants 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 STALE_LOC_TIME_KEY = "stale_loc_time"
private const val LOC_HISTORY_TIME_KEY = "loc_history_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 DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id" 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 staleLocTime = STALE_LOC_VALUES_SEC[STALE_LOC_DEFAULT_INDEX]
var locHistoryTime = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] var locHistoryTime = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX]
var appToConnectPackage = OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
init { init {
updatePrefs() updatePrefs()
read() read()
@ -152,6 +157,8 @@ class TelegramSettings(private val app: TelegramApplication) {
edit.putLong(STALE_LOC_TIME_KEY, staleLocTime) edit.putLong(STALE_LOC_TIME_KEY, staleLocTime)
edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime) edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime)
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
edit.apply() edit.apply()
} }
@ -185,6 +192,10 @@ class TelegramSettings(private val app: TelegramApplication) {
staleLocTime = prefs.getLong(STALE_LOC_TIME_KEY, staleLocDef) staleLocTime = prefs.getLong(STALE_LOC_TIME_KEY, staleLocDef)
val locHistoryDef = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX] val locHistoryDef = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX]
locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef) locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef)
appToConnectPackage = prefs.getString(
APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
)
} }
private fun updatePrefs() { private fun updatePrefs() {

View file

@ -1,6 +1,5 @@
package net.osmand.telegram.helpers package net.osmand.telegram.helpers
import android.app.Application
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent 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.StartVideoRecordingParams
import net.osmand.aidl.note.StopRecordingParams import net.osmand.aidl.note.StopRecordingParams
import net.osmand.aidl.note.TakePhotoNoteParams import net.osmand.aidl.note.TakePhotoNoteParams
import net.osmand.telegram.TelegramApplication
import java.io.File import java.io.File
import java.util.* import java.util.*
class OsmandAidlHelper(private val app: Application) { class OsmandAidlHelper(private val app: TelegramApplication) {
companion object { companion object {
const val OSMAND_FREE_PACKAGE_NAME = "net.osmand" const val OSMAND_FREE_PACKAGE_NAME = "net.osmand"
const val OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus" const val OSMAND_PLUS_PACKAGE_NAME = "net.osmand.plus"
var OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME
private set
} }
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
@ -58,6 +56,8 @@ class OsmandAidlHelper(private val app: Application) {
private var initialized: Boolean = false private var initialized: Boolean = false
private var bound: Boolean = false private var bound: Boolean = false
private var boundPackage = ""
var listener: OsmandHelperListener? = null var listener: OsmandHelperListener? = null
interface OsmandHelperListener { interface OsmandHelperListener {
@ -127,20 +127,21 @@ class OsmandAidlHelper(private val app: Application) {
connectOsmand() connectOsmand()
} }
fun reconnectOsmand() {
if (boundPackage != app.settings.appToConnectPackage) {
cleanupResources()
connectOsmand()
}
}
fun connectOsmand() { fun connectOsmand() {
when { if (bindService(app.settings.appToConnectPackage)) {
bindService(OSMAND_PLUS_PACKAGE_NAME) -> { boundPackage = app.settings.appToConnectPackage
OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME bound = true
bound = true } else {
} boundPackage = ""
bindService(OSMAND_FREE_PACKAGE_NAME) -> { bound = false
OSMAND_PACKAGE_NAME = OSMAND_FREE_PACKAGE_NAME initialized = true
bound = true
}
else -> {
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 { fun importGpxFromUri(gpxUri: Uri, fileName: String, color: String, show: Boolean): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { 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)) return mIOsmAndAidlInterface!!.importGpx(ImportGpxParams(gpxUri, fileName, color, show))
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()
@ -669,7 +670,7 @@ class OsmandAidlHelper(private val app: Application) {
fun navigateGpxFromUri(gpxUri: Uri, force: Boolean): Boolean { fun navigateGpxFromUri(gpxUri: Uri, force: Boolean): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { 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)) return mIOsmAndAidlInterface!!.navigateGpx(NavigateGpxParams(gpxUri, force))
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()

View file

@ -141,7 +141,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
} }
val photoUri = AndroidUtils.getUriForFile(app, File(photoPath)) val photoUri = AndroidUtils.getUriForFile(app, File(photoPath))
app.grantUriPermission( app.grantUriPermission(
OsmandAidlHelper.OSMAND_PACKAGE_NAME, app.settings.appToConnectPackage,
photoUri, photoUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
) )

View file

@ -18,7 +18,6 @@ import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener
import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.TelegramHelper.* import net.osmand.telegram.helpers.TelegramHelper.*
import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem import net.osmand.telegram.helpers.TelegramUiHelper.ChatItem
@ -46,7 +45,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private lateinit var adapter: LiveNowListAdapter private lateinit var adapter: LiveNowListAdapter
private lateinit var locationViewCache: UpdateLocationViewCache private lateinit var locationViewCache: UpdateLocationViewCache
private lateinit var openOsmAndBtn: View private lateinit var openOsmAndBtn: TextView
private var location: Location? = null private var location: Location? = null
private var heading: Float? = null private var heading: Float? = null
@ -78,9 +77,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
(activity as MainActivity).setupOptionsBtn(mainView.findViewById<ImageView>(R.id.options)) (activity as MainActivity).setupOptionsBtn(mainView.findViewById<ImageView>(R.id.options))
openOsmAndBtn = mainView.findViewById<View>(R.id.open_osmand_btn).apply { openOsmAndBtn = mainView.findViewById<TextView>(R.id.open_osmand_btn).apply {
setOnClickListener { setOnClickListener {
activity?.packageManager?.getLaunchIntentForPackage(OsmandAidlHelper.OSMAND_PACKAGE_NAME) activity?.packageManager?.getLaunchIntentForPackage(settings.appToConnectPackage)
?.also { intent -> ?.also { intent ->
startActivity(intent) startActivity(intent)
} }
@ -96,6 +95,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
telegramHelper.addIncomingMessagesListener(this) telegramHelper.addIncomingMessagesListener(this)
telegramHelper.addFullInfoUpdatesListener(this) telegramHelper.addFullInfoUpdatesListener(this)
startLocationUpdate() startLocationUpdate()
updateOpenOsmAndIcon()
} }
override fun onPause() { 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.addLocationListener(this)
app.locationProvider.addCompassListener(this) app.locationProvider.addCompassListener(this)
updateLocationUi() updateLocationUi()
} }
fun stopLocationUpdate() { private fun stopLocationUpdate() {
app.locationProvider.removeLocationListener(this) app.locationProvider.removeLocationListener(this)
app.locationProvider.removeCompassListener(this) app.locationProvider.removeCompassListener(this)
} }

View file

@ -75,8 +75,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
} }
if (pos != -1 && pos != viewPager.currentItem) { if (pos != -1 && pos != viewPager.currentItem) {
when (pos) { when (pos) {
MY_LOCATION_TAB_POS -> liveNowTabFragment?.stopLocationUpdate() MY_LOCATION_TAB_POS -> liveNowTabFragment?.tabClosed()
LIVE_NOW_TAB_POS -> liveNowTabFragment?.startLocationUpdate() LIVE_NOW_TAB_POS -> liveNowTabFragment?.tabOpened()
} }
viewPager.currentItem = pos viewPager.currentItem = pos
return@setOnNavigationItemSelectedListener true return@setOnNavigationItemSelectedListener true

View file

@ -71,9 +71,13 @@ class SettingsDialogFragment : DialogFragment() {
findViewById<TextView>(R.id.title).text = appConn.title findViewById<TextView>(R.id.title).text = appConn.title
if (installed) { if (installed) {
findViewById<View>(R.id.primary_btn).visibility = View.GONE findViewById<View>(R.id.primary_btn).visibility = View.GONE
findViewById<RadioButton>(R.id.radio_button).visibility = View.VISIBLE findViewById<RadioButton>(R.id.radio_button).apply {
visibility = View.VISIBLE
isChecked = pack == settings.appToConnectPackage
}
setOnClickListener { setOnClickListener {
// FIXME settings.appToConnectPackage = appConn.appPackage
app.osmandAidlHelper.reconnectOsmand()
updateSelectedAppConn() updateSelectedAppConn()
} }
} else { } else {
@ -144,9 +148,10 @@ class SettingsDialogFragment : DialogFragment() {
private fun updateSelectedAppConn() { private fun updateSelectedAppConn() {
view?.findViewById<ViewGroup>(R.id.osmand_connect_container)?.apply { view?.findViewById<ViewGroup>(R.id.osmand_connect_container)?.apply {
for (i in 0..childCount) { for (i in 0 until childCount) {
getChildAt(i).apply { getChildAt(i).apply {
// FIXME findViewById<RadioButton>(R.id.radio_button).isChecked =
tag == settings.appToConnectPackage
} }
} }
} }
@ -221,21 +226,37 @@ class SettingsDialogFragment : DialogFragment() {
fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) } fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) }
} }
private enum class AppConnect( enum class AppConnect(
@DrawableRes val iconId: Int, @DrawableRes val iconId: Int,
@DrawableRes val whiteIconId: Int,
val title: String, val title: String,
val appPackage: String val appPackage: String
) { ) {
OSMAND_PLUS( OSMAND_PLUS(
R.drawable.ic_logo_osmand_plus, R.drawable.ic_logo_osmand_plus,
R.drawable.ic_action_osmand_plus,
"OsmAnd+", "OsmAnd+",
OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
), ),
OSMAND_FREE( OSMAND_FREE(
R.drawable.ic_logo_osmand_free, R.drawable.ic_logo_osmand_free,
R.drawable.ic_action_osmand_free,
"OsmAnd", "OsmAnd",
OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME 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 { companion object {