From cd9a169e6c3fec31131c6f9571ca6fbd61f27c76 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 23 Jul 2019 16:01:46 +0200 Subject: [PATCH] MOBILE-3084 firebase: Allow disabling analytics at runtime --- scripts/langindex.json | 2 ++ src/assets/lang/en.json | 2 ++ src/core/constants.ts | 1 + .../providers/pushnotifications.ts | 21 +++++++++++++++++++ src/core/settings/lang/en.json | 2 ++ src/core/settings/pages/general/general.html | 7 +++++++ src/core/settings/pages/general/general.ts | 21 ++++++++++++++++++- 7 files changed, 55 insertions(+), 1 deletion(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index 055b3b689..5d112af30 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1778,6 +1778,8 @@ "core.settings.disabled": "lesson", "core.settings.displayformat": "local_moodlemobileapp", "core.settings.enabledownloadsection": "local_moodlemobileapp", + "core.settings.enablefirebaseanalytics": "local_moodlemobileapp", + "core.settings.enablefirebaseanalyticsdescription": "local_moodlemobileapp", "core.settings.enablerichtexteditor": "local_moodlemobileapp", "core.settings.enablerichtexteditordescription": "local_moodlemobileapp", "core.settings.enablesyncwifi": "local_moodlemobileapp", diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index ac44418b2..975a16a7a 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -1779,6 +1779,8 @@ "core.settings.disabled": "Disabled", "core.settings.displayformat": "Display format", "core.settings.enabledownloadsection": "Enable download sections", + "core.settings.enablefirebaseanalytics": "Enable Firebase analytics", + "core.settings.enablefirebaseanalyticsdescription": "If enabled, the app will collect anonymous data usage.", "core.settings.enablerichtexteditor": "Enable text editor", "core.settings.enablerichtexteditordescription": "If enabled, a text editor will be available when entering content.", "core.settings.enablesyncwifi": "Allow sync only when on Wi-Fi", diff --git a/src/core/constants.ts b/src/core/constants.ts index 8884ec530..33e20f0c2 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -36,6 +36,7 @@ export class CoreConstants { static SETTINGS_REPORT_IN_BACKGROUND = 'CoreSettingsReportInBackground'; // @deprecated since 3.5.0 static SETTINGS_SEND_ON_ENTER = 'CoreSettingsSendOnEnter'; static SETTINGS_FONT_SIZE = 'CoreSettingsFontSize'; + static SETTINGS_ANALYTICS_ENABLED = 'CoreSettingsAnalyticsEnabled'; // WS constants. static WS_TIMEOUT = 30000; diff --git a/src/core/pushnotifications/providers/pushnotifications.ts b/src/core/pushnotifications/providers/pushnotifications.ts index f6b0201d4..17fe16bb5 100644 --- a/src/core/pushnotifications/providers/pushnotifications.ts +++ b/src/core/pushnotifications/providers/pushnotifications.ts @@ -240,6 +240,27 @@ export class CorePushNotificationsProvider { }); } + /** + * Enable or disable Firebase analytics. + * + * @param {boolean} enable Whether to enable or disable. + * @return {Promise} Promise resolved when done. + */ + enableAnalytics(enable: boolean): Promise { + const win = window; // This feature is only present in our fork of the plugin. + + if (CoreConfigConstants.enableanalytics && win.PushNotification && win.PushNotification.enableAnalytics) { + return new Promise((resolve, reject): void => { + win.PushNotification.enableAnalytics(resolve, (error) => { + this.logger.error('Error enabling or disabling Firebase analytics', enable, error); + resolve(); + }, !!enable); + }); + } + + return Promise.resolve(); + } + /** * Returns options for push notifications based on device. * diff --git a/src/core/settings/lang/en.json b/src/core/settings/lang/en.json index b4eab9bce..f0e09d02d 100644 --- a/src/core/settings/lang/en.json +++ b/src/core/settings/lang/en.json @@ -21,6 +21,8 @@ "disabled": "Disabled", "displayformat": "Display format", "enabledownloadsection": "Enable download sections", + "enablefirebaseanalytics": "Enable Firebase analytics", + "enablefirebaseanalyticsdescription": "If enabled, the app will collect anonymous data usage.", "enablerichtexteditor": "Enable text editor", "enablerichtexteditordescription": "If enabled, a text editor will be available when entering content.", "enablesyncwifi": "Allow sync only when on Wi-Fi", diff --git a/src/core/settings/pages/general/general.html b/src/core/settings/pages/general/general.html index 13d4e9681..0e1418813 100644 --- a/src/core/settings/pages/general/general.html +++ b/src/core/settings/pages/general/general.html @@ -34,4 +34,11 @@ + + +

{{ 'core.settings.enablefirebaseanalytics' | translate }}

+

{{ 'core.settings.enablefirebaseanalyticsdescription' | translate }}

+
+ +
diff --git a/src/core/settings/pages/general/general.ts b/src/core/settings/pages/general/general.ts index f5615c4ce..b752f8d5e 100644 --- a/src/core/settings/pages/general/general.ts +++ b/src/core/settings/pages/general/general.ts @@ -22,6 +22,7 @@ import { CoreEventsProvider } from '@providers/events'; import { CoreLangProvider } from '@providers/lang'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreLocalNotificationsProvider } from '@providers/local-notifications'; +import { CorePushNotificationsProvider } from '@core/pushnotifications/providers/pushnotifications'; import { CoreConfigConstants } from '../../../../configconstants'; /** @@ -41,10 +42,12 @@ export class CoreSettingsGeneralPage { rteSupported: boolean; richTextEditor: boolean; debugDisplay: boolean; + analyticsSupported: boolean; + analyticsEnabled: boolean; constructor(appProvider: CoreAppProvider, private configProvider: CoreConfigProvider, fileProvider: CoreFileProvider, private eventsProvider: CoreEventsProvider, private langProvider: CoreLangProvider, - private domUtils: CoreDomUtilsProvider, + private domUtils: CoreDomUtilsProvider, private pushNotificationsProvider: CorePushNotificationsProvider, localNotificationsProvider: CoreLocalNotificationsProvider) { // Get the supported languages. @@ -93,6 +96,13 @@ export class CoreSettingsGeneralPage { this.configProvider.get(CoreConstants.SETTINGS_DEBUG_DISPLAY, false).then((debugDisplay) => { this.debugDisplay = !!debugDisplay; }); + + this.analyticsSupported = CoreConfigConstants.enableanalytics; + if (this.analyticsSupported) { + this.configProvider.get(CoreConstants.SETTINGS_ANALYTICS_ENABLED, true).then((enabled) => { + this.analyticsEnabled = !!enabled; + }); + } } @ViewChild(Segment) @@ -134,4 +144,13 @@ export class CoreSettingsGeneralPage { this.configProvider.set(CoreConstants.SETTINGS_DEBUG_DISPLAY, this.debugDisplay ? 1 : 0); this.domUtils.setDebugDisplay(this.debugDisplay); } + + /** + * Called when the analytics setting is enabled or disabled. + */ + analyticsEnabledChanged(): void { + this.pushNotificationsProvider.enableAnalytics(this.analyticsEnabled).then(() => { + this.configProvider.set(CoreConstants.SETTINGS_ANALYTICS_ENABLED, this.analyticsEnabled ? 1 : 0); + }); + } }