Skip to content

Commit 708002a

Browse files
authored
Merge pull request #429 from Iterable/jay/MOB-4025-post-PR-refactor
[MOB-4025] in-app top layout fix refactor
2 parents 917f263 + e590e82 commit 708002a

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableInAppFragmentHTMLNotification.java

+35-35
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@
3535
import androidx.fragment.app.DialogFragment;
3636

3737
public class IterableInAppFragmentHTMLNotification extends DialogFragment implements IterableWebView.HTMLNotificationCallbacks {
38-
3938
private static final String BACK_BUTTON = "itbl://backButton";
4039
private static final String TAG = "IterableInAppFragmentHTMLNotification";
4140
private static final String HTML_STRING = "HTML";
4241
private static final String BACKGROUND_ALPHA = "BackgroundAlpha";
4342
private static final String INSET_PADDING = "InsetPadding";
4443
private static final String CALLBACK_ON_CANCEL = "CallbackOnCancel";
4544
private static final String MESSAGE_ID = "MessageId";
46-
private static final String INAPP_OPEN_TRACKED = "InAppOpenTracked";
47-
private static final String INAPP_BGALPHA = "InAppBgAlpha";
48-
private static final String INAPP_BGCOLOR = "InAppBgColor";
49-
private static final String INAPP_SHOULD_ANIMATE = "ShouldAnimate";
45+
private static final String IN_APP_OPEN_TRACKED = "InAppOpenTracked";
46+
private static final String IN_APP_BG_ALPHA = "InAppBgAlpha";
47+
private static final String IN_APP_BG_COLOR = "InAppBgColor";
48+
private static final String IN_APP_SHOULD_ANIMATE = "ShouldAnimate";
5049

5150
private static final int DELAY_THRESHOLD_MS = 500;
5251

@@ -61,19 +60,12 @@ public class IterableInAppFragmentHTMLNotification extends DialogFragment implem
6160
private String htmlString;
6261
private String messageId;
6362

64-
//TODO: To delete this variable in future iterations
65-
private double backgroundAlpha;
63+
private double backgroundAlpha; //TODO: remove in a future version
6664
private Rect insetPadding;
6765
private boolean shouldAnimate;
6866
private double inAppBackgroundAlpha;
6967
private String inAppBackgroundColor;
7068

71-
/**
72-
* Creates a static instance of the notification
73-
*
74-
* @param htmlString
75-
* @return notification instance
76-
*/
7769
public static IterableInAppFragmentHTMLNotification createInstance(@NonNull String htmlString, boolean callbackOnCancel, @NonNull IterableHelper.IterableUrlCallback clickCallback, @NonNull IterableInAppLocation location, @NonNull String messageId, @NonNull Double backgroundAlpha, @NonNull Rect padding) {
7870
return IterableInAppFragmentHTMLNotification.createInstance(htmlString, callbackOnCancel, clickCallback, location, messageId, backgroundAlpha, padding, false, new IterableInAppMessage.InAppBgColor(null, 0.0f));
7971
}
@@ -86,9 +78,9 @@ public static IterableInAppFragmentHTMLNotification createInstance(@NonNull Stri
8678
args.putString(MESSAGE_ID, messageId);
8779
args.putDouble(BACKGROUND_ALPHA, backgroundAlpha);
8880
args.putParcelable(INSET_PADDING, padding);
89-
args.putString(INAPP_BGCOLOR, inAppBgColor.bgHexColor);
90-
args.putDouble(INAPP_BGALPHA, inAppBgColor.bgAlpha);
91-
args.putBoolean(INAPP_SHOULD_ANIMATE, shouldAnimate);
81+
args.putString(IN_APP_BG_COLOR, inAppBgColor.bgHexColor);
82+
args.putDouble(IN_APP_BG_ALPHA, inAppBgColor.bgAlpha);
83+
args.putBoolean(IN_APP_SHOULD_ANIMATE, shouldAnimate);
9284

9385
IterableInAppFragmentHTMLNotification.clickCallback = clickCallback;
9486
IterableInAppFragmentHTMLNotification.location = location;
@@ -119,16 +111,18 @@ public IterableInAppFragmentHTMLNotification() {
119111
public void onCreate(@Nullable Bundle savedInstanceState) {
120112
super.onCreate(savedInstanceState);
121113
Bundle args = getArguments();
114+
122115
if (args != null) {
123116
htmlString = args.getString(HTML_STRING, null);
124117
callbackOnCancel = args.getBoolean(CALLBACK_ON_CANCEL, false);
125118
messageId = args.getString(MESSAGE_ID);
126119
backgroundAlpha = args.getDouble(BACKGROUND_ALPHA);
127120
insetPadding = args.getParcelable(INSET_PADDING);
128-
inAppBackgroundAlpha = args.getDouble(INAPP_BGALPHA);
129-
inAppBackgroundColor = args.getString(INAPP_BGCOLOR, null);
130-
shouldAnimate = args.getBoolean(INAPP_SHOULD_ANIMATE);
121+
inAppBackgroundAlpha = args.getDouble(IN_APP_BG_ALPHA);
122+
inAppBackgroundColor = args.getString(IN_APP_BG_COLOR, null);
123+
shouldAnimate = args.getBoolean(IN_APP_SHOULD_ANIMATE);
131124
}
125+
132126
notification = this;
133127
}
134128

@@ -154,7 +148,7 @@ public void onCancel(DialogInterface dialog) {
154148
if (getInAppLayout(insetPadding) == InAppLayout.FULLSCREEN) {
155149
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
156150
} else if (getInAppLayout(insetPadding) != InAppLayout.TOP) {
157-
// For TOP layout in-app, status bar will be opaque so that the inapp content does not overlap with translucent status bar.
151+
// For TOP layout in-app, status bar will be opaque so that the in-app content does not overlap with translucent status bar.
158152
// For other non-fullscreen in-apps layouts (BOTTOM and CENTER), status bar will be translucent
159153
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
160154
}
@@ -169,6 +163,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
169163
if (getInAppLayout(insetPadding) == InAppLayout.FULLSCREEN) {
170164
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
171165
}
166+
172167
webView = new IterableWebView(getContext());
173168
webView.setId(R.id.webView);
174169
webView.createWithHtml(this, htmlString);
@@ -183,8 +178,8 @@ public boolean onPreDraw() {
183178

184179
if (orientationListener == null) {
185180
orientationListener = new OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {
181+
// Resize the webView on device rotation
186182
public void onOrientationChanged(int orientation) {
187-
// Resize the webview on device rotation
188183
if (loaded) {
189184
final Handler handler = new Handler();
190185
handler.postDelayed(new Runnable() {
@@ -197,36 +192,33 @@ public void run() {
197192
}
198193
};
199194
}
195+
200196
orientationListener.enable();
201197

202198
RelativeLayout relativeLayout = new RelativeLayout(this.getContext());
203199
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
204200
relativeLayout.setVerticalGravity(getVerticalLocation(insetPadding));
205201
relativeLayout.addView(webView, layoutParams);
206202

207-
if (savedInstanceState == null || !savedInstanceState.getBoolean(INAPP_OPEN_TRACKED, false)) {
203+
if (savedInstanceState == null || !savedInstanceState.getBoolean(IN_APP_OPEN_TRACKED, false)) {
208204
IterableApi.sharedInstance.trackInAppOpen(messageId, location);
209205
}
206+
210207
prepareToShowWebView();
211208
return relativeLayout;
212209
}
213210

214-
/**
215-
* Sets the loaded flag
216-
*
217-
* @param loaded
218-
*/
219211
public void setLoaded(boolean loaded) {
220212
this.loaded = loaded;
221213
}
222214

223215
/**
224-
* Sets up the webview and the dialog layout
216+
* Sets up the webView and the dialog layout
225217
*/
226218
@Override
227219
public void onSaveInstanceState(@NonNull Bundle outState) {
228220
super.onSaveInstanceState(outState);
229-
outState.putBoolean(INAPP_OPEN_TRACKED, true);
221+
outState.putBoolean(IN_APP_OPEN_TRACKED, true);
230222
}
231223

232224
/**
@@ -235,6 +227,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
235227
@Override
236228
public void onStop() {
237229
orientationListener.disable();
230+
238231
super.onStop();
239232
}
240233

@@ -255,9 +248,11 @@ public void onDestroy() {
255248
public void onUrlClicked(String url) {
256249
IterableApi.sharedInstance.trackInAppClick(messageId, url, location);
257250
IterableApi.sharedInstance.trackInAppClose(messageId, url, IterableInAppCloseAction.LINK, location);
251+
258252
if (clickCallback != null) {
259253
clickCallback.execute(Uri.parse(url));
260254
}
255+
261256
processMessageRemoval();
262257
hideWebView();
263258
}
@@ -268,6 +263,7 @@ public void onUrlClicked(String url) {
268263
public void onBackPressed() {
269264
IterableApi.sharedInstance.trackInAppClick(messageId, BACK_BUTTON);
270265
IterableApi.sharedInstance.trackInAppClose(messageId, BACK_BUTTON, IterableInAppCloseAction.BACK, location);
266+
271267
processMessageRemoval();
272268
}
273269

@@ -316,26 +312,28 @@ private void animateBackground(Drawable from, Drawable to) {
316312
}
317313

318314
private ColorDrawable getInAppBackgroundDrawable() {
319-
320315
if (inAppBackgroundColor == null) {
321316
IterableLogger.d(TAG, "Background Color does not exist. In App background animation will not be performed");
322317
return null;
323318
}
324319

325320
int backgroundColorWithAlpha;
321+
326322
try {
327323
backgroundColorWithAlpha = ColorUtils.setAlphaComponent(Color.parseColor(inAppBackgroundColor), (int) (inAppBackgroundAlpha * 255));
328324
} catch (IllegalArgumentException e) {
329325
IterableLogger.e(TAG, "Background color could not be identified for input string \"" + inAppBackgroundColor + "\". Failed to load in-app background.");
330326
return null;
331327
}
328+
332329
ColorDrawable backgroundColorDrawable = new ColorDrawable(backgroundColorWithAlpha);
333330
return backgroundColorDrawable;
334331
}
335332

336333
private void showAndAnimateWebView() {
337334
webView.setAlpha(1.0f);
338335
webView.setVisibility(View.VISIBLE);
336+
339337
if (shouldAnimate) {
340338
int animationResource;
341339
InAppLayout inAppLayout = getInAppLayout(insetPadding);
@@ -353,6 +351,7 @@ private void showAndAnimateWebView() {
353351
default:
354352
animationResource = R.anim.fade_in_custom;
355353
}
354+
356355
Animation anim = AnimationUtils.loadAnimation(getContext(), animationResource);
357356
anim.setDuration(IterableConstants.ITERABLE_IN_APP_ANIMATION_DURATION);
358357
webView.startAnimation(anim);
@@ -378,14 +377,15 @@ private void hideWebView() {
378377
default:
379378
animationResource = R.anim.fade_out_custom;
380379
}
380+
381381
Animation anim = AnimationUtils.loadAnimation(getContext(),
382382
animationResource);
383383
anim.setDuration(IterableConstants.ITERABLE_IN_APP_ANIMATION_DURATION);
384384
webView.startAnimation(anim);
385385
}
386386

387387
hideInAppBackground();
388-
Runnable dismissWebviewRunnable = new Runnable() {
388+
Runnable dismissWebViewRunnable = new Runnable() {
389389
@Override
390390
public void run() {
391391
if (getContext() != null && getDialog() != null && getDialog().getWindow() != null) {
@@ -395,11 +395,10 @@ public void run() {
395395
};
396396

397397
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
398-
webView.postOnAnimationDelayed(dismissWebviewRunnable, 400);
398+
webView.postOnAnimationDelayed(dismissWebViewRunnable, 400);
399399
} else {
400-
webView.postDelayed(dismissWebviewRunnable, 400);
400+
webView.postDelayed(dismissWebViewRunnable, 400);
401401
}
402-
403402
}
404403

405404
private void processMessageRemoval() {
@@ -420,7 +419,7 @@ public void runResizeScript() {
420419
}
421420

422421
/**
423-
* Resizes the dialog window based upon the size of its webview html content
422+
* Resizes the dialog window based upon the size of its webView HTML content
424423
* @param height
425424
*/
426425
public void resize(final float height) {
@@ -454,6 +453,7 @@ public void run() {
454453
} else {
455454
display.getSize(size);
456455
}
456+
457457
int webViewWidth = size.x;
458458
int webViewHeight = size.y;
459459

0 commit comments

Comments
 (0)