8
8
import android .support .v4 .media .RatingCompat ;
9
9
import android .support .v4 .media .session .PlaybackStateCompat ;
10
10
import com .facebook .react .bridge .Callback ;
11
- import com .facebook .react .bridge .LifecycleEventListener ;
12
11
import com .facebook .react .bridge .ReactApplicationContext ;
13
12
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
14
13
import com .facebook .react .bridge .ReactMethod ;
15
14
import com .facebook .react .bridge .ReadableMap ;
16
15
import guichaguri .trackplayer .logic .PlayerService ;
17
16
import guichaguri .trackplayer .logic .components .MediaWrapper ;
18
17
import java .io .IOException ;
18
+ import java .util .Arrays ;
19
19
import java .util .HashMap ;
20
20
import java .util .Map ;
21
21
import javax .annotation .Nullable ;
22
22
23
23
/**
24
24
* @author Guilherme Chaguri
25
25
*/
26
- public class TrackModule extends ReactContextBaseJavaModule implements ServiceConnection , LifecycleEventListener {
26
+ public class TrackModule extends ReactContextBaseJavaModule implements ServiceConnection {
27
27
28
28
private MediaWrapper manager = null ;
29
- private Callback initCallback = null ;
29
+ private Callback [] initCallbacks = null ;
30
30
31
31
public TrackModule (ReactApplicationContext context ) {
32
32
super (context );
@@ -43,46 +43,25 @@ public void initialize() {
43
43
44
44
ReactApplicationContext context = getReactApplicationContext ();
45
45
46
- context .addLifecycleEventListener (this );
47
-
48
46
Intent intent = new Intent (context , PlayerService .class );
49
47
intent .setAction (PlayerService .ACTION_MEDIA );
50
48
context .bindService (intent , this , Service .BIND_AUTO_CREATE );
51
49
}
52
50
53
51
@ Override
54
52
public void onCatalystInstanceDestroy () {
55
- ReactApplicationContext context = getReactApplicationContext ();
56
-
57
- context .removeLifecycleEventListener (this );
58
- context .unbindService (this );
59
- }
60
-
61
- @ Override
62
- public void onHostResume () {
63
-
64
- }
65
-
66
- @ Override
67
- public void onHostPause () {
68
-
69
- }
70
-
71
- @ Override
72
- public void onHostDestroy () {
73
- ReactApplicationContext context = getReactApplicationContext ();
74
-
75
- context .removeLifecycleEventListener (this );
76
- context .unbindService (this );
53
+ getReactApplicationContext ().unbindService (this );
77
54
}
78
55
79
56
@ Override
80
57
public void onServiceConnected (ComponentName name , IBinder service ) {
81
58
manager = (MediaWrapper )service ;
82
59
83
- if (initCallback != null ) {
84
- initCallback .invoke ();
85
- initCallback = null ;
60
+ if (initCallbacks != null ) {
61
+ for (Callback cb : initCallbacks ) {
62
+ cb .invoke ();
63
+ }
64
+ initCallbacks = null ;
86
65
}
87
66
}
88
67
@@ -91,6 +70,8 @@ public void onServiceDisconnected(ComponentName name) {
91
70
manager = null ;
92
71
}
93
72
73
+ /* ****************************** API ****************************** */
74
+
94
75
@ Nullable
95
76
@ Override
96
77
public Map <String , Object > getConstants () {
@@ -123,14 +104,19 @@ public Map<String, Object> getConstants() {
123
104
return constants ;
124
105
}
125
106
126
- /* ****************************** Native Functions ****************************** */
127
-
128
107
@ ReactMethod
129
108
public void onReady (Callback callback ) {
130
109
if (manager != null ) {
131
110
callback .invoke ();
111
+ return ;
112
+ }
113
+
114
+ if (initCallbacks == null ) {
115
+ initCallbacks = new Callback []{callback };
132
116
} else {
133
- initCallback = callback ;
117
+ int index = initCallbacks .length ;
118
+ initCallbacks = Arrays .copyOf (initCallbacks , index + 1 );
119
+ initCallbacks [index ] = callback ;
134
120
}
135
121
}
136
122
0 commit comments