25
25
import android .view .MotionEvent ;
26
26
import android .view .View ;
27
27
import android .view .ViewTreeObserver .OnGlobalLayoutListener ;
28
- import androidx .annotation .Keep ;
29
28
import androidx .annotation .NonNull ;
30
29
import androidx .annotation .Nullable ;
31
30
import androidx .annotation .VisibleForTesting ;
78
77
* <p>To delete the Instance ID and the data associated with it, see {@link
79
78
* FirebaseInstallationsApi#delete}.
80
79
*/
81
- @ Keep
82
80
@ FirebaseAppScope
83
81
public class FirebaseInAppMessagingDisplay extends FirebaseInAppMessagingDisplayImpl {
84
82
static final long IMPRESSION_THRESHOLD_MILLIS = 5 * 1000 ; // 5 seconds is a valid impression
@@ -101,6 +99,8 @@ public class FirebaseInAppMessagingDisplay extends FirebaseInAppMessagingDisplay
101
99
private InAppMessage inAppMessage ;
102
100
private FirebaseInAppMessagingDisplayCallbacks callbacks ;
103
101
102
+ @ VisibleForTesting @ Nullable String currentlyBoundActivityName ;
103
+
104
104
@ Inject
105
105
FirebaseInAppMessagingDisplay (
106
106
FirebaseInAppMessaging headlessInAppMessaging ,
@@ -129,7 +129,6 @@ public class FirebaseInAppMessagingDisplay extends FirebaseInAppMessagingDisplay
129
129
* FirebaseApp#getInstance()}
130
130
*/
131
131
@ NonNull
132
- @ Keep
133
132
public static FirebaseInAppMessagingDisplay getInstance () {
134
133
return FirebaseApp .getInstance ().get (FirebaseInAppMessagingDisplay .class );
135
134
}
@@ -143,7 +142,6 @@ private static int getScreenOrientation(Application app) {
143
142
*
144
143
* @hide
145
144
*/
146
- @ Keep
147
145
public void testMessage (
148
146
Activity activity ,
149
147
InAppMessage inAppMessage ,
@@ -158,7 +156,6 @@ public void testMessage(
158
156
*
159
157
* @hide
160
158
*/
161
- @ Keep
162
159
public void setFiamListener (FiamListener listener ) {
163
160
this .fiamListener = listener ;
164
161
}
@@ -168,79 +165,73 @@ public void setFiamListener(FiamListener listener) {
168
165
*
169
166
* @hide
170
167
*/
171
- @ Keep
172
168
public void clearFiamListener () {
173
169
this .fiamListener = null ;
174
170
}
175
171
176
172
/**
177
- * Clears fiam listener
173
+ * Bind FIAM listener on Activity resume.
178
174
*
179
175
* @hide
180
176
*/
181
- @ Keep
182
177
@ Override
183
- public void onActivityStarted (final Activity activity ) {
184
- super .onActivityStarted (activity );
185
- // Register FIAM listener with the headless sdk.
186
- headlessInAppMessaging .setMessageDisplayComponent (
187
- (iam , cb ) -> {
188
- // When we are in the middle of showing a message, we ignore other notifications these
189
- // messages will be fired when the corresponding events happen the next time.
190
- if (inAppMessage != null || headlessInAppMessaging .areMessagesSuppressed ()) {
191
- Logging .logd ("Active FIAM exists. Skipping trigger" );
192
- return ;
193
- }
194
- inAppMessage = iam ;
195
- callbacks = cb ;
196
- showActiveFiam (activity );
197
- });
178
+ public void onActivityResumed (Activity activity ) {
179
+ super .onActivityResumed (activity );
180
+ bindFiamToActivity (activity );
198
181
}
199
182
200
183
/**
201
- * Clear fiam listener on activity paused
184
+ * Clear FIAM listener on activity paused
202
185
*
203
186
* @hide
204
187
*/
205
- @ Keep
206
188
@ Override
207
189
public void onActivityPaused (Activity activity ) {
208
- // clear all state scoped to activity and dismiss fiam
209
- headlessInAppMessaging .clearDisplayListener ();
210
- imageLoader .cancelTag (activity .getClass ());
211
- removeDisplayedFiam (activity );
190
+ unbindFiamFromActivity (activity );
191
+ headlessInAppMessaging .removeAllListeners ();
212
192
super .onActivityPaused (activity );
213
193
}
214
194
215
- /**
216
- * Clear fiam listener on activity destroyed
217
- *
218
- * @hide
219
- */
220
- @ Keep
221
- @ Override
222
- public void onActivityDestroyed (Activity activity ) {
223
- // clear all state scoped to activity and dismiss fiam
224
- headlessInAppMessaging .clearDisplayListener ();
225
- imageLoader .cancelTag (activity .getClass ());
226
- removeDisplayedFiam (activity );
227
- super .onActivityDestroyed (activity );
228
- }
229
-
230
- /**
231
- * Clear fiam listener on activity resumed
232
- *
233
- * @hide
234
- */
235
- @ Keep
236
- @ Override
237
- public void onActivityResumed (Activity activity ) {
238
- super .onActivityResumed (activity );
195
+ private void bindFiamToActivity (Activity activity ) {
196
+ // If we have no currently bound activity or are currently bound to a different activity then
197
+ // bind to this new activity.
198
+ if (currentlyBoundActivityName == null
199
+ || !currentlyBoundActivityName .equals (activity .getLocalClassName ())) {
200
+ Logging .logi ("Binding to activity: " + activity .getLocalClassName ());
201
+ headlessInAppMessaging .setMessageDisplayComponent (
202
+ (iam , cb ) -> {
203
+ // When we are in the middle of showing a message, we ignore other notifications these
204
+ // messages will be fired when the corresponding events happen the next time.
205
+ if (inAppMessage != null || headlessInAppMessaging .areMessagesSuppressed ()) {
206
+ Logging .logd ("Active FIAM exists. Skipping trigger" );
207
+ return ;
208
+ }
209
+ inAppMessage = iam ;
210
+ callbacks = cb ;
211
+ showActiveFiam (activity );
212
+ });
213
+ // set the current activity to be the one passed in so that we know not to bind again to the
214
+ // same activity
215
+ currentlyBoundActivityName = activity .getLocalClassName ();
216
+ }
239
217
if (inAppMessage != null ) {
240
218
showActiveFiam (activity );
241
219
}
242
220
}
243
221
222
+ private void unbindFiamFromActivity (Activity activity ) {
223
+ // If we are attempting to unbind from an activity, first check to see that we are currently
224
+ // bound to it
225
+ if (currentlyBoundActivityName != null
226
+ && currentlyBoundActivityName .equals (activity .getLocalClassName ())) {
227
+ Logging .logi ("Unbinding from activity: " + activity .getLocalClassName ());
228
+ headlessInAppMessaging .clearDisplayListener ();
229
+ imageLoader .cancelTag (activity .getClass ());
230
+ removeDisplayedFiam (activity );
231
+ currentlyBoundActivityName = null ;
232
+ }
233
+ }
234
+
244
235
// The current FIAM might be null
245
236
@ VisibleForTesting
246
237
InAppMessage getCurrentInAppMessage () {
@@ -318,8 +309,7 @@ public void onClick(View v) {
318
309
319
310
Map <Action , View .OnClickListener > actionListeners = new HashMap <>();
320
311
// If the message has an action, but not an action url, we dismiss when the action
321
- // button is
322
- // clicked;
312
+ // button is clicked
323
313
for (Action action : extractActions (inAppMessage )) {
324
314
325
315
final View .OnClickListener actionListener ;
0 commit comments