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/5] 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/5] 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/5] 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); } From 8cd3dee335a4a04a0089fc2c5bfa498050d2e3fa Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Thu, 16 Jan 2025 14:40:47 +0300 Subject: [PATCH 4/5] fix: before merge --- .../android/sdk/TransparentActivity.java | 60 +++++++------------ 1 file changed, 22 insertions(+), 38 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 7282a1140..ea9327b5e 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java @@ -44,9 +44,7 @@ protected void onCreate(Bundle savedInstanceState) { Log.d(Countly.TAG, "[TransparentActivity] onCreate, content received, showing it"); // there is a stripe at the top of the screen for contents - // we eliminate it with no action bar full screen and this adds more smoothness - // the stripe is because of our transparency - //setTheme(android.R.style.Theme_DeviceDefault_NoActionBar_Fullscreen); + // we eliminate it with hiding the system ui hideSystemUI(); super.onCreate(savedInstanceState); overridePendingTransition(0, 0); @@ -61,53 +59,39 @@ protected void onCreate(Bundle savedInstanceState) { Log.v(Countly.TAG, "[TransparentActivity] onCreate, configPortrait x: [" + configPortrait.x + "] y: [" + configPortrait.y + "] width: [" + configPortrait.width + "] height: [" + configPortrait.height + "]"); TransparentActivityConfig config; - int navBarHeight = 0; if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { config = configLandscape; } else { - // This is only needed for the portrait mode and - // after android 35 the function that gives height gives the full height of the screen - // so we need to subtract the height of the navigation bar - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - navBarHeight = getNavigationBarHeight(); - } - config = configPortrait; } config = setupConfig(config); - - int width = config.width; - int height = config.height - navBarHeight; - - configLandscape.listeners.add((url, webView) -> { - if (url.startsWith(URL_START)) { - return contentUrlAction(url, configLandscape, webView); + WebViewUrlListener listener = new WebViewUrlListener() { + @Override public boolean onUrl(String url, WebView webView) { + if (url.startsWith(URL_START)) { + return contentUrlAction(url, webView); + } + return false; } - return false; - }); + }; - configPortrait.listeners.add((url, webView) -> { - if (url.startsWith(URL_START)) { - return contentUrlAction(url, configPortrait, webView); - } - return false; - }); + configLandscape.listeners.add(listener); + configPortrait.listeners.add(listener); // Configure window layout parameters WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.gravity = Gravity.TOP | Gravity.LEFT; // try out START params.x = config.x; params.y = config.y; - params.height = height; - params.width = width; + params.height = config.height; + params.width = config.width; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; getWindow().setAttributes(params); getWindow().setBackgroundDrawableResource(android.R.color.transparent); // Create and configure the layout relativeLayout = new RelativeLayout(this); - RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height); + RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(config.width, config.height); relativeLayout.setLayoutParams(layoutParams); webView = createWebView(config); @@ -127,7 +111,7 @@ private void hideSystemUI() { | View.SYSTEM_UI_FLAG_FULLSCREEN ); } - + private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfig config) { final WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); final Display display = wm.getDefaultDisplay(); @@ -154,23 +138,23 @@ private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfi return config; } - 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 + "]"); + private void resizeContent(TransparentActivityConfig config) { + Log.d(Countly.TAG, "[TransparentActivity] resizeContent, config x: [" + config.x + "] y: [" + config.y + "] width: [" + config.width + "] height: [" + config.height + "]"); WindowManager.LayoutParams params = getWindow().getAttributes(); params.x = config.x; params.y = config.y; - params.height = config.height - navBarHeight; + params.height = config.height; params.width = config.width; getWindow().setAttributes(params); ViewGroup.LayoutParams layoutParams = relativeLayout.getLayoutParams(); layoutParams.width = config.width; - layoutParams.height = config.height - navBarHeight; + layoutParams.height = config.height; relativeLayout.setLayoutParams(layoutParams); ViewGroup.LayoutParams webLayoutParams = webView.getLayoutParams(); webLayoutParams.width = config.width; - webLayoutParams.height = config.height - navBarHeight; + webLayoutParams.height = config.height; webView.setLayoutParams(webLayoutParams); } @@ -201,7 +185,7 @@ private void resizeContentInternal() { case Configuration.ORIENTATION_LANDSCAPE: if (configLandscape != null) { configLandscape = setupConfig(configLandscape); - resizeContent(configLandscape, 0); + resizeContent(configLandscape); } break; case Configuration.ORIENTATION_PORTRAIT: @@ -215,7 +199,7 @@ private void resizeContentInternal() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { navBarHeight = getNavigationBarHeight(); } - resizeContent(configPortrait, navBarHeight); + resizeContent(configPortrait); } break; default: @@ -223,7 +207,7 @@ private void resizeContentInternal() { } } - private boolean contentUrlAction(String url, TransparentActivityConfig config, WebView view) { + private boolean contentUrlAction(String url, WebView view) { Log.d(Countly.TAG, "[TransparentActivity] contentUrlAction, url: [" + url + "]"); Map query = splitQuery(url); Log.v(Countly.TAG, "[TransparentActivity] contentUrlAction, query: [" + query + "]"); From ad6a5fd443b438e7ee53836a90ebf08e17f19590 Mon Sep 17 00:00:00 2001 From: Arif Burak Demiray Date: Fri, 17 Jan 2025 15:37:03 +0300 Subject: [PATCH 5/5] feat: resize me action done --- .../android/sdk/CountlyWebViewClient.java | 4 +-- .../android/sdk/TransparentActivity.java | 29 +++++-------------- .../sdk/TransparentActivityConfig.java | 4 --- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java b/sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java index 4b123d85f..708aff8e7 100644 --- a/sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java +++ b/sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java @@ -38,7 +38,7 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request return false; } - public void registerWebViewUrlListeners(List listener) { - this.listeners.addAll(listener); + public void registerWebViewUrlListener(WebViewUrlListener listener) { + this.listeners.add(listener); } } 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 bb15fc161..c376c7361 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java @@ -7,7 +7,6 @@ import android.content.res.Configuration; import android.graphics.Color; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; @@ -76,9 +75,6 @@ protected void onCreate(Bundle savedInstanceState) { } }; - configLandscape.listeners.add(listener); - configPortrait.listeners.add(listener); - // Configure window layout parameters WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.gravity = Gravity.TOP | Gravity.LEFT; // try out START @@ -192,14 +188,6 @@ private void resizeContentInternal() { case Configuration.ORIENTATION_PORTRAIT: if (configPortrait != null) { configPortrait = setupConfig(configPortrait); - // This is only needed for the portrait mode and - // after android 35 the function that gives height gives the full height of the screen - // so we need to subtract the height of the navigation bar - // this is implemented twice because in the future resize_me action will be able to change the height of the content - int navBarHeight = 0; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { - navBarHeight = getNavigationBarHeight(); - } resizeContent(configPortrait); } break; @@ -393,18 +381,17 @@ private WebView createWebView(TransparentActivityConfig config) { webView.clearHistory(); CountlyWebViewClient client = new CountlyWebViewClient(); - client.registerWebViewUrlListeners(config.listeners); + client.registerWebViewUrlListener(new WebViewUrlListener() { + @Override public boolean onUrl(String url, WebView webView) { + if (url.startsWith(URL_START)) { + return contentUrlAction(url, webView); + } + return false; + } + }); webView.setWebViewClient(client); webView.loadUrl(config.url); return webView; } - - private int getNavigationBarHeight() { - int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android"); - if (resourceId > 0) { - return getResources().getDimensionPixelSize(resourceId); - } - return 0; - } } diff --git a/sdk/src/main/java/ly/count/android/sdk/TransparentActivityConfig.java b/sdk/src/main/java/ly/count/android/sdk/TransparentActivityConfig.java index 466416e48..5a764d66f 100644 --- a/sdk/src/main/java/ly/count/android/sdk/TransparentActivityConfig.java +++ b/sdk/src/main/java/ly/count/android/sdk/TransparentActivityConfig.java @@ -1,8 +1,6 @@ package ly.count.android.sdk; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; class TransparentActivityConfig implements Serializable { Integer x; @@ -10,13 +8,11 @@ class TransparentActivityConfig implements Serializable { Integer width; Integer height; String url; - List listeners; TransparentActivityConfig(Integer x, Integer y, Integer width, Integer height) { this.x = x; this.y = y; this.width = width; this.height = height; - this.listeners = new ArrayList<>(); } } \ No newline at end of file