From 56a6cbf701453bce47abb8fe24b40d47dc3a66cc Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Mon, 6 Jan 2025 14:57:19 +0300 Subject: [PATCH 1/3] feat: call js function --- sdk/src/main/AndroidManifest.xml | 2 +- .../android/sdk/TransparentActivity.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/sdk/src/main/AndroidManifest.xml b/sdk/src/main/AndroidManifest.xml index 4a73b03db..c64605f87 100644 --- a/sdk/src/main/AndroidManifest.xml +++ b/sdk/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:exported="false"/> diff --git a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java index b18ebcace..0754a7a1d 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java @@ -37,6 +37,8 @@ public class TransparentActivity extends Activity { WebView webView; RelativeLayout relativeLayout; static ContentCallback globalContentCallback; + private int lastWidth = -1; + private int lastHeight = -1; @Override protected void onCreate(Bundle savedInstanceState) { @@ -114,6 +116,8 @@ private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfi final Display display = wm.getDefaultDisplay(); final DisplayMetrics metrics = new DisplayMetrics(); // this gets all display.getMetrics(metrics); + lastWidth = metrics.widthPixels; + lastHeight = metrics.heightPixels; if (config == null) { Log.w(Countly.TAG, "[TransparentActivity] setupConfig, Config is null, using default values with full screen size"); @@ -160,6 +164,28 @@ public void onConfigurationChanged(android.content.res.Configuration newConfig) super.onConfigurationChanged(newConfig); Log.d(Countly.TAG, "[TransparentActivity] onConfigurationChanged orientation: [" + newConfig.orientation + "], currentOrientation: [" + currentOrientation + "]"); Log.v(Countly.TAG, "[TransparentActivity] onConfigurationChanged, Landscape: [" + Configuration.ORIENTATION_LANDSCAPE + "] Portrait: [" + Configuration.ORIENTATION_PORTRAIT + "]"); + + // CHANGE SCREEN SIZE + final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); + final Display display = wm.getDefaultDisplay(); + final DisplayMetrics metrics = new DisplayMetrics(); + display.getMetrics(metrics); + boolean portrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT; + + if (metrics.widthPixels != lastWidth || metrics.heightPixels != lastHeight) { + int scaledWidth = (int) Math.ceil(metrics.widthPixels / metrics.density); + int scaledHeight = (int) Math.ceil(metrics.heightPixels / metrics.density); + + int portraitWidth = portrait ? scaledWidth : scaledHeight; + int portraitHeight = portrait ? scaledHeight : scaledWidth; + int landscapeWidth = portrait ? scaledHeight : scaledWidth; + int landscapeHeight = portrait ? scaledWidth : scaledHeight; + webView.loadUrl("javascript:resizeContent(" + portraitWidth + "," + portraitHeight + "," + landscapeWidth + "," + landscapeHeight + ");"); + + lastWidth = metrics.widthPixels; + lastHeight = metrics.heightPixels; + } + if (currentOrientation != newConfig.orientation) { currentOrientation = newConfig.orientation; Log.i(Countly.TAG, "[TransparentActivity] onConfigurationChanged, orientation changed to currentOrientation: [" + currentOrientation + "]"); From 41171762fbd7149452ebb1875af751fe39bd21ad Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Mon, 6 Jan 2025 16:08:28 +0300 Subject: [PATCH 2/3] feat: convert to post message --- .../java/ly/count/android/sdk/TransparentActivity.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java index 0754a7a1d..7db43de5e 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java @@ -170,17 +170,12 @@ public void onConfigurationChanged(android.content.res.Configuration newConfig) final Display display = wm.getDefaultDisplay(); final DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); - boolean portrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT; if (metrics.widthPixels != lastWidth || metrics.heightPixels != lastHeight) { int scaledWidth = (int) Math.ceil(metrics.widthPixels / metrics.density); int scaledHeight = (int) Math.ceil(metrics.heightPixels / metrics.density); - int portraitWidth = portrait ? scaledWidth : scaledHeight; - int portraitHeight = portrait ? scaledHeight : scaledWidth; - int landscapeWidth = portrait ? scaledHeight : scaledWidth; - int landscapeHeight = portrait ? scaledWidth : scaledHeight; - webView.loadUrl("javascript:resizeContent(" + portraitWidth + "," + portraitHeight + "," + landscapeWidth + "," + landscapeHeight + ");"); + webView.loadUrl("javascript:window.postMessage({type: 'resize', width: " + scaledWidth + ", height: " + scaledHeight + "}, '*');"); lastWidth = metrics.widthPixels; lastHeight = metrics.heightPixels; From 27f411ff39753cf32971ff5845bfd3798ff96117 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Tue, 7 Jan 2025 09:44:56 +0300 Subject: [PATCH 3/3] feat: fully change to resize_me --- CHANGELOG.md | 3 ++ .../android/sdk/TransparentActivity.java | 37 +++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f1245b8d..674ae564a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## XX.XX.XX +* Improved content size management of content blocks. + ## 24.7.8 * Added a config option to content (setZoneTimerInterval) to set content zone timer. (Experimental!) diff --git a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java index 7db43de5e..26c47baa4 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java @@ -37,8 +37,6 @@ public class TransparentActivity extends Activity { WebView webView; RelativeLayout relativeLayout; static ContentCallback globalContentCallback; - private int lastWidth = -1; - private int lastHeight = -1; @Override protected void onCreate(Bundle savedInstanceState) { @@ -116,8 +114,6 @@ private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfi final Display display = wm.getDefaultDisplay(); final DisplayMetrics metrics = new DisplayMetrics(); // this gets all display.getMetrics(metrics); - lastWidth = metrics.widthPixels; - lastHeight = metrics.heightPixels; if (config == null) { Log.w(Countly.TAG, "[TransparentActivity] setupConfig, Config is null, using default values with full screen size"); @@ -139,7 +135,7 @@ private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfi return config; } - private void changeOrientation(TransparentActivityConfig config, int navBarHeight) { + private void resizeContent(TransparentActivityConfig config, int navBarHeight) { Log.d(Countly.TAG, "[TransparentActivity] changeOrientation, config x: [" + config.x + "] y: [" + config.y + "] width: [" + config.width + "] height: [" + config.height + "]"); WindowManager.LayoutParams params = getWindow().getAttributes(); params.x = config.x; @@ -163,7 +159,10 @@ private void changeOrientation(TransparentActivityConfig config, int navBarHeigh public void onConfigurationChanged(android.content.res.Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(Countly.TAG, "[TransparentActivity] onConfigurationChanged orientation: [" + newConfig.orientation + "], currentOrientation: [" + currentOrientation + "]"); - Log.v(Countly.TAG, "[TransparentActivity] onConfigurationChanged, Landscape: [" + Configuration.ORIENTATION_LANDSCAPE + "] Portrait: [" + Configuration.ORIENTATION_PORTRAIT + "]"); + + if (currentOrientation != newConfig.orientation) { + currentOrientation = newConfig.orientation; + } // CHANGE SCREEN SIZE final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); @@ -171,29 +170,19 @@ public void onConfigurationChanged(android.content.res.Configuration newConfig) final DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); - if (metrics.widthPixels != lastWidth || metrics.heightPixels != lastHeight) { - int scaledWidth = (int) Math.ceil(metrics.widthPixels / metrics.density); - int scaledHeight = (int) Math.ceil(metrics.heightPixels / metrics.density); - - webView.loadUrl("javascript:window.postMessage({type: 'resize', width: " + scaledWidth + ", height: " + scaledHeight + "}, '*');"); + int scaledWidth = (int) Math.ceil(metrics.widthPixels / metrics.density); + int scaledHeight = (int) Math.ceil(metrics.heightPixels / metrics.density); - lastWidth = metrics.widthPixels; - lastHeight = metrics.heightPixels; - } - - if (currentOrientation != newConfig.orientation) { - currentOrientation = newConfig.orientation; - Log.i(Countly.TAG, "[TransparentActivity] onConfigurationChanged, orientation changed to currentOrientation: [" + currentOrientation + "]"); - changeOrientationInternal(); - } + // refactor in the future to use the resize_me action + webView.loadUrl("javascript:window.postMessage({type: 'resize', width: " + scaledWidth + ", height: " + scaledHeight + "}, '*');"); } - private void changeOrientationInternal() { + private void resizeContentInternal() { switch (currentOrientation) { case Configuration.ORIENTATION_LANDSCAPE: if (configLandscape != null) { configLandscape = setupConfig(configLandscape); - changeOrientation(configLandscape, 0); + resizeContent(configLandscape, 0); } break; case Configuration.ORIENTATION_PORTRAIT: @@ -207,7 +196,7 @@ private void changeOrientationInternal() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { navBarHeight = getNavigationBarHeight(); } - changeOrientation(configPortrait, navBarHeight); + resizeContent(configPortrait, navBarHeight); } break; default: @@ -309,7 +298,7 @@ private void resizeMeAction(Map query) { configLandscape.width = (int) Math.ceil(landscape.getInt("w") * density); configLandscape.height = (int) Math.ceil(landscape.getInt("h") * density); - changeOrientationInternal(); + resizeContentInternal(); } catch (JSONException e) { Log.e(Countly.TAG, "[TransparentActivity] resizeMeAction, Failed to parse resize JSON", e); }