Merge pull request #6517 from osmandapp/TelegramImprovements

Enable service for users locations and fix minor bugs
This commit is contained in:
Alexey 2019-02-05 19:20:33 +03:00 committed by GitHub
commit ec734672e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 333 additions and 162 deletions

View file

@ -129,13 +129,20 @@
android:visibility="gone"
app:typeface="@string/font_roboto_regular" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/list_item_content_margin"
android:scrollbars="vertical"/>
android:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/open_osmand_btn"

View file

@ -107,22 +107,11 @@
</android.support.design.widget.AppBarLayout>
<FrameLayout
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/last_telegram_update_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:gravity="center"
android:paddingTop="@dimen/content_padding_half"
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
android:visibility="gone"
app:typeface="@string/font_roboto_regular" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
@ -131,6 +120,6 @@
android:paddingBottom="@dimen/list_item_content_margin"
android:scrollbars="vertical" />
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

View file

@ -1,11 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/card_bg_color">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -73,7 +78,7 @@
android:ellipsize="end"
android:maxLines="1"
android:text="@string/start_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" />
@ -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" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/share_gpx_title"
@ -513,6 +518,9 @@
app:typeface="@string/font_roboto_regular" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>

View file

@ -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">
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/title"
@ -54,11 +55,47 @@
app:typeface="@string/font_roboto_regular"
tools:text="Share location"/>
<LinearLayout
android:id="@+id/details_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<LinearLayout
android:id="@+id/distance_and_points_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/distance_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginEnd="@dimen/content_padding_small"
android:layout_marginRight="@dimen/content_padding_small"
tools:src="@drawable/ic_action_distance_16dp"
tools:tint="@color/icon_light" />
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/distance_and_points_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height"
app:typeface="@string/font_roboto_regular"
tools:text="213 m" />
</LinearLayout>
<LinearLayout
android:id="@+id/group_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:visibility="gone"
tools:visibility="visible">
@ -77,7 +114,7 @@
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?attr/android:textColorSecondary"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"
app:firstBaselineToTopHeight="@dimen/list_item_baseline_to_top_height"
app:typeface="@string/font_roboto_regular"
@ -85,10 +122,12 @@
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/location_with_descr_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/content_padding_half">
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/location_view_container"

View file

@ -1,4 +1,6 @@
<resources>
<string name="please_update_osmand">Please update OsmAnd to view data on the map</string>
<string name="shared_string_update">Update</string>
<string name="gps_points_in_buffer">sent (%1$d in buffer)</string>
<string name="points_size">%1$d points</string>
<string name="shared_string_date">Date</string>

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<SwipeRefreshLayout>(R.id.swipe_refresh).apply {
setOnRefreshListener {
app.shareLocationHelper.checkNetworkType()
updateList()
isRefreshing = false
}
setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN)
}
openOsmAndBtn = mainView.findViewById<TextView>(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,11 +547,14 @@ 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 (app.isOsmAndInstalled()) {
if (allSelected) {
app.showLocationHelper.showChatMessages(chatId)
} else {
@ -545,6 +562,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
app.showLocationHelper.startShowingLocation()
}
}
} else {
app.showLocationHelper.stopShowingLocation()
if (!allSelected) {

View file

@ -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<TdApi.Message>) {}
@ -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()
}
}

View file

