Clean up SupportUtils
parent
e413da29f6
commit
b77c4d2e2e
|
@ -12,6 +12,7 @@ import kotlinx.coroutines.MainScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.mozilla.fenix.components.metrics.Event
|
import org.mozilla.fenix.components.metrics.Event
|
||||||
import org.mozilla.fenix.customtabs.AuthCustomTabActivity
|
import org.mozilla.fenix.customtabs.AuthCustomTabActivity
|
||||||
|
import org.mozilla.fenix.customtabs.AuthCustomTabActivity.Companion.EXTRA_AUTH_CUSTOM_TAB
|
||||||
import org.mozilla.fenix.customtabs.CustomTabActivity
|
import org.mozilla.fenix.customtabs.CustomTabActivity
|
||||||
import org.mozilla.fenix.ext.components
|
import org.mozilla.fenix.ext.components
|
||||||
import org.mozilla.fenix.ext.metrics
|
import org.mozilla.fenix.ext.metrics
|
||||||
|
@ -62,7 +63,7 @@ class IntentReceiverActivity : Activity() {
|
||||||
private fun setIntentActivity(intent: Intent) {
|
private fun setIntentActivity(intent: Intent) {
|
||||||
val openToBrowser = when {
|
val openToBrowser = when {
|
||||||
components.utils.customTabIntentProcessor.matches(intent) -> {
|
components.utils.customTabIntentProcessor.matches(intent) -> {
|
||||||
val activityClass = if (intent.hasExtra(getString(R.string.intent_extra_auth))) {
|
val activityClass = if (intent.hasExtra(EXTRA_AUTH_CUSTOM_TAB)) {
|
||||||
AuthCustomTabActivity::class
|
AuthCustomTabActivity::class
|
||||||
} else {
|
} else {
|
||||||
CustomTabActivity::class
|
CustomTabActivity::class
|
||||||
|
|
|
@ -28,4 +28,8 @@ class AuthCustomTabActivity : CustomTabActivity() {
|
||||||
val accountManager = components.backgroundServices.accountManager
|
val accountManager = components.backgroundServices.accountManager
|
||||||
accountManager.register(accountStateObserver, this, true)
|
accountManager.register(accountStateObserver, this, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val EXTRA_AUTH_CUSTOM_TAB = "support.customtabs.extra.AUTH"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ class OnboardingPrivacyNoticeViewHolder(view: View) : RecyclerView.ViewHolder(vi
|
||||||
view.description_text.text = view.context.getString(R.string.onboarding_privacy_notice_description, appName)
|
view.description_text.text = view.context.getString(R.string.onboarding_privacy_notice_description, appName)
|
||||||
|
|
||||||
view.read_button.setOnClickListener {
|
view.read_button.setOnClickListener {
|
||||||
val intent = SupportUtils.createCustomTabIntent(view.context, SupportUtils.PRIVACY_NOTICE_URL)
|
val intent = SupportUtils.createCustomTabIntent(view.context, SupportUtils.getPrivacyNoticeUrl())
|
||||||
view.context.startActivity(intent)
|
view.context.startActivity(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -201,7 +201,7 @@ class SettingsFragment : PreferenceFragmentCompat(), AccountObserver {
|
||||||
}
|
}
|
||||||
resources.getString(pref_key_privacy_link) -> {
|
resources.getString(pref_key_privacy_link) -> {
|
||||||
requireContext().let { context ->
|
requireContext().let { context ->
|
||||||
val intent = SupportUtils.createCustomTabIntent(context, SupportUtils.PRIVACY_NOTICE_URL)
|
val intent = SupportUtils.createCustomTabIntent(context, SupportUtils.getPrivacyNoticeUrl())
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,13 @@ package org.mozilla.fenix.settings
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import androidx.browser.customtabs.CustomTabsIntent
|
||||||
import android.net.Uri
|
import androidx.core.net.toUri
|
||||||
|
import mozilla.components.support.ktx.android.content.appVersionName
|
||||||
import org.mozilla.fenix.BuildConfig
|
import org.mozilla.fenix.BuildConfig
|
||||||
import org.mozilla.fenix.IntentReceiverActivity
|
import org.mozilla.fenix.IntentReceiverActivity
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.customtabs.AuthCustomTabActivity.Companion.EXTRA_AUTH_CUSTOM_TAB
|
||||||
import org.mozilla.fenix.ext.getColorFromAttr
|
import org.mozilla.fenix.ext.getColorFromAttr
|
||||||
import java.io.UnsupportedEncodingException
|
import java.io.UnsupportedEncodingException
|
||||||
import java.net.URLEncoder
|
import java.net.URLEncoder
|
||||||
|
@ -20,12 +22,8 @@ object SupportUtils {
|
||||||
const val RATE_APP_URL = "market://details?id=" + BuildConfig.APPLICATION_ID
|
const val RATE_APP_URL = "market://details?id=" + BuildConfig.APPLICATION_ID
|
||||||
const val MOZILLA_MANIFESTO_URL = "https://www.mozilla.org/en-GB/about/manifesto/"
|
const val MOZILLA_MANIFESTO_URL = "https://www.mozilla.org/en-GB/about/manifesto/"
|
||||||
const val FENIX_PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}"
|
const val FENIX_PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}"
|
||||||
val PRIVACY_NOTICE_URL: String
|
|
||||||
get() = "https://www.mozilla.org/${getLanguageTag(Locale.getDefault())}/privacy/firefox/"
|
|
||||||
|
|
||||||
enum class SumoTopic(
|
enum class SumoTopic(internal val topicStr: String) {
|
||||||
internal val topicStr: String
|
|
||||||
) {
|
|
||||||
HELP("faq-android"),
|
HELP("faq-android"),
|
||||||
PRIVATE_BROWSING_MYTHS("common-myths-about-private-browsing"),
|
PRIVATE_BROWSING_MYTHS("common-myths-about-private-browsing"),
|
||||||
YOUR_RIGHTS("your-rights"),
|
YOUR_RIGHTS("your-rights"),
|
||||||
|
@ -33,38 +31,46 @@ object SupportUtils {
|
||||||
WHATS_NEW("whats-new-firefox-preview")
|
WHATS_NEW("whats-new-firefox-preview")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSumoURLForTopic(context: Context, topic: SumoTopic): String {
|
/**
|
||||||
|
* Gets a support page URL for the corresponding topic.
|
||||||
|
*/
|
||||||
|
fun getSumoURLForTopic(
|
||||||
|
context: Context,
|
||||||
|
topic: SumoTopic,
|
||||||
|
locale: Locale = Locale.getDefault()
|
||||||
|
): String {
|
||||||
val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
|
val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
|
||||||
// Remove the whitespace so a search is not triggered:
|
// Remove the whitespace so a search is not triggered:
|
||||||
val appVersion = getAppVersion(context).replace(" ", "")
|
val appVersion = context.appVersionName?.replace(" ", "")
|
||||||
val osTarget = "Android"
|
val osTarget = "Android"
|
||||||
val langTag = getLanguageTag(Locale.getDefault())
|
val langTag = getLanguageTag(locale)
|
||||||
return "https://support.mozilla.org/1/mobile/$appVersion/$osTarget/$langTag/$escapedTopic"
|
return "https://support.mozilla.org/1/mobile/$appVersion/$osTarget/$langTag/$escapedTopic"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used when the app version and os are not part of the URL
|
/**
|
||||||
fun getGenericSumoURLForTopic(topic: SumoTopic): String {
|
* Gets a support page URL for the corresponding topic.
|
||||||
|
* Used when the app version and os are not part of the URL.
|
||||||
|
*/
|
||||||
|
fun getGenericSumoURLForTopic(topic: SumoTopic, locale: Locale = Locale.getDefault()): String {
|
||||||
val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
|
val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
|
||||||
val langTag = getLanguageTag(Locale.getDefault())
|
val langTag = getLanguageTag(locale)
|
||||||
return "https://support.mozilla.org/$langTag/kb/$escapedTopic"
|
return "https://support.mozilla.org/$langTag/kb/$escapedTopic"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply {
|
fun getPrivacyNoticeUrl(locale: Locale = Locale.getDefault()) =
|
||||||
putExtra(context.getString(R.string.intent_extra_toolbar_color), context.getColorFromAttr(R.attr.foundation))
|
"https://www.mozilla.org/${getLanguageTag(locale)}/privacy/firefox/"
|
||||||
putExtra(context.getString(R.string.intent_extra_session), true)
|
|
||||||
setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)
|
|
||||||
data = Uri.parse(url)
|
|
||||||
setPackage(context.packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createAuthCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply {
|
fun createCustomTabIntent(context: Context, url: String): Intent = CustomTabsIntent.Builder()
|
||||||
putExtra(context.getString(R.string.intent_extra_toolbar_color), context.getColorFromAttr(R.attr.foundation))
|
.setInstantAppsEnabled(false)
|
||||||
putExtra(context.getString(R.string.intent_extra_session), true)
|
.setToolbarColor(context.getColorFromAttr(R.attr.foundation))
|
||||||
putExtra(context.getString(R.string.intent_extra_auth), true)
|
.build()
|
||||||
setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)
|
.intent
|
||||||
data = Uri.parse(url)
|
.setData(url.toUri())
|
||||||
setPackage(context.packageName)
|
.setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)
|
||||||
}
|
.setPackage(context.packageName)
|
||||||
|
|
||||||
|
fun createAuthCustomTabIntent(context: Context, url: String): Intent =
|
||||||
|
createCustomTabIntent(context, url).putExtra(EXTRA_AUTH_CUSTOM_TAB, true)
|
||||||
|
|
||||||
private fun getEncodedTopicUTF8(topic: String): String {
|
private fun getEncodedTopicUTF8(topic: String): String {
|
||||||
try {
|
try {
|
||||||
|
@ -74,20 +80,9 @@ object SupportUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getAppVersion(context: Context): String {
|
|
||||||
try {
|
|
||||||
return context.packageManager.getPackageInfo(context.packageName, 0).versionName
|
|
||||||
} catch (e: PackageManager.NameNotFoundException) {
|
|
||||||
// This should be impossible - we should always be able to get information about ourselves:
|
|
||||||
throw IllegalStateException("Unable find package details for Fenix", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getLanguageTag(locale: Locale): String {
|
private fun getLanguageTag(locale: Locale): String {
|
||||||
val language = locale.language
|
val language = locale.language
|
||||||
val country = locale.country // Can be an empty string.
|
val country = locale.country // Can be an empty string.
|
||||||
return if (country == "") {
|
return if (country.isEmpty()) language else "$language-$country"
|
||||||
language
|
|
||||||
} else "$language-$country"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix.settings
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageInfo
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.TestApplication
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
|
@ObsoleteCoroutinesApi
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(application = TestApplication::class)
|
||||||
|
class SupportUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getSumoURLForTopic() {
|
||||||
|
assertEquals(
|
||||||
|
"https://support.mozilla.org/1/mobile/1.6/Android/en-US/common-myths-about-private-browsing",
|
||||||
|
SupportUtils.getSumoURLForTopic(mockContext("1.6"), SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS, Locale("en", "US"))
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"https://support.mozilla.org/1/mobile/20/Android/fr/tracking-protection-firefox-preview",
|
||||||
|
SupportUtils.getSumoURLForTopic(mockContext("2 0"), SupportUtils.SumoTopic.TRACKING_PROTECTION, Locale("fr"))
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"https://support.mozilla.org/1/mobile/three/Android/es-CL/whats-new-firefox-preview",
|
||||||
|
SupportUtils.getSumoURLForTopic(mockContext("three"), SupportUtils.SumoTopic.WHATS_NEW, Locale("es", "CL"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getGenericSumoURLForTopic() {
|
||||||
|
assertEquals(
|
||||||
|
"https://support.mozilla.org/en-GB/kb/faq-android",
|
||||||
|
SupportUtils.getGenericSumoURLForTopic(SupportUtils.SumoTopic.HELP, Locale("en", "GB"))
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"https://support.mozilla.org/de/kb/your-rights",
|
||||||
|
SupportUtils.getGenericSumoURLForTopic(SupportUtils.SumoTopic.YOUR_RIGHTS, Locale("de"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun getPrivacyNoticeUrl() {
|
||||||
|
assertEquals(
|
||||||
|
"https://www.mozilla.org/en-CA/privacy/firefox/",
|
||||||
|
SupportUtils.getPrivacyNoticeUrl(Locale("en", "CA"))
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
"https://www.mozilla.org/zh/privacy/firefox/",
|
||||||
|
SupportUtils.getPrivacyNoticeUrl(Locale("zh"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mockContext(versionName: String): Context {
|
||||||
|
val context: Context = mockk()
|
||||||
|
val packageManager: PackageManager = mockk()
|
||||||
|
val packageInfo = PackageInfo()
|
||||||
|
|
||||||
|
every { context.packageName } returns "org.mozilla.fenix"
|
||||||
|
every { context.packageManager } returns packageManager
|
||||||
|
every { packageManager.getPackageInfo("org.mozilla.fenix", 0) } returns packageInfo
|
||||||
|
packageInfo.versionName = versionName
|
||||||
|
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue