Skip to content

Commit 764f529

Browse files
Merge pull request #517 from Countly/activity_err_hand_load_after
Activity err hand load after
2 parents 8bdc24b + 3959352 commit 764f529

File tree

4 files changed

+97
-36
lines changed

4 files changed

+97
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 25.4.6
2+
* Improved content error handling and display mechanics.
23
* Updated user properties caching mechanism according to sessions.
34

45
## 25.4.5

sdk/src/main/java/ly/count/android/sdk/CountlyWebViewClient.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package ly.count.android.sdk;
22

3+
import android.os.Build;
34
import android.util.Log;
5+
import android.webkit.WebResourceError;
46
import android.webkit.WebResourceRequest;
7+
import android.webkit.WebResourceResponse;
58
import android.webkit.WebView;
69
import android.webkit.WebViewClient;
710
import java.net.URLDecoder;
@@ -44,7 +47,6 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
4447
@Override
4548
public void onPageFinished(WebView view, String url) {
4649
Log.v(Countly.TAG, "[CountlyWebViewClient] onPageFinished, url: [" + url + "]");
47-
super.onPageFinished(view, url);
4850
if (afterPageFinished != null) {
4951
pageLoadTime = System.currentTimeMillis() - pageLoadTime;
5052
boolean timeOut = (pageLoadTime / 1000L) >= 60;
@@ -55,6 +57,31 @@ public void onPageFinished(WebView view, String url) {
5557
}
5658
}
5759

60+
@Override
61+
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
62+
if (request.isForMainFrame() && afterPageFinished != null) {
63+
String errorString;
64+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
65+
errorString = error.getDescription() + " (code: " + error.getErrorCode() + ")";
66+
} else {
67+
errorString = error.toString();
68+
}
69+
Log.v(Countly.TAG, "[CountlyWebViewClient] onReceivedError, error: [" + errorString + "]");
70+
71+
afterPageFinished.onPageLoaded(true);
72+
afterPageFinished = null;
73+
}
74+
}
75+
76+
@Override
77+
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
78+
if (request.isForMainFrame() && afterPageFinished != null) {
79+
Log.v(Countly.TAG, "[CountlyWebViewClient] onReceivedHttpError, errorResponseCode: [" + errorResponse.getStatusCode() + "]");
80+
afterPageFinished.onPageLoaded(true);
81+
afterPageFinished = null;
82+
}
83+
}
84+
5885
public void registerWebViewUrlListener(WebViewUrlListener listener) {
5986
this.listeners.add(listener);
6087
}

