|
|
|
|
@ -12,7 +12,9 @@ import androidx.viewbinding.ViewBinding; |
|
|
|
|
|
|
|
|
|
import com.android.cast.dlna.dmc.DLNACastManager; |
|
|
|
|
import com.android.cast.dlna.dmc.OnDeviceRegistryListener; |
|
|
|
|
import com.android.cast.dlna.dmc.control.ICastInterface; |
|
|
|
|
import com.android.cast.dlna.dmc.control.DeviceControl; |
|
|
|
|
import com.android.cast.dlna.dmc.control.OnDeviceControlListener; |
|
|
|
|
import com.android.cast.dlna.dmc.control.ServiceActionCallback; |
|
|
|
|
import com.fongmi.android.tv.App; |
|
|
|
|
import com.fongmi.android.tv.Constant; |
|
|
|
|
import com.fongmi.android.tv.R; |
|
|
|
|
@ -24,7 +26,6 @@ import com.fongmi.android.tv.cast.CastVideo; |
|
|
|
|
import com.fongmi.android.tv.cast.ScanEvent; |
|
|
|
|
import com.fongmi.android.tv.cast.ScanTask; |
|
|
|
|
import com.fongmi.android.tv.databinding.DialogDeviceBinding; |
|
|
|
|
import com.fongmi.android.tv.impl.Callback; |
|
|
|
|
import com.fongmi.android.tv.server.Server; |
|
|
|
|
import com.fongmi.android.tv.ui.activity.ScanActivity; |
|
|
|
|
import com.fongmi.android.tv.ui.adapter.DeviceAdapter; |
|
|
|
|
@ -33,6 +34,8 @@ import com.github.catvod.net.OkHttp; |
|
|
|
|
import com.github.catvod.utils.Path; |
|
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
|
|
|
|
|
|
|
|
|
import org.fourthline.cling.support.lastchange.EventedValue; |
|
|
|
|
import org.fourthline.cling.support.model.TransportState; |
|
|
|
|
import org.greenrobot.eventbus.EventBus; |
|
|
|
|
import org.greenrobot.eventbus.Subscribe; |
|
|
|
|
import org.greenrobot.eventbus.ThreadMode; |
|
|
|
|
@ -40,19 +43,22 @@ import org.greenrobot.eventbus.ThreadMode; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import kotlin.Unit; |
|
|
|
|
import okhttp3.Call; |
|
|
|
|
import okhttp3.FormBody; |
|
|
|
|
import okhttp3.OkHttpClient; |
|
|
|
|
import okhttp3.Response; |
|
|
|
|
|
|
|
|
|
public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListener, ScanTask.Listener, OnDeviceRegistryListener, ICastInterface.CastEventListener { |
|
|
|
|
public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListener, ScanTask.Listener, OnDeviceRegistryListener, OnDeviceControlListener, ServiceActionCallback<Unit>, okhttp3.Callback { |
|
|
|
|
|
|
|
|
|
private final FormBody.Builder body; |
|
|
|
|
private final OkHttpClient client; |
|
|
|
|
|
|
|
|
|
private DialogDeviceBinding binding; |
|
|
|
|
private DeviceAdapter adapter; |
|
|
|
|
private Listener listener; |
|
|
|
|
private DeviceControl control; |
|
|
|
|
private CastVideo video; |
|
|
|
|
private long position; |
|
|
|
|
private boolean fm; |
|
|
|
|
|
|
|
|
|
public static CastDialog create() { |
|
|
|
|
@ -73,6 +79,7 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
if (fd.startsWith("file")) fd = Server.get().getAddress() + "/" + fd.replace(Path.rootPath(), ""); |
|
|
|
|
if (fd.contains("127.0.0.1")) fd = fd.replace("127.0.0.1", Server.get().getIP()); |
|
|
|
|
body.add("history", history.toString().replace(id, fd)); |
|
|
|
|
position = history.getPosition(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -89,7 +96,6 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
public void show(FragmentActivity activity) { |
|
|
|
|
for (Fragment f : activity.getSupportFragmentManager().getFragments()) if (f instanceof BottomSheetDialogFragment) return; |
|
|
|
|
show(activity.getSupportFragmentManager(), null); |
|
|
|
|
this.listener = (Listener) activity; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@ -119,14 +125,12 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
private void getDevice() { |
|
|
|
|
if (fm) adapter.addAll(Device.getAll()); |
|
|
|
|
adapter.addAll(CastDevice.get().getAll()); |
|
|
|
|
if (CastDevice.get().isEmpty()) App.post(() -> onRefresh(false), 1000); |
|
|
|
|
onRefresh(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void initDLNA() { |
|
|
|
|
DLNACastManager.getInstance().bindCastService(App.get()); |
|
|
|
|
DLNACastManager.getInstance().registerDeviceListener(this); |
|
|
|
|
DLNACastManager.getInstance().registerActionCallbacks(this); |
|
|
|
|
DLNACastManager.INSTANCE.bindCastService(App.get()); |
|
|
|
|
DLNACastManager.INSTANCE.registerDeviceListener(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onRefresh() { |
|
|
|
|
@ -135,7 +139,7 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
private void onRefresh(boolean clear) { |
|
|
|
|
if (fm) ScanTask.create(this).start(adapter.getIps()); |
|
|
|
|
DLNACastManager.getInstance().search(null, 3); |
|
|
|
|
DLNACastManager.INSTANCE.search(null); |
|
|
|
|
if (clear) adapter.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -143,27 +147,14 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
ScanActivity.start(getActivity()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onSuccess(Device item) { |
|
|
|
|
if (!item.isMobile()) listener.onCastTo(); |
|
|
|
|
private void onSuccess() { |
|
|
|
|
dismiss(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onError() { |
|
|
|
|
private void onFailure() { |
|
|
|
|
Notify.show(R.string.device_offline); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onSuccess(String result) { |
|
|
|
|
DLNACastManager.getInstance().play(); |
|
|
|
|
listener.onCastTo(); |
|
|
|
|
dismiss(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onFailed(String errMsg) { |
|
|
|
|
Notify.show(errMsg); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN) |
|
|
|
|
public void onScanEvent(ScanEvent event) { |
|
|
|
|
ScanTask.create(this).start(event.getAddress()); |
|
|
|
|
@ -175,53 +166,81 @@ public class CastDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDeviceAdded(org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
public void onDeviceAdded(@NonNull org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
adapter.addAll(CastDevice.get().add(device)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDeviceRemoved(org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
public void onDeviceRemoved(@NonNull org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
adapter.remove(CastDevice.get().remove(device)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDeviceUpdated(org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
public void onConnected(@NonNull org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
control.setAVTransportURI(video.getUrl(), video.getName(), this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onItemClick(Device item) { |
|
|
|
|
if (item.isDLNA()) DLNACastManager.getInstance().cast(CastDevice.get().find(item), video); |
|
|
|
|
else OkHttp.newCall(client, item.getIp().concat("/action?do=cast"), body.build()).enqueue(new Callback() { |
|
|
|
|
@Override |
|
|
|
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { |
|
|
|
|
boolean ok = response.body().string().equals("OK"); |
|
|
|
|
if (ok) App.post(() -> onSuccess(item)); |
|
|
|
|
else App.post(() -> onError()); |
|
|
|
|
} |
|
|
|
|
public void onDisconnected(@NonNull org.fourthline.cling.model.meta.Device<?, ?, ?> device) { |
|
|
|
|
onFailure(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull IOException e) { |
|
|
|
|
App.post(() -> onError()); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
@Override |
|
|
|
|
public void onSuccess(Unit unit) { |
|
|
|
|
control.play("1", null); |
|
|
|
|
onSuccess(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onLongClick(Device item) { |
|
|
|
|
return false; |
|
|
|
|
public void onFailure(@NonNull String s) { |
|
|
|
|
onFailure(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull IOException e) { |
|
|
|
|
App.post(this::onFailure); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { |
|
|
|
|
boolean ok = response.body().string().equals("OK"); |
|
|
|
|
if (ok) App.post(this::onSuccess); |
|
|
|
|
else App.post(this::onFailure); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onItemClick(Device item) { |
|
|
|
|
if (item.isDLNA()) control = DLNACastManager.INSTANCE.connectDevice(CastDevice.get().find(item), this); |
|
|
|
|
else OkHttp.newCall(client, item.getIp().concat("/action?do=cast"), body.build()).enqueue(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDestroyView() { |
|
|
|
|
super.onDestroyView(); |
|
|
|
|
CastDevice.get().clear(); |
|
|
|
|
EventBus.getDefault().unregister(this); |
|
|
|
|
DLNACastManager.getInstance().unregisterListener(this); |
|
|
|
|
DLNACastManager.getInstance().unregisterActionCallbacks(); |
|
|
|
|
DLNACastManager.getInstance().unbindCastService(App.get()); |
|
|
|
|
DLNACastManager.INSTANCE.unregisterListener(this); |
|
|
|
|
DLNACastManager.INSTANCE.unbindCastService(App.get()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onAvTransportStateChanged(@NonNull TransportState transportState) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onEventChanged(@NonNull EventedValue<?> eventedValue) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public interface Listener { |
|
|
|
|
@Override |
|
|
|
|
public void onRendererVolumeChanged(int i) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void onCastTo(); |
|
|
|
|
@Override |
|
|
|
|
public void onRendererVolumeMuteChanged(boolean b) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onLongClick(Device item) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|