Skip to content

Commit

Permalink
Fixes #130, fixes some warnings in AdMob and Crossbow Android
Browse files Browse the repository at this point in the history
  • Loading branch information
enfipy committed Aug 4, 2022
1 parent df02215 commit 97f351b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Crossbow : Fragment() {
pluginRegistry = CrossbowPluginRegistry.initializePluginRegistry(this)

Log.v(TAG, "Initializing CrossbowLib Instance")
// CrossbowLib.initialize(activity, this, activity!!.assets)
onRenderInit()
CrossbowLib.initialize(activity, this, activity!!.assets)
}

override fun onAttach(context: Context) {
Expand Down Expand Up @@ -151,7 +150,7 @@ class Crossbow : Fragment() {
}
}
if (shouldQuit) {
// CrossbowLib.back()
CrossbowLib.onBackPressed()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ object CrossbowLib {
@JvmStatic
external fun onDestroy()

// /**
// * Forward [Activity.onBackPressed] event from the main thread to the GL thread.
// */
// @JvmStatic
// external fun back()
/**
* Forward [Activity.onBackPressed] event from the main thread to the GL thread.
*/
@JvmStatic
external fun onBackPressed()

/**
* Forward the results from a permission request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class CrossbowPlugin(
* Provides access to the underlying [Activity].
*/
protected val activity: Activity?
@Suppress("DEPRECATION")
get() = crossbow.activity

/**
Expand All @@ -78,7 +79,7 @@ abstract class CrossbowPlugin(
* @see Activity.onCreate
* @return the plugin's view to be included; null if no views should be included.
*/
open fun onMainCreate(activity: Activity): View? {
open fun onMainCreate(pActivity: Activity): View? {
return null
}

Expand Down
28 changes: 28 additions & 0 deletions platform/android/src/crossbow/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::error::*;
use jni::{objects::JObject, JNIEnv};

pub(crate) fn crossbow_initialize(
env: JNIEnv,
activity: JObject,
crossbow_instance: JObject,
_asset_manager: JObject,
) -> Result<()> {
println!("CrossbowLib_initialize: {:?}", activity);

env.call_method(crossbow_instance, "onRenderInit", "()V", &[])?;
env.exception_check()?;

// TODO: Create wrapper around CrossbowInstance

Ok(())
}

pub(crate) fn crossbow_on_back_pressed(_env: JNIEnv) -> Result<()> {
println!("CrossbowLib_onBackPressed");
Ok(())
}

pub(crate) fn crossbow_on_destroy(_env: JNIEnv) -> Result<()> {
println!("CrossbowLib_onDestroy");
Ok(())
}
19 changes: 10 additions & 9 deletions platform/android/src/externs/crossbow_lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::permission::*;
use crate::{crossbow::*, permission::*};
use jni::{
objects::{JClass, JObject, JString},
sys::jboolean,
Expand All @@ -12,20 +12,21 @@ pub extern "C" fn Java_com_crossbow_library_CrossbowLib_initialize(
_class: JClass,
activity: JObject,
crossbow_instance: JObject,
_asset_manager: JObject,
asset_manager: JObject,
) {
println!("CrossbowLib_initialize: {:?}", activity);

env.call_method(crossbow_instance, "onRenderInit", "()V", &[])
.unwrap();
crossbow_initialize(env, activity, crossbow_instance, asset_manager).unwrap();
}

// TODO: Create wrapper around CrossbowInstance
#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn Java_com_crossbow_library_CrossbowLib_onBackPressed(env: JNIEnv, _class: JClass) {
crossbow_on_back_pressed(env).unwrap();
}

#[no_mangle]
#[allow(non_snake_case)]
pub extern "C" fn Java_com_crossbow_library_CrossbowLib_onDestroy(_env: JNIEnv, _class: JClass) {
println!("CrossbowLib_onDestroy");
pub extern "C" fn Java_com_crossbow_library_CrossbowLib_onDestroy(env: JNIEnv, _class: JClass) {
crossbow_on_destroy(env).unwrap();
}

#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod externs;
pub(crate) mod utils;

pub mod crossbow;
pub mod error;
pub mod permission;
pub mod plugin;
Expand Down
1 change: 1 addition & 0 deletions platform/android/src/plugin/jni_singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl JniSingleton {
method.signature.ret.clone(),
args,
)?;
env.exception_check()?;
Ok(result)
}
}
19 changes: 13 additions & 6 deletions plugins/admob/android/src/com/crossbow/admob/AdMob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ class AdMob(crossbow: Crossbow) : CrossbowPlugin(crossbow) {
"MEDIUM_RECTANGLE" -> aAdView!!.setAdSize(AdSize.MEDIUM_RECTANGLE)
"FULL_BANNER" -> aAdView!!.setAdSize(AdSize.FULL_BANNER)
"LEADERBOARD" -> aAdView!!.setAdSize(AdSize.LEADERBOARD)
"ADAPTIVE" -> aAdView!!.setAdSize(adSizeAdaptive)
else -> aAdView!!.setAdSize(AdSize.SMART_BANNER) // Replaced by getCurrentOrientationAnchoredAdaptiveBannerAdSize(Context, int)
else -> aAdView!!.setAdSize(adSizeAdaptive)
}
aAdSize =
aAdView!!.getAdSize() //store AdSize of banner due a bug (throws error when do aAdView!!.getAdSize() called by Crossbow)
Expand Down Expand Up @@ -569,7 +568,7 @@ class AdMob(crossbow: Crossbow) : CrossbowPlugin(crossbow) {
if (aConsentInformation!!.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
consentForm.show(
aActivity!!
) { formError ->
) { _ ->
loadConsentForm()
emitSignal("consent_form_dismissed")
}
Expand Down Expand Up @@ -650,10 +649,18 @@ class AdMob(crossbow: Crossbow) : CrossbowPlugin(crossbow) {
// If the ad hasn't been laid out, default to the full screen width.
get() {
// Determine the screen width (less decorations) to use for the ad width.
val display: Display = aActivity!!.getWindowManager().getDefaultDisplay() // getDefaultDisplay() method was deprecated in API level 30. Use Context#getDisplay() instead.
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density: Float = outMetrics.density
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
val display = aActivity!!.display
@Suppress("DEPRECATION")
display?.getRealMetrics(outMetrics)
} else {
@Suppress("DEPRECATION")
val display = aActivity!!.windowManager.defaultDisplay
@Suppress("DEPRECATION")
display.getMetrics(outMetrics)
}
val density: Float = outMetrics.density
var adWidthPixels: Float = aCrossbowLayout!!.getWidth().toFloat()

// If the ad hasn't been laid out, default to the full screen width.
Expand Down

0 comments on commit 97f351b

Please sign in to comment.