mirror of https://github.com/FongMi/TV.git
parent
5474a1af50
commit
a533d0e0ce
@ -0,0 +1,52 @@ |
||||
package com.fongmi.android.tv.model; |
||||
|
||||
import androidx.lifecycle.MutableLiveData; |
||||
import androidx.lifecycle.ViewModel; |
||||
|
||||
import com.fongmi.android.tv.bean.Channel; |
||||
import com.fongmi.android.tv.player.source.Force; |
||||
|
||||
import java.util.concurrent.Callable; |
||||
import java.util.concurrent.ExecutorService; |
||||
import java.util.concurrent.Executors; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
public class LiveViewModel extends ViewModel { |
||||
|
||||
public MutableLiveData<Channel> result; |
||||
public ExecutorService executor; |
||||
|
||||
public LiveViewModel() { |
||||
this.result = new MutableLiveData<>(); |
||||
} |
||||
|
||||
public MutableLiveData<Channel> getResult() { |
||||
return result; |
||||
} |
||||
|
||||
public void getUrl(Channel item) { |
||||
execute(() -> { |
||||
String url = item.getUrls().get(item.getLine()); |
||||
if (item.isForce()) item.setUrl(Force.get().fetch(url)); |
||||
else item.setUrl(url); |
||||
return item; |
||||
}); |
||||
} |
||||
|
||||
private void execute(Callable<Channel> callable) { |
||||
if (executor != null) executor.shutdownNow(); |
||||
executor = Executors.newFixedThreadPool(2); |
||||
executor.execute(() -> { |
||||
try { |
||||
if (!Thread.interrupted()) result.postValue(executor.submit(callable).get(5, TimeUnit.SECONDS)); |
||||
} catch (Throwable e) { |
||||
e.printStackTrace(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
protected void onCleared() { |
||||
if (executor != null) executor.shutdownNow(); |
||||
} |
||||
} |
||||
@ -0,0 +1,70 @@ |
||||
package com.fongmi.android.tv.player.source; |
||||
|
||||
import android.content.ComponentName; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.content.ServiceConnection; |
||||
import android.net.Uri; |
||||
import android.os.IBinder; |
||||
|
||||
import com.fongmi.android.tv.App; |
||||
import com.fongmi.android.tv.net.OKHttp; |
||||
import com.forcetech.Port; |
||||
import com.forcetech.service.ForceService; |
||||
import com.gsoft.mitv.MainActivity; |
||||
|
||||
import okhttp3.Headers; |
||||
|
||||
public class Force { |
||||
|
||||
private boolean init; |
||||
|
||||
private static class Loader { |
||||
static volatile Force INSTANCE = new Force(); |
||||
} |
||||
|
||||
public static Force get() { |
||||
return Loader.INSTANCE; |
||||
} |
||||
|
||||
private void init() { |
||||
App.get().bindService(new Intent(App.get(), MainActivity.class), mConn, Context.BIND_AUTO_CREATE); |
||||
App.get().bindService(new Intent(App.get(), ForceService.class), mConn, Context.BIND_AUTO_CREATE); |
||||
init = true; |
||||
} |
||||
|
||||
public void destroy() { |
||||
try { |
||||
if (init) App.get().unbindService(mConn); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
public String fetch(String url) { |
||||
try { |
||||
if (!init) init(); |
||||
int port = Port.get(url); |
||||
Uri uri = Uri.parse(url); |
||||
String id = uri.getLastPathSegment(); |
||||
String cmd = "http://127.0.0.1:" + port + "/cmd.xml?cmd=switch_chan&server=" + uri.getHost() + ":" + uri.getPort() + "&id=" + id; |
||||
String result = "http://127.0.0.1:" + port + "/" + id; |
||||
OKHttp.newCall(cmd, Headers.of("user-agent", "MTV")).execute(); |
||||
return result; |
||||
} catch (Exception e) { |
||||
return url; |
||||
} |
||||
} |
||||
|
||||
private final ServiceConnection mConn = new ServiceConnection() { |
||||
@Override |
||||
public void onServiceConnected(ComponentName name, IBinder service) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onServiceDisconnected(ComponentName name) { |
||||
|
||||
} |
||||
}; |
||||
} |
||||
Loading…
Reference in new issue