diff --git a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml index 6f8bc78b23..b23ed65e33 100644 --- a/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml +++ b/OsmAnd-telegram/res/layout/fragment_live_now_tab.xml @@ -129,13 +129,20 @@ android:visibility="gone" app:typeface="@string/font_roboto_regular" /> - + android:layout_height="match_parent"> + + + + - - - - + diff --git a/OsmAnd-telegram/res/layout/fragment_user_gpx_info.xml b/OsmAnd-telegram/res/layout/fragment_user_gpx_info.xml index 986c92e5b7..4d7636dc1b 100644 --- a/OsmAnd-telegram/res/layout/fragment_user_gpx_info.xml +++ b/OsmAnd-telegram/res/layout/fragment_user_gpx_info.xml @@ -1,11 +1,16 @@ - + + @@ -89,7 +94,7 @@ android:ellipsize="end" android:maxLines="1" android:text="@string/end_date" - android:textColor="?attr/android:textColorSecondary" + android:textColor="?android:attr/textColorSecondary" android:textSize="@dimen/list_item_description_text_size" app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height" app:typeface="@string/font_roboto_regular" /> @@ -498,8 +503,8 @@ android:id="@+id/share_gpx_icon" android:layout_width="@dimen/list_item_icon_size" android:layout_height="@dimen/list_item_icon_size" - android:src="@drawable/ic_action_share" - android:tint="@color/ctrl_active_light" /> + tools:src="@drawable/ic_action_share" + tools:tint="@color/ctrl_active_light" /> + - \ No newline at end of file + + + \ No newline at end of file diff --git a/OsmAnd-telegram/res/layout/live_now_chat_card.xml b/OsmAnd-telegram/res/layout/live_now_chat_card.xml index 70091f6c0b..a72f3a80fe 100644 --- a/OsmAnd-telegram/res/layout/live_now_chat_card.xml +++ b/OsmAnd-telegram/res/layout/live_now_chat_card.xml @@ -40,7 +40,8 @@ android:layout_marginEnd="@dimen/content_padding_standard" android:layout_marginRight="@dimen/content_padding_standard" android:layout_weight="1" - android:orientation="vertical"> + android:orientation="vertical" + android:paddingBottom="@dimen/content_padding_half"> + android:gravity="center_vertical"> - - - + android:orientation="horizontal" + android:visibility="gone" + tools:visibility="visible"> + + + + + + + + + + + + + + + android:layout_height="wrap_content"> + Please update OsmAnd to view data on the map + Update sent (%1$d in buffer) %1$d points Date diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index 33758e4ad3..dc38030779 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -85,6 +85,10 @@ class TelegramApplication : Application(), OsmandHelperListener { telegramHelper.stopSendingLiveLocationMessages(settings.getChatsShareInfo()) } + fun isAnyOsmAndInstalled() = TelegramSettings.AppConnect.getInstalledApps(this).isNotEmpty() + + fun isOsmAndChosen() = settings.appToConnectPackage.isNotEmpty() + fun isOsmAndInstalled() = AndroidUtils.isAppInstalled(this, settings.appToConnectPackage) val isWifiConnected: Boolean diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index 938067b07e..3b3f3146b3 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -528,7 +528,8 @@ class TelegramSettings(private val app: TelegramApplication) { currentSharingMode = prefs.getString(SHARING_MODE_KEY, "") - appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, "") + val defPackage = if (AppConnect.getInstalledApps(app).size == 1) AppConnect.getInstalledApps(app).first().appPackage else "" + appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, defPackage) liveNowSortType = LiveNowSortType.valueOf( prefs.getString(LIVE_NOW_SORT_TYPE_KEY, LiveNowSortType.SORT_BY_DISTANCE.name) @@ -772,6 +773,16 @@ class TelegramSettings(private val app: TelegramApplication) { return 0 } + @DrawableRes + fun getIconId(appPackage: String): Int { + for (item in values()) { + if (item.appPackage == appPackage) { + return item.iconId + } + } + return 0 + } + fun getInstalledApps(context: Context) = values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 17ee84eec9..74dbec35be 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -301,7 +301,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { } } - private fun checkNetworkType(){ + fun checkNetworkType(){ if (app.isInternetConnectionAvailable) { val networkType = when { app.isWifiConnected -> TdApi.NetworkTypeWiFi() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index e30fe971aa..810347e536 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -158,7 +158,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { fun startShowingLocation() { if (!showingLocation && !forcedStop) { - showingLocation = if (isUseOsmandCallback()) { + showingLocation = if (isUseOsmandCallback() && !app.settings.monitoringEnabled) { osmandAidlHelper.registerForUpdates() } else { app.startUserLocationService() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/notifications/LocationNotification.kt b/OsmAnd-telegram/src/net/osmand/telegram/notifications/LocationNotification.kt index 5e55476462..b41306ebca 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/notifications/LocationNotification.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/notifications/LocationNotification.kt @@ -77,6 +77,6 @@ class LocationNotification(app: TelegramApplication) : TelegramNotification(app, .setStyle(NotificationCompat.BigTextStyle().bigText(notificationText)) } - private fun isShowingChatsNotificationEnabled() = !app.showLocationHelper.isUseOsmandCallback() - && app.isOsmAndInstalled() && app.settings.hasAnyChatToShowOnMap() + private fun isShowingChatsNotificationEnabled() = (!app.showLocationHelper.isUseOsmandCallback() || app.settings.monitoringEnabled) + && app.isAnyOsmAndInstalled() && app.settings.hasAnyChatToShowOnMap() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index 3d99af0a8f..c0a52921a9 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -1,8 +1,10 @@ package net.osmand.telegram.ui import android.content.Intent +import android.graphics.Color import android.os.Bundle import android.support.v4.app.Fragment +import android.support.v4.widget.SwipeRefreshLayout import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.ListPopupWindow import android.support.v7.widget.RecyclerView @@ -11,7 +13,10 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.animation.LinearInterpolator -import android.widget.* +import android.widget.ArrayAdapter +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView import net.osmand.Location import net.osmand.data.LatLon import net.osmand.telegram.R @@ -42,7 +47,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage get() = activity?.application as TelegramApplication private val telegramHelper get() = app.telegramHelper - private val osmandAidlHelper get() = app.osmandAidlHelper private val settings get() = app.settings private lateinit var adapter: LiveNowListAdapter @@ -97,11 +101,19 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage } } + mainView.findViewById(R.id.swipe_refresh).apply { + setOnRefreshListener { + app.shareLocationHelper.checkNetworkType() + updateList() + isRefreshing = false + } + setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN) + } + openOsmAndBtn = mainView.findViewById(R.id.open_osmand_btn).apply { setOnClickListener { - val pack = settings.appToConnectPackage - if (AndroidUtils.isAppInstalled(context, pack)) { - activity?.packageManager?.getLaunchIntentForPackage(pack)?.also { intent -> + if (app.isOsmAndInstalled()) { + activity?.packageManager?.getLaunchIntentForPackage(settings.appToConnectPackage)?.also { intent -> startActivity(intent) } } else { @@ -438,9 +450,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage openOnMapView?.isEnabled = canBeOpenedOnMap if (canBeOpenedOnMap) { openOnMapView?.setOnClickListener { - if (!app.isOsmAndInstalled()) { + if (!app.isAnyOsmAndInstalled()) { showOsmAndMissingDialog() - } else { + } else if (!app.isOsmAndChosen() || (app.isOsmAndChosen() && !app.isOsmAndInstalled())) { + fragmentManager?.also { ChooseOsmAndBottomSheet.showInstance(it, this@LiveNowTabFragment) } + } else if(app.isOsmAndInstalled()){ app.showLocationHelper.showLocationOnMap(item, staleLocation) } } @@ -533,17 +547,21 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage settings.showChatOnMap(chatId, allSelected) if (settings.hasAnyChatToShowOnMap()) { - if (!app.isOsmAndInstalled()) { + if (!app.isAnyOsmAndInstalled()) { if (allSelected) { showOsmAndMissingDialog() } + } else if (!app.isOsmAndChosen() || (app.isOsmAndChosen() && !app.isOsmAndInstalled())) { + fragmentManager?.also { ChooseOsmAndBottomSheet.showInstance(it, this@LiveNowTabFragment) } } else { - if (allSelected) { - app.showLocationHelper.showChatMessages(chatId) - } else { - app.showLocationHelper.hideChatMessages(chatId) + if (app.isOsmAndInstalled()) { + if (allSelected) { + app.showLocationHelper.showChatMessages(chatId) + } else { + app.showLocationHelper.hideChatMessages(chatId) + } + app.showLocationHelper.startShowingLocation() } - app.showLocationHelper.startShowingLocation() } } else { app.showLocationHelper.stopShowingLocation() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 6f6b880ad4..b36a164c01 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -165,7 +165,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene if (AndroidUtils.isLocationPermissionAvailable(this)) { app.locationProvider.resumeAllUpdates() } - if (settings.hasAnyChatToShowOnMap() && !app.isOsmAndInstalled()) { + if (settings.hasAnyChatToShowOnMap() && !app.isAnyOsmAndInstalled()) { showOsmandMissingDialog() } } @@ -290,13 +290,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene if (!app.showLocationHelper.showingLocation && settings.hasAnyChatToShowOnMap()) { app.showLocationHelper.startShowingLocation() } - if (app.telegramService == null) { - messages.forEach { - if (!it.isOutgoing) { - app.locationMessages.addNewLocationMessage(it) - } - } - } } override fun onDeleteChatLocationMessages(chatId: Long, messages: List) {} @@ -455,7 +448,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene settings.stopSharingLocationToChats() app.shareLocationHelper.stopSharingLocation() } - if (settings.hasAnyChatToShowOnMap() && !app.isOsmAndInstalled()) { + if (settings.hasAnyChatToShowOnMap() && !app.isAnyOsmAndInstalled()) { showOsmandMissingDialog() } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt index 06c1dbc0f9..83b7c9b7ea 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt @@ -1,12 +1,15 @@ package net.osmand.telegram.ui +import android.annotation.SuppressLint import android.app.DatePickerDialog +import android.graphics.Color import android.graphics.drawable.Drawable import android.os.Build import android.os.Bundle import android.os.Handler import android.support.annotation.DrawableRes import android.support.v4.app.Fragment +import android.support.v4.widget.SwipeRefreshLayout import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.LayoutInflater @@ -55,8 +58,9 @@ class TimelineTabFragment : Fragment() { mainView = inflater.inflate(R.layout.fragment_timeline_tab, container, false) val appBarLayout = mainView.findViewById(R.id.app_bar_layout) - start = System.currentTimeMillis() - end = System.currentTimeMillis() + val calendar = Calendar.getInstance() + start = getStartOfDay(calendar) + end = getEndOfDay(calendar) AndroidUtils.addStatusBarPadding19v(context!!, appBarLayout) adapter = LiveNowListAdapter() @@ -67,6 +71,7 @@ class TimelineTabFragment : Fragment() { val switcher = mainView.findViewById(R.id.monitoring_switcher) val monitoringTv = mainView.findViewById(R.id.monitoring_title) + switcher.isChecked = settings.monitoringEnabled monitoringTv.setText(if (settings.monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled) mainView.findViewById(R.id.monitoring_container).setOnClickListener { @@ -82,8 +87,18 @@ class TimelineTabFragment : Fragment() { } setCompoundDrawablesWithIntrinsicBounds(getPressedStateIcon(R.drawable.ic_action_date_start), null, null, null) setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.ctrl_active_light, R.color.ctrl_light)) + text = OsmandFormatter.getFormattedDate(start / 1000) } + mainView.findViewById(R.id.swipe_refresh).apply { + setOnRefreshListener { + updateList() + isRefreshing = false + } + setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN) + } + updateList() + return mainView } @@ -101,22 +116,13 @@ class TimelineTabFragment : Fragment() { private fun selectDate() { val dateFromDialog = DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth -> - val from = Calendar.getInstance() - from.set(Calendar.YEAR, year) - from.set(Calendar.MONTH, monthOfYear) - from.set(Calendar.DAY_OF_MONTH, dayOfMonth) + val calendar = Calendar.getInstance() + calendar.set(Calendar.YEAR, year) + calendar.set(Calendar.MONTH, monthOfYear) + calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth) - from.set(Calendar.HOUR_OF_DAY, 0) - from.clear(Calendar.MINUTE) - from.clear(Calendar.SECOND) - from.clear(Calendar.MILLISECOND) - start = from.timeInMillis - - from.set(Calendar.HOUR_OF_DAY, 23) - from.set(Calendar.MINUTE, 59) - from.set(Calendar.SECOND, 59) - from.set(Calendar.MILLISECOND, 999) - end = from.timeInMillis + start = getStartOfDay(calendar) + end = getEndOfDay(calendar) updateList() updateDateButton() @@ -130,6 +136,22 @@ class TimelineTabFragment : Fragment() { ).show() } + private fun getStartOfDay(calendar: Calendar): Long { + calendar.set(Calendar.HOUR_OF_DAY, 0) + calendar.clear(Calendar.MINUTE) + calendar.clear(Calendar.SECOND) + calendar.clear(Calendar.MILLISECOND) + return calendar.timeInMillis + } + + private fun getEndOfDay(calendar: Calendar): Long { + calendar.set(Calendar.HOUR_OF_DAY, 23) + calendar.set(Calendar.MINUTE, 59) + calendar.set(Calendar.SECOND, 59) + calendar.set(Calendar.MILLISECOND, 999) + return calendar.timeInMillis + } + private fun updateDateButton() { dateBtn.text = OsmandFormatter.getFormattedDate(start / 1000) } @@ -157,23 +179,19 @@ class TimelineTabFragment : Fragment() { private fun updateList() { val res = mutableListOf() - val currentUserId = telegramHelper.getCurrentUser()?.id - - val ingoingUserLocations = app.locationMessages.getIngoingUserLocations(start, end) - ingoingUserLocations.forEach { - TelegramUiHelper.userLocationsToChatItem(telegramHelper, it)?.also { chatItem -> - res.add(chatItem) - } + app.locationMessages.getIngoingUserLocations(start, end).forEach { + TelegramUiHelper.userLocationsToChatItem(telegramHelper, it)?.also { chatItem -> + res.add(chatItem) } - + } adapter.items = sortAdapterItems(res) } private fun sortAdapterItems(list: MutableList): MutableList { - val currentUserId = telegramHelper.getCurrentUser()?.id ?: 0 + val currentUserId = telegramHelper.getCurrentUserId() list.sortWith(java.util.Comparator { lhs, rhs -> when (currentUserId) { - lhs.userId -> return@Comparator 1 + lhs.userId -> return@Comparator -1 rhs.userId -> return@Comparator 1 else -> return@Comparator lhs.name.compareTo(rhs.name) } @@ -194,25 +212,34 @@ class TimelineTabFragment : Fragment() { return BaseViewHolder(inflater.inflate(R.layout.live_now_chat_card, parent, false)) } + @SuppressLint("SetTextI18n") override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { val lastItem = position == itemCount - 1 val item = items[position] - val currentUserId = telegramHelper.getCurrentUser()?.id ?: 0 + val currentUserId = telegramHelper.getCurrentUserId() TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, R.drawable.img_user_picture_active, false) holder.title?.text = item.name holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE holder.lastTelegramUpdateTime?.visibility = View.GONE - if (item is TelegramUiHelper.LocationMessagesChatItem ) { + if (item is TelegramUiHelper.LocationMessagesChatItem) { val userLocations = item.userLocations - - if(userLocations!=null){ + if (userLocations != null) { val trackData = getDistanceAndCountedPoints(userLocations) - val distance = OsmandFormatter.getFormattedDistance(trackData.dist,app) - val name = if ((!item.privateChat || item.chatWithBot) && item.userId != currentUserId) item.getVisibleName() else "" - holder.groupDescrContainer?.visibility = View.VISIBLE - holder.groupTitle?.text = "$distance (${getString(R.string.points_size, trackData.points)}) $name" - TelegramUiHelper.setupPhoto(app, holder.groupImage, item.groupPhotoPath, item.placeholderId, false) + val distance = OsmandFormatter.getFormattedDistance(trackData.dist, app) + val groupDescrRowVisible = (!item.privateChat || item.chatWithBot) && item.userId != currentUserId + if (groupDescrRowVisible) { + holder.groupDescrContainer?.visibility = View.VISIBLE + holder.groupTitle?.text = item.getVisibleName() + TelegramUiHelper.setupPhoto(app, holder.groupImage, item.groupPhotoPath, item.placeholderId, false) + } else { + holder.groupDescrContainer?.visibility = View.GONE + } + holder.locationAndDescrContainer?.visibility = View.GONE + holder.distanceAndPointsContainer?.visibility = View.VISIBLE + holder.distanceImage?.setImageDrawable(app.uiUtils.getThemedIcon(R.drawable.ic_action_distance_16dp)) + val point = if (groupDescrRowVisible) " • " else "" + holder.distanceAndPointsTitle?.text = "$distance (${getString(R.string.points_size, trackData.points)}) $point " holder.userRow?.setOnClickListener { childFragmentManager.also { UserGpxInfoFragment.showInstance(it, item.userId, item.chatId, trackData.minTime, trackData.maxTime) @@ -229,11 +256,10 @@ class TimelineTabFragment : Fragment() { private fun getDistanceAndCountedPoints(userLocations: LocationMessages.UserLocations): UITrackData { - var uiTrackData= UITrackData(0.0f, 0, 0, 0) - + val uiTrackData = UITrackData(0.0f, 0, 0, 0) userLocations.getUniqueSegments().forEach { - if(uiTrackData.minTime == 0L) { + if (uiTrackData.minTime == 0L) { uiTrackData.minTime = it.minTime } uiTrackData.dist += it.distance.toFloat(); @@ -253,6 +279,10 @@ class TimelineTabFragment : Fragment() { val lastTelegramUpdateTime: TextView? = view.findViewById(R.id.last_telegram_update_time) val userRow: View? = view.findViewById(R.id.user_row) + val distanceAndPointsContainer: View? = view.findViewById(R.id.distance_and_points_container) + val locationAndDescrContainer: View? = view.findViewById(R.id.location_with_descr_container) + val distanceImage: ImageView? = view.findViewById(R.id.distance_icon) + val distanceAndPointsTitle: TextView? = view.findViewById(R.id.distance_and_points_text) val groupDescrContainer: View? = view.findViewById(R.id.group_container) val groupImage: ImageView? = view.findViewById(R.id.group_icon) val groupTitle: TextView? = view.findViewById(R.id.group_title) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/UserGpxInfoFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/UserGpxInfoFragment.kt index 1f2474ffe7..27c82e681c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/UserGpxInfoFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/UserGpxInfoFragment.kt @@ -6,7 +6,10 @@ import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.graphics.drawable.BitmapDrawable +import android.graphics.drawable.Drawable +import android.os.Build import android.os.Bundle +import android.support.design.widget.Snackbar import android.support.v4.app.FragmentManager import android.util.DisplayMetrics import android.view.LayoutInflater @@ -20,6 +23,7 @@ import android.widget.Toast import net.osmand.PlatformUtil import net.osmand.aidl.gpx.AGpxBitmap import net.osmand.telegram.R +import net.osmand.telegram.TelegramSettings import net.osmand.telegram.helpers.LocationMessages import net.osmand.telegram.helpers.OsmandAidlHelper import net.osmand.telegram.helpers.TelegramUiHelper @@ -37,6 +41,7 @@ class UserGpxInfoFragment : BaseDialogFragment() { private var gpxFile = GPXUtilities.GPXFile() + private lateinit var mainView: View private lateinit var dateStartBtn: TextView private lateinit var timeStartBtn: TextView private lateinit var dateEndBtn: TextView @@ -60,7 +65,7 @@ class UserGpxInfoFragment : BaseDialogFragment() { parent: ViewGroup?, savedInstanceState: Bundle? ): View { - val mainView = inflater.inflate(R.layout.fragment_user_gpx_info, parent) + mainView = inflater.inflate(R.layout.fragment_user_gpx_info, parent) AndroidUtils.addStatusBarPadding19v(context!!, mainView) readFromBundle(savedInstanceState ?: arguments) @@ -71,19 +76,40 @@ class UserGpxInfoFragment : BaseDialogFragment() { TelegramUiHelper.setupPhoto(app, mainView.findViewById(R.id.user_icon), telegramHelper.getUserPhotoPath(user), R.drawable.img_user_placeholder, false) } + val openGpxListener = View.OnClickListener { + val gpx = gpxFile + if (gpx.path.isNotEmpty()) { + openGpx(gpx.path) + } else { + saveCurrentGpxToFile(object : + OsmandLocationUtils.SaveGpxListener { + + override fun onSavingGpxFinish(path: String) { + openGpx(path) + } + + override fun onSavingGpxError(warnings: List?) { + Toast.makeText(app, warnings?.firstOrNull(), Toast.LENGTH_LONG).show() + } + }) + } + } + val iconMap = mainView.findViewById(R.id.gpx_map) app.osmandAidlHelper.setGpxBitmapCreatedListener( object : OsmandAidlHelper.GpxBitmapCreatedListener { override fun onGpxBitmapCreated(bitmap: AGpxBitmap) { activity?.runOnUiThread { - mainView.findViewById(R.id.gpx_map).setImageDrawable(BitmapDrawable(app.resources, bitmap.bitmap)) + iconMap.setImageDrawable(BitmapDrawable(app.resources, bitmap.bitmap)) + iconMap.setOnClickListener(openGpxListener) } } }) - val backBtn = mainView.findViewById(R.id.back_button) - backBtn.setImageDrawable(uiUtils.getThemedIcon(R.drawable.ic_arrow_back)) - backBtn.setOnClickListener { - dismiss() + mainView.findViewById(R.id.back_button).apply { + setImageDrawable(uiUtils.getThemedIcon(R.drawable.ic_arrow_back)) + setOnClickListener { + dismiss() + } } dateStartBtn = mainView.findViewById(R.id.date_start_btn) @@ -123,31 +149,18 @@ class UserGpxInfoFragment : BaseDialogFragment() { updateGPXStatisticRow() - mainView.findViewById(R.id.open_in_osmand_icon).setImageResource(R.drawable.ic_logo_osmand_free) - - mainView.findViewById(R.id.open_in_osmand_btn).apply { - setOnClickListener { - val gpx = gpxFile - if (gpx.path.isNotEmpty()) { - openGpx(gpx.path) - } else { - saveCurrentGpxToFile(object : - OsmandLocationUtils.SaveGpxListener { - - override fun onSavingGpxFinish(path: String) { - openGpx(path) - } - - override fun onSavingGpxError(warnings: List?) { - Toast.makeText(app, warnings?.firstOrNull(), Toast.LENGTH_LONG).show() - } - }) - } - } + val imageRes = if (app.isOsmAndInstalled()) { + TelegramSettings.AppConnect.getIconId(settings.appToConnectPackage) + } else { + R.drawable.ic_logo_osmand_free } + mainView.findViewById(R.id.open_in_osmand_icon).setImageResource(imageRes) + mainView.findViewById(R.id.open_in_osmand_btn).setOnClickListener(openGpxListener) - mainView.findViewById(R.id.share_gpx_icon).setImageDrawable(uiUtils.getActiveIcon(R.drawable.ic_action_share)) + mainView.findViewById(R.id.open_in_osmand_title).setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.primary_text_light, R.color.ctrl_light)) + mainView.findViewById(R.id.share_gpx_title).setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.primary_text_light, R.color.ctrl_light)) + mainView.findViewById(R.id.share_gpx_icon).setImageDrawable(getShareIcon()) mainView.findViewById(R.id.share_gpx_btn).apply { setOnClickListener { val gpx = gpxFile @@ -179,6 +192,18 @@ class UserGpxInfoFragment : BaseDialogFragment() { outState.putLong(END_KEY, endCalendar.timeInMillis) } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + ChooseOsmAndBottomSheet.OSMAND_CHOSEN_REQUEST_CODE -> updateGPXMap() + } + } + + private fun canOsmandCreateBitmap(): Boolean { + val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage) + return version >= MIN_OSMAND_BITMAP_VERSION_CODE + } + private fun openGpx(path: String) { val fileUri = AndroidUtils.getUriForFile(app, File(path)) val openGpxIntent = Intent(Intent.ACTION_VIEW) @@ -210,7 +235,19 @@ class UserGpxInfoFragment : BaseDialogFragment() { textView.setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.ctrl_active_light, R.color.ctrl_light)) } + private fun getShareIcon(): Drawable? { + val normal = app.uiUtils.getActiveIcon(R.drawable.ic_action_share) + if (Build.VERSION.SDK_INT >= 21) { + val active = app.uiUtils.getIcon(R.drawable.ic_action_share, R.color.ctrl_light) + if (normal != null && active != null) { + return AndroidUtils.createPressedStateListDrawable(normal, active) + } + } + return normal + } + private fun updateGpxInfo() { + checkTime() locationMessages = app.locationMessages.getMessagesForUserInChat(userId, chatId, startCalendar.timeInMillis, endCalendar.timeInMillis) gpxFile = OsmandLocationUtils.convertLocationMessagesToGpxFiles(locationMessages).firstOrNull()?:GPXUtilities.GPXFile() @@ -219,6 +256,14 @@ class UserGpxInfoFragment : BaseDialogFragment() { updateGPXMap() } + private fun checkTime() { + if (startCalendar.timeInMillis > endCalendar.timeInMillis) { + val time = startCalendar.timeInMillis + startCalendar.timeInMillis = endCalendar.timeInMillis + endCalendar.timeInMillis = time + } + } + private fun updateDateAndTimeButtons() { dateStartBtn.text = SimpleDateFormat("dd MMM", Locale.getDefault()).format(startCalendar.timeInMillis) dateEndBtn.text = SimpleDateFormat("dd MMM", Locale.getDefault()).format(endCalendar.timeInMillis) @@ -237,26 +282,42 @@ class UserGpxInfoFragment : BaseDialogFragment() { } private fun updateGPXMap() { - saveCurrentGpxToFile(object : - OsmandLocationUtils.SaveGpxListener { - override fun onSavingGpxFinish(path: String) { - val mgr = activity?.getSystemService(Context.WINDOW_SERVICE) - if (mgr != null) { - val dm = DisplayMetrics() - (mgr as WindowManager).defaultDisplay.getMetrics(dm) - val widthPixels = dm.widthPixels - (2 * app.resources.getDimensionPixelSize(R.dimen.content_padding_standard)) - val heightPixels = AndroidUtils.dpToPx(app, 152f) - val gpxUri = AndroidUtils.getUriForFile(app, File(path)) - app.osmandAidlHelper.execOsmandApi { - app.osmandAidlHelper.getBitmapForGpx(gpxUri, dm.density , widthPixels, heightPixels, GPX_TRACK_COLOR) + if (!app.isAnyOsmAndInstalled()) { + activity?.let { + MainActivity.OsmandMissingDialogFragment().show(it.supportFragmentManager, null) + } + } else if (!app.isOsmAndChosen() || (app.isOsmAndChosen() && !app.isOsmAndInstalled())) { + fragmentManager?.also { ChooseOsmAndBottomSheet.showInstance(it, this) } + } else if (!canOsmandCreateBitmap()) { + val snackbar = Snackbar.make(mainView, R.string.please_update_osmand, Snackbar.LENGTH_LONG).setAction(R.string.shared_string_update) { + val packageName = if (app.settings.appToConnectPackage == OsmandAidlHelper.OSMAND_NIGHTLY_PACKAGE_NAME) + OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME else app.settings.appToConnectPackage + startActivity(AndroidUtils.getPlayMarketIntent(app, packageName)) + } + AndroidUtils.setSnackbarTextColor(snackbar, R.color.ctrl_active_dark) + snackbar.show() + } else { + saveCurrentGpxToFile(object : + OsmandLocationUtils.SaveGpxListener { + override fun onSavingGpxFinish(path: String) { + val mgr = activity?.getSystemService(Context.WINDOW_SERVICE) + if (mgr != null) { + val dm = DisplayMetrics() + (mgr as WindowManager).defaultDisplay.getMetrics(dm) + val widthPixels = dm.widthPixels - (2 * app.resources.getDimensionPixelSize(R.dimen.content_padding_standard)) + val heightPixels = AndroidUtils.dpToPx(app, 152f) + val gpxUri = AndroidUtils.getUriForFile(app, File(path)) + app.osmandAidlHelper.execOsmandApi { + app.osmandAidlHelper.getBitmapForGpx(gpxUri, dm.density, widthPixels, heightPixels, GPX_TRACK_COLOR) + } } } - } - override fun onSavingGpxError(warnings: List?) { - log.debug("onSavingGpxError ${warnings?.firstOrNull()}") - } - }) + override fun onSavingGpxError(warnings: List?) { + log.debug("onSavingGpxError ${warnings?.firstOrNull()}") + } + }) + } } private fun selectStartDate() { @@ -314,6 +375,7 @@ class UserGpxInfoFragment : BaseDialogFragment() { private const val CHAT_ID_KEY = "chat_id_key" private const val GPX_TRACK_COLOR = -65536 + private const val MIN_OSMAND_BITMAP_VERSION_CODE = 330 fun showInstance(fm: FragmentManager,userId:Int,chatId:Long, start: Long, end: Long): Boolean { return try { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt index 45f9a4d22e..858278976a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/AndroidUtils.kt @@ -17,6 +17,7 @@ import android.os.Build import android.support.annotation.AttrRes import android.support.annotation.ColorInt import android.support.annotation.ColorRes +import android.support.design.widget.Snackbar import android.support.v4.app.ActivityCompat import android.support.v4.app.DialogFragment import android.support.v4.app.FragmentManager @@ -27,6 +28,7 @@ import android.util.TypedValue.COMPLEX_UNIT_DIP import android.view.View import android.view.WindowManager import android.view.inputmethod.InputMethodManager +import android.widget.TextView import net.osmand.telegram.R import java.io.File @@ -136,6 +138,12 @@ object AndroidUtils { return ctx.resources.getDimensionPixelSize(R.dimen.list_popup_window_height) } + fun setSnackbarTextColor(snackbar: Snackbar, @ColorRes colorId: Int) { + val view = snackbar.view + val tv = view.findViewById(android.support.design.R.id.snackbar_action) as TextView + tv.setTextColor(ContextCompat.getColor(view.context, colorId)) + } + fun createPressedColorStateList( ctx: Context, light: Boolean, @ColorRes lightNormal: Int, @ColorRes lightPressed: Int,