<commit_before>#import "MWMLocationManager.h"
#import "MWMAlertViewController.h"
#import "MWMLocationObserver.h"
#import "MWMLocationPredictor.h"
#import "MWMRouter.h"
#import "MapsAppDelegate.h"
#import "SwiftBridge.h"
#import "location_util.h"

#include <CoreApi/Framework.h>

#include "map/gps_tracker.hpp"

namespace
{
using Observer = id<MWMLocationObserver>;
using Observers = NSHashTable<Observer>;

typedef NS_OPTIONS(NSUInteger, MWMLocationFrameworkUpdate) {
  MWMLocationFrameworkUpdateNone = 0,
  MWMLocationFrameworkUpdateLocation = 1 << 0,
  MWMLocationFrameworkUpdateHeading = 1 << 1,
  MWMLocationFrameworkUpdateStatus = 1 << 2
};

enum class GeoMode
{
  Pending,
  InPosition,
  NotInPosition,
  FollowAndRotate,
  VehicleRouting,
  PedestrianRouting,
  BicycleRouting
};

struct DesiredAccuracy
{
  CLLocationAccuracy charging;
  CLLocationAccuracy battery;
};

struct GeoModeSettings
{
  CLLocationDistance distanceFilter;
  DesiredAccuracy accuracy;
};

std::map<GeoMode, GeoModeSettings> const kGeoSettings{
    {GeoMode::Pending,
     {.distanceFilter = kCLDistanceFilterNone,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBestForNavigation}}},
    {GeoMode::InPosition,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::NotInPosition,
     {.distanceFilter = 5,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::FollowAndRotate,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::VehicleRouting,
     {.distanceFilter = kCLDistanceFilterNone,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::PedestrianRouting,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::BicycleRouting,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}}};

BOOL keepRunningInBackground()
{
  bool const needGPSForTrackRecorder = GpsTracker::Instance().IsEnabled();
  if (needGPSForTrackRecorder)
    return YES;

  auto const isRouteBuilt = [MWMRouter isRouteBuilt];
  auto const isRouteFinished = [MWMRouter isRouteFinished];
  auto const isRouteRebuildingOnly = [MWMRouter isRouteRebuildingOnly];
  auto const needGPSForRouting = ((isRouteBuilt || isRouteRebuildingOnly) && !isRouteFinished);
  if (needGPSForRouting)
    return YES;

  return NO;
}

NSString * const kLocationPermissionRequestedKey = @"kLocationPermissionRequestedKey";
NSString * const kLocationAlertNeedShowKey = @"kLocationAlertNeedShowKey";

BOOL isPermissionRequested() {
  return [NSUserDefaults.standardUserDefaults boolForKey:kLocationPermissionRequestedKey];
}

void setPermissionRequested() {
  NSUserDefaults * ud = NSUserDefaults.standardUserDefaults;
  [ud setBool:YES forKey:kLocationPermissionRequestedKey];
  [ud synchronize];
}
       
BOOL needShowLocationAlert() {
 if ([NSUserDefaults.standardUserDefaults objectForKey:kLocationAlertNeedShowKey] == nil)<commit_msg>[iOS] Background location update activates in route planning mode<commit_after>#import "MWMLocationManager.h"
#import "MWMAlertViewController.h"
#import "MWMLocationObserver.h"
#import "MWMLocationPredictor.h"
#import "MWMRouter.h"
#import "MapsAppDelegate.h"
#import "SwiftBridge.h"
#import "location_util.h"

#include <CoreApi/Framework.h>

#include "map/gps_tracker.hpp"

namespace
{
using Observer = id<MWMLocationObserver>;
using Observers = NSHashTable<Observer>;

typedef NS_OPTIONS(NSUInteger, MWMLocationFrameworkUpdate) {
  MWMLocationFrameworkUpdateNone = 0,
  MWMLocationFrameworkUpdateLocation = 1 << 0,
  MWMLocationFrameworkUpdateHeading = 1 << 1,
  MWMLocationFrameworkUpdateStatus = 1 << 2
};

enum class GeoMode
{
  Pending,
  InPosition,
  NotInPosition,
  FollowAndRotate,
  VehicleRouting,
  PedestrianRouting,
  BicycleRouting
};

struct DesiredAccuracy
{
  CLLocationAccuracy charging;
  CLLocationAccuracy battery;
};

struct GeoModeSettings
{
  CLLocationDistance distanceFilter;
  DesiredAccuracy accuracy;
};

std::map<GeoMode, GeoModeSettings> const kGeoSettings{
    {GeoMode::Pending,
     {.distanceFilter = kCLDistanceFilterNone,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBestForNavigation}}},
    {GeoMode::InPosition,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::NotInPosition,
     {.distanceFilter = 5,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::FollowAndRotate,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::VehicleRouting,
     {.distanceFilter = kCLDistanceFilterNone,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::PedestrianRouting,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}},
    {GeoMode::BicycleRouting,
     {.distanceFilter = 2,
      .accuracy = {.charging = kCLLocationAccuracyBestForNavigation,
                   .battery = kCLLocationAccuracyBest}}}};

BOOL keepRunningInBackground()
{
  bool const needGPSForTrackRecorder = GpsTracker::Instance().IsEnabled();
  if (needGPSForTrackRecorder)
    return YES;

  auto const isOnRoute = [MWMRouter isOnRoute];
  auto const isRouteFinished = [MWMRouter isRouteFinished];
  auto const needGPSForRouting = (isOnRoute && !isRouteFinished);
  if (needGPSForRouting)
    return YES;

  return NO;
}

NSString * const kLocationPermissionRequestedKey = @"kLocationPermissionRequestedKey";
NSString * const kLocationAlertNeedShowKey = @"kLocationAlertNeedShowKey";

BOOL isPermissionRequested() {
  return [NSUserDefaults.standardUserDefaults boolForKey:kLocationPermissionRequestedKey];
}

void setPermissionRequested() {
  NSUserDefaults * ud = NSUserDefaults.standardUserDefaults;
  [ud setBool:YES forKey:kLocationPermissionRequestedKey];
  [ud synchronize];
}
       
BOOL needShowLocationAlert() {
 if ([NSUserDefaults.standardUserDefaults objectForKey:kLocationAlertNeedShowKey] == nil)
{'fraction_non_alphanumeric': 0.09525196621031168, 'fraction_numerical': 0.003495484998543548, 'mean_word_length': 4.524537409493162, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7051386', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2095, 'n_words': 379}