Skip to content

Commit

Permalink
Add openUri extension functions (#5214)
Browse files Browse the repository at this point in the history
* Add openUri extension functions

* Replace setUrlClickListener with extension function
  • Loading branch information
tapetis authored Aug 27, 2023
1 parent b623dcc commit d8589ef
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.westnordost.streetcomplete.quests.max_height

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.core.view.isGone
Expand All @@ -12,6 +10,7 @@ import de.westnordost.streetcomplete.osm.Length
import de.westnordost.streetcomplete.quests.AbstractOsmQuestForm
import de.westnordost.streetcomplete.screens.measure.ArSupportChecker
import de.westnordost.streetcomplete.screens.measure.MeasureContract
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.view.controller.LengthInputViewController
import org.koin.android.ext.android.inject

Expand Down Expand Up @@ -53,7 +52,7 @@ class AddMaxPhysicalHeightForm : AbstractOsmQuestForm<MaxPhysicalHeightAnswer>()
try {
launcher.launch(MeasureContract.Params(lengthUnit, true))
} catch (e: ActivityNotFoundException) {
context?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=de.westnordost.streetmeasure")))
context?.openUri("market://details?id=de.westnordost.streetmeasure")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.westnordost.streetcomplete.quests.width

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
Expand All @@ -15,6 +13,7 @@ import de.westnordost.streetcomplete.osm.hasDubiousRoadWidth
import de.westnordost.streetcomplete.quests.AbstractOsmQuestForm
import de.westnordost.streetcomplete.screens.measure.ArSupportChecker
import de.westnordost.streetcomplete.screens.measure.MeasureContract
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.view.controller.LengthInputViewController
import org.koin.android.ext.android.inject

Expand Down Expand Up @@ -61,7 +60,7 @@ class AddWidthForm : AbstractOsmQuestForm<WidthAnswer>() {
try {
launcher.launch(MeasureContract.Params(lengthUnit, false))
} catch (e: ActivityNotFoundException) {
context?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=de.westnordost.streetmeasure")))
context?.openUri("market://details?id=de.westnordost.streetmeasure")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.westnordost.streetcomplete.screens.about

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AlertDialog
import androidx.core.net.toUri
import androidx.core.widget.TextViewCompat
import androidx.preference.Preference
import de.westnordost.streetcomplete.ApplicationConstants.COPYRIGHT_YEARS
Expand All @@ -16,8 +14,8 @@ import de.westnordost.streetcomplete.databinding.CellLabeledIconSelectRightBindi
import de.westnordost.streetcomplete.databinding.DialogDonateBinding
import de.westnordost.streetcomplete.screens.HasTitle
import de.westnordost.streetcomplete.screens.TwoPaneListFragment
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.ktx.setUpToolbarTitleAndIcon
import de.westnordost.streetcomplete.util.ktx.tryStartActivity
import de.westnordost.streetcomplete.view.ListAdapter
import java.util.Locale

Expand All @@ -35,38 +33,31 @@ class AboutFragment : TwoPaneListFragment(), HasTitle {
findPreference<Preference>("authors")?.summary =
getString(R.string.about_summary_authors, COPYRIGHT_YEARS)

findPreference<Preference>("license")?.setOnPreferenceClickListener {
openUrl("https://www.gnu.org/licenses/gpl-3.0.html")
}

findPreference<Preference>("repository")?.setOnPreferenceClickListener {
openUrl("https://github.com/streetcomplete/StreetComplete/")
}
findPreference<Preference>("license")
?.setClickOpensUrl("https://www.gnu.org/licenses/gpl-3.0.html")
findPreference<Preference>("repository")
?.setClickOpensUrl("https://github.com/streetcomplete/StreetComplete")
findPreference<Preference>("faq")
?.setClickOpensUrl("https://wiki.openstreetmap.org/wiki/StreetComplete/FAQ")

findPreference<Preference>("faq")?.setOnPreferenceClickListener {
openUrl("https://wiki.openstreetmap.org/wiki/StreetComplete/FAQ")
}

findPreference<Preference>("translate")?.summary = resources.getString(
val translatePreference = findPreference<Preference>("translate")
translatePreference?.summary = resources.getString(
R.string.about_description_translate,
Locale.getDefault().displayLanguage,
resources.getInteger(R.integer.translation_completeness)
)
findPreference<Preference>("translate")?.setOnPreferenceClickListener {
openUrl("https://poeditor.com/join/project/IE4GC127Ki")
}
translatePreference?.setClickOpensUrl("https://poeditor.com/join/project/IE4GC127Ki")

findPreference<Preference>("report_error")?.setOnPreferenceClickListener {
openUrl("https://github.com/streetcomplete/StreetComplete/issues/")
}

findPreference<Preference>("give_feedback")?.setOnPreferenceClickListener {
openUrl("https://github.com/streetcomplete/StreetComplete/discussions/")
}
findPreference<Preference>("report_error")
?.setClickOpensUrl("https://github.com/streetcomplete/StreetComplete/issues")
findPreference<Preference>("give_feedback")
?.setClickOpensUrl("https://github.com/streetcomplete/StreetComplete/discussions")

findPreference<Preference>("rate")?.isVisible = isInstalledViaGooglePlay()
findPreference<Preference>("rate")?.setOnPreferenceClickListener {
val ratePreference = findPreference<Preference>("rate")
ratePreference?.isVisible = isInstalledViaGooglePlay()
ratePreference?.setOnPreferenceClickListener {
openGooglePlayStorePage()
true
}

findPreference<Preference>("donate")?.setOnPreferenceClickListener {
Expand All @@ -86,15 +77,16 @@ class AboutFragment : TwoPaneListFragment(), HasTitle {
return installerPackageName == "com.android.vending"
}

private fun openUrl(url: String): Boolean {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
tryStartActivity(intent)
return true
private fun Preference.setClickOpensUrl(url: String) {
setOnPreferenceClickListener {
openUri(url)
true
}
}

private fun openGooglePlayStorePage(): Boolean {
val appPackageName = context?.applicationContext?.packageName ?: return false
return openUrl("market://details?id=$appPackageName")
private fun openGooglePlayStorePage() {
val appPackageName = context?.packageName ?: return
openUri("market://details?id=$appPackageName")
}

private fun showDonateDialog() {
Expand Down Expand Up @@ -123,7 +115,7 @@ class AboutFragment : TwoPaneListFragment(), HasTitle {
override fun onBind(with: DonationPlatform) {
binding.imageView.setImageResource(with.iconId)
binding.textView.text = with.title
binding.root.setOnClickListener { openUrl(with.url) }
binding.root.setOnClickListener { openUri(with.url) }
TextViewCompat.setTextAppearance(binding.textView, R.style.TextAppearance_Title)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.westnordost.streetcomplete.screens.main.map

import android.app.Activity
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.PointF
import android.graphics.RectF
Expand All @@ -14,7 +13,6 @@ import android.view.animation.Interpolator
import androidx.annotation.CallSuper
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import com.mapzen.tangram.TouchInput.DoubleTapResponder
import com.mapzen.tangram.TouchInput.LongPressResponder
Expand All @@ -40,9 +38,9 @@ import de.westnordost.streetcomplete.screens.main.map.tangram.MapChangingListene
import de.westnordost.streetcomplete.screens.main.map.tangram.initMap
import de.westnordost.streetcomplete.util.ktx.awaitLayout
import de.westnordost.streetcomplete.util.ktx.containsAll
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.ktx.putDouble
import de.westnordost.streetcomplete.util.ktx.setMargins
import de.westnordost.streetcomplete.util.ktx.tryStartActivity
import de.westnordost.streetcomplete.util.ktx.viewLifecycleScope
import de.westnordost.streetcomplete.util.math.distanceTo
import de.westnordost.streetcomplete.util.viewBinding
Expand Down Expand Up @@ -145,18 +143,11 @@ open class MapFragment :
AlertDialog.Builder(requireContext())
.setTitle(R.string.open_url)
.setMessage(url)
.setPositiveButton(android.R.string.ok) { _, _ ->
openUrl(url)
}
.setPositiveButton(android.R.string.ok) { _, _ -> openUri(url) }
.setNegativeButton(android.R.string.cancel, null)
.show()
}

private fun openUrl(url: String): Boolean {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
return tryStartActivity(intent)
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == Prefs.THEME_BACKGROUND) {
sceneMapComponent?.isAerialView = sharedPrefs.getString(Prefs.THEME_BACKGROUND, "MAP") == "AERIAL"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package de.westnordost.streetcomplete.screens.main.messages

import android.app.Dialog
import android.content.Intent
import android.graphics.drawable.AnimatedVectorDrawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.animation.DecelerateInterpolator
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.databinding.FragmentUnreadOsmMessageBinding
import de.westnordost.streetcomplete.util.SoundFx
import de.westnordost.streetcomplete.util.ktx.dpToPx
import de.westnordost.streetcomplete.util.ktx.tryStartActivity
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.ktx.viewLifecycleScope
import de.westnordost.streetcomplete.util.viewBinding
import kotlinx.coroutines.launch
Expand All @@ -39,7 +37,7 @@ class OsmUnreadMessagesFragment : DialogFragment(R.layout.fragment_unread_osm_me
binding.mailContainer.alpha = 0.0f
binding.dialogContainer.setOnClickListener { dismiss() }
binding.readMailButton.setOnClickListener {
openUrl("https://www.openstreetmap.org/messages/inbox")
openUri("https://www.openstreetmap.org/messages/inbox")
dismiss()
}
val unreadMessagesCount = arguments?.getInt(ARG_UNREAD_MESSAGE_COUNT, 0) ?: 0
Expand Down Expand Up @@ -115,11 +113,6 @@ class OsmUnreadMessagesFragment : DialogFragment(R.layout.fragment_unread_osm_me
}
}

private fun openUrl(url: String): Boolean {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
return tryStartActivity(intent)
}

companion object {
private const val ARG_UNREAD_MESSAGE_COUNT = "unread_message_count"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package de.westnordost.streetcomplete.screens.settings.debug

import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.user.achievements.Link
import de.westnordost.streetcomplete.databinding.FragmentShowLinksBinding
import de.westnordost.streetcomplete.screens.BaseActivity
import de.westnordost.streetcomplete.screens.user.links.GroupedLinksAdapter
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.viewBinding
import org.koin.android.ext.android.inject
import org.koin.core.qualifier.named
Expand All @@ -29,12 +28,7 @@ class ShowLinksActivity : BaseActivity() {
binding.linksList.apply {
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
layoutManager = LinearLayoutManager(context)
adapter = GroupedLinksAdapter(links, ::openUrl)
adapter = GroupedLinksAdapter(links, ::openUri)
}
}

private fun openUrl(url: String) {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.animation.LayoutTransition
import android.animation.LayoutTransition.APPEARING
import android.animation.LayoutTransition.DISAPPEARING
import android.animation.TimeAnimator
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewPropertyAnimator
Expand All @@ -13,15 +12,14 @@ import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.OvershootInterpolator
import androidx.activity.OnBackPressedCallback
import androidx.core.net.toUri
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.data.user.achievements.Achievement
import de.westnordost.streetcomplete.databinding.FragmentAchievementInfoBinding
import de.westnordost.streetcomplete.screens.user.links.LinksAdapter
import de.westnordost.streetcomplete.util.ktx.tryStartActivity
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.viewBinding
import de.westnordost.streetcomplete.view.Transforms
import de.westnordost.streetcomplete.view.ViewPropertyAnimatorsPlayer
Expand Down Expand Up @@ -143,7 +141,7 @@ class AchievementInfoFragment : Fragment(R.layout.fragment_achievement_info) {
if (unlockedLinks.size == 1) R.string.achievements_unlocked_link
else R.string.achievements_unlocked_links
)
binding.unlockedLinksList.adapter = LinksAdapter(unlockedLinks, this::openUrl)
binding.unlockedLinksList.adapter = LinksAdapter(unlockedLinks, ::openUri)
}
}

Expand Down Expand Up @@ -316,11 +314,6 @@ class AchievementInfoFragment : Fragment(R.layout.fragment_achievement_info) {
binding.dialogAndBackgroundContainer.visibility = View.INVISIBLE
}

private fun openUrl(url: String) {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
tryStartActivity(intent)
}

private fun playAll(vararg animators: ViewPropertyAnimator) {
animatorsPlayer = ViewPropertyAnimatorsPlayer(animators.toMutableList()).also {
it.onEnd = { animatorsPlayer = null }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package de.westnordost.streetcomplete.screens.user.links

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.net.toUri
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
Expand All @@ -13,8 +11,8 @@ import de.westnordost.streetcomplete.data.user.achievements.AchievementsSource
import de.westnordost.streetcomplete.data.user.statistics.StatisticsSource
import de.westnordost.streetcomplete.databinding.FragmentLinksBinding
import de.westnordost.streetcomplete.util.ktx.awaitLayout
import de.westnordost.streetcomplete.util.ktx.openUri
import de.westnordost.streetcomplete.util.ktx.pxToDp
import de.westnordost.streetcomplete.util.ktx.tryStartActivity
import de.westnordost.streetcomplete.util.ktx.viewLifecycleScope
import de.westnordost.streetcomplete.util.viewBinding
import de.westnordost.streetcomplete.view.GridLayoutSpacingItemDecoration
Expand Down Expand Up @@ -46,7 +44,7 @@ class LinksFragment : Fragment(R.layout.fragment_links) {
val spanCount = (viewWidth / minCellWidth).toInt()

val links = withContext(Dispatchers.IO) { achievementsSource.getLinks() }
val adapter = GroupedLinksAdapter(links, this@LinksFragment::openUrl)
val adapter = GroupedLinksAdapter(links, ::openUri)
// headers should span the whole width
val spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int =
Expand Down Expand Up @@ -76,9 +74,4 @@ class LinksFragment : Fragment(R.layout.fragment_links) {
binding.emptyText.setText(R.string.links_empty)
}
}

private fun openUrl(url: String) {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
tryStartActivity(intent)
}
}
Loading

0 comments on commit d8589ef

Please sign in to comment.