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
|
#include "Framework.hpp"
#include "MapStorage.hpp"
#include "defines.hpp"
#include "coding/file_name_utils.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/reader_streambuf.hpp"
#include "coding/url_encode.hpp"
#include "platform/platform.hpp"
#include "platform/http_request.hpp"
#include "platform/servers_list.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include "std/vector.hpp"
#include "std/string.hpp"
#include "std/bind.hpp"
#include "std/shared_ptr.hpp"
using namespace downloader;
/// Special error codes to notify GUI about free space
//@{
#define ERR_DOWNLOAD_SUCCESS 0
#define ERR_NOT_ENOUGH_MEMORY -1
#define ERR_NOT_ENOUGH_FREE_SPACE -2
#define ERR_STORAGE_DISCONNECTED -3
#define ERR_DOWNLOAD_ERROR -4
#define ERR_NO_MORE_FILES -5
#define ERR_FILE_IN_PROGRESS -6
//@}
struct FileToDownload
{
vector<string> m_urls;
string m_fileName;
string m_pathOnSdcard;
uint64_t m_fileSize;
};
//static string g_apkPath;
//static string g_sdcardPath;
static vector<FileToDownload> g_filesToDownload;
static int g_totalDownloadedBytes;
static int g_totalBytesToDownload;
static shared_ptr<HttpRequest> g_currentRequest;
extern "C"
{
int HasSpaceForFiles(Platform & pl, string const & sdcardPath, size_t fileSize)
{
switch (pl.GetWritableStorageStatus(fileSize))
{
case Platform::STORAGE_DISCONNECTED: return ERR_STORAGE_DISCONNECTED;
case Platform::NOT_ENOUGH_SPACE: return ERR_NOT_ENOUGH_FREE_SPACE;
default: return fileSize;
}
}
// Check if we need to download mandatory resource file.
bool NeedToDownload(Platform & pl, string const & name, int size)
{
try
{
ModelReaderPtr reader(pl.GetReader(name));
if (name == WORLD_FILE_NAME DATA_FILE_EXTENSION)
{
FilesContainerR cont(reader);
if (!cont.IsExist(SEARCH_INDEX_FILE_TAG))
{
// World.mwm file doesn't have search index - need to download new one.
return true;
}
}
// file exists - no need to download
return false;
}
catch (RootException const &)
{
// TODO: A hack for a changed font file name. Remove when this stops being relevant.
// This is because we don't want to force update users with the old font (which is good enough)
if (name == "02_droidsans-fallback.ttf")
return NeedToDownload(pl, "02_wqy-microhei.ttf", size); // Size is not checked anyway
}
return true;
}
JNIEXPORT jint JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_getBytesToDownload(JNIEnv * env, jobject thiz)
{
// clear all
g_filesToDownload.clear();
g_totalBytesToDownload = 0;
g_totalDownloadedBytes = 0;
Platform & pl = GetPlatform();
string const path = pl.WritableDir();
ReaderStreamBuf buffer(pl.GetReader("external_resources.txt"));
istream in(&buffer);
string name;
int size;
while (true)
{
in >> name;
if (!in.good())
break;
in >> size;
if (!in.good())
break;
if (NeedToDownload(pl, name, size))
{
LOG(LDEBUG, ("Should download", name, "sized", size, "bytes"));
FileToDownload f;
f.m_pathOnSdcard = path + name;
f.m_fileName = name;
f.m_fileSize = size;
g_filesToDownload.push_back(f);
g_totalBytesToDownload += size;
}
}
int const res = HasSpaceForFiles(pl, path, g_totalBytesToDownload);
switch (res)
{
case ERR_STORAGE_DISCONNECTED:
LOG(LWARNING, ("External file system is not available"));
break;
case ERR_NOT_ENOUGH_FREE_SPACE:
LOG(LWARNING, ("Not enough space to extract files"));
break;
};
g_currentRequest.reset();
return res;
}
void DownloadFileFinished(shared_ptr<jobject> obj, HttpRequest const & req)
{
HttpRequest::StatusT const status = req.Status();
ASSERT_NOT_EQUAL(status, HttpRequest::EInProgress, ());
int errorCode = 0;
switch (status)
{
case HttpRequest::ECompleted:
errorCode = ERR_DOWNLOAD_SUCCESS;
break;
case HttpRequest::EFailed:
errorCode = ERR_DOWNLOAD_ERROR;
break;
};
g_currentRequest.reset();
if (errorCode == ERR_DOWNLOAD_SUCCESS)
{
FileToDownload & curFile = g_filesToDownload.back();
LOG(LDEBUG, ("finished downloading", curFile.m_fileName, "sized", curFile.m_fileSize, "bytes"));
g_totalDownloadedBytes += curFile.m_fileSize;
LOG(LDEBUG, ("totalDownloadedBytes:", g_totalDownloadedBytes));
g_filesToDownload.pop_back();
}
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "onDownloadFinished", "(I)V");
env->CallVoidMethod(*obj.get(), methodID, errorCode);
}
void DownloadFileProgress(shared_ptr<jobject> obj, HttpRequest const & req)
{
//LOG(LDEBUG, (req.Progress().first, "bytes for", g_filesToDownload.back().m_fileName, "was downloaded"));
FileToDownload & curFile = g_filesToDownload.back();
jint curTotal = req.Progress().second;
jint curProgress = req.Progress().first;
jint glbTotal = g_totalBytesToDownload;
jint glbProgress = g_totalDownloadedBytes + req.Progress().first;
JNIEnv * env = jni::GetEnv();
jmethodID methodID = jni::GetJavaMethodID(env, *obj.get(), "onDownloadProgress", "(IIII)V");
env->CallVoidMethod(*obj.get(), methodID,
curTotal, curProgress,
glbTotal, glbProgress);
}
typedef HttpRequest::CallbackT CallbackT;
void DownloadURLListFinished(HttpRequest const & req,
CallbackT const & onFinish, CallbackT const & onProgress)
{
FileToDownload & curFile = g_filesToDownload.back();
LOG(LINFO, ("Finished URL list download for", curFile.m_fileName));
GetServerListFromRequest(req, curFile.m_urls);
storage::Storage const & storage = g_framework->Storage();
for (size_t i = 0; i < curFile.m_urls.size(); ++i)
{
curFile.m_urls[i] = storage.GetFileDownloadUrl(curFile.m_urls[i], curFile.m_fileName);
LOG(LDEBUG, (curFile.m_urls[i]));
}
g_currentRequest.reset(HttpRequest::GetFile(
curFile.m_urls, curFile.m_pathOnSdcard, curFile.m_fileSize,
onFinish, onProgress,
512 * 1024, false));
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_cancelCurrentFile(JNIEnv * env, jobject thiz)
{
LOG(LDEBUG, ("cancelCurrentFile, currentRequest=", g_currentRequest.get()));
g_currentRequest.reset();
}
JNIEXPORT int JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_startNextFileDownload(JNIEnv * env,
jobject thiz, jobject observer)
{
if (g_filesToDownload.empty())
return ERR_NO_MORE_FILES;
FileToDownload & curFile = g_filesToDownload.back();
LOG(LDEBUG, ("downloading", curFile.m_fileName, "sized", curFile.m_fileSize, "bytes"));
CallbackT onFinish(bind(&DownloadFileFinished, jni::make_global_ref(observer), _1));
CallbackT onProgress(bind(&DownloadFileProgress, jni::make_global_ref(observer), _1));
g_currentRequest.reset(HttpRequest::PostJson(
GetPlatform().ResourcesMetaServerUrl(), curFile.m_fileName,
bind(&DownloadURLListFinished, _1, onFinish, onProgress)));
return ERR_FILE_IN_PROGRESS;
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_DownloadResourcesActivity_loadKmzFile(
JNIEnv * env, jobject thiz, jstring path)
{
return g_framework->NativeFramework()->AddBookmarksFile(jni::ToNativeString(env, path));
}
}
|