Skip to content

Commit 8bdc24b

Browse files
Merge pull request #518 from Countly/preflight_unne
refactor: remove unnecessary preflight check
2 parents c39557e + 036423a commit 8bdc24b

File tree

2 files changed

+52
-61
lines changed

2 files changed

+52
-61
lines changed

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

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,28 @@ void fetchContentsInternal(@NonNull String[] categories) {
8585
if (validateResponse(checkResponse)) {
8686
L.d("[ModuleContent] fetchContentsInternal, got new content data, showing it");
8787

88-
iRGenerator.CreatePreflightRequestMaker().doWork(checkResponse.optString("html"), null, cp, false, true, preflightResponse -> {
89-
if (preflightResponse == null) {
90-
L.d("[ModuleContent] fetchContentsInternal, preflight check failed, skipping showing content");
91-
return;
92-
}
93-
94-
Map<Integer, TransparentActivityConfig> placementCoordinates = parseContent(checkResponse, displayMetrics);
95-
if (placementCoordinates.isEmpty()) {
96-
L.d("[ModuleContent] fetchContentsInternal, placement coordinates are empty, skipping");
97-
return;
98-
}
99-
100-
L.d("[ModuleContent] fetchContentsInternal, preflight check succeeded");
101-
Intent intent = new Intent(_cly.context_, TransparentActivity.class);
102-
intent.putExtra(TransparentActivity.CONFIGURATION_LANDSCAPE, placementCoordinates.get(Configuration.ORIENTATION_LANDSCAPE));
103-
intent.putExtra(TransparentActivity.CONFIGURATION_PORTRAIT, placementCoordinates.get(Configuration.ORIENTATION_PORTRAIT));
104-
intent.putExtra(TransparentActivity.ORIENTATION, _cly.context_.getResources().getConfiguration().orientation);
105-
106-
Long id = System.currentTimeMillis();
107-
intent.putExtra(TransparentActivity.ID_CALLBACK, id);
108-
if (globalContentCallback != null) {
109-
TransparentActivity.contentCallbacks.put(id, globalContentCallback);
110-
}
111-
112-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
113-
_cly.context_.startActivity(intent);
114-
115-
shouldFetchContents = false; // disable fetching contents until the next time, this will disable the timer fetching
116-
isCurrentlyInContentZone = true;
117-
}, L);
88+
Map<Integer, TransparentActivityConfig> placementCoordinates = parseContent(checkResponse, displayMetrics);
89+
if (placementCoordinates.isEmpty()) {
90+
L.d("[ModuleContent] fetchContentsInternal, placement coordinates are empty, skipping");
91+
return;
92+
}
93+
94+
Intent intent = new Intent(_cly.context_, TransparentActivity.class);
95+
intent.putExtra(TransparentActivity.CONFIGURATION_LANDSCAPE, placementCoordinates.get(Configuration.ORIENTATION_LANDSCAPE));
96+
intent.putExtra(TransparentActivity.CONFIGURATION_PORTRAIT, placementCoordinates.get(Configuration.ORIENTATION_PORTRAIT));
97+
intent.putExtra(TransparentActivity.ORIENTATION, _cly.context_.getResources().getConfiguration().orientation);
98+
99+
Long id = System.currentTimeMillis();
100+
intent.putExtra(TransparentActivity.ID_CALLBACK, id);
101+
if (globalContentCallback != null) {
102+
TransparentActivity.contentCallbacks.put(id, globalContentCallback);
103+
}
104+
105+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
106+
_cly.context_.startActivity(intent);
107+
108+
shouldFetchContents = false; // disable fetching contents until the next time, this will disable the timer fetching
109+
isCurrentlyInContentZone = true;
118110
} else {
119111
L.w("[ModuleContent] fetchContentsInternal, response is not valid, skipping");
120112
}
@@ -204,13 +196,13 @@ private String prepareContentFetchRequest(@NonNull DisplayMetrics displayMetrics
204196
if (displayOption == WebViewDisplayOption.SAFE_AREA) {
205197
L.d("[ModuleContent] prepareContentFetchRequest, calculating safe area dimensions...");
206198
SafeAreaDimensions safeArea = SafeAreaCalculator.calculateSafeAreaDimensions(_cly.context_, L);
207-
199+
208200
// px to dp
209201
portraitWidth = (int) Math.ceil(safeArea.portraitWidth / displayMetrics.density);
210202
portraitHeight = (int) Math.ceil(safeArea.portraitHeight / displayMetrics.density);
211203
landscapeWidth = (int) Math.ceil(safeArea.landscapeWidth / displayMetrics.density);
212204
landscapeHeight = (int) Math.ceil(safeArea.landscapeHeight / displayMetrics.density);
213-
205+
214206
L.d("[ModuleContent] prepareContentFetchRequest, safe area dimensions (px->dp) - Portrait: [" + safeArea.portraitWidth + "x" + safeArea.portraitHeight + " px] -> [" + portraitWidth + "x" + portraitHeight + " dp], topOffset: [" + safeArea.portraitTopOffset + " px]");
215207
L.d("[ModuleContent] prepareContentFetchRequest, safe area dimensions (px->dp) - Landscape: [" + safeArea.landscapeWidth + "x" + safeArea.landscapeHeight + " px] -> [" + landscapeWidth + "x" + landscapeHeight + " dp], topOffset: [" + safeArea.landscapeTopOffset + " px]");
216208
} else {
@@ -221,7 +213,7 @@ private String prepareContentFetchRequest(@NonNull DisplayMetrics displayMetrics
221213
portraitHeight = portrait ? scaledHeight : scaledWidth;
222214
landscapeWidth = portrait ? scaledHeight : scaledWidth;
223215
landscapeHeight = portrait ? scaledWidth : scaledHeight;
224-
216+
225217
L.d("[ModuleContent] prepareContentFetchRequest, using immersive mode (full screen) dimensions (dp) - Portrait: [" + portraitWidth + "x" + portraitHeight + "], Landscape: [" + landscapeWidth + "x" + landscapeHeight + "]");
226218
}
227219

@@ -250,24 +242,24 @@ Map<Integer, TransparentActivityConfig> parseContent(@NonNull JSONObject respons
250242

251243
WebViewDisplayOption displayOption = _cly.config_.webViewDisplayOption;
252244
SafeAreaDimensions safeArea = null;
253-
245+
254246
if (displayOption == WebViewDisplayOption.SAFE_AREA) {
255247
L.d("[ModuleContent] parseContent, calculating safe area for coordinate adjustment...");
256248
safeArea = SafeAreaCalculator.calculateSafeAreaDimensions(_cly.context_, L);
257249
}
258250

259-
placementCoordinates.put(Configuration.ORIENTATION_PORTRAIT,
260-
extractOrientationPlacements(coordinates, displayMetrics.density, "p", content,
251+
placementCoordinates.put(Configuration.ORIENTATION_PORTRAIT,
252+
extractOrientationPlacements(coordinates, displayMetrics.density, "p", content,
261253
displayOption, safeArea != null ? safeArea.portraitTopOffset : 0, safeArea != null ? safeArea.portraitLeftOffset : 0));
262-
placementCoordinates.put(Configuration.ORIENTATION_LANDSCAPE,
263-
extractOrientationPlacements(coordinates, displayMetrics.density, "l", content,
254+
placementCoordinates.put(Configuration.ORIENTATION_LANDSCAPE,
255+
extractOrientationPlacements(coordinates, displayMetrics.density, "l", content,
264256
displayOption, safeArea != null ? safeArea.landscapeTopOffset : 0, safeArea != null ? safeArea.landscapeLeftOffset : 0));
265257

266258
return placementCoordinates;
267259
}
268260

269-
private TransparentActivityConfig extractOrientationPlacements(@NonNull JSONObject placements,
270-
float density, @NonNull String orientation, @NonNull String content,
261+
private TransparentActivityConfig extractOrientationPlacements(@NonNull JSONObject placements,
262+
float density, @NonNull String orientation, @NonNull String content,
271263
WebViewDisplayOption displayOption, int topOffset, int leftOffset) {
272264
if (placements.has(orientation)) {
273265
JSONObject orientationPlacements = placements.optJSONObject(orientation);
@@ -277,21 +269,21 @@ private TransparentActivityConfig extractOrientationPlacements(@NonNull JSONObje
277269
int w = orientationPlacements.optInt("w");
278270
int h = orientationPlacements.optInt("h");
279271
L.d("[ModuleContent] extractOrientationPlacements, orientation: [" + orientation + "], x: [" + x + "], y: [" + y + "], w: [" + w + "], h: [" + h + "]");
280-
272+
281273
int xPx = Math.round(x * density);
282274
int yPx = Math.round(y * density);
283275
int wPx = Math.round(w * density);
284276
int hPx = Math.round(h * density);
285277
L.d("[ModuleContent] extractOrientationPlacements, orientation: [" + orientation + "], converting dp->px: [" + w + "x" + h + " dp] -> [" + wPx + "x" + hPx + " px], density: [" + density + "]");
286-
278+
287279
TransparentActivityConfig config = new TransparentActivityConfig(xPx, yPx, wPx, hPx);
288280
config.url = content;
289281
config.useSafeArea = (displayOption == WebViewDisplayOption.SAFE_AREA);
290282
config.topOffset = topOffset;
291283
config.leftOffset = leftOffset;
292-
284+
293285
L.d("[ModuleContent] extractOrientationPlacements, orientation: [" + orientation + "], created config - useSafeArea: [" + config.useSafeArea + "], topOffset: [" + config.topOffset + "], leftOffset: [" + config.leftOffset + "]");
294-
286+
295287
return config;
296288
}
297289

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,18 @@ void presentFeedbackWidgetInternal(@Nullable final CountlyFeedbackWidget widgetI
270270
String preparedWidgetUrl = widgetListUrl.toString();
271271
L.d("[ModuleFeedback] Using following url for widget:[" + preparedWidgetUrl + "]");
272272

273-
iRGenerator.CreatePreflightRequestMaker().doWork(preparedWidgetUrl, null, requestQueueProvider.createConnectionProcessor(), false, true, preflightResponse -> {
274-
if (preflightResponse == null) {
275-
L.e("[ModuleFeedback] Failed to do preflight check for the widget url");
276-
if (devCallback != null) {
277-
devCallback.onFinished("Failed to do preflight check for the widget url");
273+
if (!Utils.isNullOrEmpty(widgetInfo.widgetVersion)) {
274+
L.d("[ModuleFeedback] Will use transparent activity for displaying the widget");
275+
showFeedbackWidget_newActivity(context, preparedWidgetUrl, widgetInfo, devCallback);
276+
} else {
277+
iRGenerator.CreatePreflightRequestMaker().doWork(preparedWidgetUrl, null, requestQueueProvider.createConnectionProcessor(), false, true, preflightResponse -> {
278+
if (preflightResponse == null) {
279+
L.e("[ModuleFeedback] Failed to do preflight check for the widget url");
280+
if (devCallback != null) {
281+
devCallback.onFinished("Failed to do preflight check for the widget url");
282+
}
283+
return;
278284
}
279-
return;
280-
}
281-
282-
if (!Utils.isNullOrEmpty(widgetInfo.widgetVersion)) {
283-
L.d("[ModuleFeedback] Will use transparent activity for displaying the widget");
284-
showFeedbackWidget_newActivity(context, preparedWidgetUrl, widgetInfo, devCallback);
285-
} else {
286285
L.d("[ModuleFeedback] Will use dialog for displaying the widget");
287286
//enable for chrome debugging
288287
// WebView.setWebContentsDebuggingEnabled(true);
@@ -305,8 +304,8 @@ public void run() {
305304
}
306305
}
307306
});
308-
}
309-
}, L);
307+
}, L);
308+
}
310309
}
311310

312311
private void showFeedbackWidget(Context context, CountlyFeedbackWidget widgetInfo, String closeButtonText, FeedbackCallback devCallback, String url) {
@@ -360,7 +359,7 @@ private void showFeedbackWidget_newActivity(@NonNull Context context, String url
360359
if (displayOption == WebViewDisplayOption.SAFE_AREA) {
361360
L.d("[ModuleFeedback] showFeedbackWidget_newActivity, calculating safe area dimensions...");
362361
SafeAreaDimensions safeArea = SafeAreaCalculator.calculateSafeAreaDimensions(context, L);
363-
362+
364363
portraitWidth = safeArea.portraitWidth;
365364
portraitHeight = safeArea.portraitHeight;
366365
landscapeWidth = safeArea.landscapeWidth;
@@ -369,7 +368,7 @@ private void showFeedbackWidget_newActivity(@NonNull Context context, String url
369368
landscapeTopOffset = safeArea.landscapeTopOffset;
370369
portraitLeftOffset = safeArea.portraitLeftOffset;
371370
landscapeLeftOffset = safeArea.landscapeLeftOffset;
372-
371+
373372
L.d("[ModuleFeedback] showFeedbackWidget_newActivity, safe area dimensions (px) - Portrait: [" + portraitWidth + "x" + portraitHeight + "], topOffset: [" + portraitTopOffset + "], leftOffset: [" + portraitLeftOffset + "]");
374373
L.d("[ModuleFeedback] showFeedbackWidget_newActivity, safe area dimensions (px) - Landscape: [" + landscapeWidth + "x" + landscapeHeight + "], topOffset: [" + landscapeTopOffset + "], leftOffset: [" + landscapeLeftOffset + "]");
375374
} else {
@@ -380,7 +379,7 @@ private void showFeedbackWidget_newActivity(@NonNull Context context, String url
380379
portraitHeight = portrait ? height : width;
381380
landscapeWidth = portrait ? height : width;
382381
landscapeHeight = portrait ? width : height;
383-
382+
384383
L.d("[ModuleFeedback] showFeedbackWidget_newActivity, using immersive mode (full screen) dimensions (px) - Portrait: [" + portraitWidth + "x" + portraitHeight + "], Landscape: [" + landscapeWidth + "x" + landscapeHeight + "]");
385384
}
386385

0 commit comments

Comments
 (0)