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_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"/>
</FrameLayout>

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -71,9 +71,13 @@ class SettingsDialogFragment : DialogFragment() {
findViewById<TextView>(R.id.title).text = appConn.title
if (installed) {
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 {
// FIXME
settings.appToConnectPackage = appConn.appPackage
app.osmandAidlHelper.reconnectOsmand()
updateSelectedAppConn()
}
} else {
@ -144,9 +148,10 @@ class SettingsDialogFragment : DialogFragment() {
private fun updateSelectedAppConn() {
view?.findViewById<ViewGroup>(R.id.osmand_connect_container)?.apply {
for (i in 0..childCount) {
for (i in 0 until childCount) {
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) }
}
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 {