Page MenuHomePhabricator

Introduce MMKV to Android app
ClosedPublic

Authored by marcin on Feb 12 2024, 9:09 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Dec 2, 6:01 PM
Unknown Object (File)
Fri, Nov 29, 3:45 PM
Unknown Object (File)
Fri, Nov 29, 12:24 AM
Unknown Object (File)
Thu, Nov 28, 9:18 PM
Unknown Object (File)
Thu, Nov 28, 11:20 AM
Unknown Object (File)
Thu, Nov 28, 4:13 AM
Unknown Object (File)
Wed, Nov 27, 8:10 PM
Unknown Object (File)
Wed, Nov 27, 11:00 AM
Subscribers

Details

Summary

This differential introduces MMKV to Android app. For now only string getter and setter is implemented but future diffs will introduce new methods. This
differential introduces simplest API we can test.

Test Plan
  1. Below the CommMMKV.initialize() line add line that sets value under a string key.
  2. In CommNotificationsHandler.java at the beginning add line that tries to get string from the same key and logs this key in case it is present.
  3. Send notification and check it the same string is logged.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Rebase to update commit message

native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
20 ↗(On Diff #37011)

Same as here.

27 ↗(On Diff #37011)

I have to admit that is a little bit of code duplication - we could export comm::crypto:Tools::generateRandomString to Java. It could add some additional boilerplate so I opted for faster option. Additionally Base 64 strings don't differ much from the output comm::crypto:Tools::generateRandomString - the differences are that instead of " " and "_" we use "+" and "=".

40 ↗(On Diff #37011)

I tried to recreate dispatch_once functionality here being inspired by what we do with CommSecureStore.

59 ↗(On Diff #37011)
native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
27–30 ↗(On Diff #37011)

On iOS, you generate random string of length 16, while here you base64-encode 16 bytes, which results in ~20-24 char long string.
You can simply use substr()

Fix bug: replace "initializeMMKV" with "initialize" when calling Java method from C++ CommMMKVJavaClass.

tomek added inline comments.
native/android/app/src/cpp/CommMMKV.cpp
36 ↗(On Diff #37064)

Does this method return std::optional<std::string>?

44–52 ↗(On Diff #37064)

It doesn't seem we're capturing anything

69 ↗(On Diff #37064)

Can we have the new line at the end?

native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
18–19 ↗(On Diff #37064)

It doesn't seem efficient to create a new instance of MMKV on every call of getMMKVInstance. Can we store it as a static variable?

82 ↗(On Diff #37064)

newline

27–30 ↗(On Diff #37011)

Doesn't that mean that the number of possible encryption keys is different on different platforms?

This revision is now accepted and ready to land.Feb 14 2024, 8:44 AM
  1. Refactor to match new iOS logic.
  2. Address Bartek and Tomek comments.
native/android/app/src/cpp/CommMMKV.cpp
36 ↗(On Diff #37064)

Call to result->toStdString() returns std::string but it is a C++ feature that we can return type A from a function which signature says that the return type is T if T has constructor from A.

native/android/app/src/main/java/app/comm/android/fbjni/CommMMKV.java
18–19 ↗(On Diff #37064)

It is not recreated - MMKV has a global dict mapping from ID to instance. See: https://github.com/Tencent/MMKV/blob/master/Core/MMKV_Android.cpp#L139

This revision was automatically updated to reflect the committed changes.