-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContentDirectoryBrowseTaskFragment.java
431 lines (340 loc) · 14.7 KB
/
ContentDirectoryBrowseTaskFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package com.cajor.dk.dlna;
import android.app.Activity;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import org.fourthline.cling.android.AndroidUpnpService;
import org.fourthline.cling.android.AndroidUpnpServiceImpl;
import org.fourthline.cling.controlpoint.ActionCallback;
import org.fourthline.cling.model.action.ActionException;
import org.fourthline.cling.model.action.ActionInvocation;
import org.fourthline.cling.model.message.UpnpResponse;
import org.fourthline.cling.model.meta.Device;
import org.fourthline.cling.model.meta.LocalDevice;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.model.meta.Service;
import org.fourthline.cling.model.types.ErrorCode;
import org.fourthline.cling.registry.DefaultRegistryListener;
import org.fourthline.cling.registry.Registry;
import org.fourthline.cling.support.contentdirectory.callback.Browse;
import org.fourthline.cling.support.model.BrowseFlag;
import org.fourthline.cling.support.model.DIDLContent;
import org.fourthline.cling.support.model.DIDLObject;
import org.fourthline.cling.support.model.SortCriterion;
import org.fourthline.cling.support.model.container.Container;
import org.fourthline.cling.support.model.item.Item;
import java.net.URI;
import java.util.ArrayList;
import java.util.Stack;
import static android.R.attr.data;
public class ContentDirectoryBrowseTaskFragment extends Fragment {
static interface Callbacks {
void onDisplayDevices();
void onDisplayDirectories();
void onDisplayItems(ArrayList<ItemModel> items);
void onDisplayItemsError(String error);
void onDeviceAdded(DeviceModel device);
void onDeviceRemoved(DeviceModel device);
}
private Callbacks mCallbacks;
private BrowseRegistryListener mListener = new BrowseRegistryListener();
public static AndroidUpnpService mService;
private Stack<ItemModel> mFolders = new Stack<ItemModel>();
private Boolean mIsShowingDeviceList = true;
public static DeviceModel mCurrentDevice = null;
private Activity mActivity;
//MainActivity.ListenerThread r =null;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity == null)
return;
mActivity = activity;
mCallbacks = (Callbacks)activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Log.d("BZA", "CDBTF" + "oncreate" );
bindServiceConnection();
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onDestroy() {
super.onDestroy();
unbindServiceConnection();
}
public void navigateTo(Object model) {
Log.d("BZA", "CDBTF" + "navigateTo" );
if (model instanceof DeviceModel) {
DeviceModel deviceModel = (DeviceModel)model;
Device device = deviceModel.getDevice();
if (device.isFullyHydrated()) {
Service conDir = deviceModel.getContentDirectory();
if (conDir != null)
mService.getControlPoint().execute(
new CustomContentBrowseActionCallback(conDir, "0"));
if (mCallbacks != null)
mCallbacks.onDisplayDirectories();
mIsShowingDeviceList = false;
mCurrentDevice = deviceModel;
} else {
Toast.makeText(mActivity, R.string.info_still_loading, Toast.LENGTH_SHORT)
.show();
}
}
if (model instanceof ItemModel) {
ItemModel item = (ItemModel)model;
if (item.isContainer()) {
if (mFolders.isEmpty())
mFolders.push(item);
else
if (mFolders.peek().getId() != item.getId())
mFolders.push(item);
Log.d("BZA", "CDBTF" + "control point" );
mService.getControlPoint().execute(
new CustomContentBrowseActionCallback(item.getService(),
item.getId()));
} else {
try {
Uri uri = Uri.parse(item.getUrl());
Log.d("BZA", "CDBTF" + "URI"+ uri.toString() );
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromUrl(uri.toString());
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri, type);
startActivity(intent);
} catch(NullPointerException ex) {
Toast.makeText(mActivity, R.string.info_could_not_start_activity, Toast.LENGTH_SHORT)
.show();
} catch(ActivityNotFoundException ex) {
Toast.makeText(mActivity, R.string.info_no_handler, Toast.LENGTH_SHORT)
.show();
}
}
}
}
public Boolean goBack() {
if (mFolders.empty()) {
if (!mIsShowingDeviceList) {
mIsShowingDeviceList = true;
if (mCallbacks != null)
mCallbacks.onDisplayDevices();
} else {
return true;
}
} else {
ItemModel item = mFolders.pop();
mService.getControlPoint().execute(
new CustomContentBrowseActionCallback(item.getService(),
item.getContainer().getParentID()));
}
return false;
}
public void refreshDevices() {
if (mService == null)
return;
mService.getRegistry().removeAllRemoteDevices();
for (Device device : mService.getRegistry().getDevices())
mListener.deviceAdded(device);
mService.getControlPoint().search();
}
public void refreshCurrent() {
if (mService == null)
return;
if (mIsShowingDeviceList != null && mIsShowingDeviceList) {
if (mCallbacks != null)
mCallbacks.onDisplayDevices();
mService.getRegistry().removeAllRemoteDevices();
for (Device device : mService.getRegistry().getDevices())
mListener.deviceAdded(device);
mService.getControlPoint().search();
} else {
if (!mFolders.empty()) {
ItemModel item = mFolders.peek();
if (item == null)
return;
mService.getControlPoint().execute(
new CustomContentBrowseActionCallback(item.getService(),
item.getId()));
} else {
if (mCurrentDevice != null) {
Service service = mCurrentDevice.getContentDirectory();
if (service != null)
mService.getControlPoint().execute(
new CustomContentBrowseActionCallback(service, "0"));
}
}
}
}
private Boolean bindServiceConnection() {
Context context = mActivity.getApplicationContext();
if (context == null)
return false;
context.bindService(
new Intent(mActivity, AndroidUpnpServiceImpl.class),
serviceConnection, Context.BIND_AUTO_CREATE
);
return true;
}
private Boolean unbindServiceConnection() {
if (mService != null)
mService.getRegistry().removeListener(mListener);
Context context = mActivity.getApplicationContext();
if (context == null)
return false;
context.unbindService(serviceConnection);
return true;
}
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = (AndroidUpnpService) service;
mService.getRegistry().addListener(mListener);
for (Device device : mService.getRegistry().getDevices())
mListener.deviceAdded(device);
Log.d("BZA", "CDBTF" + "serviceConnection" );
mService.getControlPoint().search();
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
private class BrowseRegistryListener extends DefaultRegistryListener {
@Override
public void remoteDeviceDiscoveryStarted(Registry registry, RemoteDevice device) {
deviceAdded(device);
}
@Override
public void remoteDeviceDiscoveryFailed(Registry registry, RemoteDevice device, Exception ex) {
deviceRemoved(device);
}
@Override
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
deviceAdded(device);
}
@Override
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
deviceRemoved(device);
}
@Override
public void localDeviceAdded(Registry registry, LocalDevice device) {
deviceAdded(device);
}
@Override
public void localDeviceRemoved(Registry registry, LocalDevice device) {
deviceRemoved(device);
}
public void deviceAdded(Device device) {
DeviceModel deviceModel = new DeviceModel(R.drawable.ic_device, device);
Service conDir = deviceModel.getContentDirectory();
if (conDir != null) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(mActivity);
if (prefs.getBoolean("settings_validate_devices", false)) {
if (device.isFullyHydrated())
mService.getControlPoint().execute(
new CustomContentBrowseTestCallback(device, conDir));
} else {
if (mCallbacks != null)
mCallbacks.onDeviceAdded(deviceModel);
}
}
}
public void deviceRemoved(Device device) {
if (mCallbacks != null)
mCallbacks.onDeviceRemoved(new DeviceModel(R.drawable.ic_device, device));
}
}
private class CustomContentBrowseActionCallback extends Browse {
private Service service;
public CustomContentBrowseActionCallback(Service service, String id) {
super(service, id, BrowseFlag.DIRECT_CHILDREN, "*", 0, 99999l,
new SortCriterion(true, "dc:title"));
this.service = service;
if (mCallbacks != null)
mCallbacks.onDisplayDirectories();
}
private ItemModel createItemModel(DIDLObject item) {
ItemModel itemModel = new ItemModel(getResources(),
R.drawable.ic_folder, service, item);
URI usableIcon = item.getFirstPropertyValue(DIDLObject.Property.UPNP.ICON.class);
if (usableIcon == null || usableIcon.toString().isEmpty()) {
usableIcon = item.getFirstPropertyValue(DIDLObject.Property.UPNP.ALBUM_ART_URI.class);
}
if (usableIcon != null)
itemModel.setIconUrl(usableIcon.toString());
if (item instanceof Item) {
itemModel.setIcon(R.drawable.ic_file);
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(mActivity);
if (prefs.getBoolean("settings_hide_file_icons", false))
itemModel.setHideIcon(true);
if (prefs.getBoolean("settings_show_extensions", false))
itemModel.setShowExtension(true);
}
return itemModel;
}
@Override
public void received(final ActionInvocation actionInvocation, final DIDLContent didl) {
ArrayList<ItemModel> items = new ArrayList<ItemModel>();
try {
for (Container childContainer : didl.getContainers())
items.add(createItemModel(childContainer));
for (Item childItem : didl.getItems())
items.add(createItemModel(childItem));
if (mCallbacks != null)
mCallbacks.onDisplayItems(items);
} catch (Exception ex) {
actionInvocation.setFailure(new ActionException(
ErrorCode.ACTION_FAILED,
"Can't create list childs: " + ex, ex));
failure(actionInvocation, null, ex.getMessage());
}
}
@Override
public void updateStatus(Status status) {
}
@Override
public void failure(ActionInvocation invocation, UpnpResponse response, String s) {
if (mCallbacks != null)
mCallbacks.onDisplayItemsError(createDefaultFailureMessage(invocation, response));
}
}
private class CustomContentBrowseTestCallback extends Browse {
private Device device;
private Service service;
public CustomContentBrowseTestCallback(Device device, Service service) {
super(service, "0", BrowseFlag.DIRECT_CHILDREN, "*", 0, 99999l,
new SortCriterion(true, "dc:title"));
this.device = device;
this.service = service;
}
@Override
public void received(final ActionInvocation actionInvocation, final DIDLContent didl) {
if (mCallbacks != null)
mCallbacks.onDeviceAdded(new DeviceModel(R.drawable.ic_device, device));
}
@Override
public void updateStatus(Status status) {
}
@Override
public void failure(ActionInvocation actionInvocation, UpnpResponse upnpResponse, String s) {
}
}
}