@ -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<View>(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<Switch>(R.id.monitoring_switcher)
val monitoringTv = mainView.findViewById<TextView>(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<View>(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<SwipeRefreshLayout>(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<ListItem>()
val currentUserId = telegramHelper.getCurrentUser()?.id
val ingoingUserLocations = app.locationMessages.getIngoingUserLocations(start, end)
ingoingUserLocations.forEach {
app.locationMessages.getIngoingUserLocations(start, end).forEach {
TelegramUiHelper.userLocationsToChatItem(telegramHelper, it)?.also { chatItem ->
res.add(chatItem)
}
}
adapter.items = sortAdapterItems(res)
}
private fun sortAdapterItems(list: MutableList<ListItem>): MutableList<ListItem> {
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 ""
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 = "$distance (${getString(R.string.points_size, trackData.points)}) $name"
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)

View file

@ -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,20 +76,41 @@ class UserGpxInfoFragment : BaseDialogFragment() {
TelegramUiHelper.setupPhoto(app, mainView.findViewById<ImageView>(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<String>?) {
Toast.makeText(app, warnings?.firstOrNull(), Toast.LENGTH_LONG).show()
}
})
}
}
val iconMap = mainView.findViewById<ImageView>(R.id.gpx_map)
app.osmandAidlHelper.setGpxBitmapCreatedListener(
object : OsmandAidlHelper.GpxBitmapCreatedListener {
override fun onGpxBitmapCreated(bitmap: AGpxBitmap) {
activity?.runOnUiThread {
mainView.findViewById<ImageView>(R.id.gpx_map).setImageDrawable(BitmapDrawable(app.resources, bitmap.bitmap))
iconMap.setImageDrawable(BitmapDrawable(app.resources, bitmap.bitmap))
iconMap.setOnClickListener(openGpxListener)
}
}
})
val backBtn = mainView.findViewById<ImageView>(R.id.back_button)
backBtn.setImageDrawable(uiUtils.getThemedIcon(R.drawable.ic_arrow_back))
backBtn.setOnClickListener {
mainView.findViewById<ImageView>(R.id.back_button).apply {
setImageDrawable(uiUtils.getThemedIcon(R.drawable.ic_arrow_back))
setOnClickListener {
dismiss()
}
}
dateStartBtn = mainView.findViewById<TextView>(R.id.date_start_btn)
timeStartBtn = mainView.findViewById<TextView>(R.id.time_start_btn)
@ -123,31 +149,18 @@ class UserGpxInfoFragment : BaseDialogFragment() {
updateGPXStatisticRow()
mainView.findViewById<ImageView>(R.id.open_in_osmand_icon).setImageResource(R.drawable.ic_logo_osmand_free)
mainView.findViewById<LinearLayout>(R.id.open_in_osmand_btn).apply {
setOnClickListener {
val gpx = gpxFile
if (gpx.path.isNotEmpty()) {
openGpx(gpx.path)
val imageRes = if (app.isOsmAndInstalled()) {
TelegramSettings.AppConnect.getIconId(settings.appToConnectPackage)
} else {
saveCurrentGpxToFile(object :
OsmandLocationUtils.SaveGpxListener {
R.drawable.ic_logo_osmand_free
}
mainView.findViewById<ImageView>(R.id.open_in_osmand_icon).setImageResource(imageRes)
mainView.findViewById<LinearLayout>(R.id.open_in_osmand_btn).setOnClickListener(openGpxListener)
override fun onSavingGpxFinish(path: String) {
openGpx(path)
}
override fun onSavingGpxError(warnings: List<String>?) {
Toast.makeText(app, warnings?.firstOrNull(), Toast.LENGTH_LONG).show()
}
})
}
}
}
mainView.findViewById<ImageView>(R.id.share_gpx_icon).setImageDrawable(uiUtils.getActiveIcon(R.drawable.ic_action_share))
mainView.findViewById<TextView>(R.id.open_in_osmand_title).setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.primary_text_light, R.color.ctrl_light))
mainView.findViewById<TextView>(R.id.share_gpx_title).setTextColor(AndroidUtils.createPressedColorStateList(app, true, R.color.primary_text_light, R.color.ctrl_light))
mainView.findViewById<ImageView>(R.id.share_gpx_icon).setImageDrawable(getShareIcon())
mainView.findViewById<LinearLayout>(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,6 +282,21 @@ class UserGpxInfoFragment : BaseDialogFragment() {
}
private fun updateGPXMap() {
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) {
@ -248,7 +308,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
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)
app.osmandAidlHelper.getBitmapForGpx(gpxUri, dm.density, widthPixels, heightPixels, GPX_TRACK_COLOR)
}
}
}
@ -258,6 +318,7 @@ class UserGpxInfoFragment : BaseDialogFragment() {
}
})
}
}
private fun selectStartDate() {
val dateFromDialog =
@ -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 {

View file

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