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

platform_ios.mm « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 73c45e0f4e2693ccd38fd958add6b83bb57e9a3a (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
#include "platform/platform_ios.h"
#include "platform/constants.hpp"
#include "platform/gui_thread.hpp"
#include "platform/measurement_utils.hpp"
#include "platform/platform_unix_impl.hpp"
#include "platform/settings.hpp"

#include "coding/file_reader.hpp"

#include <utility>

#include <ifaddrs.h>

#include <mach/mach.h>

#include <net/if.h>
#include <net/if_dl.h>

#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/xattr.h>

#import "iphone/Maps/Common/MWMCommon.h"

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

#import <CoreFoundation/CFURL.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <UIKit/UIKit.h>
#import <netinet/in.h>

#include <memory>
#include <sstream>
#include <string>
#include <utility>

Platform::Platform()
{
  m_isTablet = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);

  NSBundle * bundle = NSBundle.mainBundle;
  NSString * path = [bundle resourcePath];
  m_resourcesDir = path.UTF8String;
  m_resourcesDir += "/";

  NSArray * dirPaths =
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString * docsDir = dirPaths.firstObject;
  m_writableDir = docsDir.UTF8String;
  m_writableDir += "/";
  m_settingsDir = m_writableDir;

  auto privatePaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
                                                          NSUserDomainMask, YES);
  m_privateDir = privatePaths.firstObject.UTF8String;
  m_privateDir +=  "/";

  NSString * tmpDir = NSTemporaryDirectory();
  if (tmpDir)
    m_tmpDir = tmpDir.UTF8String;
  else
  {
    m_tmpDir = NSHomeDirectory().UTF8String;
    m_tmpDir += "/tmp/";
  }

  m_guiThread = std::make_unique<platform::GuiThread>();

  UIDevice * device = UIDevice.currentDevice;
  device.batteryMonitoringEnabled = YES;

  NSLog(@"Device: %@, SystemName: %@, SystemVersion: %@", device.model, device.systemName,
        device.systemVersion);
}

//static
void Platform::DisableBackupForFile(std::string const & filePath)
{
  // We need to disable iCloud backup for downloaded files.
  // This is the reason for rejecting from the AppStore
  // https://developer.apple.com/library/iOS/qa/qa1719/_index.html
  CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
                                                         reinterpret_cast<unsigned char const *>(filePath.c_str()),
                                                         filePath.size(),
                                                         0);
  CFErrorRef err;
  BOOL valueRaw = YES;
  CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberCharType, &valueRaw);
  if (!CFURLSetResourcePropertyForKey(url, kCFURLIsExcludedFromBackupKey, value, &err))
    NSLog(@"Error while disabling iCloud backup for file: %s", filePath.c_str());

  CFRelease(value);
  CFRelease(url);
}

// static
Platform::EError Platform::MkDir(std::string const & dirName)
{
  if (::mkdir(dirName.c_str(), 0755))
    return ErrnoToError();
  return Platform::ERR_OK;
}

void Platform::GetFilesByRegExp(std::string const & directory, std::string const & regexp, FilesList & res)
{
  pl::EnumerateFilesByRegExp(directory, regexp, res);
}

bool Platform::GetFileSizeByName(std::string const & fileName, uint64_t & size) const
{
  try
  {
    return GetFileSizeByFullPath(ReadPathForFile(fileName), size);
  }
  catch (RootException const &)
  {
    return false;
  }
}

std::unique_ptr<ModelReader> Platform::GetReader(std::string const & file, std::string const & searchScope) const
{
  return std::make_unique<FileReader>(ReadPathForFile(file, searchScope), READER_CHUNK_LOG_SIZE,
                                      READER_CHUNK_LOG_COUNT);
}

int Platform::VideoMemoryLimit() const { return 8 * 1024 * 1024; }
int Platform::PreCachingDepth() const { return 2; }

std::string Platform::UniqueClientId() const { return [Alohalytics installationId].UTF8String; }

std::string Platform::AdvertisingId() const
{
  //TODO(@beloal): Implement me.
  return {};
}

std::string Platform::MacAddress(bool md5Decoded) const
{
  // Not implemented.
  UNUSED_VALUE(md5Decoded);
  return {};
}

