Refactor and move out openUrl method to utils
This commit is contained in:
parent
d6d1262629
commit
ff7f18d2ae
@ -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))
|
||||
try {
|
||||
startActivity(browserIntent)
|
||||
} catch(e: ActivityNotFoundException) {
|
||||
return failedRegistration(getString(R.string.browser_launch_failed))
|
||||
}
|
||||
}
|
||||
if (!openUrl(url)) return failedRegistration(getString(R.string.browser_launch_failed))
|
||||
}
|
||||
|
||||
private fun authenticate(code: String?) {
|
||||
|
@ -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?) {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user