forked from mozilla-mobile/focus-android
/
TelemetryWrapper.kt
995 lines (847 loc) · 35.6 KB
/
TelemetryWrapper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* 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/. */
// We haven't migrated from TelemetryEventPingBuilder to MobileEventPingBuilder yet.
@file:Suppress("DEPRECATION")
package org.mozilla.focus.telemetry
import android.content.Context
import android.net.http.SslError
import android.os.Build
import android.os.StrictMode
import androidx.annotation.CheckResult
import androidx.annotation.VisibleForTesting
import androidx.preference.PreferenceManager
import kotlinx.coroutines.runBlocking
import mozilla.components.browser.state.selector.privateTabs
import mozilla.components.ui.autocomplete.InlineAutocompleteEditText
import org.json.JSONObject
import org.mozilla.focus.BuildConfig
import org.mozilla.focus.R
import org.mozilla.focus.ext.components
import org.mozilla.focus.search.CustomSearchEngineStore
import org.mozilla.focus.utils.AppConstants
import org.mozilla.focus.utils.MobileMetricsPingStorage
import org.mozilla.focus.utils.Settings
import org.mozilla.focus.utils.UrlUtils
import org.mozilla.telemetry.Telemetry
import org.mozilla.telemetry.TelemetryHolder
import org.mozilla.telemetry.config.TelemetryConfiguration
import org.mozilla.telemetry.event.TelemetryEvent
import org.mozilla.telemetry.measurement.DefaultSearchMeasurement
import org.mozilla.telemetry.measurement.SearchesMeasurement
import org.mozilla.telemetry.net.TelemetryClient
import org.mozilla.telemetry.ping.TelemetryCorePingBuilder
import org.mozilla.telemetry.ping.TelemetryEventPingBuilder
import org.mozilla.telemetry.ping.TelemetryMobileMetricsPingBuilder
import org.mozilla.telemetry.schedule.jobscheduler.JobSchedulerTelemetryScheduler
import org.mozilla.telemetry.serialize.JSONPingSerializer
import org.mozilla.telemetry.storage.FileTelemetryStorage
import java.net.MalformedURLException
import java.net.URL
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.collections.HashSet
@Suppress(
// Yes, this a large class with a lot of functions. But it's very simple and still easy to read.
"TooManyFunctions",
"LargeClass"
)
object TelemetryWrapper {
private const val TELEMETRY_APP_NAME_FOCUS = "Focus"
private const val TELEMETRY_APP_NAME_KLAR = "Klar"
private const val LAST_MOBILE_METRICS_PINGS = "LAST_MOBILE_METRICS_PINGS"
private val dateFormat = SimpleDateFormat("yyyyMMdd", Locale.US)
private const val MAXIMUM_CUSTOM_TAB_EXTRAS = 10
private const val HISTOGRAM_SIZE = 200
private const val BUCKET_SIZE_MS = 100
private const val HISTOGRAM_MIN_INDEX = 0
private val isEnabledByDefault: Boolean
get() = !AppConstants.isKlarBuild
private object Category {
val ACTION = "action"
val ERROR = "error"
val HISTOGRAM = "histogram"
}
private object Method {
val TYPE_URL = "type_url"
val TYPE_QUERY = "type_query"
val TYPE_SELECT_QUERY = "select_query"
val CLICK = "click"
val SWIPE = "swipe"
val CANCEL = "cancel"
val LONG_PRESS = "long_press"
val CHANGE = "change"
val FOREGROUND = "foreground"
val BACKGROUND = "background"
val SHARE = "share"
val SAVE = "save"
val COPY = "copy"
val OPEN = "open"
val INSTALL = "install"
val INTENT_URL = "intent_url"
val INTENT_CUSTOM_TAB = "intent_custom_tab"
val TEXT_SELECTION_INTENT = "text_selection_intent"
val SHOW = "show"
val HIDE = "hide"
val SHARE_INTENT = "share_intent"
val REMOVE = "remove"
val REMOVE_ALL = "remove_all"
val REORDER = "reorder"
val RESTORE = "restore"
val PAGE = "page"
val RESOURCE = "resource"
}
private object Object {
val SEARCH_BAR = "search_bar"
val ERASE_BUTTON = "erase_button"
val SETTING = "setting"
val APP = "app"
val MENU = "menu"
val BACK_BUTTON = "back_button"
val NOTIFICATION = "notification"
val NOTIFICATION_ACTION = "notification_action"
val SHORTCUT = "shortcut"
val BLOCKING_SWITCH = "blocking_switch"
val DESKTOP_REQUEST_CHECK = "desktop_request_check"
val BROWSER = "browser"
val BROWSER_CONTEXTMENU = "browser_contextmenu"
val CUSTOM_TAB_CLOSE_BUTTON = "custom_tab_close_but"
val CUSTOM_TAB_ACTION_BUTTON = "custom_tab_action_bu"
val FIRSTRUN = "firstrun"
val DOWNLOAD_DIALOG = "download_dialog"
val ADD_TO_HOMESCREEN_DIALOG = "add_to_homescreen_dialog"
val HOMESCREEN_SHORTCUT = "homescreen_shortcut"
val TABS_TRAY = "tabs_tray"
val RECENT_APPS = "recent_apps"
val APP_ICON = "app_icon"
val AUTOCOMPLETE_DOMAIN = "autocomplete_domain"
val AUTOFILL = "autofill"
val SEARCH_ENGINE_SETTING = "search_engine_setting"
val ADD_SEARCH_ENGINE_LEARN_MORE = "search_engine_learn_more"
val CUSTOM_SEARCH_ENGINE = "custom_search_engine"
val REMOVE_SEARCH_ENGINES = "remove_search_engines"
val GECKO_ENGINE = "gecko_engine"
val TIP = "tip"
val SEARCH_SUGGESTION_PROMPT = "search_suggestion_prompt"
val MAKE_DEFAULT_BROWSER_OPEN_WITH = "make_default_browser_open_with"
val MAKE_DEFAULT_BROWSER_SETTINGS = "make_default_browser_settings"
val ALLOWLIST = "allowlist"
val CRASH_REPORTER = "crash_reporter"
}
private object Value {
val DEFAULT = "default"
val FIREFOX = "firefox"
val SELECTION = "selection"
val ERASE = "erase"
val ERASE_AND_OPEN = "erase_open"
val ERASE_TO_HOME = "erase_home"
val ERASE_TO_APP = "erase_app"
val IMAGE = "image"
val LINK = "link"
val IMAGE_WITH_LINK = "image+link"
val CUSTOM_TAB = "custom_tab"
val SKIP = "skip"
val FINISH = "finish"
val OPEN = "open"
val DOWNLOAD = "download"
val URL = "url"
val SEARCH = "search"
val CANCEL = "cancel"
val ADD_TO_HOMESCREEN = "add_to_homescreen"
val TAB = "tab"
val WHATS_NEW = "whats_new"
val RESUME = "resume"
val RELOAD = "refresh"
val FULL_BROWSER = "full_browser"
val REPORT_ISSUE = "report_issue"
val SETTINGS = "settings"
val QUICK_ADD = "quick_add"
val FIND_IN_PAGE = "find_in_page"
val DEFAULT_BROWSER_TIP = "default_browser_tip"
val DISABLE_TRACKING_PROTECTION_TIP = "disable_tracking_protection_tip"
val ADD_TO_HOMESCREEN_TIP = "add_to_homescreen_tip"
val AUTOCOMPLETE_URL_TIP = "autocomplete_url_tip"
val OPEN_IN_NEW_TAB_TIP = "open_in_new_tab_tip"
val DISABLE_TIPS_TIP = "disable_tips_tip"
val SURVEY_TIP = "survey_tip"
val SURVEY_TIP_ES = "survey_tip_es"
val SURVEY_TIP_FR = "survey_tip_fr"
val CLOSE_TAB = "close_tab"
}
private object Extra {
val FROM = "from"
val TO = "to"
val TOTAL = "total"
val SELECTED = "selected"
val HIGHLIGHTED = "highlighted"
val AUTOCOMPLETE = "autocomplete"
val SOURCE = "source"
val SUCCESS = "success"
val ERROR_CODE = "error_code"
val SEARCH_SUGGESTION = "search_suggestion"
val TOTAL_URI_COUNT = "total_uri_count"
val UNIQUE_DOMAINS_COUNT = "unique_domains_count"
val SUBMIT_CRASH = "submit_crash"
}
enum class BrowserContextMenuValue(val value: String) {
Link(Value.LINK),
Image(Value.IMAGE),
ImageWithLink(Value.IMAGE_WITH_LINK);
override fun toString(): String = value
}
@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)
}
}
@JvmStatic
fun init(context: Context) {
// When initializing the telemetry library it will make sure that all directories exist and
// are readable/writable.
val threadPolicy = StrictMode.allowThreadDiskWrites()
try {
val resources = context.resources
val telemetryEnabled = isTelemetryEnabled(context)
val configuration = TelemetryConfiguration(context)
.setServerEndpoint("https://incoming.telemetry.mozilla.org")
.setAppName(if (AppConstants.isKlarBuild) TELEMETRY_APP_NAME_KLAR else TELEMETRY_APP_NAME_FOCUS)
.setUpdateChannel(BuildConfig.BUILD_TYPE)
.setPreferencesImportantForTelemetry(
resources.getString(R.string.pref_key_search_engine),
resources.getString(R.string.pref_key_privacy_block_ads),
resources.getString(R.string.pref_key_privacy_block_analytics),
resources.getString(R.string.pref_key_privacy_block_social),
resources.getString(R.string.pref_key_privacy_block_other),
resources.getString(R.string.pref_key_performance_block_javascript),
resources.getString(R.string.pref_key_performance_enable_cookies),
resources.getString(R.string.pref_key_performance_block_webfonts),
resources.getString(R.string.pref_key_locale),
resources.getString(R.string.pref_key_secure),
resources.getString(R.string.pref_key_default_browser),
resources.getString(R.string.pref_key_autocomplete_preinstalled),
resources.getString(R.string.pref_key_autocomplete_custom),
resources.getString(R.string.pref_key_remote_debugging),
resources.getString(R.string.pref_key_homescreen_tips),
resources.getString(R.string.pref_key_open_new_tab),
resources.getString(R.string.pref_key_show_search_suggestions)
)
.setSettingsProvider(TelemetrySettingsProvider(context))
.setCollectionEnabled(telemetryEnabled)
.setUploadEnabled(telemetryEnabled)
.setBuildId(TelemetryConfiguration(context).buildId)
val serializer = JSONPingSerializer()
val storage = FileTelemetryStorage(configuration, serializer)
val client = TelemetryClient(context.components.client.unwrap())
val scheduler = JobSchedulerTelemetryScheduler()
TelemetryHolder.set(Telemetry(configuration, storage, client, scheduler)
.addPingBuilder(TelemetryCorePingBuilder(configuration))
.addPingBuilder(TelemetryEventPingBuilder(configuration))
.also {
if (!dayPassedSinceLastUpload(context)) return@also
runBlocking {
val metricsStorage = MobileMetricsPingStorage(context)
val mobileMetrics = metricsStorage.load() ?: JSONObject()
metricsStorage.clearStorage()
it.addPingBuilder(TelemetryMobileMetricsPingBuilder(mobileMetrics,
configuration))
}
// Record new edited date
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putLong(LAST_MOBILE_METRICS_PINGS, (dateFormat.format(Date()).toLong()))
.apply()
}
.setDefaultSearchProvider(createDefaultSearchProvider(context)))
} finally {
StrictMode.setThreadPolicy(threadPolicy)
}
}
val clientId: String
get() = TelemetryHolder.get().clientId
private fun createDefaultSearchProvider(context: Context): DefaultSearchMeasurement.DefaultSearchEngineProvider {
return DefaultSearchMeasurement.DefaultSearchEngineProvider {
runBlocking {
getDefaultSearchEngineIdentifierForTelemetry(context)
}
}
}
/**
* Add the position of the current session and total number of sessions as extras to the event
* and return it.
*/
@CheckResult
private fun withSessionCounts(event: TelemetryEvent): TelemetryEvent {
val context = TelemetryHolder.get().configuration.context
val state = context.components.store.state
val privateTabs = state.privateTabs
val positionOfSelectedTab = privateTabs.indexOfFirst { tab -> tab.id == state.selectedTabId }
event.extra(Extra.SELECTED, positionOfSelectedTab.toString())
event.extra(Extra.TOTAL, privateTabs.size.toString())
return event
}
@JvmStatic
fun startSession() {
TelemetryHolder.get().recordSessionStart()
TelemetryEvent.create(Category.ACTION, Method.FOREGROUND, Object.APP).queue()
}
@VisibleForTesting var histogram = IntArray(HISTOGRAM_SIZE)
@VisibleForTesting var domainMap = HashSet<String>()
@VisibleForTesting var numUri = 0
@JvmStatic
fun addLoadToHistogram(url: String, newLoadTime: Long) {
try {
domainMap.add(UrlUtils.stripCommonSubdomains(URL(url).host))
numUri++
var histogramLoadIndex = (newLoadTime / BUCKET_SIZE_MS).toInt()
if (histogramLoadIndex > (HISTOGRAM_SIZE - 2)) {
histogramLoadIndex = HISTOGRAM_SIZE - 1
} else if (histogramLoadIndex < HISTOGRAM_MIN_INDEX) {
histogramLoadIndex = HISTOGRAM_MIN_INDEX
}
histogram[histogramLoadIndex]++
} catch (e: MalformedURLException) {
// ignore invalid URLs
}
}
@JvmStatic
fun stopSession() {
TelemetryHolder.get().recordSessionEnd()
val histogramEvent = TelemetryEvent.create(Category.HISTOGRAM, Method.FOREGROUND, Object.BROWSER)
for (bucketIndex in histogram.indices) {
histogramEvent.extra((bucketIndex * BUCKET_SIZE_MS).toString(), histogram[bucketIndex].toString())
}
histogramEvent.queue()
// Clear histogram array after queueing it
histogram = IntArray(HISTOGRAM_SIZE)
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.BROWSER).extra(
Extra.UNIQUE_DOMAINS_COUNT,
domainMap.size.toString()
).queue()
domainMap.clear()
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.BROWSER).extra(
Extra.TOTAL_URI_COUNT,
numUri.toString()
).queue()
numUri = 0
TelemetryEvent.create(Category.ACTION, Method.BACKGROUND, Object.APP).queue()
}
@JvmStatic
fun stopMainActivity() {
TelemetryHolder.get()
.queuePing(TelemetryCorePingBuilder.TYPE)
.queuePing(TelemetryEventPingBuilder.TYPE)
.queuePing(TelemetryMobileMetricsPingBuilder.TYPE)
.scheduleUpload()
}
@JvmStatic
fun urlBarEvent(isUrl: Boolean, autocompleteResult: InlineAutocompleteEditText.AutocompleteResult) {
if (isUrl) {
TelemetryWrapper.browseEvent(autocompleteResult)
} else {
TelemetryWrapper.searchEnterEvent()
}
}
private fun browseEvent(autocompleteResult: InlineAutocompleteEditText.AutocompleteResult) {
val event = TelemetryEvent.create(Category.ACTION, Method.TYPE_URL, Object.SEARCH_BAR)
.extra(Extra.AUTOCOMPLETE, (!autocompleteResult.text.isEmpty()).toString())
if (!autocompleteResult.text.isEmpty()) {
event.extra(Extra.TOTAL, autocompleteResult.totalItems.toString())
event.extra(Extra.SOURCE, autocompleteResult.source)
}
event.queue()
}
@JvmStatic
fun browseIntentEvent() {
TelemetryEvent.create(Category.ACTION, Method.INTENT_URL, Object.APP).queue()
}
@JvmStatic
fun shareIntentEvent(isSearch: Boolean) {
if (isSearch) {
TelemetryEvent.create(Category.ACTION, Method.SHARE_INTENT, Object.APP, Value.SEARCH).queue()
} else {
TelemetryEvent.create(Category.ACTION, Method.SHARE_INTENT, Object.APP, Value.URL).queue()
}
}
/**
* Sends a list of the custom tab options that a custom-tab intent made use of.
*/
@JvmStatic
fun customTabsIntentEvent(options: List<String>) {
val event = TelemetryEvent.create(Category.ACTION, Method.INTENT_CUSTOM_TAB, Object.APP)
// We can send at most 10 extras per event - we just ignore the rest if there are too many
val extrasCount: Int = if (options.size > MAXIMUM_CUSTOM_TAB_EXTRAS) {
MAXIMUM_CUSTOM_TAB_EXTRAS
} else {
options.size
}
for (option in options.subList(0, extrasCount)) {
event.extra(option, "true")
}
event.queue()
}
@JvmStatic
fun downloadDialogDownloadEvent(sentToDownload: Boolean) {
if (sentToDownload) {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.DOWNLOAD_DIALOG, Value.DOWNLOAD).queue()
} else {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.DOWNLOAD_DIALOG, Value.CANCEL).queue()
}
}
@JvmStatic
fun closeCustomTabEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.CUSTOM_TAB_CLOSE_BUTTON))
.queue()
}
@JvmStatic
fun customTabActionButtonEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.CUSTOM_TAB_ACTION_BUTTON).queue()
}
@JvmStatic
fun customTabMenuEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.CUSTOM_TAB).queue()
}
@JvmStatic
fun textSelectionIntentEvent() {
TelemetryEvent.create(Category.ACTION, Method.TEXT_SELECTION_INTENT, Object.APP).queue()
}
private fun searchEnterEvent() {
val telemetry = TelemetryHolder.get()
TelemetryEvent.create(Category.ACTION, Method.TYPE_QUERY, Object.SEARCH_BAR).queue()
val searchEngine = getDefaultSearchEngineIdentifierForTelemetry(telemetry.configuration.context)
telemetry.recordSearch(SearchesMeasurement.LOCATION_ACTIONBAR, searchEngine)
}
@JvmStatic
fun searchSelectEvent(isSearchSuggestion: Boolean) {
val telemetry = TelemetryHolder.get()
TelemetryEvent
.create(Category.ACTION, Method.TYPE_SELECT_QUERY, Object.SEARCH_BAR)
.extra(Extra.SEARCH_SUGGESTION, "$isSearchSuggestion")
.queue()
val searchEngineIdentifier = getDefaultSearchEngineIdentifierForTelemetry(telemetry.configuration.context)
telemetry.recordSearch(SearchesMeasurement.LOCATION_SUGGESTION, searchEngineIdentifier)
}
private fun getDefaultSearchEngineIdentifierForTelemetry(context: Context): String {
val searchEngine = context.components.searchEngineManager.getDefaultSearchEngine(
context,
Settings.getInstance(context).defaultSearchEngineName
).identifier
return if (CustomSearchEngineStore.isCustomSearchEngine(searchEngine, context)) {
// Don't collect possibly sensitive info for custom search engines, send "custom" instead
CustomSearchEngineStore.ENGINE_TYPE_CUSTOM
} else {
searchEngine
}
}
@JvmStatic
fun eraseEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.ERASE_BUTTON))
.queue()
}
@JvmStatic
fun eraseBackToHomeEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.BACK_BUTTON, Value.ERASE_TO_HOME))
.queue()
}
@JvmStatic
fun eraseBackToAppEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.BACK_BUTTON, Value.ERASE_TO_APP))
.queue()
}
@JvmStatic
fun eraseNotificationEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.NOTIFICATION, Value.ERASE))
.queue()
}
@JvmStatic
fun eraseAndOpenNotificationActionEvent() {
withSessionCounts(TelemetryEvent.create(
Category.ACTION,
Method.CLICK,
Object.NOTIFICATION_ACTION,
Value.ERASE_AND_OPEN)
).queue()
}
@JvmStatic
fun crashReporterOpened() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.CRASH_REPORTER).queue()
}
@JvmStatic
fun closeTabButtonTapped(crashSubmitted: Boolean) {
TelemetryEvent.create(
Category.ACTION,
Method.CLICK,
Object.CRASH_REPORTER,
Value.CLOSE_TAB)
.extra(Extra.SUBMIT_CRASH, crashSubmitted.toString()).queue()
}
@JvmStatic
fun openNotificationActionEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.NOTIFICATION_ACTION, Value.OPEN).queue()
}
@JvmStatic
fun openHomescreenShortcutEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.HOMESCREEN_SHORTCUT, Value.OPEN).queue()
}
@JvmStatic
fun addToHomescreenShortcutEvent() {
TelemetryEvent.create(
Category.ACTION,
Method.CLICK,
Object.ADD_TO_HOMESCREEN_DIALOG,
Value.ADD_TO_HOMESCREEN
).queue()
}
@JvmStatic
fun cancelAddToHomescreenShortcutEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.ADD_TO_HOMESCREEN_DIALOG, Value.CANCEL).queue()
}
@JvmStatic
fun eraseShortcutEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.SHORTCUT, Value.ERASE))
.queue()
}
@JvmStatic
fun eraseAndOpenShortcutEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.SHORTCUT, Value.ERASE_AND_OPEN))
.queue()
}
@JvmStatic
fun eraseTaskRemoved() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.RECENT_APPS, Value.ERASE))
.queue()
}
@JvmStatic
fun settingsEvent(key: String, value: String) {
TelemetryEvent.create(Category.ACTION, Method.CHANGE, Object.SETTING, key)
.extra(Extra.TO, value)
.queue()
}
@JvmStatic
fun shareEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHARE, Object.MENU).queue()
}
@JvmStatic
fun shareLinkEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHARE, Object.BROWSER_CONTEXTMENU, Value.LINK).queue()
}
@JvmStatic
fun shareImageEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHARE, Object.BROWSER_CONTEXTMENU, Value.IMAGE).queue()
}
@JvmStatic
fun saveImageEvent() {
TelemetryEvent.create(Category.ACTION, Method.SAVE, Object.BROWSER_CONTEXTMENU, Value.IMAGE).queue()
}
@JvmStatic
fun copyLinkEvent() {
TelemetryEvent.create(Category.ACTION, Method.COPY, Object.BROWSER_CONTEXTMENU, Value.LINK).queue()
}
@JvmStatic
fun copyImageEvent() {
TelemetryEvent.create(Category.ACTION, Method.COPY, Object.BROWSER_CONTEXTMENU, Value.IMAGE).queue()
}
@JvmStatic
fun openLinkInNewTabEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.BROWSER_CONTEXTMENU, Value.TAB))
.queue()
}
@JvmStatic
fun openLinkInFullBrowserFromCustomTabEvent() {
withSessionCounts(
TelemetryEvent.create(
Category.ACTION,
Method.OPEN,
Object.BROWSER_CONTEXTMENU,
Value.FULL_BROWSER
)
)
.queue()
}
@JvmStatic
fun openWebContextMenuEvent() {
TelemetryEvent.create(Category.ACTION, Method.LONG_PRESS, Object.BROWSER).queue()
}
@JvmStatic
fun cancelWebContextMenuEvent(value: BrowserContextMenuValue) {
TelemetryEvent.create(Category.ACTION, Method.CANCEL, Object.BROWSER_CONTEXTMENU, value.toString()).queue()
}
@JvmStatic
fun openDefaultAppEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.DEFAULT).queue()
}
/**
* Switching from a custom tab to the full-featured browser (regular tab).
*/
@JvmStatic
fun openFullBrowser() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.FULL_BROWSER).queue()
}
@JvmStatic
fun openFromIconEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.APP_ICON, Value.OPEN).queue()
}
@JvmStatic
fun resumeFromIconEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.APP_ICON, Value.RESUME).queue()
}
@JvmStatic
fun openFirefoxEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.FIREFOX).queue()
}
@JvmStatic
fun installFirefoxEvent() {
TelemetryEvent.create(Category.ACTION, Method.INSTALL, Object.APP, Value.FIREFOX).queue()
}
@JvmStatic
fun openSelectionEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.SELECTION).queue()
}
@JvmStatic
fun blockingSwitchEvent(isBlockingEnabled: Boolean) {
TelemetryEvent.create(
Category.ACTION,
Method.CLICK,
Object.BLOCKING_SWITCH,
isBlockingEnabled.toString()
).queue()
}
@JvmStatic
fun openExceptionsListSetting() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.ALLOWLIST).queue()
}
fun removeExceptionDomains(count: Int) {
TelemetryEvent.create(Category.ACTION, Method.REMOVE, Object.ALLOWLIST)
.extra(Extra.TOTAL, count.toString())
.queue()
}
fun removeAllExceptionDomains(count: Int) {
TelemetryEvent.create(Category.ACTION, Method.REMOVE_ALL, Object.ALLOWLIST)
.extra(Extra.TOTAL, count.toString())
.queue()
}
@JvmStatic
fun desktopRequestCheckEvent(shouldRequestDesktop: Boolean) {
TelemetryEvent.create(
Category.ACTION,
Method.CLICK,
Object.DESKTOP_REQUEST_CHECK,
shouldRequestDesktop.toString()
).queue()
}
@JvmStatic
fun showFirstRunPageEvent(page: Int) {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.FIRSTRUN, page.toString()).queue()
}
@JvmStatic
fun skipFirstRunEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.FIRSTRUN, Value.SKIP).queue()
}
@JvmStatic
fun finishFirstRunEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.FIRSTRUN, Value.FINISH).queue()
}
@JvmStatic
fun openTabsTrayEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.TABS_TRAY).queue()
}
@JvmStatic
fun openWhatsNewEvent(highlighted: Boolean) {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.MENU, Value.WHATS_NEW)
.extra(Extra.HIGHLIGHTED, highlighted.toString())
.queue()
}
@JvmStatic
fun findInPageMenuEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.MENU, Value.FIND_IN_PAGE).queue()
}
@JvmStatic
fun closeTabsTrayEvent() {
TelemetryEvent.create(Category.ACTION, Method.HIDE, Object.TABS_TRAY).queue()
}
@JvmStatic
fun switchTabInTabsTrayEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.TABS_TRAY, Value.TAB))
.queue()
}
@JvmStatic
fun eraseInTabsTrayEvent() {
withSessionCounts(TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.TABS_TRAY, Value.ERASE))
.queue()
}
@JvmStatic
fun swipeReloadEvent() {
TelemetryEvent.create(Category.ACTION, Method.SWIPE, Object.BROWSER, Value.RELOAD).queue()
}
@JvmStatic
fun menuReloadEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.MENU, Value.RELOAD).queue()
}
@JvmStatic
fun sslErrorEvent(fromPage: Boolean, error: SslError) {
// SSL Errors from https://developer.android.com/reference/android/net/http/SslError.html
val primaryErrorMessage = when (error.primaryError) {
SslError.SSL_DATE_INVALID -> "SSL_DATE_INVALID"
SslError.SSL_EXPIRED -> "SSL_EXPIRED"
SslError.SSL_IDMISMATCH -> "SSL_IDMISMATCH"
SslError.SSL_NOTYETVALID -> "SSL_NOTYETVALID"
SslError.SSL_UNTRUSTED -> "SSL_UNTRUSTED"
SslError.SSL_INVALID -> "SSL_INVALID"
else -> "Undefined SSL Error"
}
TelemetryEvent.create(Category.ERROR, if (fromPage) Method.PAGE else Method.RESOURCE, Object.BROWSER)
.extra(Extra.ERROR_CODE, primaryErrorMessage)
.queue()
}
enum class AutoCompleteEventSource {
SETTINGS, QUICK_ADD
}
fun saveAutocompleteDomainEvent(eventSource: AutoCompleteEventSource) {
val source = when (eventSource) {
AutoCompleteEventSource.SETTINGS -> Value.SETTINGS
AutoCompleteEventSource.QUICK_ADD -> Value.QUICK_ADD
}
TelemetryEvent.create(Category.ACTION, Method.SAVE, Object.AUTOCOMPLETE_DOMAIN)
.extra(Extra.SOURCE, source)
.queue()
}
fun removeAutocompleteDomainsEvent(count: Int) {
TelemetryEvent.create(Category.ACTION, Method.REMOVE, Object.AUTOCOMPLETE_DOMAIN)
.extra(Extra.TOTAL, count.toString())
.queue()
}
fun reorderAutocompleteDomainEvent(from: Int, to: Int) {
TelemetryEvent.create(Category.ACTION, Method.REORDER, Object.AUTOCOMPLETE_DOMAIN)
.extra(Extra.FROM, from.toString())
.extra(Extra.TO, to.toString())
.queue()
}
fun autofillShownEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.AUTOFILL).queue()
}
@JvmStatic
fun autofillPerformedEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.AUTOFILL).queue()
}
@JvmStatic
fun setDefaultSearchEngineEvent(source: String) {
TelemetryEvent.create(Category.ACTION, Method.SAVE, Object.SEARCH_ENGINE_SETTING)
.extra(Extra.SOURCE, source)
.queue()
}
@JvmStatic
fun openSearchSettingsEvent() {
TelemetryEvent.create(Category.ACTION, Method.OPEN, Object.SEARCH_ENGINE_SETTING).queue()
}
@JvmStatic
fun menuRemoveEnginesEvent() {
TelemetryEvent.create(Category.ACTION, Method.REMOVE, Object.SEARCH_ENGINE_SETTING).queue()
}
@JvmStatic
fun menuRestoreEnginesEvent() {
TelemetryEvent.create(Category.ACTION, Method.RESTORE, Object.SEARCH_ENGINE_SETTING).queue()
}
@JvmStatic
fun menuAddSearchEngineEvent() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.CUSTOM_SEARCH_ENGINE).queue()
}
@JvmStatic
fun saveCustomSearchEngineEvent(success: Boolean) {
TelemetryEvent.create(Category.ACTION, Method.SAVE, Object.CUSTOM_SEARCH_ENGINE)
.extra(Extra.SUCCESS, success.toString())
.queue()
}
@JvmStatic
fun removeSearchEnginesEvent(selected: Int) {
TelemetryEvent.create(Category.ACTION, Method.REMOVE, Object.REMOVE_SEARCH_ENGINES)
.extra(Extra.SELECTED, selected.toString())
.queue()
}
@JvmStatic
fun addSearchEngineLearnMoreEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.ADD_SEARCH_ENGINE_LEARN_MORE).queue()
}
@JvmStatic
fun changeToGeckoEngineEvent() {
TelemetryEvent.create(Category.ACTION, Method.CHANGE, Object.GECKO_ENGINE).queue()
}
@JvmStatic
fun reportSiteIssueEvent() {
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.MENU, Value.REPORT_ISSUE).queue()
}
@JvmStatic
fun respondToSearchSuggestionPrompt(enable: Boolean) {
TelemetryEvent
.create(Category.ACTION, Method.CLICK, Object.SEARCH_SUGGESTION_PROMPT, "$enable")
.queue()
}
@JvmStatic
fun makeDefaultBrowserOpenWith() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.MAKE_DEFAULT_BROWSER_OPEN_WITH).queue()
}
@JvmStatic
fun makeDefaultBrowserSettings() {
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.MAKE_DEFAULT_BROWSER_SETTINGS).queue()
}
@JvmStatic
fun dayPassedSinceLastUpload(context: Context): Boolean {
val dateOfLastPing = PreferenceManager
.getDefaultSharedPreferences(context).getLong(LAST_MOBILE_METRICS_PINGS, 0)
// Make sure a minimum of 1 day has passed since we collected data
val currentDateLong = dateFormat.format(Date()).toLong()
return currentDateLong > dateOfLastPing
}
@JvmStatic
@Suppress("ComplexMethod") // Not actually complex
fun displayTipEvent(tipId: Int) {
val telemetryValue = when (tipId) {
R.string.tip_open_in_new_tab -> Value.OPEN_IN_NEW_TAB_TIP
R.string.tip_add_to_homescreen -> Value.ADD_TO_HOMESCREEN_TIP
R.string.tip_disable_tracking_protection -> Value.DISABLE_TRACKING_PROTECTION_TIP
R.string.tip_set_default_browser -> Value.DEFAULT_BROWSER_TIP
R.string.tip_autocomplete_url -> Value.AUTOCOMPLETE_URL_TIP
R.string.tip_disable_tips2 -> Value.DISABLE_TIPS_TIP
R.string.tip_take_survey -> Value.SURVEY_TIP
R.string.tip_take_survey_es -> Value.SURVEY_TIP_ES
R.string.tip_take_survey_fr -> Value.SURVEY_TIP_FR
else -> {
// Unknown tip, fail silently rather than crashing.
return
}
}
TelemetryEvent.create(Category.ACTION, Method.SHOW, Object.TIP, telemetryValue).queue()
}
@JvmStatic
fun pressTipEvent(tipId: Int) {
val telemetryValue = when (tipId) {
R.string.tip_open_in_new_tab -> Value.OPEN_IN_NEW_TAB_TIP
R.string.tip_add_to_homescreen -> Value.ADD_TO_HOMESCREEN_TIP
R.string.tip_set_default_browser -> Value.DEFAULT_BROWSER_TIP
R.string.tip_autocomplete_url -> Value.AUTOCOMPLETE_URL_TIP
R.string.tip_disable_tips2 -> Value.DISABLE_TIPS_TIP
R.string.tip_take_survey -> Value.SURVEY_TIP
R.string.tip_take_survey_es -> Value.SURVEY_TIP_ES
R.string.tip_take_survey_fr -> Value.SURVEY_TIP_FR
else -> {
// Unknown tip, fail silently rather than crashing.
return
}
}
TelemetryEvent.create(Category.ACTION, Method.CLICK, Object.TIP, telemetryValue).queue()
}
private fun isDeviceWithTelemetryDisabled(): Boolean {
val brand = "blackberry"
val device = "bbf100"
return Build.BRAND == brand && Build.DEVICE == device
}
}