std::string Platform::GetMemoryInfo() const
{
  struct task_basic_info info;
  mach_msg_type_number_t size = sizeof(info);
  kern_return_t const kerr =
      task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
  std::stringstream ss;
  if (kerr == KERN_SUCCESS)
  {
    ss << "Memory info: Resident_size = " << info.resident_size / 1024
       << "KB; virtual_size = " << info.resident_size / 1024
       << "KB; suspend_count = " << info.suspend_count << " policy = " << info.policy;
  }
  else
  {
    ss << "Error with task_info(): " << mach_error_string(kerr);
  }
  return ss.str();
}

std::string Platform::DeviceName() const { return UIDevice.currentDevice.name.UTF8String; }

std::string Platform::DeviceModel() const
{
  utsname systemInfo;
  uname(&systemInfo);
  NSString * deviceModel = @(systemInfo.machine);
  if (auto m = platform::kDeviceModelsBeforeMetalDriver[deviceModel])
    deviceModel = m;
  else if (auto m = platform::kDeviceModelsWithiOS10MetalDriver[deviceModel])
    deviceModel = m;
  else if (auto m = platform::kDeviceModelsWithMetalDriver[deviceModel])
    deviceModel = m;
  return deviceModel.UTF8String;
}

void Platform::RunOnGuiThread(base::TaskLoop::Task && task)
{
  ASSERT(m_guiThread, ());
  m_guiThread->Push(std::move(task));
}

void Platform::RunOnGuiThread(base::TaskLoop::Task const & task)
{
  ASSERT(m_guiThread, ());
  m_guiThread->Push(task);
}

Platform::EConnectionType Platform::ConnectionStatus()
{
  struct sockaddr_in zero;
  bzero(&zero, sizeof(zero));
  zero.sin_len = sizeof(zero);
  zero.sin_family = AF_INET;
  SCNetworkReachabilityRef reachability =
      SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&zero);
  if (!reachability)
    return EConnectionType::CONNECTION_NONE;
  SCNetworkReachabilityFlags flags;
  bool const gotFlags = SCNetworkReachabilityGetFlags(reachability, &flags);
  CFRelease(reachability);
  if (!gotFlags || ((flags & kSCNetworkReachabilityFlagsReachable) == 0))
    return EConnectionType::CONNECTION_NONE;
  SCNetworkReachabilityFlags userActionRequired = kSCNetworkReachabilityFlagsConnectionRequired |
                                                  kSCNetworkReachabilityFlagsInterventionRequired;
  if ((flags & userActionRequired) == userActionRequired)
    return EConnectionType::CONNECTION_NONE;
  if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
    return EConnectionType::CONNECTION_WWAN;
  else
    return EConnectionType::CONNECTION_WIFI;
}

Platform::ChargingStatus Platform::GetChargingStatus()
{
  switch (UIDevice.currentDevice.batteryState)
  {
  case UIDeviceBatteryStateUnknown: return Platform::ChargingStatus::Unknown;
  case UIDeviceBatteryStateUnplugged: return Platform::ChargingStatus::Unplugged;
  case UIDeviceBatteryStateCharging:
  case UIDeviceBatteryStateFull: return Platform::ChargingStatus::Plugged;
  }
}

uint8_t Platform::GetBatteryLevel()
{
  auto const level = UIDevice.currentDevice.batteryLevel;

  ASSERT_GREATER_OR_EQUAL(level, -1.0, ());
  ASSERT_LESS_OR_EQUAL(level, 1.0, ());

  if (level == -1.0)
    return 100;

  auto const result = static_cast<uint8_t>(level * 100);

  CHECK_LESS_OR_EQUAL(result, 100, ());

  return result;
}

void Platform::SetupMeasurementSystem() const
{
  auto units = measurement_utils::Units::Metric;
  if (settings::Get(settings::kMeasurementUnits, units))
    return;
  BOOL const isMetric =
      [[[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
  units = isMetric ? measurement_utils::Units::Metric : measurement_utils::Units::Imperial;
  settings::Set(settings::kMeasurementUnits, units);
}

void Platform::SetGuiThread(std::unique_ptr<base::TaskLoop> guiThread)
{
  m_guiThread = std::move(guiThread);
}

////////////////////////////////////////////////////////////////////////
extern Platform & GetPlatform()
{
  static Platform platform;
  return platform;
}