1
1
package com .onesignal ;
2
2
3
+ import android .content .Context ;
4
+ import android .support .annotation .NonNull ;
5
+ import android .support .v4 .app .DialogFragment ;
6
+ import android .support .v4 .app .Fragment ;
7
+ import android .support .v4 .app .FragmentManager ;
8
+ import android .support .v7 .app .AppCompatActivity ;
9
+
3
10
import java .lang .ref .WeakReference ;
11
+ import java .util .List ;
4
12
5
13
class OSSystemConditionController {
6
14
7
15
interface OSSystemConditionObserver {
8
16
// Alerts the systemConditionObserver that a system condition has being activated
9
- void messageTriggerConditionChanged ();
17
+ void systemConditionChanged ();
10
18
}
11
19
12
20
private static final String TAG = OSSystemConditionController .class .getCanonicalName ();
@@ -18,13 +26,51 @@ interface OSSystemConditionObserver {
18
26
19
27
boolean systemConditionsAvailable () {
20
28
if (ActivityLifecycleHandler .curActivity == null ) {
29
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .WARN , "OSSystemConditionObserver curActivity null" );
21
30
return false ;
22
31
}
32
+
33
+ if (isDialogFragmentShowing (ActivityLifecycleHandler .curActivity )) {
34
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .WARN , "OSSystemConditionObserver dialog fragment detected" );
35
+ return false ;
36
+ }
37
+
23
38
boolean keyboardUp = OSViewUtils .isKeyboardUp (new WeakReference <>(ActivityLifecycleHandler .curActivity ));
24
39
if (keyboardUp ) {
25
40
ActivityLifecycleHandler .setSystemConditionObserver (TAG , systemConditionObserver );
41
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .WARN , "OSSystemConditionObserver keyboard up detected" );
26
42
}
27
43
return !keyboardUp ;
28
44
}
29
45
46
+ boolean isDialogFragmentShowing (Context context ) {
47
+ // Detect if user has a dialog fragment showing
48
+ if (context instanceof AppCompatActivity ) {
49
+ final FragmentManager manager = ((AppCompatActivity ) context ).getSupportFragmentManager ();
50
+ manager .registerFragmentLifecycleCallbacks (new FragmentManager .FragmentLifecycleCallbacks () {
51
+
52
+ @ Override
53
+ public void onFragmentDetached (@ NonNull FragmentManager fm , @ NonNull Fragment fragmentDetached ) {
54
+ super .onFragmentDetached (fm , fragmentDetached );
55
+ if (fragmentDetached instanceof DialogFragment ) {
56
+ manager .unregisterFragmentLifecycleCallbacks (this );
57
+ systemConditionObserver .systemConditionChanged ();
58
+ }
59
+ }
60
+
61
+ }, true );
62
+ List <Fragment > fragments = manager .getFragments ();
63
+ int size = fragments .size ();
64
+ if (size > 0 ) {
65
+ // We only care of the last fragment available
66
+ Fragment fragment = fragments .get (size - 1 );
67
+ return fragment .isVisible () && fragment instanceof DialogFragment ;
68
+ }
69
+ }
70
+
71
+ // We already have Activity lifecycle listener, that listener will handle Activity focus/unFocus state
72
+ // - Permission prompts will make activity loose focus
73
+ // We cannot detect AlertDialogs because they are added to the decor view as linear layout without an identification
74
+ return false ;
75
+ }
30
76
}
0 commit comments