From 8df86dd4aca3320e13cf1f917a30cd8bceceafee Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Mon, 10 Aug 2020 13:49:56 +0200 Subject: [PATCH] For #162 - Add build flag to exclude proprietary dependencies. --- app/build.gradle | 23 ++++++--- .../components/metrics/AdvertisingIDImpl.kt | 13 +++++ .../metrics/LeanplumMetricsServiceImpl.kt | 19 +++++++ .../org/mozilla/fenix/push/PushServiceImpl.kt | 15 ++++++ .../settings/about/OssLicensesMenuImpl.kt | 15 ++++++ app/src/main/AndroidManifest.xml | 2 +- .../org/mozilla/fenix/components/Analytics.kt | 4 +- .../java/org/mozilla/fenix/components/Push.kt | 4 +- .../fenix/components/metrics/MetricsUtils.kt | 50 +++++-------------- .../fenix/settings/about/AboutFragment.kt | 20 +++----- .../fenix/settings/about/OssLicensesMenu.kt | 11 ++++ .../components/metrics/AdvertisingIDImpl.kt | 37 ++++++++++++++ .../metrics/LeanplumMetricsServiceImpl.kt} | 2 +- .../push/LeanplumNotificationCustomizer.kt | 0 .../mozilla/fenix/push/PushServiceImpl.kt} | 2 +- .../settings/about/OssLicensesMenuImpl.kt | 16 ++++++ .../LeanplumNotificationCustomizerTest.kt | 3 +- gradle.properties | 2 + 18 files changed, 173 insertions(+), 65 deletions(-) create mode 100644 app/src/free/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt create mode 100644 app/src/free/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt create mode 100644 app/src/free/java/org/mozilla/fenix/push/PushServiceImpl.kt create mode 100644 app/src/free/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt create mode 100644 app/src/main/java/org/mozilla/fenix/settings/about/OssLicensesMenu.kt create mode 100644 app/src/nonFree/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt rename app/src/{main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt => nonFree/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt} (99%) rename app/src/{main => nonFree}/java/org/mozilla/fenix/push/LeanplumNotificationCustomizer.kt (100%) rename app/src/{main/java/org/mozilla/fenix/push/FirebasePushService.kt => nonFree/java/org/mozilla/fenix/push/PushServiceImpl.kt} (97%) create mode 100644 app/src/nonFree/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt diff --git a/app/build.gradle b/app/build.gradle index 2035810be84d..5e83c3aa6603 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,6 +17,8 @@ import org.mozilla.fenix.gradle.tasks.LintUnitTestRunner import static org.gradle.api.tasks.testing.TestResult.ResultType +def useFreeImplementation = project.properties['org.mozilla.fenix.useFreeImplementation'].toBoolean() + android { compileSdkVersion Config.compileSdkVersion defaultConfig { @@ -31,6 +33,7 @@ android { resValue "bool", "IS_DEBUG", "false" buildConfigField "boolean", "USE_RELEASE_VERSIONING", "false" buildConfigField "String", "AMO_COLLECTION", "\"3204bb44a6ef44d39ee34917f28055\"" + buildConfigField "boolean", "USE_FREE_IMPLEMENTATION", useFreeImplementation.toString() def deepLinkSchemeValue = "fenix-dev" buildConfigField "String", "DEEP_LINK_SCHEME", "\"$deepLinkSchemeValue\"" manifestPlaceholders = [ @@ -142,6 +145,13 @@ android { java.srcDirs = ['src/migration/java', 'src/geckoRelease/java'] manifest.srcFile "src/migration/AndroidManifest.xml" } + main { + if (useFreeImplementation) { + java.srcDirs += ["src/free"] + } else { + java.srcDirs += ["src/nonFree"] + } + } } splits { @@ -330,10 +340,6 @@ dependencies { implementation Deps.androidx_coordinatorlayout implementation Deps.sentry - implementation Deps.osslicenses_library - - implementation Deps.leanplum_core - implementation Deps.leanplum_fcm implementation Deps.mozilla_concept_engine implementation Deps.mozilla_concept_menu @@ -412,7 +418,6 @@ dependencies { implementation Deps.mozilla_ui_widgets implementation Deps.mozilla_lib_crash - implementation Deps.mozilla_lib_push_firebase implementation Deps.mozilla_lib_dataprotect debugImplementation Deps.leakcanary @@ -441,7 +446,13 @@ dependencies { implementation Deps.adjust implementation Deps.installreferrer // Required by Adjust - implementation Deps.google_ads_id // Required for the Google Advertising ID + if (!useFreeImplementation) { + implementation Deps.leanplum_core + implementation Deps.leanplum_fcm + implementation Deps.mozilla_lib_push_firebase + implementation Deps.osslicenses_library + implementation Deps.google_ads_id // Required for the Google Advertising ID + } androidTestImplementation Deps.uiautomator // Removed pending AndroidX fixes diff --git a/app/src/free/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt b/app/src/free/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt new file mode 100644 index 000000000000..3b801a9a6931 --- /dev/null +++ b/app/src/free/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt @@ -0,0 +1,13 @@ +/* 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.components.metrics + +import android.content.Context + +interface AdvertisingIDImpl : AdvertisingID { + override fun getAdvertisingID(context: Context): String? { + return null + } +} diff --git a/app/src/free/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt b/app/src/free/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt new file mode 100644 index 000000000000..614d79e2dfad --- /dev/null +++ b/app/src/free/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt @@ -0,0 +1,19 @@ +/* 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.components.metrics + +import android.app.Application +import java.util.UUID + +class LeanplumMetricsServiceImpl( + private val application: Application, + private val deviceIdGenerator: () -> String = { UUID.randomUUID().toString() } +) : MetricsService { + override val type = MetricServiceType.Marketing + override fun start() {} + override fun stop() {} + override fun track(event: Event) {} + override fun shouldTrack(event: Event): Boolean {return false;} +} diff --git a/app/src/free/java/org/mozilla/fenix/push/PushServiceImpl.kt b/app/src/free/java/org/mozilla/fenix/push/PushServiceImpl.kt new file mode 100644 index 000000000000..769144375214 --- /dev/null +++ b/app/src/free/java/org/mozilla/fenix/push/PushServiceImpl.kt @@ -0,0 +1,15 @@ +/* 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.push + +import android.content.Context +import mozilla.components.concept.push.PushService + +class PushServiceImpl : PushService { + override fun deleteToken() {} + override fun isServiceAvailable(context: Context): Boolean { return false } + override fun start(context: Context) {} + override fun stop() {} +} diff --git a/app/src/free/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt b/app/src/free/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt new file mode 100644 index 000000000000..e1c937caa57b --- /dev/null +++ b/app/src/free/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt @@ -0,0 +1,15 @@ +/* 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.about + +import android.content.Context +import android.content.Intent + +class OssLicensesMenuImpl : OssLicensesMenu { + override fun getIntent(context: Context?, title: String): Intent { + throw NotImplementedError() + } +} + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bb20352e0903..196bdb77bb05 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -264,7 +264,7 @@ android:exported="false" /> diff --git a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt index d9512579661e..6cb1aa9658a9 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt @@ -20,7 +20,7 @@ import org.mozilla.fenix.R import org.mozilla.fenix.ReleaseChannel import org.mozilla.fenix.components.metrics.AdjustMetricsService import org.mozilla.fenix.components.metrics.GleanMetricsService -import org.mozilla.fenix.components.metrics.LeanplumMetricsService +import org.mozilla.fenix.components.metrics.LeanplumMetricsServiceImpl import org.mozilla.fenix.components.metrics.MetricController import org.mozilla.fenix.ext.settings import org.mozilla.fenix.utils.Mockable @@ -88,7 +88,7 @@ class Analytics( MetricController.create( listOf( GleanMetricsService(context), - LeanplumMetricsService(context as Application), + LeanplumMetricsServiceImpl(context as Application), AdjustMetricsService(context) ), isDataTelemetryEnabled = { context.settings().isTelemetryEnabled }, diff --git a/app/src/main/java/org/mozilla/fenix/components/Push.kt b/app/src/main/java/org/mozilla/fenix/components/Push.kt index 7f712a7aeda2..c323660e0b6e 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Push.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Push.kt @@ -10,7 +10,7 @@ import mozilla.components.feature.push.PushConfig import mozilla.components.lib.crash.CrashReporter import mozilla.components.support.base.log.logger.Logger import org.mozilla.fenix.R -import org.mozilla.fenix.push.FirebasePushService +import org.mozilla.fenix.push.PushServiceImpl /** * Component group for push services. These components use services that strongly depend on @@ -42,5 +42,5 @@ class Push(context: Context, crashReporter: CrashReporter) { PushConfig(projectId) } - private val pushService by lazy { FirebasePushService() } + private val pushService by lazy { PushServiceImpl() } } diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/MetricsUtils.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/MetricsUtils.kt index 2c9bf3eff72c..ffcef5da421f 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/MetricsUtils.kt +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/MetricsUtils.kt @@ -7,9 +7,6 @@ package org.mozilla.fenix.components.metrics import android.content.Context import android.util.Base64 import androidx.annotation.VisibleForTesting -import com.google.android.gms.ads.identifier.AdvertisingIdClient -import com.google.android.gms.common.GooglePlayServicesNotAvailableException -import com.google.android.gms.common.GooglePlayServicesRepairableException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import mozilla.components.browser.search.SearchEngine @@ -17,13 +14,24 @@ import mozilla.components.support.base.log.logger.Logger import org.mozilla.fenix.components.metrics.Event.PerformedSearch.SearchAccessPoint import org.mozilla.fenix.components.searchengine.CustomSearchEngineStore import org.mozilla.fenix.ext.searchEngineManager -import java.io.IOException import java.security.NoSuchAlgorithmException import java.security.spec.InvalidKeySpecException import javax.crypto.SecretKeyFactory import javax.crypto.spec.PBEKeySpec -object MetricsUtils { +interface AdvertisingID { + /** + * Query the Google Advertising API to get the Google Advertising ID. + * + * This is meant to be used off the main thread. The API will throw an + * exception and we will print a log message otherwise. + * + * @return a String containing the Google Advertising ID or null. + */ + fun getAdvertisingID(context: Context): String? +} + +object MetricsUtils : AdvertisingIDImpl { fun createSearchEvent( engine: SearchEngine, context: Context, @@ -72,38 +80,6 @@ object MetricsUtils { @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal fun getHashingSalt(): String = "org.mozilla.fenix-salt" - /** - * Query the Google Advertising API to get the Google Advertising ID. - * - * This is meant to be used off the main thread. The API will throw an - * exception and we will print a log message otherwise. - * - * @return a String containing the Google Advertising ID or null. - */ - @Suppress("TooGenericExceptionCaught") - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - internal fun getAdvertisingID(context: Context): String? { - return try { - AdvertisingIdClient.getAdvertisingIdInfo(context).id - } catch (e: GooglePlayServicesNotAvailableException) { - Logger.debug("ActivationPing - Google Play not installed on the device") - null - } catch (e: GooglePlayServicesRepairableException) { - Logger.debug("ActivationPing - recoverable error connecting to Google Play Services") - null - } catch (e: IllegalStateException) { - // This is unlikely to happen, as this should be running off the main thread. - Logger.debug("ActivationPing - AdvertisingIdClient must be called off the main thread") - null - } catch (e: IOException) { - Logger.debug("ActivationPing - unable to connect to Google Play Services") - null - } catch (e: NullPointerException) { - Logger.debug("ActivationPing - no Google Advertising ID available") - null - } - } - /** * Produces a hashed version of the Google Advertising ID. * We want users using more than one of our products to report a different diff --git a/app/src/main/java/org/mozilla/fenix/settings/about/AboutFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/about/AboutFragment.kt index ece022c027a7..591ad279530b 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/about/AboutFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/about/AboutFragment.kt @@ -13,7 +13,6 @@ import android.view.ViewGroup import androidx.core.content.pm.PackageInfoCompat import androidx.fragment.app.Fragment import androidx.recyclerview.widget.DividerItemDecoration -import com.google.android.gms.oss.licenses.OssLicensesMenuActivity import kotlinx.android.synthetic.main.fragment_about.* import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.BuildConfig @@ -119,7 +118,7 @@ class AboutFragment : Fragment(), AboutPageListener { private fun populateAboutList(): List { val context = requireContext() - return listOf( + return listOfNotNull( AboutPageItem( AboutItem.ExternalLink( WHATS_NEW, @@ -148,13 +147,9 @@ class AboutFragment : Fragment(), AboutPageListener { SupportUtils.getSumoURLForTopic(context, SupportUtils.SumoTopic.YOUR_RIGHTS) ), getString(R.string.about_know_your_rights) ), - AboutPageItem( + if (BuildConfig.USE_FREE_IMPLEMENTATION) null else AboutPageItem( AboutItem.ExternalLink(LICENSING_INFO, ABOUT_LICENSE_URL), getString(R.string.about_licensing_information) - ), - AboutPageItem( - AboutItem.Libraries, - getString(R.string.about_other_open_source_libraries) ) ) } @@ -168,13 +163,10 @@ class AboutFragment : Fragment(), AboutPageListener { } private fun openLibrariesPage() { - startActivity(Intent(context, OssLicensesMenuActivity::class.java)) - OssLicensesMenuActivity.setActivityTitle( - getString( - R.string.open_source_licenses_title, - appName - ) - ) + val title = getString(R.string.open_source_licenses_title, appName) + val ossLicensesMenu = OssLicensesMenuImpl() + val intent = ossLicensesMenu.getIntent(context, title) + startActivity(intent) } override fun onAboutItemClicked(item: AboutItem) { diff --git a/app/src/main/java/org/mozilla/fenix/settings/about/OssLicensesMenu.kt b/app/src/main/java/org/mozilla/fenix/settings/about/OssLicensesMenu.kt new file mode 100644 index 000000000000..f85ac42a536b --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/settings/about/OssLicensesMenu.kt @@ -0,0 +1,11 @@ +/* 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.about + +import android.content.Context +import android.content.Intent + +interface OssLicensesMenu { + fun getIntent(context: Context?, title: String): Intent +} \ No newline at end of file diff --git a/app/src/nonFree/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt b/app/src/nonFree/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt new file mode 100644 index 000000000000..fae0c354768d --- /dev/null +++ b/app/src/nonFree/java/org/mozilla/fenix/components/metrics/AdvertisingIDImpl.kt @@ -0,0 +1,37 @@ +/* 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.components.metrics + +import android.content.Context +import com.google.android.gms.ads.identifier.AdvertisingIdClient +import com.google.android.gms.common.GooglePlayServicesNotAvailableException +import com.google.android.gms.common.GooglePlayServicesRepairableException +import mozilla.components.support.base.log.logger.Logger +import java.io.IOException + +interface AdvertisingIDImpl : AdvertisingID { + @Suppress("TooGenericExceptionCaught") + override fun getAdvertisingID(context: Context): String? { + return try { + AdvertisingIdClient.getAdvertisingIdInfo(context).id + } catch (e: GooglePlayServicesNotAvailableException) { + Logger.debug("ActivationPing - Google Play not installed on the device") + null + } catch (e: GooglePlayServicesRepairableException) { + Logger.debug("ActivationPing - recoverable error connecting to Google Play Services") + null + } catch (e: IllegalStateException) { + // This is unlikely to happen, as this should be running off the main thread. + Logger.debug("ActivationPing - AdvertisingIdClient must be called off the main thread") + null + } catch (e: IOException) { + Logger.debug("ActivationPing - unable to connect to Google Play Services") + null + } catch (e: NullPointerException) { + Logger.debug("ActivationPing - no Google Advertising ID available") + null + } + } +} diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt b/app/src/nonFree/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt similarity index 99% rename from app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt rename to app/src/nonFree/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt index 4d9ca393eed7..cb56be2d208f 100644 --- a/app/src/main/java/org/mozilla/fenix/components/metrics/LeanplumMetricsService.kt +++ b/app/src/nonFree/java/org/mozilla/fenix/components/metrics/LeanplumMetricsServiceImpl.kt @@ -52,7 +52,7 @@ private val Event.name: String? else -> null } -class LeanplumMetricsService( +class LeanplumMetricsServiceImpl( private val application: Application, private val deviceIdGenerator: () -> String = { randomUUID().toString() } ) : MetricsService { diff --git a/app/src/main/java/org/mozilla/fenix/push/LeanplumNotificationCustomizer.kt b/app/src/nonFree/java/org/mozilla/fenix/push/LeanplumNotificationCustomizer.kt similarity index 100% rename from app/src/main/java/org/mozilla/fenix/push/LeanplumNotificationCustomizer.kt rename to app/src/nonFree/java/org/mozilla/fenix/push/LeanplumNotificationCustomizer.kt diff --git a/app/src/main/java/org/mozilla/fenix/push/FirebasePushService.kt b/app/src/nonFree/java/org/mozilla/fenix/push/PushServiceImpl.kt similarity index 97% rename from app/src/main/java/org/mozilla/fenix/push/FirebasePushService.kt rename to app/src/nonFree/java/org/mozilla/fenix/push/PushServiceImpl.kt index 2c060a2b4d13..68c9da0a496d 100644 --- a/app/src/main/java/org/mozilla/fenix/push/FirebasePushService.kt +++ b/app/src/nonFree/java/org/mozilla/fenix/push/PushServiceImpl.kt @@ -41,7 +41,7 @@ import mozilla.components.feature.push.AutoPushFeature * that lets us easily delegate the implementation to that, as well as make invocations when FCM * receives new messages. */ -class FirebasePushService : LeanplumPushFirebaseMessagingService(), +class PushServiceImpl : LeanplumPushFirebaseMessagingService(), PushService by AutoPushService { override fun onCreate() { diff --git a/app/src/nonFree/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt b/app/src/nonFree/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt new file mode 100644 index 000000000000..af695c799c5c --- /dev/null +++ b/app/src/nonFree/java/org/mozilla/fenix/settings/about/OssLicensesMenuImpl.kt @@ -0,0 +1,16 @@ +/* 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.about + +import android.content.Context +import android.content.Intent +import com.google.android.gms.oss.licenses.OssLicensesMenuActivity + +class OssLicensesMenuImpl : OssLicensesMenu { + override fun getIntent(context: Context?, title: String): Intent { + OssLicensesMenuActivity.setActivityTitle(title) + return Intent(context, OssLicensesMenuActivity::class.java) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/push/LeanplumNotificationCustomizerTest.kt b/app/src/test/java/org/mozilla/fenix/push/LeanplumNotificationCustomizerTest.kt index 709b07c16183..56077d7b6db1 100644 --- a/app/src/test/java/org/mozilla/fenix/push/LeanplumNotificationCustomizerTest.kt +++ b/app/src/test/java/org/mozilla/fenix/push/LeanplumNotificationCustomizerTest.kt @@ -14,7 +14,8 @@ import org.mozilla.fenix.R class LeanplumNotificationCustomizerTest { - private val customizer = LeanplumNotificationCustomizer() + private val customizer = + LeanplumNotificationCustomizer() @Test fun `customize adds icon`() { diff --git a/gradle.properties b/gradle.properties index 03d5464256aa..f9e5e7152778 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,3 +19,5 @@ android.enableR8=true android.enableR8.fullMode=true android.enableUnitTestBinaryResources=false org.gradle.parallel=false + +org.mozilla.fenix.useFreeImplementation=false