18
18
import com .facebook .react .bridge .WritableNativeMap ;
19
19
20
20
import java .util .ArrayList ;
21
+ import java .util .HashMap ;
21
22
import java .util .List ;
23
+ import java .util .Map ;
22
24
23
25
import jp .manse .util .DefaultEventEmitter ;
24
26
@@ -46,14 +48,20 @@ public class BrightcovePlayerAccount implements OfflineVideoDownloadSession.OnOf
46
48
private List <OfflineVideoDownloadSession > offlineVideoDownloadSessions = new ArrayList <>();
47
49
private boolean getOfflineVideoStatusesRunning = false ;
48
50
private List <Promise > getOfflineVideoStatusesPendingPromises = new ArrayList <>();
49
- private List <Video > allDownloadedVideos ;
51
+ private List <Video > allDownloadedVideos = new ArrayList <>() ;
50
52
private Catalog catalog ;
51
53
private OfflineCatalog offlineCatalog ;
54
+ private OnBrightcovePlayerAccountListener listener ;
52
55
53
- public BrightcovePlayerAccount (final ReactApplicationContext context , final String accountId , final String policyKey ) {
56
+ public interface OnBrightcovePlayerAccountListener {
57
+ void onOfflineStorageStateChanged (NativeArray array );
58
+ }
59
+
60
+ public BrightcovePlayerAccount (final ReactApplicationContext context , final String accountId , final String policyKey , OnBrightcovePlayerAccountListener listener ) {
54
61
this .context = context ;
55
62
this .accountId = accountId ;
56
63
this .policyKey = policyKey ;
64
+ this .listener = listener ;
57
65
handler = new Handler (Looper .myLooper ());
58
66
this .catalog = new Catalog (DefaultEventEmitter .sharedEventEmitter , accountId , policyKey );
59
67
this .offlineCatalog = new OfflineCatalog (context , DefaultEventEmitter .sharedEventEmitter , accountId , policyKey );
@@ -84,6 +92,7 @@ public void requestDownloadWithReferenceId(String referenceId, int bitRate, Prom
84
92
OfflineVideoDownloadSession session = new OfflineVideoDownloadSession (this .context , this .accountId , this .policyKey , this );
85
93
session .requestDownloadWithReferenceId (referenceId , bitRate , promise );
86
94
this .offlineVideoDownloadSessions .add (session );
95
+ this .listener .onOfflineStorageStateChanged (collectNativeOfflineVideoStatuses ());
87
96
}
88
97
89
98
public void requestDownloadWithVideoId (String videoId , int bitRate , Promise promise ) {
@@ -94,6 +103,7 @@ public void requestDownloadWithVideoId(String videoId, int bitRate, Promise prom
94
103
OfflineVideoDownloadSession session = new OfflineVideoDownloadSession (this .context , this .accountId , this .policyKey , this );
95
104
session .requestDownloadWithVideoId (videoId , bitRate , promise );
96
105
this .offlineVideoDownloadSessions .add (session );
106
+ this .listener .onOfflineStorageStateChanged (collectNativeOfflineVideoStatuses ());
97
107
}
98
108
99
109
public void getOfflineVideoStatuses (Promise promise ) {
@@ -134,19 +144,27 @@ private void sendOfflineVideoStatuses() {
134
144
}
135
145
}
136
146
137
- public void deleteOfflineVideo (String videoId , final Promise promise ) {
147
+ public void deleteOfflineVideo (final String videoId , final Promise promise ) {
138
148
try {
149
+ if (videoId == null ) throw new Exception ();
139
150
this .offlineCatalog .cancelVideoDownload (videoId );
140
151
for (int i = this .offlineVideoDownloadSessions .size () - 1 ; i >= 0 ; i --) {
141
152
OfflineVideoDownloadSession session = this .offlineVideoDownloadSessions .get (i );
142
153
if (videoId .equals (session .videoId )) {
143
- this .offlineVideoDownloadSessions .remove (session );
154
+ this .offlineVideoDownloadSessions .remove (i );
144
155
}
145
156
}
146
157
this .offlineCatalog .deleteVideo (videoId , new OfflineCallback <Boolean >() {
147
158
@ Override
148
159
public void onSuccess (Boolean aBoolean ) {
149
160
promise .resolve (null );
161
+ for (int i = allDownloadedVideos .size () - 1 ; i >= 0 ; i --) {
162
+ Video video = allDownloadedVideos .get (i );
163
+ if (videoId .equals (video .getId ())) {
164
+ allDownloadedVideos .remove (i );
165
+ }
166
+ }
167
+ listener .onOfflineStorageStateChanged (collectNativeOfflineVideoStatuses ());
150
168
}
151
169
152
170
@ Override
@@ -192,6 +210,8 @@ private NativeArray collectNativeOfflineVideoStatuses() {
192
210
WritableNativeArray statuses = new WritableNativeArray ();
193
211
for (Video video : this .allDownloadedVideos ) {
194
212
WritableNativeMap map = new WritableNativeMap ();
213
+ map .putString (CALLBACK_KEY_ACCOUNT_ID , this .accountId );
214
+ map .putString (CALLBACK_KEY_VIDEO_ID , video .getId ());
195
215
map .putString (CALLBACK_KEY_VIDEO_TOKEN , video .getId ());
196
216
map .putDouble (CALLBACK_KEY_DOWNLOAD_PROGRESS , 1 );
197
217
statuses .pushMap (map );
@@ -200,13 +220,15 @@ private NativeArray collectNativeOfflineVideoStatuses() {
200
220
if (session .videoId == null ) continue ;
201
221
boolean found = false ;
202
222
for (Video video : this .allDownloadedVideos ) {
203
- if (video . getId () .equals (session . videoId )) {
223
+ if (session . videoId .equals (video . getId () )) {
204
224
found = true ;
205
225
break ;
206
226
}
207
227
}
208
228
if (found ) continue ;
209
229
WritableNativeMap map = new WritableNativeMap ();
230
+ map .putString (CALLBACK_KEY_ACCOUNT_ID , this .accountId );
231
+ map .putString (CALLBACK_KEY_VIDEO_ID , session .videoId );
210
232
map .putString (CALLBACK_KEY_VIDEO_TOKEN , session .videoId );
211
233
map .putDouble (CALLBACK_KEY_DOWNLOAD_PROGRESS , session .downloadProgress );
212
234
statuses .pushMap (map );
@@ -246,5 +268,16 @@ private boolean hasOfflineVideoDownloadSessionWithVideoId(String videoId) {
246
268
@ Override
247
269
public void onCompleted (OfflineVideoDownloadSession session ) {
248
270
this .offlineVideoDownloadSessions .remove (session );
271
+ if (session .downloadProgress == 1 ) {
272
+ Map <String , Object > param = new HashMap <>();
273
+ param .put (Video .Fields .ID , session .videoId );
274
+ this .allDownloadedVideos .add (new Video (param ));
275
+ }
276
+ this .listener .onOfflineStorageStateChanged (collectNativeOfflineVideoStatuses ());
277
+ }
278
+
279
+ @ Override
280
+ public void onProgress () {
281
+ this .listener .onOfflineStorageStateChanged (collectNativeOfflineVideoStatuses ());
249
282
}
250
283
}
0 commit comments