|
1 | 1 | #include "firestore/src/android/timestamp_android.h" |
2 | 2 |
|
3 | | -#include <stdint.h> |
4 | | - |
5 | | -#include "app/src/util_android.h" |
6 | | -#include "firestore/src/android/util_android.h" |
| 3 | +#include "firestore/src/jni/env.h" |
| 4 | +#include "firestore/src/jni/loader.h" |
7 | 5 |
|
8 | 6 | namespace firebase { |
9 | 7 | namespace firestore { |
| 8 | +namespace { |
10 | 9 |
|
11 | | -// clang-format off |
12 | | -#define TIMESTAMP_METHODS(X) \ |
13 | | - X(Constructor, "<init>", "(JI)V", util::kMethodTypeInstance), \ |
14 | | - X(GetSeconds, "getSeconds", "()J"), \ |
15 | | - X(GetNanoseconds, "getNanoseconds", "()I") |
16 | | -// clang-format on |
| 10 | +using jni::Class; |
| 11 | +using jni::Constructor; |
| 12 | +using jni::Env; |
| 13 | +using jni::Local; |
| 14 | +using jni::Method; |
17 | 15 |
|
18 | | -METHOD_LOOKUP_DECLARATION(timestamp, TIMESTAMP_METHODS) |
19 | | -METHOD_LOOKUP_DEFINITION(timestamp, |
20 | | - PROGUARD_KEEP_CLASS "com/google/firebase/Timestamp", |
21 | | - TIMESTAMP_METHODS) |
| 16 | +constexpr char kClassName[] = |
| 17 | + PROGUARD_KEEP_CLASS "com/google/firebase/Timestamp"; |
| 18 | +Constructor<TimestampInternal> kConstructor("(JI)V"); |
| 19 | +Method<int64_t> kGetSeconds("getSeconds", "()J"); |
| 20 | +Method<int32_t> kGetNanoseconds("getNanoseconds", "()I"); |
22 | 21 |
|
23 | | -/* static */ |
24 | | -jobject TimestampInternal::TimestampToJavaTimestamp( |
25 | | - JNIEnv* env, const Timestamp& timestamp) { |
26 | | - jobject result = env->NewObject( |
27 | | - timestamp::GetClass(), timestamp::GetMethodId(timestamp::kConstructor), |
28 | | - static_cast<jlong>(timestamp.seconds()), |
29 | | - static_cast<jint>(timestamp.nanoseconds())); |
30 | | - CheckAndClearJniExceptions(env); |
31 | | - return result; |
32 | | -} |
| 22 | +jclass g_clazz = nullptr; |
| 23 | + |
| 24 | +} // namespace |
33 | 25 |
|
34 | | -/* static */ |
35 | | -Timestamp TimestampInternal::JavaTimestampToTimestamp(JNIEnv* env, |
36 | | - jobject obj) { |
37 | | - jlong seconds = |
38 | | - env->CallLongMethod(obj, timestamp::GetMethodId(timestamp::kGetSeconds)); |
39 | | - jint nanoseconds = env->CallIntMethod( |
40 | | - obj, timestamp::GetMethodId(timestamp::kGetNanoseconds)); |
41 | | - CheckAndClearJniExceptions(env); |
42 | | - return Timestamp{static_cast<int64_t>(seconds), |
43 | | - static_cast<int32_t>(nanoseconds)}; |
| 26 | +void TimestampInternal::Initialize(jni::Loader& loader) { |
| 27 | + g_clazz = |
| 28 | + loader.LoadClass(kClassName, kConstructor, kGetSeconds, kGetNanoseconds); |
44 | 29 | } |
45 | 30 |
|
46 | | -/* static */ |
47 | | -jclass TimestampInternal::GetClass() { return timestamp::GetClass(); } |
| 31 | +Class TimestampInternal::GetClass() { return Class(g_clazz); } |
48 | 32 |
|
49 | | -/* static */ |
50 | | -bool TimestampInternal::Initialize(App* app) { |
51 | | - JNIEnv* env = app->GetJNIEnv(); |
52 | | - jobject activity = app->activity(); |
53 | | - bool result = timestamp::CacheMethodIds(env, activity); |
54 | | - util::CheckAndClearJniExceptions(env); |
55 | | - return result; |
| 33 | +Local<TimestampInternal> TimestampInternal::Create(Env& env, |
| 34 | + const Timestamp& timestamp) { |
| 35 | + return env.New(kConstructor, timestamp.seconds(), timestamp.nanoseconds()); |
56 | 36 | } |
57 | 37 |
|
58 | | -/* static */ |
59 | | -void TimestampInternal::Terminate(App* app) { |
60 | | - JNIEnv* env = app->GetJNIEnv(); |
61 | | - timestamp::ReleaseClass(env); |
62 | | - util::CheckAndClearJniExceptions(env); |
| 38 | +Timestamp TimestampInternal::ToPublic(Env& env) const { |
| 39 | + int64_t seconds = env.Call(*this, kGetSeconds); |
| 40 | + int32_t nanoseconds = env.Call(*this, kGetNanoseconds); |
| 41 | + return Timestamp(seconds, nanoseconds); |
63 | 42 | } |
64 | 43 |
|
65 | 44 | } // namespace firestore |
|
0 commit comments