|
|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package com.fongmi.android.tv.ui.dialog; |
|
|
|
|
|
|
|
|
|
import android.content.res.TypedArray; |
|
|
|
|
import android.view.LayoutInflater; |
|
|
|
|
import android.view.View; |
|
|
|
|
import android.view.ViewGroup; |
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
|
@ -12,18 +14,20 @@ import androidx.viewbinding.ViewBinding; |
|
|
|
|
import com.fongmi.android.tv.App; |
|
|
|
|
import com.fongmi.android.tv.Constant; |
|
|
|
|
import com.fongmi.android.tv.R; |
|
|
|
|
import com.fongmi.android.tv.Setting; |
|
|
|
|
import com.fongmi.android.tv.api.config.VodConfig; |
|
|
|
|
import com.fongmi.android.tv.bean.Config; |
|
|
|
|
import com.fongmi.android.tv.bean.Device; |
|
|
|
|
import com.fongmi.android.tv.bean.History; |
|
|
|
|
import com.fongmi.android.tv.bean.Keep; |
|
|
|
|
import com.fongmi.android.tv.event.ScanEvent; |
|
|
|
|
import com.fongmi.android.tv.utils.ScanTask; |
|
|
|
|
import com.fongmi.android.tv.databinding.DialogDeviceBinding; |
|
|
|
|
import com.fongmi.android.tv.event.ScanEvent; |
|
|
|
|
import com.fongmi.android.tv.impl.Callback; |
|
|
|
|
import com.fongmi.android.tv.ui.activity.ScanActivity; |
|
|
|
|
import com.fongmi.android.tv.ui.adapter.DeviceAdapter; |
|
|
|
|
import com.fongmi.android.tv.utils.Notify; |
|
|
|
|
import com.fongmi.android.tv.utils.ResUtil; |
|
|
|
|
import com.fongmi.android.tv.utils.ScanTask; |
|
|
|
|
import com.github.catvod.net.OkHttp; |
|
|
|
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
|
|
|
|
|
|
|
|
|
@ -33,6 +37,7 @@ import org.greenrobot.eventbus.ThreadMode; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
|
|
import okhttp3.Call; |
|
|
|
|
import okhttp3.FormBody; |
|
|
|
|
@ -43,6 +48,7 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
private final FormBody.Builder body; |
|
|
|
|
private final OkHttpClient client; |
|
|
|
|
private final TypedArray mode; |
|
|
|
|
private DialogDeviceBinding binding; |
|
|
|
|
private DeviceAdapter adapter; |
|
|
|
|
private String type; |
|
|
|
|
@ -53,23 +59,22 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
public SyncDialog() { |
|
|
|
|
client = OkHttp.client(Constant.TIMEOUT_SYNC); |
|
|
|
|
mode = ResUtil.getTypedArray(R.array.cast_mode); |
|
|
|
|
body = new FormBody.Builder(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SyncDialog history() { |
|
|
|
|
type = "history"; |
|
|
|
|
body.add("device", Device.get().toString()); |
|
|
|
|
body.add("config", Config.vod().toString()); |
|
|
|
|
body.add("targets", App.gson().toJson(History.get())); |
|
|
|
|
if (VodConfig.getUrl() != null) body.add("url", VodConfig.getUrl()); |
|
|
|
|
return this; |
|
|
|
|
return type("history"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SyncDialog keep() { |
|
|
|
|
type = "keep"; |
|
|
|
|
body.add("device", Device.get().toString()); |
|
|
|
|
body.add("targets", App.gson().toJson(Keep.getVod())); |
|
|
|
|
body.add("configs", App.gson().toJson(Config.findUrls())); |
|
|
|
|
return this; |
|
|
|
|
return type("keep"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void show(FragmentActivity activity) { |
|
|
|
|
@ -77,6 +82,11 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
show(activity.getSupportFragmentManager(), null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private SyncDialog type(String type) { |
|
|
|
|
this.type = type; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected ViewBinding getBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) { |
|
|
|
|
return binding = DialogDeviceBinding.inflate(inflater, container, false); |
|
|
|
|
@ -84,13 +94,16 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void initView() { |
|
|
|
|
binding.mode.setVisibility(View.VISIBLE); |
|
|
|
|
EventBus.getDefault().register(this); |
|
|
|
|
setRecyclerView(); |
|
|
|
|
getDevice(); |
|
|
|
|
setMode(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void initEvent() { |
|
|
|
|
binding.mode.setOnClickListener(v -> onMode()); |
|
|
|
|
binding.scan.setOnClickListener(v -> onScan()); |
|
|
|
|
binding.refresh.setOnClickListener(v -> onRefresh()); |
|
|
|
|
} |
|
|
|
|
@ -105,21 +118,30 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
if (adapter.getItemCount() == 0) App.post(this::onRefresh, 1000); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onRefresh() { |
|
|
|
|
ScanTask.create(this).start(adapter.getIps()); |
|
|
|
|
adapter.clear(); |
|
|
|
|
private void setMode() { |
|
|
|
|
int index = Setting.getSyncMode(); |
|
|
|
|
binding.mode.setImageResource(mode.getResourceId(index, 0)); |
|
|
|
|
binding.mode.setTag(String.valueOf(index)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onMode() { |
|
|
|
|
int index = Setting.getSyncMode(); |
|
|
|
|
Setting.putSyncMode(index = index == mode.length() - 1 ? 0 : ++index); |
|
|
|
|
binding.mode.setImageResource(mode.getResourceId(index, 0)); |
|
|
|
|
binding.mode.setTag(String.valueOf(index)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onScan() { |
|
|
|
|
ScanActivity.start(getActivity()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onSuccess() { |
|
|
|
|
dismiss(); |
|
|
|
|
private void onRefresh() { |
|
|
|
|
ScanTask.create(this).start(adapter.getIps()); |
|
|
|
|
adapter.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void onError() { |
|
|
|
|
Notify.show(R.string.device_offline); |
|
|
|
|
private void onSuccess() { |
|
|
|
|
dismiss(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN) |
|
|
|
|
@ -134,12 +156,16 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onItemClick(Device item) { |
|
|
|
|
OkHttp.newCall(client, item.getIp().concat("/action?do=sync&mode=0&type=").concat(type), body.build()).enqueue(getCallback()); |
|
|
|
|
OkHttp.newCall(client, String.format(Locale.getDefault(), "%s/action?do=sync&mode=%s&type=%s", item.getIp(), binding.mode.getTag().toString(), type), body.build()).enqueue(getCallback()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onLongClick(Device item) { |
|
|
|
|
OkHttp.newCall(client, item.getIp().concat("/action?do=sync&mode=1&type=").concat(type), body.build()).enqueue(getCallback()); |
|
|
|
|
String mode = binding.mode.getTag().toString(); |
|
|
|
|
if (mode.equals("0")) return false; |
|
|
|
|
if (mode.equals("2") && type.equals("keep")) Keep.deleteAll(); |
|
|
|
|
if (mode.equals("2") && type.equals("history")) History.delete(VodConfig.getCid()); |
|
|
|
|
OkHttp.newCall(client, String.format(Locale.getDefault(), "%s/action?do=sync&mode=%s&type=%s&force=true", item.getIp(), binding.mode.getTag().toString(), type), body.build()).enqueue(getCallback()); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -152,7 +178,7 @@ public class SyncDialog extends BaseDialog implements DeviceAdapter.OnClickListe |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull IOException e) { |
|
|
|
|
App.post(() -> onError()); |
|
|
|
|
App.post(() -> Notify.show(e.getMessage())); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|