Clean code and change cache path to tv
This commit is contained in:
parent
f75bb8a679
commit
a93edd1188
|
|
@ -73,18 +73,19 @@ public class AliYun {
|
|||
}
|
||||
|
||||
public File getUserCache() {
|
||||
return Path.cache("aliyundrive_user");
|
||||
return Path.tv("aliyundrive_user");
|
||||
}
|
||||
|
||||
public File getOAuthCache() {
|
||||
return Path.cache("aliyundrive_oauth");
|
||||
return Path.tv("aliyundrive_oauth");
|
||||
}
|
||||
|
||||
public File getDriveCache() {
|
||||
return Path.cache("aliyundrive_drive");
|
||||
return Path.tv("aliyundrive_drive");
|
||||
}
|
||||
|
||||
private AliYun() {
|
||||
Init.checkPermission();
|
||||
tempIds = new ArrayList<>();
|
||||
user = User.objectFrom(Path.read(getUserCache()));
|
||||
oauth = OAuth.objectFrom(Path.read(getOAuthCache()));
|
||||
|
|
@ -424,7 +425,7 @@ public class AliYun {
|
|||
|
||||
public String getMultiThreadedDownloadUrl(String shareId, String fileId) {
|
||||
String url = getDownloadUrl(shareId, fileId);
|
||||
url = MultiThread.proxyUrl(url, 20);
|
||||
url = MultiThread.proxyUrl(url, 2);
|
||||
return url;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ public class Bili extends Spider {
|
|||
}
|
||||
|
||||
private File getUserCache() {
|
||||
return Path.cache("bilibili_user");
|
||||
return Path.tv("bilibili_user");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package com.github.catvod.spider;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
|
|
@ -53,6 +55,16 @@ public class Init {
|
|||
get().handler.postDelayed(runnable, delay);
|
||||
}
|
||||
|
||||
public static void checkPermission() {
|
||||
try {
|
||||
Activity activity = Init.getActivity();
|
||||
if (activity == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
|
||||
activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9999);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Activity getActivity() throws Exception {
|
||||
Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
|
||||
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package com.github.catvod.spider;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
|
||||
import com.github.catvod.bean.Class;
|
||||
import com.github.catvod.bean.Result;
|
||||
|
|
@ -46,7 +44,7 @@ public class Market extends Spider {
|
|||
public void init(Context context, String extend) throws Exception {
|
||||
if (extend.startsWith("http")) extend = OkHttp.string(extend);
|
||||
datas = Data.arrayFrom(extend);
|
||||
checkPermission();
|
||||
Init.checkPermission();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -72,15 +70,6 @@ public class Market extends Spider {
|
|||
return Result.string(vod);
|
||||
}
|
||||
|
||||
private void checkPermission() {
|
||||
try {
|
||||
Activity activity = Init.getActivity();
|
||||
if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) activity.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9999);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void finish() {
|
||||
try {
|
||||
Activity activity = Init.getActivity();
|
||||
|
|
|
|||
|
|
@ -15,10 +15,6 @@ import java.util.zip.ZipFile;
|
|||
|
||||
public class FileUtil {
|
||||
|
||||
public static File getWall(int index) {
|
||||
return Path.files("wallpaper_" + index);
|
||||
}
|
||||
|
||||
public static void openFile(File file) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package com.github.catvod.utils;
|
||||
|
||||
import com.github.catvod.downloader.MultiThreadedMemoryDownloader;
|
||||
import com.github.catvod.spider.Proxy;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
|
|
@ -17,21 +16,14 @@ public class MultiThread {
|
|||
|
||||
public static Object[] proxy(Map<String, String> params) throws Exception {
|
||||
String url = params.get("url");
|
||||
int threadNum = Integer.parseInt(params.get("thread"));
|
||||
int thread = Integer.parseInt(params.get("thread"));
|
||||
Map<String, String> reqHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
if (entry.getKey() != null) {
|
||||
reqHeaders.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
for (String key : params.keySet()) reqHeaders.put(key, params.get(key));
|
||||
if (reqHeaders.containsKey("do")) reqHeaders.remove("do");
|
||||
if (reqHeaders.containsKey("url")) reqHeaders.remove("url");
|
||||
if (reqHeaders.containsKey("thread")) reqHeaders.remove("thread");
|
||||
MultiThreadedMemoryDownloader downloader = new MultiThreadedMemoryDownloader(url, reqHeaders, threadNum);
|
||||
MultiThreadedDownloader downloader = new MultiThreadedDownloader(url, reqHeaders, thread);
|
||||
NanoHTTPD.Response response = downloader.start();
|
||||
Object[] result = new Object[1];
|
||||
result[0] = response;
|
||||
return result;
|
||||
return new Object[]{response};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.github.catvod.downloader;
|
||||
package com.github.catvod.utils;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||
|
|
@ -21,7 +21,8 @@ import fi.iki.elonen.NanoHTTPD;
|
|||
import okhttp3.Response;
|
||||
|
||||
// 多线程内存下载器
|
||||
public class MultiThreadedMemoryDownloader {
|
||||
public class MultiThreadedDownloader {
|
||||
|
||||
private String url; // 资源URL
|
||||
private Map<String, String> headers; // HTTP Headers. 主要关注`Range-Bytes`这个字段.
|
||||
private int chunkSize = 1024 * 128; // 每个线程每轮下载的字节数.
|
||||
|
|
@ -36,7 +37,7 @@ public class MultiThreadedMemoryDownloader {
|
|||
private BlockingQueue<Chunk> readyChunkQueue = new LinkedBlockingQueue<>(); // 已开始下载的chunk队列(有序).
|
||||
private Lock lock = new ReentrantLock(); // 锁.
|
||||
|
||||
public MultiThreadedMemoryDownloader(String url, Map<String, String> headers, int numThreads) {
|
||||
public MultiThreadedDownloader(String url, Map<String, String> headers, int numThreads) {
|
||||
this.url = url;
|
||||
this.headers = headers;
|
||||
this.numThreads = numThreads;
|
||||
|
|
@ -115,7 +116,7 @@ public class MultiThreadedMemoryDownloader {
|
|||
if (hContentLength == null) {
|
||||
throw new Exception("missing response header: Content-Length");
|
||||
}
|
||||
Long contentLength = Long.parseLong(hContentLength);
|
||||
long contentLength = Long.parseLong(hContentLength);
|
||||
|
||||
// 尝试从Content-Range获取下载结束的偏移量
|
||||
if (this.endOffset <= 0) {
|
||||
|
|
@ -142,9 +143,7 @@ public class MultiThreadedMemoryDownloader {
|
|||
// 开启多线程下载
|
||||
this.running = true;
|
||||
for (int i = 0; i < this.numThreads; ++i) {
|
||||
new Thread(() -> {
|
||||
MultiThreadedMemoryDownloader.this.worker();
|
||||
}).start();
|
||||
new Thread(MultiThreadedDownloader.this::worker).start();
|
||||
}
|
||||
|
||||
// 构造response
|
||||
|
|
@ -153,7 +152,7 @@ public class MultiThreadedMemoryDownloader {
|
|||
NanoHTTPD.Response mResponse = newFixedLengthResponse(status, contentType, input, contentLength);
|
||||
for (String key : response.headers().names()) {
|
||||
String value = response.headers().get(key);
|
||||
if (key != null && !key.equalsIgnoreCase("Content-Type") && !key.equalsIgnoreCase("Content-Length")) {
|
||||
if (!key.equalsIgnoreCase("Content-Type") && !key.equalsIgnoreCase("Content-Length")) {
|
||||
mResponse.addHeader(key, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -296,11 +295,8 @@ public class MultiThreadedMemoryDownloader {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean shouldFilterRequestHeaderKey(String key) {
|
||||
if (key == null) {
|
||||
return true;
|
||||
}
|
||||
if (key == null) return true;
|
||||
key = key.toLowerCase();
|
||||
return key.equals("host") || key.equals("http-client-ip") || key.equals("remote-addr");
|
||||
}
|
||||
|
|
@ -309,9 +305,10 @@ public class MultiThreadedMemoryDownloader {
|
|||
this.running = false;
|
||||
}
|
||||
|
||||
private class Chunk {
|
||||
private long startOffset;
|
||||
private long endOffset;
|
||||
private static class Chunk {
|
||||
|
||||
private final long startOffset;
|
||||
private final long endOffset;
|
||||
private byte[] buffer;
|
||||
|
||||
public Chunk(long startOffset, long endOffset) {
|
||||
|
|
@ -3,8 +3,6 @@ package com.github.catvod.utils;
|
|||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.catvod.spider.Init;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
|
@ -24,52 +22,21 @@ public class Path {
|
|||
return file;
|
||||
}
|
||||
|
||||
public static boolean exists(String path) {
|
||||
return new File(path.replace("file://", "")).exists();
|
||||
public static File download() {
|
||||
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||
}
|
||||
|
||||
public static File root() {
|
||||
return Environment.getExternalStorageDirectory();
|
||||
}
|
||||
|
||||
public static File cache() {
|
||||
return Init.context().getCacheDir();
|
||||
public static File tv() {
|
||||
return check(new File(root() + File.separator + "TV"));
|
||||
}
|
||||
|
||||
public static File files() {
|
||||
return Init.context().getFilesDir();
|
||||
}
|
||||
|
||||
public static File download() {
|
||||
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||
}
|
||||
|
||||
public static String rootPath() {
|
||||
return root().getAbsolutePath();
|
||||
}
|
||||
|
||||
public static File root(String name) {
|
||||
return new File(root(), name);
|
||||
}
|
||||
|
||||
public static File root(String child, String name) {
|
||||
return new File(check(new File(root(), child)), name);
|
||||
}
|
||||
|
||||
public static File cache(String name) {
|
||||
return new File(cache(), name);
|
||||
}
|
||||
|
||||
public static File files(String name) {
|
||||
return new File(files(), name);
|
||||
}
|
||||
|
||||
public static String asset(String fileName) {
|
||||
try {
|
||||
return read(Init.context().getAssets().open(fileName));
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
public static File tv(String name) {
|
||||
if (!name.startsWith(".")) name = "." + name;
|
||||
return new File(tv(), name);
|
||||
}
|
||||
|
||||
public static String read(File file) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue