Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocalNotificationManager.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ddf9ec4e661959542d2b5fbcdaeff2a90492f166 (plain)
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
#import "AppInfo.h"
#import "Common.h"
#import "Framework.h"
#import "LocalNotificationInfoProvider.h"
#import "LocalNotificationManager.h"
#import "LocationManager.h"
#import "MapsAppDelegate.h"
#import "MapViewController.h"
#import "Statistics.h"
#import "TimeUtils.h"
#import "UIKitCategories.h"

#import "3party/Alohalytics/src/alohalytics_objc.h"

#include "platform/platform.hpp"
#include "storage/storage_defines.hpp"

static NSString * kDownloadMapActionName = @"DownloadMapAction";

static NSString * kFlagsKey = @"DownloadMapNotificationFlags";
static constexpr const double kRepeatedNotificationIntervalInSeconds = 3 * 30 * 24 * 60 * 60; // three months

NSString * const LocalNotificationManagerSpecialNotificationInfoKey = @"LocalNotificationManagerSpecialNotificationInfoKey";
NSString * const LocalNotificationManagerNumberOfViewsPrefix = @"LocalNotificationManagerNumberOfViewsPrefix";

using namespace storage;

typedef void (^CompletionHandler)(UIBackgroundFetchResult);

@interface LocalNotificationManager () <CLLocationManagerDelegate, UIAlertViewDelegate>

@property (nonatomic) CLLocationManager * locationManager;
@property (nonatomic) TIndex countryIndex;
@property (nonatomic, copy) CompletionHandler downloadMapCompletionHandler;
@property (nonatomic, weak) NSTimer * timer;

@end

@implementation LocalNotificationManager

+ (instancetype)sharedManager
{
  static LocalNotificationManager * manager;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    manager = [[self alloc] init];
  });
  return manager;
}

- (void)updateLocalNotifications
{
  [self scheduleSpecialLocalNotifications];
}

- (void)processNotification:(UILocalNotification *)notification onLaunch:(BOOL)onLaunch
{
  NSDictionary * userInfo = [notification userInfo];
  if ([userInfo[@"Action"] isEqualToString:kDownloadMapActionName])
  {
    [[Statistics instance] logEvent:@"'Download Map' Notification Clicked"];
    [[MapsAppDelegate theApp].m_mapViewController.navigationController popToRootViewControllerAnimated:NO];

    TIndex const index = TIndex([userInfo[@"Group"] intValue], [userInfo[@"Country"] intValue], [userInfo[@"Region"] intValue]);
    [self downloadCountryWithIndex:index];
  }
  else if (userInfo[LocalNotificationManagerSpecialNotificationInfoKey])
  {
    NSDictionary * notificationInfo = userInfo[LocalNotificationManagerSpecialNotificationInfoKey];
    if (onLaunch)
      [self runNotificationAction:notificationInfo];
    else
    {
      NSString * dismissiveAction = L(@"later");
      NSString * positiveAction = [self actionTitleWithAction:notificationInfo[@"NotificationAction"]];
      NSString * notificationTitle = L(notificationInfo[@"NotificationLocalizedAlertBodyKey"]);
      if (![notificationTitle length])
        notificationTitle = L(notificationInfo[@"NotificationLocalizedBodyKey"]);
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:notificationTitle message:nil delegate:nil cancelButtonTitle:dismissiveAction otherButtonTitles:positiveAction, nil];
      alertView.tapBlock = ^(UIAlertView *alertView, NSInteger buttonIndex) {
        NSString * notificationID = notificationInfo[@"NotificationID"];
        BOOL shared = (buttonIndex != alertView.cancelButtonIndex);
        [[Statistics instance] logEvent:[NSString stringWithFormat:@"'%@' Notification Show", notificationID] withParameters:@{@"Shared" : @(shared)}];
        if (shared)
          [self runNotificationAction:notificationInfo];
      };
      [alertView show];
    }
  }
}

- (NSString *)actionTitleWithAction:(NSString *)action
{
  if ([action isEqualToString:@"Share"])
    return L(@"share");
  else if ([action isEqualToString:@"AppStoreProVersion"])
    return L(@"download");
  else
    return nil;
}

- (void)runNotificationAction:(NSDictionary *)notificationInfo
{
  NSString * action = notificationInfo[@"NotificationAction"];
  
  if ([action isEqualToString:@"Share"])
  {
    UIImage * shareImage = [UIImage imageNamed:notificationInfo[@"NotifiicationShareImage"]];
    LocalNotificationInfoProvider * infoProvider = [[LocalNotificationInfoProvider alloc] initWithDictionary:notificationInfo];

    NSMutableArray * itemsToShare = [NSMutableArray arrayWithObject:infoProvider];
    if (shareImage)
      [itemsToShare addObject:shareImage];

    UIActivityViewController * activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
    NSMutableArray * excludedActivityTypes = [@[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll] mutableCopy];
    [excludedActivityTypes addObject:UIActivityTypeAirDrop];
    activityVC.excludedActivityTypes = excludedActivityTypes;
    UIWindow * window = [[UIApplication sharedApplication].windows firstObject];
    NavigationController * vc = (NavigationController *)window.rootViewController;
    [vc presentViewController:activityVC animated:YES completion:nil];
  }
}

