Merge branch 'fix_browser_intents' into 'master'

Fix browser intents on newer android versions

See merge request pixeldroid/PixelDroid!275
This commit is contained in:
Matthieu 2020-12-28 17:23:16 +01:00
commit 555c3e63da
3 changed files with 25 additions and 18 deletions

View File

@ -18,6 +18,7 @@ import com.h.pixeldroid.utils.api.objects.*
import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.hasInternet
import com.h.pixeldroid.utils.normalizeDomain
import com.h.pixeldroid.utils.openUrl
import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
@ -217,18 +218,7 @@ class LoginActivity : BaseActivity() {
"response_type=code" + "&" +
"scope=$SCOPE"
val intent = CustomTabsIntent.Builder().build()
try {
intent.launchUrl(this, Uri.parse(url))
} catch (e: ActivityNotFoundException) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
if (browserIntent.resolveActivity(packageManager) != null) {
startActivity(browserIntent)
} else {
return failedRegistration(getString(R.string.browser_launch_failed))
}
}
if (!openUrl(url)) return failedRegistration(getString(R.string.browser_launch_failed))
}
private fun authenticate(code: String?) {

View File

@ -23,6 +23,7 @@ import com.h.pixeldroid.utils.api.objects.Status
import com.h.pixeldroid.posts.parseHTMLText
import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.ImageConverter
import com.h.pixeldroid.utils.openUrl
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
@ -214,12 +215,7 @@ class ProfileActivity : BaseActivity() {
private fun onClickEditButton() {
val url = "$domain/settings/home"
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
if(browserIntent.resolveActivity(packageManager) != null) {
startActivity(browserIntent)
} else {
Log.e("ProfileActivity", "Cannot open this link")
}
if (!openUrl(url)) Log.e("ProfileActivity", "Cannot open this link")
}
private fun onClickFollowers(account: Account?) {

View File

@ -1,11 +1,15 @@
package com.h.pixeldroid.utils
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Resources
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.browser.customtabs.CustomTabsIntent
import com.h.pixeldroid.R
fun hasInternet(context: Context): Boolean {
@ -20,6 +24,23 @@ fun normalizeDomain(domain: String): String {
.trim(Char::isWhitespace)
}
fun BaseActivity.openUrl(url: String): Boolean{
val intent = CustomTabsIntent.Builder().build()
return try {
intent.launchUrl(this, Uri.parse(url))
true
} catch (e: ActivityNotFoundException) {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
try {
startActivity(browserIntent)
true
} catch(e: ActivityNotFoundException) {
false
}
}
}
/**
* @brief Updates the application's theme depending on the given preferences and resources