sdk/src/main/java/ly/count/android/sdk/TransparentActivity.java

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,21 @@ protected void onCreate(Bundle savedInstanceState) {
5555
configLandscape = (TransparentActivityConfig) intent.getSerializableExtra(CONFIGURATION_LANDSCAPE);
5656
configPortrait = (TransparentActivityConfig) intent.getSerializableExtra(CONFIGURATION_PORTRAIT);
5757
Log.v(Countly.TAG, "[TransparentActivity] onCreate, orientation: " + currentOrientation);
58-
Log.v(Countly.TAG, "[TransparentActivity] onCreate, configLandscape x: [" + configLandscape.x + "] y: [" + configLandscape.y + "] width: [" + configLandscape.width + "] height: [" + configLandscape.height + "], topOffset: [" + configLandscape.topOffset + "], leftOffset: [" + configLandscape.leftOffset + "]");
59-
Log.v(Countly.TAG, "[TransparentActivity] onCreate, configPortrait x: [" + configPortrait.x + "] y: [" + configPortrait.y + "] width: [" + configPortrait.width + "] height: [" + configPortrait.height + "], topOffset: [" + configPortrait.topOffset + "], leftOffset: [" + configPortrait.leftOffset + "]");
58+
Log.v(Countly.TAG, "[TransparentActivity] onCreate, configLandscape x: ["
59+
+ configLandscape.x
60+
+ "] y: ["
61+
+ configLandscape.y
62+
+ "] width: ["
63+
+ configLandscape.width
64+
+ "] height: ["
65+
+ configLandscape.height
66+
+ "], topOffset: ["
67+
+ configLandscape.topOffset
68+
+ "], leftOffset: ["
69+
+ configLandscape.leftOffset
70+
+ "]");
71+
Log.v(Countly.TAG,
72+
"[TransparentActivity] onCreate, configPortrait x: [" + configPortrait.x + "] y: [" + configPortrait.y + "] width: [" + configPortrait.width + "] height: [" + configPortrait.height + "], topOffset: [" + configPortrait.topOffset + "], leftOffset: [" + configPortrait.leftOffset + "]");
6073

6174
TransparentActivityConfig config;
6275
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
@@ -70,10 +83,10 @@ protected void onCreate(Bundle savedInstanceState) {
7083
// Configure window layout parameters
7184
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
7285
params.gravity = Gravity.TOP | Gravity.START; // try out START
73-
86+
7487
int adjustedX = config.x;
7588
int adjustedY = config.y;
76-
89+
7790
if (config.useSafeArea) {
7891
if (config.leftOffset > 0) {
7992
adjustedX += config.leftOffset;
@@ -84,19 +97,19 @@ protected void onCreate(Bundle savedInstanceState) {
8497
Log.d(Countly.TAG, "[TransparentActivity] onCreate, using safe area mode, adjusting y from [" + config.y + "] to [" + adjustedY + "] (topOffset: " + config.topOffset + ")");
8598
}
8699
}
87-
100+
88101
params.x = adjustedX;
89102
params.y = adjustedY;
90-
103+
91104
params.height = config.height;
92105
params.width = config.width;
93-
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
94-
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
106+
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
107+
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
95108
getWindow().setAttributes(params);
96-
109+
97110
WindowManager.LayoutParams verifyParams = getWindow().getAttributes();
98111
Log.d(Countly.TAG, "[TransparentActivity] onCreate, AFTER setAttributes - params.x: [" + verifyParams.x + "], params.y: [" + verifyParams.y + "], params.gravity: [" + verifyParams.gravity + "], width: [" + verifyParams.width + "], height: [" + verifyParams.height + "]");
99-
112+
100113
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
101114

102115
// Create and configure the layout
@@ -119,32 +132,33 @@ private TransparentActivityConfig setupConfig(@Nullable TransparentActivityConfi
119132

120133
if (!config.useSafeArea) {
121134
final DisplayMetrics metrics = UtilsDevice.getDisplayMetrics(this);
122-
135+
123136
if (config.width < 1) {
124137
config.width = metrics.widthPixels;
125138
}
126139
if (config.height < 1) {
127140
config.height = metrics.heightPixels;
128141
}
129142
}
130-
143+
131144
if (config.x < 1) {
132145
config.x = 0;
133146
}
134147
if (config.y < 1) {
135148
config.y = 0;
136149
}
137-
150+
138151
Log.d(Countly.TAG, "[TransparentActivity] setupConfig, final config - x: [" + config.x + "], y: [" + config.y + "], width: [" + config.width + "], height: [" + config.height + "], useSafeArea: [" + config.useSafeArea + "]");
139152
return config;
140153
}
141154

142155
private void resizeContent(TransparentActivityConfig config) {
143-
Log.d(Countly.TAG, "[TransparentActivity] resizeContent(config), config dimensions (px): [" + config.width + "x" + config.height + "], x: [" + config.x + "], y: [" + config.y + "], useSafeArea: [" + config.useSafeArea + "], topOffset: [" + config.topOffset + "], leftOffset: [" + config.leftOffset + "]");
144-
156+
Log.d(Countly.TAG,
157+
"[TransparentActivity] resizeContent(config), config dimensions (px): [" + config.width + "x" + config.height + "], x: [" + config.x + "], y: [" + config.y + "], useSafeArea: [" + config.useSafeArea + "], topOffset: [" + config.topOffset + "], leftOffset: [" + config.leftOffset + "]");
158+
145159
int adjustedX = config.x;
146160
int adjustedY = config.y;
147-
161+
148162
if (config.useSafeArea) {
149163
if (config.leftOffset > 0) {
150164
adjustedX += config.leftOffset;
@@ -155,7 +169,7 @@ private void resizeContent(TransparentActivityConfig config) {
155169
Log.d(Countly.TAG, "[TransparentActivity] resizeContent(config), applying top offset, adjusted y: [" + adjustedY + "]");
156170
}
157171
}
158-
172+
159173
WindowManager.LayoutParams params = getWindow().getAttributes();
160174
Log.d(Countly.TAG, "[TransparentActivity] resizeContent(config), BEFORE - params.x: [" + params.x + "], params.y: [" + params.y + "], params.gravity: [" + params.gravity + "]");
161175
params.gravity = Gravity.TOP | Gravity.START; // safe?
@@ -164,7 +178,7 @@ private void resizeContent(TransparentActivityConfig config) {
164178
params.height = config.height;
165179
params.width = config.width;
166180
getWindow().setAttributes(params);
167-
181+
168182
WindowManager.LayoutParams verifyParams = getWindow().getAttributes();
169183
Log.d(Countly.TAG, "[TransparentActivity] resizeContent(config), AFTER - params.x: [" + verifyParams.x + "], params.y: [" + verifyParams.y + "], params.gravity: [" + verifyParams.gravity + "], width: [" + verifyParams.width + "], height: [" + verifyParams.height + "]");
170184

@@ -177,7 +191,7 @@ private void resizeContent(TransparentActivityConfig config) {
177191
webLayoutParams.width = config.width;
178192
webLayoutParams.height = config.height;
179193
webView.setLayoutParams(webLayoutParams);
180-
194+
181195
Log.d(Countly.TAG, "[TransparentActivity] resizeContent(config), layout params set - relativeLayout: [" + layoutParams.width + "x" + layoutParams.height + "], webView: [" + webLayoutParams.width + "x" + webLayoutParams.height + "]");
182196
}
183197

@@ -195,23 +209,23 @@ public void onConfigurationChanged(android.content.res.Configuration newConfig)
195209

196210
private void resizeContent() {
197211
TransparentActivityConfig currentConfig = (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) ? configLandscape : configPortrait;
198-
212+
199213
float density = getResources().getDisplayMetrics().density;
200214
int widthPx, heightPx;
201-
215+
202216
if (currentConfig != null && currentConfig.useSafeArea) {
203217
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, recalculating safe area dimensions for orientation change");
204-
218+
205219
SafeAreaDimensions safeArea = SafeAreaCalculator.calculateSafeAreaDimensions(this, Countly.sharedInstance().L);
206-
220+
207221
configPortrait.topOffset = safeArea.portraitTopOffset;
208222
configPortrait.leftOffset = safeArea.portraitLeftOffset;
209223
configLandscape.topOffset = safeArea.landscapeTopOffset;
210224
configLandscape.leftOffset = safeArea.landscapeLeftOffset;
211-
225+
212226
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, updated offsets - Portrait: topOffset=[" + configPortrait.topOffset + "], leftOffset=[" + configPortrait.leftOffset + "]");
213227
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, updated offsets - Landscape: topOffset=[" + configLandscape.topOffset + "], leftOffset=[" + configLandscape.leftOffset + "]");
214-
228+
215229
int topOffset, leftOffset;
216230
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
217231
widthPx = safeArea.landscapeWidth;
@@ -224,16 +238,30 @@ private void resizeContent() {
224238
topOffset = safeArea.portraitTopOffset;
225239
leftOffset = safeArea.portraitLeftOffset;
226240
}
227-
228-
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, safe area mode - sending dimensions to webview (px): [" + widthPx + "x" + heightPx + "], (dp): [" + Math.round(widthPx / density) + "x" + Math.round(heightPx / density) + "], density: [" + density + "], topOffset: [" + topOffset + "], leftOffset: [" + leftOffset + "]");
241+
242+
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, safe area mode - sending dimensions to webview (px): ["
243+
+ widthPx
244+
+ "x"
245+
+ heightPx
246+
+ "], (dp): ["
247+
+ Math.round(widthPx / density)
248+
+ "x"
249+
+ Math.round(heightPx / density)
250+
+ "], density: ["
251+
+ density
252+
+ "], topOffset: ["
253+
+ topOffset
254+
+ "], leftOffset: ["
255+
+ leftOffset
256+
+ "]");
229257
} else {
230258
final DisplayMetrics metrics = UtilsDevice.getDisplayMetrics(this);
231259
widthPx = metrics.widthPixels;
232260
heightPx = metrics.heightPixels;
233-
261+
234262
Log.d(Countly.TAG, "[TransparentActivity] resizeContent, immersive mode - sending dimensions to webview (px): [" + widthPx + "x" + heightPx + "], (dp): [" + Math.round(widthPx / density) + "x" + Math.round(heightPx / density) + "], density: [" + density + "]");
235263
}
236-
264+
237265
int scaledWidth = Math.round(widthPx / density);
238266
int scaledHeight = Math.round(heightPx / density);
239267
webView.loadUrl("javascript:window.postMessage({type: 'resize', width: " + scaledWidth + ", height: " + scaledHeight + "}, '*');");
@@ -379,14 +407,14 @@ private void resizeMeAction(Map<String, Object> query) {
379407
Log.v(Countly.TAG, "[TransparentActivity] resizeMeAction, resize_me JSON: [" + resizeMeJson + "]");
380408
JSONObject portrait = resizeMeJson.getJSONObject("p");
381409
JSONObject landscape = resizeMeJson.getJSONObject("l");
382-
410+
383411
boolean portraitUseSafeArea = configPortrait.useSafeArea;
384412
boolean landscapeUseSafeArea = configLandscape.useSafeArea;
385413
int portraitTopOffset = configPortrait.topOffset;
386414
int landscapeTopOffset = configLandscape.topOffset;
387415
int portraitLeftOffset = configPortrait.leftOffset;
388416
int landscapeLeftOffset = configLandscape.leftOffset;
389-
417+
390418
configPortrait.x = (int) Math.ceil(portrait.getInt("x") * density);
391419
configPortrait.y = (int) Math.ceil(portrait.getInt("y") * density);
392420
configPortrait.width = (int) Math.ceil(portrait.getInt("w") * density);
@@ -402,7 +430,7 @@ private void resizeMeAction(Map<String, Object> query) {
402430
configLandscape.useSafeArea = landscapeUseSafeArea;
403431
configLandscape.topOffset = landscapeTopOffset;
404432
configLandscape.leftOffset = landscapeLeftOffset;
405-
433+
406434
Log.d(Countly.TAG, "[TransparentActivity] resizeMeAction, updated configs - Portrait: useSafeArea=[" + portraitUseSafeArea + "], topOffset=[" + portraitTopOffset + "], leftOffset=[" + portraitLeftOffset + "]");
407435
Log.d(Countly.TAG, "[TransparentActivity] resizeMeAction, updated configs - Landscape: useSafeArea=[" + landscapeUseSafeArea + "], topOffset=[" + landscapeTopOffset + "], leftOffset=[" + landscapeLeftOffset + "]");
408436

@@ -526,14 +554,19 @@ private WebView createWebView(TransparentActivityConfig config) {
526554
}
527555
});
528556
client.afterPageFinished = new WebViewPageLoadedListener() {
529-
@Override public void onPageLoaded(boolean timedOut) {
530-
if (timedOut) {
557+
@Override public void onPageLoaded(boolean failed) {
558+
if (failed) {
531559
close(new HashMap<>());
532560

533561
if (Countly.sharedInstance().isInitialized()) {
534562
Countly.sharedInstance().moduleContent.notifyAfterContentIsClosed();
535563
}
536564
} else {
565+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
566+
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
567+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
568+
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
569+
537570
TransparentActivityConfig currentConfig = currentOrientation == Configuration.ORIENTATION_LANDSCAPE ? configLandscape : configPortrait;
538571
if (currentConfig != null && !currentConfig.useSafeArea) {
539572
hideSystemUI();

sdk/src/main/java/ly/count/android/sdk/WebViewPageLoadedListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public interface WebViewPageLoadedListener {
44

5-
void onPageLoaded(boolean timedOut);
5+
void onPageLoaded(boolean failed);
66
}

0 commit comments

Comments
 (0)