diff --git a/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java b/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java index 7409a8cc..ce23aef0 100644 --- a/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java +++ b/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java @@ -25,15 +25,28 @@ private AndroidKeyBridge() public void init(Context context) { + if (context == null) { + Log.w("AndroidKeyBridge", "Provided context is null, trying to get UnityPlayer.currentActivity"); + try { + context = com.unity3d.player.UnityPlayer.currentActivity; + } catch (Exception e) { + Log.e("AndroidKeyBridge", "Failed to get UnityPlayer.currentActivity", e); + } + } + + if (context == null) { + Log.e("AndroidKeyBridge", "Context is still null, cannot initialize AndroidKeyBridge"); + return; + } + this.context = context; - if (masterKey == null) { + if (masterKey == null || sharedPreferences == null) { try { masterKey = new MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build(); - sharedPreferences = EncryptedSharedPreferences.create( context, "secret_shared_prefs", @@ -42,9 +55,9 @@ public void init(Context context) EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ); } catch (GeneralSecurityException e){ - Log.d("Exception", e.getMessage()); + Log.d("AndroidKeyBridge", "Encountered error when initializing AndroidKeyBridge: " + e.getMessage()); } catch (IOException e){ - Log.d("Exception", e.getMessage()); + Log.d("AndroidKeyBridge", "Encountered error when initializing AndroidKeyBridge: " + e.getMessage()); } } } @@ -74,11 +87,17 @@ public static String GetKeychainValue(String key) private void RunSaveKeychainValue(String key, String value) { + if (masterKey == null || sharedPreferences == null) { + init(context); + } sharedPreferences.edit().putString(key, value).apply(); } private String RunGetKeychainValue(String key) { + if (masterKey == null || sharedPreferences == null) { + init(context); + } return sharedPreferences.getString(key, ""); } } diff --git a/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java.meta b/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java.meta index 2b981aa8..fc8abc26 100644 --- a/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java.meta +++ b/Packages/Sequence-Unity/Plugins/Android/AndroidKeyBridge.java.meta @@ -28,7 +28,7 @@ PluginImporter: - first: Android: Android second: - enabled: 0 + enabled: 1 settings: CPU: ARMv7 - first: diff --git a/Packages/Sequence-Unity/package.json b/Packages/Sequence-Unity/package.json index 352e68f4..a813ab1f 100644 --- a/Packages/Sequence-Unity/package.json +++ b/Packages/Sequence-Unity/package.json @@ -1,6 +1,6 @@ { "name": "xyz.0xsequence.waas-unity", - "version": "4.0.4", + "version": "4.0.5", "displayName": "Sequence Embedded Wallet SDK", "description": "A Unity SDK for Sequence APIs", "unity": "2021.3",