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
|
#include "Platform.hpp"
#include "../core/jni_helper.hpp"
#include "../../../../../platform/settings.hpp"
#include "../../../../../base/logging.hpp"
string Platform::UniqueClientId() const
{
string res;
if (!Settings::Get("UniqueClientID", res))
{
JNIEnv * env = jni::GetEnv();
if (!env)
{
LOG(LWARNING, ("Can't get JNIEnv"));
return "";
}
jclass uuidClass = env->FindClass("java/util/UUID");
ASSERT(uuidClass, ("Can't find java class java/util/UUID"));
jmethodID randomUUIDId = env->GetStaticMethodID(uuidClass, "randomUUID", "()Ljava/util/UUID;");
ASSERT(randomUUIDId, ("Can't find static java/util/UUID.randomUUIDId() method"));
jobject uuidInstance = env->CallStaticObjectMethod(uuidClass, randomUUIDId);
ASSERT(uuidInstance, ("UUID.randomUUID() returned NULL"));
jmethodID toStringId = env->GetMethodID(uuidClass, "toString", "()Ljava/lang/String;");
ASSERT(toStringId, ("Can't find java/util/UUID.toString() method"));
jstring uuidString = (jstring)env->CallObjectMethod(uuidInstance, toStringId);
ASSERT(uuidString, ("UUID.toString() returned NULL"));
char const * uuidUtf8 = env->GetStringUTFChars(uuidString, 0);
if (uuidUtf8 != 0)
{
res = uuidUtf8;
env->ReleaseStringUTFChars(uuidString, uuidUtf8);
}
res = HashUniqueID(res);
Settings::Set("UniqueClientID", res);
}
return res;
}
namespace android
{
void Platform::Initialize(JNIEnv * env,
jstring apkPath,
jstring storagePath,
jstring tmpPath,
bool isPro)
{
m_resourcesDir = jni::ToNativeString(env, apkPath);
// Settings file should always be in one place (default external storage).
// It stores path to current maps storage.
m_settingsDir = jni::ToNativeString(env, storagePath);
if (!Settings::Get("StoragePath", m_writableDir) || !HasAvailableSpaceForWriting(1024))
{
// If no saved storage path or the storage is unavailable
// (disconnected from the last session), assign writable
// path to the default external storage.
m_writableDir = m_settingsDir;
}
m_tmpDir = jni::ToNativeString(env, tmpPath);
m_isPro = isPro;
LOG(LDEBUG, ("Apk path = ", m_resourcesDir));
LOG(LDEBUG, ("Writable path = ", m_writableDir));
LOG(LDEBUG, ("Temporary path = ", m_tmpDir));
LOG(LDEBUG, ("Settings path = ", m_settingsDir));
}
void Platform::OnExternalStorageStatusChanged(bool isAvailable)
{
}
string Platform::GetStoragePathPrefix() const
{
size_t const count = m_writableDir.size();
ASSERT_GREATER ( count, 2, () );
size_t const i = m_writableDir.find_last_of('/', count-2);
ASSERT_GREATER ( i, 0, () );
return m_writableDir.substr(0, i);
}
void Platform::SetStoragePath(string const & path)
{
m_writableDir = path;
Settings::Set("StoragePath", m_writableDir);
}
bool Platform::HasAvailableSpaceForWriting(uint64_t size) const
{
return (GetWritableStorageStatus(size) == ::Platform::STORAGE_OK);
}
Platform & Platform::Instance()
{
static Platform platform;
return platform;
}
}
Platform & GetPlatform()
{
return android::Platform::Instance();
}
|