SupportUtils.kt 5.72 KB
Newer Older
1
2
3
4
5
6
7
/* 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
8
import android.content.Intent
Tiger Oakes's avatar
Tiger Oakes committed
9
10
11
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
import mozilla.components.support.ktx.android.content.appVersionName
12
import mozilla.components.support.ktx.android.content.getColorFromAttr
13
import org.mozilla.fenix.BuildConfig
14
import org.mozilla.fenix.IntentReceiverActivity
15
import org.mozilla.fenix.R
16
import org.mozilla.fenix.settings.account.AuthIntentReceiverActivity
17
18
19
20
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.util.Locale

Alex Catarineu's avatar
Alex Catarineu committed
21
@Suppress("TooManyFunctions")
22
23
object SupportUtils {
    const val RATE_APP_URL = "market://details?id=" + BuildConfig.APPLICATION_ID
ekager's avatar
ekager committed
24
25
    const val POCKET_TRENDING_URL = "https://getpocket.com/fenix-top-articles"
    const val WIKIPEDIA_URL = "https://www.wikipedia.org/"
26
    const val FENIX_PLAY_STORE_URL = "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}"
27
    const val FIREFOX_BETA_PLAY_STORE_URL = "market://details?id=org.mozilla.firefox_beta"
28
    const val FIREFOX_NIGHTLY_PLAY_STORE_URL = "market://details?id=org.mozilla.fenix"
29
    const val GOOGLE_URL = "https://www.google.com/"
30
31
32
33
34
    const val BAIDU_URL = "https://m.baidu.com/?from=1000969a"
    const val JD_URL = "https://union-click.jd.com/jdc" +
            "?e=&p=AyIGZRprFDJWWA1FBCVbV0IUWVALHFRBEwQAQB1AWQkFVUVXfFkAF14lRFRbJXstVWR3WQ1rJ08AZnhS" +
            "HDJBYh4LZR9eEAMUBlccWCUBEQZRGFoXCxc3ZRteJUl8BmUZWhQ" +
            "AEwdRGF0cMhIAVB5ZFAETBVAaXRwyFQdcKydLSUpaCEtYFAIXN2UrWCUyIgdVK1slXVZaCCtZFAMWDg%3D%3D"
35
36
    const val GOOGLE_US_URL = "https://www.google.com/webhp?client=firefox-b-1-m&channel=ts"
    const val GOOGLE_XX_URL = "https://www.google.com/webhp?client=firefox-b-m&channel=ts"
Matthew Finkel's avatar
Matthew Finkel committed
37
    const val DONATE_URL = "https://donate.torproject.org/"
Alex Catarineu's avatar
Alex Catarineu committed
38
39
    const val TB_MANUAL_URL = "https://tb-manual.torproject.org/mobile-tor"
    const val TOR_RELEASES = "https://www.torproject.org/releases/"
40
    const val TOR_FEEDBACK_SURVEY_URL = "http://bogdyardcfurxcle.onion/index.php/491436"
41

Tiger Oakes's avatar
Tiger Oakes committed
42
    enum class SumoTopic(internal val topicStr: String) {
43
        FENIX_MOVING("sync-delist"),
44
        HELP("faq-android"),
45
        PRIVATE_BROWSING_MYTHS("common-myths-about-private-browsing"),
46
        YOUR_RIGHTS("your-rights"),
mcarare's avatar
mcarare committed
47
        TRACKING_PROTECTION("tracking-protection-firefox-android"),
48
        WHATS_NEW("whats-new-firefox-preview"),
49
        SEND_TABS("send-tab-preview"),
Jeff Boek's avatar
Jeff Boek committed
50
        SET_AS_DEFAULT_BROWSER("set-firefox-preview-default"),
51
        SEARCH_SUGGESTION("how-search-firefox-preview"),
52
        CUSTOM_SEARCH_ENGINES("custom-search-engines"),
mcarare's avatar
mcarare committed
53
        SYNC_SETUP("how-set-firefox-sync-firefox-android"),
54
        QR_CAMERA_ACCESS("qr-camera-access")
55
56
    }

57
58
59
60
61
    enum class MozillaPage(internal val path: String) {
        PRIVATE_NOTICE("privacy/firefox/"),
        MANIFESTO("about/manifesto/")
    }

Tiger Oakes's avatar
Tiger Oakes committed
62
63
64
65
66
67
68
69
    /**
     * Gets a support page URL for the corresponding topic.
     */
    fun getSumoURLForTopic(
        context: Context,
        topic: SumoTopic,
        locale: Locale = Locale.getDefault()
    ): String {
70
        val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
71
        // Remove the whitespace so a search is not triggered:
Tiger Oakes's avatar
Tiger Oakes committed
72
        val appVersion = context.appVersionName?.replace(" ", "")
73
        val osTarget = "Android"
Tiger Oakes's avatar
Tiger Oakes committed
74
        val langTag = getLanguageTag(locale)
75
76
77
        return "https://support.mozilla.org/1/mobile/$appVersion/$osTarget/$langTag/$escapedTopic"
    }

Tiger Oakes's avatar
Tiger Oakes committed
78
79
80
81
82
    /**
     * 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 {
83
        val escapedTopic = getEncodedTopicUTF8(topic.topicStr)
Tiger Oakes's avatar
Tiger Oakes committed
84
        val langTag = getLanguageTag(locale)
85
86
87
        return "https://support.mozilla.org/$langTag/kb/$escapedTopic"
    }

88
89
90
91
    fun getFirefoxAccountSumoUrl(): String {
        return "https://support.mozilla.org/kb/access-mozilla-services-firefox-account"
    }

Alex Catarineu's avatar
Alex Catarineu committed
92
93
94
95
96
97
98
99
    fun getTorHelpPageUrl(): String {
        return TB_MANUAL_URL
    }

    fun getTorReleasePageUrl(): String {
        return TOR_RELEASES
    }

100
101
102
103
104
    fun getMozillaPageUrl(page: MozillaPage, locale: Locale = Locale.getDefault()): String {
        val path = page.path
        val langTag = getLanguageTag(locale)
        return "https://www.mozilla.org/$langTag/$path"
    }
105

Alex Catarineu's avatar
Alex Catarineu committed
106
107
108
    fun getWhatsNewUrl(@Suppress("UNUSED_PARAMETER") context: Context): String {
        return getTorReleasePageUrl()
    }
109

Tiger Oakes's avatar
Tiger Oakes committed
110
111
112
113
114
115
    fun createCustomTabIntent(context: Context, url: String): Intent = CustomTabsIntent.Builder()
        .setInstantAppsEnabled(false)
        .setToolbarColor(context.getColorFromAttr(R.attr.foundation))
        .build()
        .intent
        .setData(url.toUri())
116
        .setClassName(context, IntentReceiverActivity::class.java.name)
Tiger Oakes's avatar
Tiger Oakes committed
117
118
119
        .setPackage(context.packageName)

    fun createAuthCustomTabIntent(context: Context, url: String): Intent =
120
        createCustomTabIntent(context, url).setClassName(context, AuthIntentReceiverActivity::class.java.name)
121

122
123
124
125
126
127
128
129
    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)
        }
    }

130
131
132
    private fun getLanguageTag(locale: Locale): String {
        val language = locale.language
        val country = locale.country // Can be an empty string.
Tiger Oakes's avatar
Tiger Oakes committed
133
        return if (country.isEmpty()) language else "$language-$country"
134
    }
135
}