diff --git a/3party/Alohalytics/src/apple/alohalytics_objc.mm b/3party/Alohalytics/src/apple/alohalytics_objc.mm index 8439d89530..197636e21e 100644 --- a/3party/Alohalytics/src/apple/alohalytics_objc.mm +++ b/3party/Alohalytics/src/apple/alohalytics_objc.mm @@ -571,7 +571,6 @@ static BOOL gIsFirstSession = NO; + (void)disable { Stats & s = Stats::Instance(); - s.LogEvent("$statisticsDisabled"); // Force uploading of all previous events. if (IsConnectionActive()) s.Upload(); @@ -582,9 +581,7 @@ static BOOL gIsFirstSession = NO; } + (void)enable { - Stats & s = Stats::Instance(); - s.Enable(); - s.LogEvent("$statisticsEnabled"); + Stats::Instance().Enable(); NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; [ud setBool:NO forKey:kIsAlohalyticsDisabledKey]; [ud synchronize]; diff --git a/iphone/Maps/Settings/SettingsViewController.mm b/iphone/Maps/Settings/SettingsViewController.mm index 086852f323..79bef8a4c1 100644 --- a/iphone/Maps/Settings/SettingsViewController.mm +++ b/iphone/Maps/Settings/SettingsViewController.mm @@ -15,6 +15,8 @@ #include "platform/platform.hpp" #include "platform/preferred_languages.hpp" +extern char const * kStatisticsEnabledSettingsKey; + typedef NS_ENUM(NSUInteger, Section) { SectionMetrics, @@ -82,8 +84,8 @@ typedef NS_ENUM(NSUInteger, Section) { cell = [tableView dequeueReusableCellWithIdentifier:[SwitchCell className]]; SwitchCell * customCell = (SwitchCell *)cell; - bool on = true; - (void)Settings::Get("StatisticsEnabled", on); + bool on = [Statistics isStatisticsEnabledByDefault]; + (void)Settings::Get(kStatisticsEnabledSettingsKey, on); customCell.switchButton.on = on; customCell.titleLabel.text = L(@"allow_statistics"); customCell.delegate = self; @@ -128,7 +130,10 @@ typedef NS_ENUM(NSUInteger, Section) { Statistics * stat = [Statistics instance]; [stat logEvent:@"StatisticsStatusChanged" withParameters:@{@"Enabled" : @(value)}]; - stat.enabled = value; + if (value) + [stat enableOnNextAppLaunch]; + else + [stat disableOnNextAppLaunch]; } else if (indexPath.section == SectionZoomButtons) { diff --git a/iphone/Maps/Statistics/Statistics.h b/iphone/Maps/Statistics/Statistics.h index ef5a239d85..ee6a8046d0 100644 --- a/iphone/Maps/Statistics/Statistics.h +++ b/iphone/Maps/Statistics/Statistics.h @@ -1,8 +1,9 @@ #import @interface Statistics : NSObject -{ -} ++ (bool)isStatisticsEnabledByDefault; +- (void)enableOnNextAppLaunch; +- (void)disableOnNextAppLaunch; // Should be called from the same method in AppDelegate. - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; @@ -14,7 +15,4 @@ - (void)logLocation:(CLLocation *)location; + (instancetype)instance; - -@property (nonatomic) BOOL enabled; - @end diff --git a/iphone/Maps/Statistics/Statistics.mm b/iphone/Maps/Statistics/Statistics.mm index b8c49b07cf..bf28a5f639 100644 --- a/iphone/Maps/Statistics/Statistics.mm +++ b/iphone/Maps/Statistics/Statistics.mm @@ -10,17 +10,70 @@ #include "platform/settings.hpp" -static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled"; +char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled"; @interface Statistics () +{ + bool _enabled; +} @property (nonatomic) NSDate * lastLocationLogTimestamp; @end @implementation Statistics ++ (bool)isStatisticsEnabledByDefault +{ +#ifdef OMIM_PRODUCTION + return true; +#else + // Make developer's life a little bit easier. + [Alohalytics setDebugMode:YES]; + return false; +#endif +} + +- (instancetype)init +{ + if ((self = [super init])) + { + _enabled = [Statistics isStatisticsEnabledByDefault]; + // Note by AlexZ: + // _enabled should be persistent across app's process lifecycle. That's why we change + // _enabled property only once - when the app is launched. In this case we don't need additional + // checks and specific initializations for different 3party engines, code is much cleaner and safer + // (actually, we don't have a choice - 3party SDKs do not guarantee correctness if not initialized + // in application:didFinishLaunchingWithOptions:). + // The (only) drawback of this approach is that to actually disable or enable 3party engines, + // the app should be restarted. + (void)Settings::Get(kStatisticsEnabledSettingsKey, _enabled); + + if (_enabled) + [Alohalytics enable]; + else + [Alohalytics disable]; + } + return self; +} + +- (void)enableOnNextAppLaunch +{ + // This setting will be checked and applied on the next launch. + Settings::Set(kStatisticsEnabledSettingsKey, true); + // It does not make sense to log statisticsEnabled with Alohalytics here, + // as it will not be stored and logged anyway. +} + +- (void)disableOnNextAppLaunch +{ + // This setting will be checked and applied on the next launch. + Settings::Set(kStatisticsEnabledSettingsKey, false); + [Alohalytics logEvent:@"statisticsDisabled"]; +} + - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - if (self.enabled) + // _enabled should be already correctly set up in init method. + if (_enabled) { [Flurry startSession:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"FlurryKey"]]; @@ -30,20 +83,16 @@ static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled #ifdef DEBUG [MRMyTracker setDebugMode:YES]; #endif - MRMyTracker.getTrackerParams.trackAppLaunch = YES; + [MRMyTracker getTrackerParams].trackAppLaunch = YES; [MRMyTracker setupTracker]; - // Initialize Alohalytics statistics engine. -#ifndef OMIM_PRODUCTION - [Alohalytics setDebugMode:YES]; -#endif [Alohalytics setup:@"http://localhost:8080" withLaunchOptions:launchOptions]; } } - (void)logLocation:(CLLocation *)location { - if (self.enabled) + if (_enabled) { if (!_lastLocationLogTimestamp || [[NSDate date] timeIntervalSinceDate:_lastLocationLogTimestamp] > (60 * 60 * 3)) { @@ -56,7 +105,7 @@ static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled - (void)logEvent:(NSString *)eventName withParameters:(NSDictionary *)parameters { - if (self.enabled) + if (_enabled) [Flurry logEvent:eventName withParameters:parameters]; } @@ -67,7 +116,7 @@ static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled - (void)logApiUsage:(NSString *)programName { - if (self.enabled) + if (_enabled) { if (programName) [self logEvent:@"Api Usage" withParameters: @{@"Application Name" : programName}]; @@ -78,7 +127,7 @@ static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled - (void)applicationDidBecomeActive { - if (self.enabled) + if (_enabled) { [FBSDKAppEvents activateApp]; // Special FB events to improve marketing campaigns quality. @@ -86,26 +135,6 @@ static constexpr char const * kStatisticsEnabledSettingsKey = "StatisticsEnabled } } -- (BOOL)enabled -{ -#ifdef DEBUG - bool statisticsEnabled = false; -#else - bool statisticsEnabled = true; -#endif - (void)Settings::Get(kStatisticsEnabledSettingsKey, statisticsEnabled); - return statisticsEnabled; -} - -- (void)setEnabled:(BOOL)enabled -{ - Settings::Set(kStatisticsEnabledSettingsKey, static_cast(enabled)); - if (enabled) - [Alohalytics enable]; - else - [Alohalytics disable]; -} - + (instancetype)instance { static Statistics * instance;