mirror of https://github.com/FongMi/TV.git
parent
c396278a8a
commit
8f97f2a32d
@ -1,44 +0,0 @@ |
||||
package com.fongmi.android.tv; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import okhttp3.Request; |
||||
|
||||
public class Constant { |
||||
|
||||
public static final String A = "https://raw.githubusercontent.com/"; |
||||
public static final String B = "https://raw.githubusercontents.com/"; |
||||
public static final String C = "https://ghproxy.com/"; |
||||
public static final String D = "https://raw.iqiq.io/"; |
||||
public static final String E = "https://raw.fastgit.org/"; |
||||
public static final String REPO = "FongMi/TV/"; |
||||
public static final String RELEASE = "release"; |
||||
private static final int TIME = 2; |
||||
|
||||
private static String getProxy() { |
||||
if (isOk(A)) return A + REPO; |
||||
if (isOk(B)) return B + REPO; |
||||
if (isOk(C)) return C + A + REPO; |
||||
if (isOk(D)) return D + REPO; |
||||
if (isOk(E)) return E + REPO; |
||||
return ""; |
||||
} |
||||
|
||||
private static boolean isOk(String url) { |
||||
try { |
||||
return new OkHttpClient.Builder().connectTimeout(TIME, TimeUnit.SECONDS).build().newCall(new Request.Builder().url(url).build()).execute().code() == 200; |
||||
} catch (IOException e) { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public static String getReleasePath(String path) { |
||||
return getProxy() + RELEASE + path; |
||||
} |
||||
|
||||
public static String getBranchPath(String branch, String path) { |
||||
return getProxy() + branch + path; |
||||
} |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
package com.fongmi.android.tv; |
||||
|
||||
import android.text.TextUtils; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import okhttp3.OkHttpClient; |
||||
import okhttp3.Request; |
||||
|
||||
public class Github { |
||||
|
||||
public static final String A = "https://raw.githubusercontent.com/"; |
||||
public static final String B = "https://ghproxy.com/"; |
||||
public static final String C = "https://raw.iqiq.io/"; |
||||
public static final String REPO = "FongMi/TV/"; |
||||
public static final String RELEASE = "release"; |
||||
public static final int TIME = 5; |
||||
|
||||
private String proxy; |
||||
|
||||
private static class Loader { |
||||
static volatile Github INSTANCE = new Github(); |
||||
} |
||||
|
||||
public static Github get() { |
||||
return Loader.INSTANCE; |
||||
} |
||||
|
||||
public Github() { |
||||
check(A); |
||||
check(B); |
||||
check(C); |
||||
} |
||||
|
||||
private void check(String url) { |
||||
try { |
||||
if (getProxy().length() > 0) return; |
||||
int code = new OkHttpClient.Builder().connectTimeout(TIME, TimeUnit.SECONDS).build().newCall(new Request.Builder().url(url).build()).execute().code(); |
||||
if (code == 200) setProxy(url); |
||||
} catch (IOException ignored) { |
||||
} |
||||
} |
||||
|
||||
private void setProxy(String url) { |
||||
this.proxy = url.equals(B) ? url + A + REPO : url + REPO; |
||||
} |
||||
|
||||
private String getProxy() { |
||||
return TextUtils.isEmpty(proxy) ? "" : proxy; |
||||
} |
||||
|
||||
public String getReleasePath(String path) { |
||||
return getProxy() + RELEASE + path; |
||||
} |
||||
|
||||
public String getBranchPath(String branch, String path) { |
||||
return getProxy() + branch + path; |
||||
} |
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
package com.fongmi.android.tv.net; |
||||
|
||||
import com.fongmi.android.tv.App; |
||||
import com.fongmi.android.tv.utils.FileUtil; |
||||
|
||||
import java.io.BufferedInputStream; |
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.util.Objects; |
||||
|
||||
import okhttp3.Response; |
||||
|
||||
public class Download { |
||||
|
||||
private final File file; |
||||
private final String url; |
||||
private final Callback callback; |
||||
|
||||
public static Download create(String url, File file, Callback callback) { |
||||
return new Download(url, file, callback); |
||||
} |
||||
|
||||
public Download(String url, File file, Callback callback) { |
||||
this.url = url; |
||||
this.file = file; |
||||
this.callback = callback; |
||||
} |
||||
|
||||
public void start() { |
||||
App.execute(this::doInBackground); |
||||
} |
||||
|
||||
private void doInBackground() { |
||||
try { |
||||
FileUtil.clearDir(file); |
||||
Response response = OkHttp.newCall(url).execute(); |
||||
download(response.body().byteStream(), Double.parseDouble(Objects.requireNonNull(response.header("Content-Length", "1")))); |
||||
App.post(callback::success); |
||||
} catch (Exception e) { |
||||
App.post(callback::error); |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void download(InputStream is, double length) throws Exception { |
||||
FileOutputStream os = new FileOutputStream(file); |
||||
try (BufferedInputStream input = new BufferedInputStream(is)) { |
||||
byte[] buffer = new byte[4096]; |
||||
int readBytes; |
||||
long totalBytes = 0; |
||||
while ((readBytes = input.read(buffer)) != -1) { |
||||
totalBytes += readBytes; |
||||
os.write(buffer, 0, readBytes); |
||||
int progress = (int) (totalBytes / length * 100.0); |
||||
App.post(() -> callback.progress(progress)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue