Import fongmi Path
This commit is contained in:
parent
4458b55d36
commit
8d41b0d323
|
|
@ -32,7 +32,7 @@ import com.github.catvod.net.OkHttp;
|
||||||
import com.github.catvod.net.OkResult;
|
import com.github.catvod.net.OkResult;
|
||||||
import com.github.catvod.spider.Init;
|
import com.github.catvod.spider.Init;
|
||||||
import com.github.catvod.spider.Proxy;
|
import com.github.catvod.spider.Proxy;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.Path;
|
||||||
import com.github.catvod.utils.QRCode;
|
import com.github.catvod.utils.QRCode;
|
||||||
import com.github.catvod.utils.Utils;
|
import com.github.catvod.utils.Utils;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
@ -72,22 +72,22 @@ public class AliYun {
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getUserCache() {
|
public File getUserCache() {
|
||||||
return FileUtil.cache("aliyundrive_user");
|
return Path.cache("aliyundrive_user");
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getOAuthCache() {
|
public File getOAuthCache() {
|
||||||
return FileUtil.cache("aliyundrive_oauth");
|
return Path.cache("aliyundrive_oauth");
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getDriveCache() {
|
public File getDriveCache() {
|
||||||
return FileUtil.cache("aliyundrive_drive");
|
return Path.cache("aliyundrive_drive");
|
||||||
}
|
}
|
||||||
|
|
||||||
private AliYun() {
|
private AliYun() {
|
||||||
tempIds = new ArrayList<>();
|
tempIds = new ArrayList<>();
|
||||||
user = User.objectFrom(FileUtil.read(getUserCache()));
|
user = User.objectFrom(Path.read(getUserCache()));
|
||||||
oauth = OAuth.objectFrom(FileUtil.read(getOAuthCache()));
|
oauth = OAuth.objectFrom(Path.read(getOAuthCache()));
|
||||||
drive = Drive.objectFrom(FileUtil.read(getDriveCache()));
|
drive = Drive.objectFrom(Path.read(getDriveCache()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRefreshToken(String token) {
|
public void setRefreshToken(String token) {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.catvod.api.AliYun;
|
import com.github.catvod.api.AliYun;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.FileUtil;
|
||||||
|
import com.github.catvod.utils.Path;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
|
@ -38,7 +39,7 @@ public class Drive {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Drive save() {
|
public Drive save() {
|
||||||
FileUtil.write(AliYun.get().getDriveCache(), toString());
|
Path.write(AliYun.get().getDriveCache(), toString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.catvod.api.AliYun;
|
import com.github.catvod.api.AliYun;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.FileUtil;
|
||||||
|
import com.github.catvod.utils.Path;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
|
@ -44,7 +45,7 @@ public class OAuth {
|
||||||
}
|
}
|
||||||
|
|
||||||
public OAuth save() {
|
public OAuth save() {
|
||||||
FileUtil.write(AliYun.get().getOAuthCache(), toString());
|
Path.write(AliYun.get().getOAuthCache(), toString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.catvod.api.AliYun;
|
import com.github.catvod.api.AliYun;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.FileUtil;
|
||||||
|
import com.github.catvod.utils.Path;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
|
@ -64,7 +65,7 @@ public class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
public User save() {
|
public User save() {
|
||||||
FileUtil.write(AliYun.get().getUserCache(), toString());
|
Path.write(AliYun.get().getUserCache(), toString());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import com.github.catvod.bean.bili.Page;
|
||||||
import com.github.catvod.bean.bili.Resp;
|
import com.github.catvod.bean.bili.Resp;
|
||||||
import com.github.catvod.crawler.Spider;
|
import com.github.catvod.crawler.Spider;
|
||||||
import com.github.catvod.net.OkHttp;
|
import com.github.catvod.net.OkHttp;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.Path;
|
||||||
import com.github.catvod.utils.Utils;
|
import com.github.catvod.utils.Utils;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
@ -66,7 +66,7 @@ public class Bili extends Spider {
|
||||||
private void setCookie() {
|
private void setCookie() {
|
||||||
cookie = extend.get("cookie").getAsString();
|
cookie = extend.get("cookie").getAsString();
|
||||||
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie).trim();
|
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie).trim();
|
||||||
if (TextUtils.isEmpty(cookie)) cookie = FileUtil.read(getUserCache());
|
if (TextUtils.isEmpty(cookie)) cookie = Path.read(getUserCache());
|
||||||
if (TextUtils.isEmpty(cookie)) cookie = COOKIE;
|
if (TextUtils.isEmpty(cookie)) cookie = COOKIE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@ public class Bili extends Spider {
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getUserCache() {
|
private File getUserCache() {
|
||||||
return FileUtil.cache("bilibili_user");
|
return Path.cache("bilibili_user");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import com.github.catvod.bean.market.Data;
|
||||||
import com.github.catvod.crawler.Spider;
|
import com.github.catvod.crawler.Spider;
|
||||||
import com.github.catvod.net.OkHttp;
|
import com.github.catvod.net.OkHttp;
|
||||||
import com.github.catvod.utils.FileUtil;
|
import com.github.catvod.utils.FileUtil;
|
||||||
|
import com.github.catvod.utils.Path;
|
||||||
import com.github.catvod.utils.Utils;
|
import com.github.catvod.utils.Utils;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
|
|
@ -82,9 +83,9 @@ public class Market extends Spider {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
Init.run(this::setDialog, 500);
|
Init.run(this::setDialog, 500);
|
||||||
Response response = OkHttp.newCall(url);
|
Response response = OkHttp.newCall(url);
|
||||||
File file = new File(FileUtil.download(), Uri.parse(url).getLastPathSegment());
|
File file = new File(Path.download(), Uri.parse(url).getLastPathSegment());
|
||||||
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1")));
|
download(file, response.body().byteStream(), Double.parseDouble(response.header("Content-Length", "1")));
|
||||||
if (file.getName().endsWith(".apk")) FileUtil.openFile(FileUtil.chmod(file));
|
if (file.getName().endsWith(".apk")) FileUtil.openFile(Path.chmod(file));
|
||||||
else Utils.notify("下載完成");
|
else Utils.notify("下載完成");
|
||||||
dismiss();
|
dismiss();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
|
|
@ -3,77 +3,20 @@ package com.github.catvod.utils;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Environment;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.catvod.spider.Init;
|
import com.github.catvod.spider.Init;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
|
|
||||||
public static File cache() {
|
public static File getWall(int index) {
|
||||||
return Init.context().getCacheDir();
|
return Path.files("wallpaper_" + index);
|
||||||
}
|
|
||||||
|
|
||||||
public static File cache(String name) {
|
|
||||||
return new File(cache(), name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File download() {
|
|
||||||
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void write(File file, String data) {
|
|
||||||
write(file, data.getBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void write(File file, byte[] data) {
|
|
||||||
try {
|
|
||||||
FileOutputStream fos = new FileOutputStream(file);
|
|
||||||
fos.write(data);
|
|
||||||
fos.flush();
|
|
||||||
fos.close();
|
|
||||||
chmod(file);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File chmod(File file) {
|
|
||||||
try {
|
|
||||||
Process process = Runtime.getRuntime().exec("chmod 777 " + file);
|
|
||||||
process.waitFor();
|
|
||||||
return file;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String read(File file) {
|
|
||||||
try {
|
|
||||||
return read(new FileInputStream(file));
|
|
||||||
} catch (Exception e) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String read(InputStream is) {
|
|
||||||
try {
|
|
||||||
byte[] data = new byte[is.available()];
|
|
||||||
is.read(data);
|
|
||||||
is.close();
|
|
||||||
return new String(data, "UTF-8");
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openFile(File file) {
|
public static void openFile(File file) {
|
||||||
|
|
@ -84,12 +27,26 @@ public class FileUtil {
|
||||||
Init.context().startActivity(intent);
|
Init.context().startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getMimeType(String fileName) {
|
public static void unzip(File target, File path) {
|
||||||
String mimeType = URLConnection.guessContentTypeFromName(fileName);
|
try (ZipFile zip = new ZipFile(target.getAbsolutePath())) {
|
||||||
return TextUtils.isEmpty(mimeType) ? "*/*" : mimeType;
|
Enumeration<?> entries = zip.entries();
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
ZipEntry entry = (ZipEntry) entries.nextElement();
|
||||||
|
File out = new File(path, entry.getName());
|
||||||
|
if (entry.isDirectory()) out.mkdirs();
|
||||||
|
else Path.copy(zip.getInputStream(entry), out);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Uri getShareUri(File file) {
|
private static Uri getShareUri(File file) {
|
||||||
return Build.VERSION.SDK_INT < Build.VERSION_CODES.N ? Uri.fromFile(file) : FileProvider.getUriForFile(Init.context(), Init.context().getPackageName() + ".provider", file);
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.N ? Uri.fromFile(file) : FileProvider.getUriForFile(Init.context(), Init.context().getPackageName() + ".provider", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getMimeType(String fileName) {
|
||||||
|
String mimeType = URLConnection.guessContentTypeFromName(fileName);
|
||||||
|
return TextUtils.isEmpty(mimeType) ? "*/*" : mimeType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,168 @@
|
||||||
|
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;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Path {
|
||||||
|
|
||||||
|
private static final String TAG = Path.class.getSimpleName();
|
||||||
|
|
||||||
|
private static File check(File file) {
|
||||||
|
if (!file.exists()) file.mkdirs();
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean exists(String path) {
|
||||||
|
return new File(path.replace("file://", "")).exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File root() {
|
||||||
|
return Environment.getExternalStorageDirectory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File cache() {
|
||||||
|
return Init.context().getCacheDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 String read(File file) {
|
||||||
|
try {
|
||||||
|
return read(new FileInputStream(file));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String read(String path) {
|
||||||
|
try {
|
||||||
|
return read(new FileInputStream(path));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String read(InputStream is) {
|
||||||
|
try {
|
||||||
|
byte[] data = new byte[is.available()];
|
||||||
|
is.read(data);
|
||||||
|
is.close();
|
||||||
|
return new String(data, "UTF-8");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File write(File file, String data) {
|
||||||
|
return write(file, data.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File write(File file, byte[] data) {
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream(file);
|
||||||
|
fos.write(data);
|
||||||
|
fos.flush();
|
||||||
|
fos.close();
|
||||||
|
chmod(file);
|
||||||
|
return file;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void move(File in, File out) {
|
||||||
|
copy(in, out);
|
||||||
|
clear(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copy(File in, File out) {
|
||||||
|
try {
|
||||||
|
copy(new FileInputStream(in), new FileOutputStream(out));
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copy(InputStream in, File out) {
|
||||||
|
try {
|
||||||
|
copy(in, new FileOutputStream(out));
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int amountRead;
|
||||||
|
while ((amountRead = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, amountRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<File> list(File dir) {
|
||||||
|
File[] files = dir.listFiles();
|
||||||
|
return files == null ? Collections.emptyList() : Arrays.asList(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear(File dir) {
|
||||||
|
if (dir == null) return;
|
||||||
|
if (dir.isDirectory()) for (File file : list(dir)) clear(file);
|
||||||
|
if (dir.delete()) Log.d(TAG, "Deleted:" + dir.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static File chmod(File file) {
|
||||||
|
try {
|
||||||
|
Process process = Runtime.getRuntime().exec("chmod 777 " + file);
|
||||||
|
process.waitFor();
|
||||||
|
return file;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue