Commit 872f3b82 by amateur

1

1 parent ec604a6a
......@@ -3,6 +3,10 @@ package com.polysoft.nafmii.utils;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import java.io.IOException;
import java.io.InputStream;
public class CtxUtils {
......@@ -17,6 +21,14 @@ public class CtxUtils {
return CtxUtils.application;
}
public static AssetManager getAssetsManager() {
return getApplication().getAssets();
}
public static InputStream openAssetsFile(String fileName) throws IOException {
return getAssetsManager().open(fileName);
}
public static PackageInfo getPackageInfo() {
PackageManager manager = getApplication().getPackageManager();
......
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.polysoft.nafmii.utils;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ExecutorUtils {
private static final ExecutorsInstance INSTANCE = new ExecutorsInstance();
public static void io(Runnable runnable) {
INSTANCE.mDiskIO.execute(runnable);
}
public static void net(Runnable runnable) {
INSTANCE.mNetworkIO.execute(runnable);
}
public static void main(Runnable runnable) {
INSTANCE.mMainThread.execute(runnable);
}
private static class ExecutorsInstance {
private final Executor mDiskIO;
private final Executor mNetworkIO;
private final Executor mMainThread;
public ExecutorsInstance() {
this.mDiskIO = Executors.newSingleThreadExecutor();
this.mNetworkIO = Executors.newFixedThreadPool(3);
this.mMainThread = new MainThreadExecutor();
}
}
private static class MainThreadExecutor implements Executor {
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
@Override
public void execute(@NonNull Runnable command) {
mainThreadHandler.post(command);
}
}
}
......@@ -31,4 +31,13 @@ public class FileUtils {
public static File getRootCacheDir() {
return CtxUtils.getApplication().getCacheDir();
}
public static void deletes(File file) {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
deletes(f);
}
}
file.delete();
}
}
package com.polysoft.nafmii.utils;
import android.net.Uri;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipUtils {
public static void unZipDistAssets() {
// 已经存在,则不解压
if (FileUtils.getIndex().exists()) {
return;
}
File distDir = FileUtils.getRootDir();
InputStream is = null;
try {
is = CtxUtils.openAssetsFile("dist.zip");
unZip(is, distDir);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.close(is);
}
}
public static void unZip(File zipFile, File outDir) {
FileInputStream fis = null;
try {
fis = new FileInputStream(zipFile);
unZip(fis, outDir);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.close(fis);
}
}
public static void unZip(InputStream is, File outDir) throws IOException {
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry = null;
while (null != (entry = zis.getNextEntry())) {
unZipEntry(zis, entry, outDir);
}
}
private static void unZipEntry(ZipInputStream zis, ZipEntry entry, File outDir) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(outDir, entryName);
file.mkdirs();
return;
}
File file = new File(outDir, entryName);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = null;
try {
file.createNewFile();
fos = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int len = -1;
while ((len = zis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
fos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.close(fos);
}
}
}
......@@ -3,6 +3,8 @@ package com.whaty.nafmii;
import android.app.Application;
import com.polysoft.nafmii.utils.CtxUtils;
import com.polysoft.nafmii.utils.ExecutorUtils;
import com.polysoft.nafmii.utils.ZipUtils;
import me.jessyan.autosize.AutoSizeConfig;
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptor;
......@@ -23,5 +25,8 @@ public class MyApplication extends Application {
WebViewCacheInterceptorInst.getInstance().init(builder);
ExecutorUtils.io(() -> {
ZipUtils.unZipDistAssets();
});
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!