From d77d75056dd3364072fa327c6b6dfa6430674700 Mon Sep 17 00:00:00 2001 From: bduyng Date: Wed, 16 Aug 2023 10:23:43 +0700 Subject: [PATCH] improvements --- android/build.gradle | 1 + .../InstantClipSyncModule.java | 57 +++++++++++++++++-- src/index.tsx | 22 ++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 91e95f7..4e123ea 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -89,6 +89,7 @@ dependencies { // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" + implementation "com.google.android.gms:play-services-instantapps:18.0.1" } if (isNewArchitectureEnabled()) { diff --git a/android/src/main/java/com/instantclipsync/InstantClipSyncModule.java b/android/src/main/java/com/instantclipsync/InstantClipSyncModule.java index c6ee798..03bbf96 100644 --- a/android/src/main/java/com/instantclipsync/InstantClipSyncModule.java +++ b/android/src/main/java/com/instantclipsync/InstantClipSyncModule.java @@ -1,6 +1,9 @@ package com.instantclipsync; import androidx.annotation.NonNull; +import android.content.Context; +import com.google.android.gms.instantapps.InstantApps; +import android.util.Log; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -8,9 +11,13 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.module.annotations.ReactModule; +import java.util.Map; +import java.util.HashMap; + @ReactModule(name = InstantClipSyncModule.NAME) public class InstantClipSyncModule extends ReactContextBaseJavaModule { public static final String NAME = "InstantClipSync"; + public static final String TAG = "InstantClipSync"; public InstantClipSyncModule(ReactApplicationContext reactContext) { super(reactContext); @@ -23,10 +30,52 @@ public String getName() { } - // Example method - // See https://reactnative.dev/docs/native-modules-android @ReactMethod - public void multiply(double a, double b, Promise promise) { - promise.resolve(a * b); + public void storeStringInInstantAppCookie(String data, Promise promise) { + ReactApplicationContext context = getReactApplicationContext(); + byte[] bytes = data.getBytes(); + // Get the maximum cookie size + int maxCookieSize = InstantApps.getPackageManagerCompat(context).getInstantAppCookieMaxSize(); + // Ensure that cookie is not over the limit + if (bytes.length > maxCookieSize) { + Log.w(TAG, "Attempt to set a cookie that is too large."); + promise.reject("Failed to store Instant App cookie", "Attempt to set a cookie that is too large."); + return; + } + + // PackageManager can throw exceptions, so it's important to handle them + try { + boolean isStored = InstantApps.getPackageManagerCompat(context).setInstantAppCookie(bytes); + + // Print out the result + if (isStored) { + // Log.i(TAG, "Successfully stored cookie."); + promise.resolve(true); + } else { + // Log.i(TAG, "Failed to store cookie."); + promise.reject("Failed to store Instant App cookie", ""); + } + } catch (Exception e) { + // Log.e(TAG, "Failed to set Instant App cookie", e); + promise.reject("Failed to set Instant App cookie", e); + } + } + + @ReactMethod + public void retrieveStringInInstantAppCookie(Promise promise) { + String retrievedData = ""; + ReactApplicationContext context = getReactApplicationContext(); + + try { + byte[] bytes = InstantApps.getPackageManagerCompat(context).getInstantAppCookie(); + // Convert the bytes back to string + retrievedData = new String(bytes); + Log.i(TAG, "Instant App cookie bytes.length " + bytes.length + " " + retrievedData); + } catch (Exception e) { + Log.e(TAG, "Failed to get Instant App cookie", e); + promise.reject("Failed to get Instant App cookie", e); + } + + promise.resolve(retrievedData); } } diff --git a/src/index.tsx b/src/index.tsx index fffbc10..6b26076 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -17,6 +17,24 @@ const InstantClipSync = NativeModules.InstantClipSync } ); -export function multiply(a: number, b: number): Promise { - return InstantClipSync.multiply(a, b); +export function save(key: string, value: string) { + return InstantClipSync.storeStringInInstantAppCookie(`${key}:${value}`); +} + +export function get(key?: string) { + return new Promise(async (resolve, reject) => { + try { + const raw = + (await InstantClipSync.retrieveStringInInstantAppCookie()) as string; + if (!key) return resolve(raw); + resolve( + raw + .split(';') + .find((pair) => pair.indexOf(`${key}:`) !== -1) + ?.replace(`${key}:`, '') + ); + } catch (error) { + reject(error); + } + }); }