Skip to content

Commit

Permalink
Move isTelemetryEnabled to GleanMetricsService
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexterp37 authored and mergify[bot] committed Jan 10, 2024
1 parent bd04436 commit 776d90e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.mozilla.focus.BuildConfig;
import org.mozilla.focus.FocusApplication;
import org.mozilla.focus.telemetry.TelemetryWrapper;
import org.mozilla.focus.telemetry.GleanMetricsService;

public class AdjustHelper {
public static void setupAdjustIfNeeded(FocusApplication application) {
Expand All @@ -27,7 +27,7 @@ public static void setupAdjustIfNeeded(FocusApplication application) {
throw new IllegalStateException("No adjust token defined for release build");
}

if (!TelemetryWrapper.isTelemetryEnabled(application)) {
if (!GleanMetricsService.isTelemetryEnabled(application)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ open class MainActivity : LocaleAwareAppCompatActivity() {
}

private fun processEraseAction(intent: SafeIntent) {
val fromShortcut = intent.getBooleanExtra(EXTRA_SHORTCUT, false)
val fromNotificationAction = intent.getBooleanExtra(EXTRA_NOTIFICATION, false)

components.tabsUseCases.removeAllTabs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class UrlInputFragment :

private fun onSearch(
query: String,
isSuggestion: Boolean = false,
@Suppress("UNUSED_PARAMETER") isSuggestion: Boolean = false,
alwaysSearch: Boolean = false,
) {
if (alwaysSearch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package org.mozilla.focus.telemetry
import android.content.Context
import android.os.Build
import android.os.RemoteException
import android.os.StrictMode
import androidx.annotation.VisibleForTesting
import androidx.core.app.NotificationManagerCompat
import androidx.preference.PreferenceManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers.IO
Expand All @@ -23,10 +25,10 @@ import mozilla.components.support.base.log.logger.Logger
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.config.Configuration
import org.mozilla.focus.BuildConfig
import org.mozilla.focus.R
import org.mozilla.focus.Components
import org.mozilla.focus.GleanMetrics.Browser
import org.mozilla.focus.GleanMetrics.GleanBuildInfo
import org.mozilla.focus.GleanMetrics.LegacyIds
import org.mozilla.focus.GleanMetrics.Metrics
import org.mozilla.focus.GleanMetrics.MozillaProducts
import org.mozilla.focus.GleanMetrics.Notifications
Expand All @@ -36,10 +38,9 @@ import org.mozilla.focus.GleanMetrics.Shortcuts
import org.mozilla.focus.GleanMetrics.TrackingProtection
import org.mozilla.focus.ext.components
import org.mozilla.focus.ext.settings
import org.mozilla.focus.telemetry.TelemetryWrapper.isTelemetryEnabled
import org.mozilla.focus.topsites.DefaultTopSitesStorage.Companion.TOP_SITES_MAX_LIMIT
import org.mozilla.focus.utils.AppConstants
import org.mozilla.focus.utils.Settings
import java.util.UUID

/**
* Glean telemetry service.
Expand All @@ -51,6 +52,37 @@ class GleanMetricsService(context: Context) : MetricsService {
@Suppress("UnusedPrivateMember")
private val activationPing = ActivationPing(context)

companion object {
private val isEnabledByDefault: Boolean
get() = !AppConstants.isKlarBuild

private fun isDeviceWithTelemetryDisabled(): Boolean {
val brand = "blackberry"
val device = "bbf100"

return Build.BRAND == brand && Build.DEVICE == device
}

@JvmStatic
fun isTelemetryEnabled(context: Context): Boolean {
if (isDeviceWithTelemetryDisabled()) { return false }

// The first access to shared preferences will require a disk read.
val threadPolicy = StrictMode.allowThreadDiskReads()
try {
val resources = context.resources
val preferences = PreferenceManager.getDefaultSharedPreferences(context)

return preferences.getBoolean(
resources.getString(R.string.pref_key_telemetry),
isEnabledByDefault,
) && !AppConstants.isDevBuild
} finally {
StrictMode.setThreadPolicy(threadPolicy)
}
}
}

@OptIn(DelicateCoroutinesApi::class)
override fun initialize(context: Context) {
val components = context.components
Expand Down Expand Up @@ -81,9 +113,6 @@ class GleanMetricsService(context: Context) : MetricsService {
// Wait for preferences to be collected before we send the activation ping.
collectPrefMetricsAsync(components, settings, context).await()

// Set the client ID in Glean as part of the deletion-request.
LegacyIds.clientId.set(UUID.fromString(TelemetryWrapper.clientId))

components.store.waitForSelectedOrDefaultSearchEngine { searchEngine ->
if (searchEngine != null) {
Browser.defaultSearchEngine.set(getDefaultSearchEngineIdentifierForTelemetry(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import android.util.AttributeSet
import mozilla.components.service.glean.Glean
import org.mozilla.focus.R
import org.mozilla.focus.settings.LearnMoreSwitchPreference
import org.mozilla.focus.telemetry.TelemetryWrapper
import org.mozilla.focus.telemetry.GleanMetricsService
import org.mozilla.focus.utils.SupportUtils
import org.mozilla.telemetry.TelemetryHolder

/**
* Switch preference for enabling/disabling telemetry
Expand All @@ -20,21 +19,13 @@ internal class TelemetrySwitchPreference(context: Context, attrs: AttributeSet?)
LearnMoreSwitchPreference(context, attrs) {

init {
isChecked = TelemetryWrapper.isTelemetryEnabled(context)
isChecked = GleanMetricsService.isTelemetryEnabled(context)
}

override fun onClick() {
super.onClick()
TelemetryHolder.get()
.configuration.isUploadEnabled = isChecked

Glean.setUploadEnabled(isChecked)

if (isChecked) {
TelemetryWrapper.startSession()
} else {
TelemetryWrapper.stopSession()
}
}

override fun getDescription(): String {
Expand Down

0 comments on commit 776d90e

Please sign in to comment.