35
35
import androidx .fragment .app .DialogFragment ;
36
36
37
37
public class IterableInAppFragmentHTMLNotification extends DialogFragment implements IterableWebView .HTMLNotificationCallbacks {
38
-
39
38
private static final String BACK_BUTTON = "itbl://backButton" ;
40
39
private static final String TAG = "IterableInAppFragmentHTMLNotification" ;
41
40
private static final String HTML_STRING = "HTML" ;
42
41
private static final String BACKGROUND_ALPHA = "BackgroundAlpha" ;
43
42
private static final String INSET_PADDING = "InsetPadding" ;
44
43
private static final String CALLBACK_ON_CANCEL = "CallbackOnCancel" ;
45
44
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" ;
50
49
51
50
private static final int DELAY_THRESHOLD_MS = 500 ;
52
51
@@ -61,19 +60,12 @@ public class IterableInAppFragmentHTMLNotification extends DialogFragment implem
61
60
private String htmlString ;
62
61
private String messageId ;
63
62
64
- //TODO: To delete this variable in future iterations
65
- private double backgroundAlpha ;
63
+ private double backgroundAlpha ; //TODO: remove in a future version
66
64
private Rect insetPadding ;
67
65
private boolean shouldAnimate ;
68
66
private double inAppBackgroundAlpha ;
69
67
private String inAppBackgroundColor ;
70
68
71
- /**
72
- * Creates a static instance of the notification
73
- *
74
- * @param htmlString
75
- * @return notification instance
76
- */
77
69
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 ) {
78
70
return IterableInAppFragmentHTMLNotification .createInstance (htmlString , callbackOnCancel , clickCallback , location , messageId , backgroundAlpha , padding , false , new IterableInAppMessage .InAppBgColor (null , 0.0f ));
79
71
}
@@ -86,9 +78,9 @@ public static IterableInAppFragmentHTMLNotification createInstance(@NonNull Stri
86
78
args .putString (MESSAGE_ID , messageId );
87
79
args .putDouble (BACKGROUND_ALPHA , backgroundAlpha );
88
80
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 );
92
84
93
85
IterableInAppFragmentHTMLNotification .clickCallback = clickCallback ;
94
86
IterableInAppFragmentHTMLNotification .location = location ;
@@ -119,16 +111,18 @@ public IterableInAppFragmentHTMLNotification() {
119
111
public void onCreate (@ Nullable Bundle savedInstanceState ) {
120
112
super .onCreate (savedInstanceState );
121
113
Bundle args = getArguments ();
114
+
122
115
if (args != null ) {
123
116
htmlString = args .getString (HTML_STRING , null );
124
117
callbackOnCancel = args .getBoolean (CALLBACK_ON_CANCEL , false );
125
118
messageId = args .getString (MESSAGE_ID );
126
119
backgroundAlpha = args .getDouble (BACKGROUND_ALPHA );
127
120
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 );
131
124
}
125
+
132
126
notification = this ;
133
127
}
134
128
@@ -154,7 +148,7 @@ public void onCancel(DialogInterface dialog) {
154
148
if (getInAppLayout (insetPadding ) == InAppLayout .FULLSCREEN ) {
155
149
dialog .getWindow ().setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN , WindowManager .LayoutParams .FLAG_FULLSCREEN );
156
150
} 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.
158
152
// For other non-fullscreen in-apps layouts (BOTTOM and CENTER), status bar will be translucent
159
153
dialog .getWindow ().setFlags (WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS , WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS );
160
154
}
@@ -169,6 +163,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
169
163
if (getInAppLayout (insetPadding ) == InAppLayout .FULLSCREEN ) {
170
164
getDialog ().getWindow ().setFlags (WindowManager .LayoutParams .FLAG_FULLSCREEN , WindowManager .LayoutParams .FLAG_FULLSCREEN );
171
165
}
166
+
172
167
webView = new IterableWebView (getContext ());
173
168
webView .setId (R .id .webView );
174
169
webView .createWithHtml (this , htmlString );
@@ -183,8 +178,8 @@ public boolean onPreDraw() {
183
178
184
179
if (orientationListener == null ) {
185
180
orientationListener = new OrientationEventListener (getContext (), SensorManager .SENSOR_DELAY_NORMAL ) {
181
+ // Resize the webView on device rotation
186
182
public void onOrientationChanged (int orientation ) {
187
- // Resize the webview on device rotation
188
183
if (loaded ) {
189
184
final Handler handler = new Handler ();
190
185
handler .postDelayed (new Runnable () {
@@ -197,36 +192,33 @@ public void run() {
197
192
}
198
193
};
199
194
}
195
+
200
196
orientationListener .enable ();
201
197
202
198
RelativeLayout relativeLayout = new RelativeLayout (this .getContext ());
203
199
RelativeLayout .LayoutParams layoutParams = new RelativeLayout .LayoutParams (RelativeLayout .LayoutParams .MATCH_PARENT , RelativeLayout .LayoutParams .MATCH_PARENT );
204
200
relativeLayout .setVerticalGravity (getVerticalLocation (insetPadding ));
205
201
relativeLayout .addView (webView , layoutParams );
206
202
207
- if (savedInstanceState == null || !savedInstanceState .getBoolean (INAPP_OPEN_TRACKED , false )) {
203
+ if (savedInstanceState == null || !savedInstanceState .getBoolean (IN_APP_OPEN_TRACKED , false )) {
208
204
IterableApi .sharedInstance .trackInAppOpen (messageId , location );
209
205
}
206
+
210
207
prepareToShowWebView ();
211
208
return relativeLayout ;
212
209
}
213
210
214
- /**
215
- * Sets the loaded flag
216
- *
217
- * @param loaded
218
- */
219
211
public void setLoaded (boolean loaded ) {
220
212
this .loaded = loaded ;
221
213
}
222
214
223
215
/**
224
- * Sets up the webview and the dialog layout
216
+ * Sets up the webView and the dialog layout
225
217
*/
226
218
@ Override
227
219
public void onSaveInstanceState (@ NonNull Bundle outState ) {
228
220
super .onSaveInstanceState (outState );
229
- outState .putBoolean (INAPP_OPEN_TRACKED , true );
221
+ outState .putBoolean (IN_APP_OPEN_TRACKED , true );
230
222
}
231
223
232
224
/**
@@ -235,6 +227,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
235
227
@ Override
236
228
public void onStop () {
237
229
orientationListener .disable ();
230
+
238
231
super .onStop ();
239
232
}
240
233
@@ -255,9 +248,11 @@ public void onDestroy() {
255
248
public void onUrlClicked (String url ) {
256
249
IterableApi .sharedInstance .trackInAppClick (messageId , url , location );
257
250
IterableApi .sharedInstance .trackInAppClose (messageId , url , IterableInAppCloseAction .LINK , location );
251
+
258
252
if (clickCallback != null ) {
259
253
clickCallback .execute (Uri .parse (url ));
260
254
}
255
+
261
256
processMessageRemoval ();
262
257
hideWebView ();
263
258
}
@@ -268,6 +263,7 @@ public void onUrlClicked(String url) {
268
263
public void onBackPressed () {
269
264
IterableApi .sharedInstance .trackInAppClick (messageId , BACK_BUTTON );
270
265
IterableApi .sharedInstance .trackInAppClose (messageId , BACK_BUTTON , IterableInAppCloseAction .BACK , location );
266
+
271
267
processMessageRemoval ();
272
268
}
273
269
@@ -316,26 +312,28 @@ private void animateBackground(Drawable from, Drawable to) {
316
312
}
317
313
318
314
private ColorDrawable getInAppBackgroundDrawable () {
319
-
320
315
if (inAppBackgroundColor == null ) {
321
316
IterableLogger .d (TAG , "Background Color does not exist. In App background animation will not be performed" );
322
317
return null ;
323
318
}
324
319
325
320
int backgroundColorWithAlpha ;
321
+
326
322
try {
327
323
backgroundColorWithAlpha = ColorUtils .setAlphaComponent (Color .parseColor (inAppBackgroundColor ), (int ) (inAppBackgroundAlpha * 255 ));
328
324
} catch (IllegalArgumentException e ) {
329
325
IterableLogger .e (TAG , "Background color could not be identified for input string \" " + inAppBackgroundColor + "\" . Failed to load in-app background." );
330
326
return null ;
331
327
}
328
+
332
329
ColorDrawable backgroundColorDrawable = new ColorDrawable (backgroundColorWithAlpha );
333
330
return backgroundColorDrawable ;
334
331
}
335
332
336
333
private void showAndAnimateWebView () {
337
334
webView .setAlpha (1.0f );
338
335
webView .setVisibility (View .VISIBLE );
336
+
339
337
if (shouldAnimate ) {
340
338
int animationResource ;
341
339
InAppLayout inAppLayout = getInAppLayout (insetPadding );
@@ -353,6 +351,7 @@ private void showAndAnimateWebView() {
353
351
default :
354
352
animationResource = R .anim .fade_in_custom ;
355
353
}
354
+
356
355
Animation anim = AnimationUtils .loadAnimation (getContext (), animationResource );
357
356
anim .setDuration (IterableConstants .ITERABLE_IN_APP_ANIMATION_DURATION );
358
357
webView .startAnimation (anim );
@@ -378,14 +377,15 @@ private void hideWebView() {
378
377
default :
379
378
animationResource = R .anim .fade_out_custom ;
380
379
}
380
+
381
381
Animation anim = AnimationUtils .loadAnimation (getContext (),
382
382
animationResource );
383
383
anim .setDuration (IterableConstants .ITERABLE_IN_APP_ANIMATION_DURATION );
384
384
webView .startAnimation (anim );
385
385
}
386
386
387
387
hideInAppBackground ();
388
- Runnable dismissWebviewRunnable = new Runnable () {
388
+ Runnable dismissWebViewRunnable = new Runnable () {
389
389
@ Override
390
390
public void run () {
391
391
if (getContext () != null && getDialog () != null && getDialog ().getWindow () != null ) {
@@ -395,11 +395,10 @@ public void run() {
395
395
};
396
396
397
397
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN ) {
398
- webView .postOnAnimationDelayed (dismissWebviewRunnable , 400 );
398
+ webView .postOnAnimationDelayed (dismissWebViewRunnable , 400 );
399
399
} else {
400
- webView .postDelayed (dismissWebviewRunnable , 400 );
400
+ webView .postDelayed (dismissWebViewRunnable , 400 );
401
401
}
402
-
403
402
}
404
403
405
404
private void processMessageRemoval () {
@@ -420,7 +419,7 @@ public void runResizeScript() {
420
419
}
421
420
422
421
/**
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
424
423
* @param height
425
424
*/
426
425
public void resize (final float height ) {
@@ -454,6 +453,7 @@ public void run() {
454
453
} else {
455
454
display .getSize (size );
456
455
}
456
+
457
457
int webViewWidth = size .x ;
458
458
int webViewHeight = size .y ;
459
459
0 commit comments