#pragma mark - Special Notifications

- (BOOL)isSpecialLocalNotification:(UILocalNotification *)notification
{
  if (notification.userInfo && notification.userInfo[LocalNotificationManagerSpecialNotificationInfoKey])
    return YES;
  else
    return NO;
}

- (NSArray *)scheduledSpecialLocalNotifications
{
  NSArray * allNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
  NSMutableArray * specialNotifications = [[NSMutableArray alloc] init];
  for (UILocalNotification * notification in allNotifications)
  {
    if ([self isSpecialLocalNotification:notification])
      [specialNotifications addObject:notification];
  }
  
  return specialNotifications;
}

- (BOOL)isSpecialNotificationScheduled:(NSString *)specialNotificationID
{
  NSArray * notifications = [self scheduledSpecialLocalNotifications];
  
  for (UILocalNotification * scheduledNotification in notifications)
  {
    NSDictionary * notificationInfo = scheduledNotification.userInfo[LocalNotificationManagerSpecialNotificationInfoKey];
    NSString * scheduledSpecialNotificationID = notificationInfo[@"NotificationID"];
    if ([scheduledSpecialNotificationID isEqualToString:specialNotificationID])
      return YES;
  }
  
  return NO;
}

- (void)increaseViewsNumberOfNotification:(NSString *)specialNotificationID
{
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  NSString * key = [NSString stringWithFormat:@"%@%@", LocalNotificationManagerNumberOfViewsPrefix, specialNotificationID];
  NSNumber * viewsNumber = [userDefaults objectForKey:key];
  viewsNumber = viewsNumber ? @([viewsNumber integerValue] + 1) : @(1);
  [userDefaults setObject:viewsNumber forKey:key];
  [userDefaults synchronize];
}

- (NSNumber *)viewNumberOfNotification:(NSString *)specialNotificationID
{
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  NSString * key = [NSString stringWithFormat:@"%@%@", LocalNotificationManagerNumberOfViewsPrefix, specialNotificationID];
  NSNumber * viewsNumber = [userDefaults objectForKey:key];
  if (!viewsNumber)
    viewsNumber = @(0);
  return viewsNumber;
}

- (void)scheduleSpecialLocalNotifications
{
  NSArray * localNotificationsInfo = [self localNotificationsInfo];
  NSMutableArray * actualSpecialLocalNotifications = [NSMutableArray array];
  for (NSDictionary * notificationInfo in localNotificationsInfo)
  {
    NSString * notificationID = notificationInfo[@"NotificationID"];
    if ([self isSpecialNotificationScheduled:notificationID])
      continue;
    
    NSNumber * viewsLimit = notificationInfo[@"NotificationViewsLimit"];
    NSNumber * viewsNumber = [self viewNumberOfNotification:notificationID];
    if ([viewsNumber integerValue] >= [viewsLimit integerValue])
      continue;
    
    NSDate * fireDate = [NSDateFormatter dateWithString:notificationInfo[@"NotificationDate"]];
    NSDate * expirationDate = [NSDateFormatter dateWithString:notificationInfo[@"NotificationExpirationDate"]];
    NSDate * currentDate = [NSDate date];
    if (expirationDate && [currentDate timeIntervalSinceDate:expirationDate] >= 0)
      continue;
    
    if ([currentDate timeIntervalSinceDate:fireDate] >= 0)
      fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0 * 60];
    
    [self increaseViewsNumberOfNotification:notificationID];
    
    UILocalNotification * notification = [[UILocalNotification alloc] init];
    notification.alertBody = L(notificationInfo[@"NotificationLocalizedBodyKey"]);
    notification.fireDate = fireDate;
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.alertAction = L(notificationInfo[@"NotificationActionTitleKey"]);
    notification.userInfo = @{LocalNotificationManagerSpecialNotificationInfoKey : notificationInfo};
    
    UIApplication * application = [UIApplication sharedApplication];
    [application scheduleLocalNotification:notification];
    [actualSpecialLocalNotifications addObject:notification];
    
    [[Statistics instance] logEvent:[NSString stringWithFormat:@"'%@' Notification Scheduled", notificationID]];
  }
  
  // We'd like to remove not actual special notifications.
  NSMutableArray * notActualSpecialLocalNotifications = [[self scheduledSpecialLocalNotifications] mutableCopy];
  [notActualSpecialLocalNotifications removeObjectsInArray:actualSpecialLocalNotifications];
  for (UILocalNotification * notification in notActualSpecialLocalNotifications)
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
}

- (NSArray *)localNotificationsInfo
{
  NSString * localNotificationsInfoFileName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"LocalNotificationsFileName"];
  NSString * localNotificationsInfoFilePath = [[NSBundle mainBundle] pathForResource:localNotificationsInfoFileName ofType:@"plist"];
  NSArray * localNotificationsInfo = [NSArray arrayWithContentsOfFile:localNotificationsInfoFilePath];
  return localNotificationsInfo;
}

#pragma mark - Location Notifications

