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

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

#include "coding/zip_reader.hpp"
#include "coding/file_name_utils.hpp"

#include "base/logging.hpp"
#include "base/thread.hpp"
#include "base/string_utils.hpp"

#include "std/regex.hpp"

#include <unistd.h>     // for sysconf
#include <sys/stat.h>

Platform::Platform()
{
  /// @see initialization routine in android/jni/com/.../Platform.hpp
}

namespace
{
enum SourceT
{
  EXTERNAL_RESOURCE,
  RESOURCE,
  WRITABLE_PATH,
  SETTINGS_PATH,
  FULL_PATH,
  SOURCE_COUNT
};

bool IsResource(string const & file, string const & ext)
{
  if (ext == DATA_FILE_EXTENSION)
  {
    return (strings::StartsWith(file, WORLD_COASTS_FILE_NAME) ||
            strings::StartsWith(file, WORLD_COASTS_OBSOLETE_FILE_NAME) ||
            strings::StartsWith(file, WORLD_FILE_NAME));
  }
  else if (ext == BOOKMARKS_FILE_EXTENSION ||
           ext == ROUTING_FILE_EXTENSION ||
           file == SETTINGS_FILE_NAME)
  {
    return false;
  }

  return true;
}

size_t GetSearchSources(string const & file, string const & searchScope,
                        SourceT (&arr)[SOURCE_COUNT])
{
  size_t ret = 0;

  for (size_t i = 0; i < searchScope.size(); ++i)
  {
    switch (searchScope[i])
    {
    case 'w': arr[ret++] = WRITABLE_PATH; break;
    case 'r': arr[ret++] = RESOURCE; break;
    case 'e': arr[ret++] = EXTERNAL_RESOURCE; break;
    case 's': arr[ret++] = SETTINGS_PATH; break;
    case 'f':
      if (strings::StartsWith(file, "/"))
        arr[ret++] = FULL_PATH;
      break;
    default : CHECK(false, ("Unsupported searchScope:", searchScope)); break;
    }
  }

  return ret;
}

#ifdef DEBUG
class DbgLogger
{
  string const & m_file;
  SourceT m_src;
public:
  DbgLogger(string const & file) : m_file(file) {}
  void SetSource(SourceT src) { m_src = src; }
  ~DbgLogger()
  {
    LOG(LDEBUG, ("Source for file", m_file, "is", m_src));
  }
};
#endif

}

unique_ptr<ModelReader> Platform::GetReader(string const & file, string const & searchScope) const
{
  string const ext = my::GetFileExtension(file);
  ASSERT(!ext.empty(), ());

  uint32_t const logPageSize = (ext == DATA_FILE_EXTENSION) ? READER_CHUNK_LOG_SIZE : 10;
  uint32_t const logPageCount = (ext == DATA_FILE_EXTENSION) ? READER_CHUNK_LOG_COUNT : 4;

  SourceT sources[SOURCE_COUNT];
  size_t n = 0;

  if (searchScope.empty())
  {
    // Default behaviour - use predefined scope for resource files and writable path for all others.

    if (IsResource(file, ext))
      n = GetSearchSources(file, m_androidDefResScope, sources);
    else
    {
      // Add source for map files and other dynamic stored data.
      sources[n++] = WRITABLE_PATH;
      sources[n++] = FULL_PATH;
    }
  }
  else
  {
    // Use passed scope as client wishes.
    n = GetSearchSources(file, searchScope, sources);
  }

#ifdef DEBUG
  DbgLogger logger(file);
#endif

  for (size_t i = 0; i < n; ++i)
  {
#ifdef DEBUG
    logger.SetSource(sources[i]);
#endif

    switch (sources[i])
    {
    case EXTERNAL_RESOURCE:
      for (size_t j = 0; j < m_extResFiles.size(); ++j)
      {
        try
        {
          return make_unique<ZipFileReader>(m_extResFiles[j], file, logPageSize, logPageCount);
        }
        catch (Reader::OpenException const &)
        {
        }
      }
      break;

    case WRITABLE_PATH:
    {
      string const path = m_writableDir + file;
      if (IsFileExistsByFullPath(path))
        return make_unique<FileReader>(path, logPageSize, logPageCount);
      break;
    }

    case SETTINGS_PATH:
    {
      string const path = m_settingsDir + file;
      if (IsFileExistsByFullPath(path))
        return make_unique<FileReader>(path, logPageSize, logPageCount);
      break;
    }

    case FULL_PATH:
      if (IsFileExistsByFullPath(file))
        return make_unique<FileReader>(file, logPageSize, logPageCount);
      break;

    case RESOURCE:
      ASSERT_EQUAL(file.find("assets/"), string::npos, ());
      try
      {
        return make_unique<ZipFileReader>(m_resourcesDir, "assets/" + file, logPageSize, logPageCount);
      }
      catch (Reader::OpenException const &)
      {
      }
      break;

    default:
      CHECK(false, ("Unsupported source:", sources[i]));
      break;
    }
  }

  LOG(LWARNING, ("Can't get reader for:", file));
  MYTHROW(FileAbsentException, ("File not found", file));
  return nullptr;
}

void Platform::GetFilesByRegExp(string const & directory, string const & regexp, FilesList & res)
{
  if (ZipFileReader::IsZip(directory))
  {
    // Get files list inside zip file
    typedef ZipFileReader::FileListT FilesT;
    FilesT fList;
    ZipFileReader::FilesList(directory, fList);

    regex exp(regexp);

    for (FilesT::iterator it = fList.begin(); it != fList.end(); ++it)
    {
      string & name = it->first;
      if (regex_search(name.begin(), name.end(), exp))
      {
        // Remove assets/ prefix - clean files are needed for fonts white/blacklisting logic
        size_t const ASSETS_LENGTH = 7;
        if (name.find("assets/") == 0)
          name.erase(0, ASSETS_LENGTH);

        res.push_back(name);
      }
    }
  }
  else
    pl::EnumerateFilesByRegExp(directory, regexp, res);
}

int Platform::VideoMemoryLimit() const
{
  return 10 * 1024 * 1024;
}

int Platform::PreCachingDepth() const
{
  return 3;
}

bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const
{
  try
  {
    size = ReaderPtr<Reader>(GetReader(fileName)).Size();
    return true;
  }
  catch (RootException const & ex)
  {
    LOG(LWARNING, ("Can't get file size for:", fileName));
    return false;
  }
}

Platform::EError Platform::MkDir(string const & dirName) const
{
  if (0 != mkdir(dirName.c_str(), 0755))
    return ErrnoToError();
  return Platform::ERR_OK;
}

void Platform::SetupMeasurementSystem() const
{
  auto units = measurement_utils::Units::Metric;
  if (settings::Get(settings::kMeasurementUnits, units))
    return;
  // @TODO Add correct implementation
  units = measurement_utils::Units::Metric;
  settings::Set(settings::kMeasurementUnits, units);
}

/// @see implementation of methods below in android/jni/com/.../Platform.cpp
// void Platform::RunOnGuiThread(TFunctor const & fn){}

namespace
{
class FunctorWrapper : public threads::IRoutine
{
  Platform::TFunctor m_fn;

public:
  FunctorWrapper(Platform::TFunctor const & fn) : m_fn(fn) {}

  void Do() override { m_fn(); }
};
}

void Platform::RunAsync(TFunctor const & fn, Priority p)
{
  UNUSED_VALUE(p);

  // We don't need to store thread handler in POSIX, just create and
  // run.  Unfortunately we can't use std::async() here since it
  // doesn't attach to JVM threads.
  threads::Thread().Create(make_unique<FunctorWrapper>(fn));
}