Hi, I've recently been reviewing crates that use the ndk-context API to ensure they are not making any assumptions that the Context reference comes from an Activity reference.
I've noticed that the manganis package appears to make this assumption via the with_activity utility that was added in #4842
As it is currently, the ndk-context API only promises to provide an application.content.Context reference and does not specifically guarantee that this context comes from an Activity.
As an implementation detail android-activity does currently initialise ndk-context with an Activity-based context reference but we need to change this in order to address other bugs that this leads to: rust-mobile/android-activity#228, rust-mobile/android-activity#58
Briefly speaking, Android's application programming model allows an application to have multiple (or zero) Activities per application and the current choice to store an Activity reference in the global, static ndk-context state is problematic because:
ndk-context is designed to only be initialized once (it will panic if trying to update its state), so even if you only ever have one Activity at a time, we have no way to reset the ndk-context state with a new Activity reference
- Since
ndk-context only exposes a single Context reference then in case you have an application that creates multiple Activities then ndk-context can only be associated with one of them.
- whatever reference is put into
ndk-context will need to have a global JNI reference to ensure it remains valid for the lifetime of the program which isn't appropriate for an Activity whose lifetime is only associated with a single thread.
For reference, and as a heads up; the plan (for android-activity) is to start initializing ndk-context with an Application Context, which is better aligned with the ndk-context design which only supports tracking one-time-initialized global state. (ref: rust-mobile/android-activity#229)
Expected behavior
Perhaps manganis will actually already work with any android.content.Context reference and doesn't strictly need an Activity in practice and so it could rename with_activity to with_context.
Note: that getAssets is a Java API that is exposed by any android.content.Context and so it would be possible to get to an AssetManager via any Context.
Technically the AssetManager associated with a specific Activity could be able to find additional resources in some circumstances (compared to the Application AssetManager) but it's more likely that there's no difference.
Perhaps the manganis crate could have some Android-specific config or initialization API that can be used to explicitly provide it a JNI reference to an Activity or maybe an AssetManager that is acquired externally - e.g. via android-activity (AndroidApp::activity_as_ptr) or perhaps via some Java native method / JNI integration with a custom Activity subclass.
Hi, I've recently been reviewing crates that use the
ndk-contextAPI to ensure they are not making any assumptions that theContextreference comes from anActivityreference.I've noticed that the
manganispackage appears to make this assumption via thewith_activityutility that was added in #4842As it is currently, the
ndk-contextAPI only promises to provide anapplication.content.Contextreference and does not specifically guarantee that this context comes from anActivity.As an implementation detail
android-activitydoes currently initialisendk-contextwith anActivity-based context reference but we need to change this in order to address other bugs that this leads to: rust-mobile/android-activity#228, rust-mobile/android-activity#58Briefly speaking, Android's application programming model allows an application to have multiple (or zero) Activities per application and the current choice to store an Activity reference in the global, static
ndk-contextstate is problematic because:ndk-contextis designed to only be initialized once (it will panic if trying to update its state), so even if you only ever have one Activity at a time, we have no way to reset thendk-contextstate with a newActivityreferencendk-contextonly exposes a singleContextreference then in case you have an application that creates multiple Activities thenndk-contextcan only be associated with one of them.ndk-contextwill need to have a global JNI reference to ensure it remains valid for the lifetime of the program which isn't appropriate for an Activity whose lifetime is only associated with a single thread.For reference, and as a heads up; the plan (for
android-activity) is to start initializingndk-contextwith anApplicationContext, which is better aligned with thendk-contextdesign which only supports tracking one-time-initialized global state. (ref: rust-mobile/android-activity#229)Expected behavior
Perhaps
manganiswill actually already work with anyandroid.content.Contextreference and doesn't strictly need anActivityin practice and so it could renamewith_activitytowith_context.Note: that
getAssetsis a Java API that is exposed by anyandroid.content.Contextand so it would be possible to get to anAssetManagervia anyContext.Technically the
AssetManagerassociated with a specificActivitycould be able to find additional resources in some circumstances (compared to theApplicationAssetManager) but it's more likely that there's no difference.Perhaps the
manganiscrate could have some Android-specific config or initialization API that can be used to explicitly provide it a JNI reference to anActivityor maybe anAssetManagerthat is acquired externally - e.g. viaandroid-activity(AndroidApp::activity_as_ptr) or perhaps via some Java native method / JNI integration with a custom Activity subclass.