- (void)showDownloadMapNotificationIfNeeded:(void (^)(UIBackgroundFetchResult))completionHandler
{
  NSTimeInterval const completionTimeIndent = 2.0;
  NSTimeInterval const backgroundTimeRemaining = UIApplication.sharedApplication.backgroundTimeRemaining - completionTimeIndent;
  if ([CLLocationManager locationServicesEnabled] && backgroundTimeRemaining > 0.0)
  {
    self.downloadMapCompletionHandler = completionHandler;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:backgroundTimeRemaining target:self selector:@selector(timerSelector:) userInfo:nil repeats:NO];
    [self.locationManager startUpdatingLocation];
  }
  else
  {
    completionHandler(UIBackgroundFetchResultFailed);
  }
}

- (void)markNotificationShowingForIndex:(TIndex)index
{
  NSMutableDictionary * flags = [[[NSUserDefaults standardUserDefaults] objectForKey:kFlagsKey] mutableCopy];
  if (!flags)
    flags = [[NSMutableDictionary alloc] init];
  
  flags[[self flagStringForIndex:index]] = [NSDate date];
  
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setObject:flags forKey:kFlagsKey];
  [userDefaults synchronize];
}

- (BOOL)shouldShowNotificationForIndex:(TIndex)index
{
  NSDictionary * flags = [[NSUserDefaults standardUserDefaults] objectForKey:kFlagsKey];
  NSDate * lastShowDate = flags[[self flagStringForIndex:index]];
  return !lastShowDate || [[NSDate date] timeIntervalSinceDate:lastShowDate] > kRepeatedNotificationIntervalInSeconds;
}

- (void)timerSelector:(id)sender
{
  // Location still was not received but it's time to finish up so system will not kill us.
  [self.locationManager stopUpdatingLocation];
  [self performCompletionHandler:UIBackgroundFetchResultFailed];
}

- (void)downloadCountryWithIndex:(TIndex)index
{
  /// @todo Fix this logic after Framework -> CountryTree -> ActiveMapLayout refactoring.
  /// Call download via Framework.

  Framework & f = GetFramework();
  f.GetCountryTree().GetActiveMapLayout().DownloadMap(index, MapOptions::Map);
  m2::RectD const rect = f.GetCountryBounds(index);
  double const lon = MercatorBounds::XToLon(rect.Center().x);
  double const lat = MercatorBounds::YToLat(rect.Center().y);
  f.ShowRect(lat, lon, 10);
}

- (NSString *)flagStringForIndex:(TIndex)index
{
  return [NSString stringWithFormat:@"%i_%i_%i", index.m_group, index.m_country, index.m_region];
}

- (TIndex)indexWithFlagString:(NSString *)flag
{
  NSArray * components = [flag componentsSeparatedByString:@"_"];
  if ([components count] == 3)
    return TIndex([components[0] intValue], [components[1] intValue], [components[2] intValue]);
  
  return TIndex();
}

- (void)performCompletionHandler:(UIBackgroundFetchResult)result
{
  if (!self.downloadMapCompletionHandler)
    return;
  self.downloadMapCompletionHandler(result);
  self.downloadMapCompletionHandler = nil;
}

#pragma mark - Location Manager

- (CLLocationManager *)locationManager
{
  if (!_locationManager)
  {
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.distanceFilter = kCLLocationAccuracyThreeKilometers;
  }
  return _locationManager;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
  [self.timer invalidate];
  [self.locationManager stopUpdatingLocation];
  NSString * flurryEventName = @"'Download Map' Notification Didn't Schedule";
  UIBackgroundFetchResult result = UIBackgroundFetchResultNoData;

  BOOL const inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;
  BOOL const onWiFi = (Platform::ConnectionStatus() == Platform::EConnectionType::CONNECTION_WIFI);
  if (inBackground && onWiFi)
  {
    Framework & f = GetFramework();
    CLLocation * lastLocation = [locations lastObject];
    TIndex const index = f.GetCountryIndex(MercatorBounds::FromLatLon(lastLocation.coordinate.latitude, lastLocation.coordinate.longitude));
    
    if (index.IsValid() && [self shouldShowNotificationForIndex:index])
    {
      TStatus const status = f.GetCountryStatus(index);
      if (status == TStatus::ENotDownloaded)
      {
        [self markNotificationShowingForIndex:index];
        
        UILocalNotification * notification = [[UILocalNotification alloc] init];
        notification.alertAction = L(@"download");
        notification.alertBody = L(@"download_map_notification");
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.userInfo = @{@"Action" : kDownloadMapActionName, @"Group" : @(index.m_group), @"Country" : @(index.m_country), @"Region" : @(index.m_region)};
        
        UIApplication * application = [UIApplication sharedApplication];
        [application presentLocalNotificationNow:notification];

        [Alohalytics logEvent:@"suggestedToDownloadMissingMapForCurrentLocation" atLocation:lastLocation];
        flurryEventName = @"'Download Map' Notification Scheduled";
        result = UIBackgroundFetchResultNewData;
      }
    }
  }
  [[Statistics instance] logEvent:flurryEventName withParameters:@{@"WiFi" : @(onWiFi)}];
  [self performCompletionHandler:result];
}

@end