1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/settings/SupportUtils.kt

119 lines
4.8 KiB
Kotlin
Raw Normal View History

/* 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.Intent
2019-09-09 20:42:52 +02:00
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
import mozilla.components.support.ktx.android.content.appVersionName
2020-02-06 17:14:32 +01:00
import mozilla.components.support.ktx.android.content.getColorFromAttr
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.util.Locale
object SupportUtils {
const val RATE_APP_URL = "market://details?id=" + BuildConfig.APPLICATION_ID
2020-02-14 05:08:05 +01:00
const val POCKET_TRENDING_URL = "https://getpocket.com/fenix-top-articles"
const val WIKIPEDIA_URL = "https://www.wikipedia.org/"
const val FENIX_PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}"
const val FIREFOX_BETA_PLAY_STORE_URL = "market://details?id=org.mozilla.firefox_beta"
const val FIREFOX_NIGHTLY_PLAY_STORE_URL = "market://details?id=org.mozilla.fenix"
const val GOOGLE_URL = "https://www.google.com/"
2019-09-09 20:42:52 +02:00
enum class SumoTopic(internal val topicStr: String) {
FENIX_MOVING("sync-delist"),
2019-06-15 00:00:09 +02:00
HELP("faq-android"),
2019-05-24 20:19:06 +02:00
PRIVATE_BROWSING_MYTHS("common-myths-about-private-browsing"),
YOUR_RIGHTS("your-rights"),
TRACKING_PROTECTION("tracking-protection-firefox-preview"),
WHATS_NEW("whats-new-firefox-preview"),
SEND_TABS("send-tab-preview"),
Adds custom search engines (#6551) * For #5577 - Adds button to add a new search engine * For #5577 - Adds custom engine store * For #5577 - Creates a custom SearchEngineProvider * For #5577 - Gives the ability to delete search engines * For #5577 - Adds the UI to add a custom search engine * For #5577 - Adds form to create a custom search engine * For #5577 - Adds the ability to add a custom search engine * For #5577 - Adds the ability to delete custom search engines * For #5577 - Selects the first element on the add custom search engine screen * For #5577 - Prevents adding a search engine that already exists * For #5577 - Styles the add search engine preference * For #5577 - Makes the name check case-insensitive * For #5577 - Fix bug where home screen doesnt see new search engines * For #5577 - Moves Search URL validation to its own type * For #5577 - Fixes linting errors * For #5577 - Adds the ability to edit a custom search engine * For #5577 - Allows the user to edit a serach engine even when it is the last item in the list * For #5577 - Adds an undo snackbar when deleting a search engine * For #5577 - Moves all of the strings to be translated * For #5577 - Fixes bug when deleting your default search engine * For #5577 - Puts adding search engines behind a feature flag * For #5577 - Navigate to custom search engine SUMO article when tapping learn more * For #5577 - Fixes nits * For #5577 - Uses concept-fetch to validate search string * For #5577 - Adds string resources for the cannot reach error state
2019-11-20 01:30:56 +01:00
SET_AS_DEFAULT_BROWSER("set-firefox-preview-default"),
SEARCH_SUGGESTION("how-search-firefox-preview"),
CUSTOM_SEARCH_ENGINES("custom-search-engines"),
UPGRADE_FAQ("firefox-preview-upgrade-faqs"),
SYNC_SETUP("how-set-firefox-sync-firefox-preview")
}
enum class MozillaPage(internal val path: String) {
PRIVATE_NOTICE("privacy/firefox/"),
MANIFESTO("about/manifesto/")
}
2019-09-09 20:42:52 +02:00
/**
* 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)
// Remove the whitespace so a search is not triggered:
2019-09-09 20:42:52 +02:00
val appVersion = context.appVersionName?.replace(" ", "")
val osTarget = "Android"
2019-09-09 20:42:52 +02:00
val langTag = getLanguageTag(locale)
return "https://support.mozilla.org/1/mobile/$appVersion/$osTarget/$langTag/$escapedTopic"
}
2019-09-09 20:42:52 +02:00
/**
* 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)
2019-09-09 20:42:52 +02:00
val langTag = getLanguageTag(locale)
return "https://support.mozilla.org/$langTag/kb/$escapedTopic"
}
fun getFirefoxAccountSumoUrl(): String {
return "https://support.mozilla.org/kb/access-mozilla-services-firefox-account"
}
fun getMozillaPageUrl(page: MozillaPage, locale: Locale = Locale.getDefault()): String {
val path = page.path
val langTag = getLanguageTag(locale)
return "https://www.mozilla.org/$langTag/$path"
}
fun getWhatsNewUrl(context: Context) = if (Config.channel.isFennec) {
getGenericSumoURLForTopic(SumoTopic.UPGRADE_FAQ)
} else {
getSumoURLForTopic(context, SumoTopic.WHATS_NEW)
}
2019-09-09 20:42:52 +02:00
fun createCustomTabIntent(context: Context, url: String): Intent = CustomTabsIntent.Builder()
.setInstantAppsEnabled(false)
.setToolbarColor(context.getColorFromAttr(R.attr.foundation))
.build()
.intent
.setData(url.toUri())
.setClassName(context, IntentReceiverActivity::class.java.name)
2019-09-09 20:42:52 +02:00
.setPackage(context.packageName)
fun createAuthCustomTabIntent(context: Context, url: String): Intent =
createCustomTabIntent(context, url).setClassName(context, AuthIntentReceiverActivity::class.java.name)
private fun getEncodedTopicUTF8(topic: String): String {
try {
return URLEncoder.encode(topic, "UTF-8")
} catch (e: UnsupportedEncodingException) {
throw IllegalStateException("utf-8 should always be available", e)
}
}
private fun getLanguageTag(locale: Locale): String {
val language = locale.language
val country = locale.country // Can be an empty string.
2019-09-09 20:42:52 +02:00
return if (country.isEmpty()) language else "$language-$country"
}
}