Update browser opening code

Make the browser opening code actually use the app picker and target browsers specifically.
This commit is contained in:
OxygenCobalt 2021-04-04 12:00:27 -06:00
parent 887868ff9c
commit 969f25176a
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
8 changed files with 48 additions and 37 deletions

View file

@ -18,8 +18,8 @@ import org.oxycblt.auxio.playback.state.PlaybackMode
import org.oxycblt.auxio.recycler.CenterSmoothScroller
import org.oxycblt.auxio.ui.ActionMenu
import org.oxycblt.auxio.ui.canScroll
import org.oxycblt.auxio.ui.createToast
import org.oxycblt.auxio.ui.newMenu
import org.oxycblt.auxio.ui.showToast
/**
* The [DetailFragment] for an album.
@ -58,7 +58,7 @@ class AlbumDetailFragment : DetailFragment() {
setupToolbar(R.menu.menu_album_detail) { itemId ->
if (itemId == R.id.action_queue_add) {
playbackModel.addToUserQueue(detailModel.currentAlbum.value!!)
getString(R.string.label_queue_added).createToast(requireContext())
requireContext().showToast(R.string.label_queue_added)
true
} else {
false

View file

@ -13,6 +13,7 @@ import org.oxycblt.auxio.logD
/**
* Class that loads/constructs [Genre]s, [Artist]s, [Album]s, and [Song] objects from the filesystem
* TODO: Use album artist instead of artist tag.
* @author OxygenCobalt
*/
class MusicLoader(private val context: Context) {

View file

@ -1,6 +1,8 @@
package org.oxycblt.auxio.settings
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -11,9 +13,8 @@ import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.DialogAboutBinding
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.logE
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.ui.createToast
import org.oxycblt.auxio.ui.showToast
/**
* A [BottomSheetDialogFragment] that shows Auxio's about screen.
@ -48,34 +49,42 @@ class AboutDialog : BottomSheetDialogFragment() {
* Go through the process of opening a [link] in a browser.
*/
private fun openLinkInBrowser(link: String) {
try {
val uri = link.toUri()
val browserIntent = Intent(Intent.ACTION_VIEW, link.toUri()).setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
)
val browserIntent = Intent(Intent.ACTION_VIEW, uri)
browserIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
val pkgName = requireContext().packageManager.resolveActivity(
browserIntent, PackageManager.MATCH_DEFAULT_ONLY
)?.activityInfo?.packageName
val fallbackCandidates = requireContext().packageManager.queryIntentActivities(
browserIntent, 0
)
// If there are candidates here, then launch those.
if (fallbackCandidates.size > 0) {
requireActivity().startActivity(browserIntent)
if (pkgName != null) {
if (pkgName == "android") {
// No default browser [Must open app chooser, may not be supported
openAppChooser(browserIntent)
} else {
// Otherwise they don't have a browser on their phone, meaning they should
// just see an error.
getString(R.string.error_no_browser).createToast(requireContext())
try {
browserIntent.setPackage(pkgName)
startActivity(browserIntent)
} catch (exception: ActivityNotFoundException) {
// Not browser but an app chooser due to OEM garbage
browserIntent.setPackage(null)
openAppChooser(browserIntent)
}
}
} catch (e: Exception) {
logE("Browser intent launching failed [Probably android's fault]")
logE(e.stackTraceToString())
// Sometimes people have """Browsers""" on their phone according to android,
// but they actually don't so here's a fallback for that.
getString(R.string.error_no_browser).createToast(requireContext())
} else {
// No app installed to open the link
requireContext().showToast(R.string.error_no_browser)
}
}
private fun openAppChooser(intent: Intent) {
val chooserIntent = Intent(Intent.ACTION_CHOOSER)
.putExtra(Intent.EXTRA_INTENT, intent)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(chooserIntent)
}
companion object {
private const val LINK_CODEBASE = "https://github.com/oxygencobalt/Auxio"
private const val LINK_FAQ = "$LINK_CODEBASE/blob/master/info/FAQ.md"

View file

@ -18,7 +18,6 @@ import org.oxycblt.auxio.settings.blacklist.BlacklistDialog
import org.oxycblt.auxio.settings.ui.IntListPrefDialog
import org.oxycblt.auxio.settings.ui.IntListPreference
import org.oxycblt.auxio.ui.Accent
import org.oxycblt.auxio.ui.createToast
/**
* The actual fragment containing the settings menu. Inherits [PreferenceFragmentCompat].
@ -130,8 +129,9 @@ class SettingsListFragment : PreferenceFragmentCompat() {
SettingsManager.KEY_SAVE_STATE -> {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
playbackModel.savePlaybackState(requireContext()) {
getString(R.string.label_state_saved).createToast(requireContext())
requireContext().getString(R.string.label_state_saved)
}
true
}
}

View file

@ -19,7 +19,7 @@ import org.oxycblt.auxio.databinding.DialogBlacklistBinding
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.settings.ui.LifecycleDialog
import org.oxycblt.auxio.ui.createToast
import org.oxycblt.auxio.ui.showToast
import kotlin.system.exitProcess
/**
@ -105,7 +105,7 @@ class BlacklistDialog : LifecycleDialog() {
if (path != null) {
blacklistModel.addPath(path)
} else {
getString(R.string.error_bad_dir).createToast(requireContext())
requireContext().showToast(R.string.error_bad_dir)
}
}

View file

@ -135,11 +135,12 @@ class ActionMenu(
when (data) {
is Song -> {
playbackModel.addToUserQueue(data)
context.getString(R.string.label_queue_added).createToast(context)
context.showToast(R.string.label_queue_added)
}
is Album -> {
playbackModel.addToUserQueue(data)
context.getString(R.string.label_queue_added).createToast(context)
context.showToast(R.string.label_queue_added)
}
else -> {}

View file

@ -20,6 +20,7 @@ import android.widget.Toast
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.PluralsRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
@ -131,11 +132,10 @@ fun Int.resolveAttr(context: Context): Int {
}
/**
* Create a [Toast] from a [String]
* @param context [Context] required to create the toast
* Create a toast using the provided string resource.
*/
fun String.createToast(context: Context) {
Toast.makeText(context.applicationContext, this, Toast.LENGTH_SHORT).show()
fun Context.showToast(@StringRes str: Int) {
Toast.makeText(applicationContext, getString(str), Toast.LENGTH_SHORT).show()
}
// --- CONFIGURATION ---
@ -220,7 +220,7 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean {
}
} else {
@Suppress("DEPRECATION")
(activity.getSystemServiceSafe(WindowManager::class)).apply {
activity.getSystemServiceSafe(WindowManager::class).apply {
defaultDisplay.getRealSize(realPoint)
defaultDisplay.getMetrics(metrics)

View file

@ -99,7 +99,7 @@
<string name="error_no_music">No music found</string>
<string name="error_load_failed">Music loading failed</string>
<string name="error_no_perms">Auxio needs permission to read your music library</string>
<string name="error_no_browser">Could not open link</string>
<string name="error_no_browser">No app can open this link</string>
<string name="error_bad_dir">This directory is not supported</string>
<!-- Hint Namespace | EditText Hints -->