Change the way of action pefrom(no longer wait for event), add loading jar file support, fix simple action memory leak

This commit is contained in:
hyb1996
2017-04-05 22:35:31 +08:00
parent b762ceb60b
commit 6844eb9841
88 changed files with 1797 additions and 832 deletions

View File

@@ -1,6 +1,7 @@
package com.stardust.pio;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import java.io.File;
@@ -239,4 +240,12 @@ public class PFile {
}
return file.delete();
}
public static String readAsset(AssetManager assets, String path) {
try {
return read(assets.open(path));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@@ -1,8 +1,12 @@
package com.stardust.util;
import android.app.Activity;
import android.content.res.AssetManager;
import android.support.v4.app.FragmentActivity;
import com.stardust.pio.PFile;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -13,15 +17,20 @@ import java.util.Map;
public class AssetsCache {
private Map<String, String> mCache = new HashMap<>();
private static final long PERSIST_TIME = 5 * 60 * 1000;
private static SimpleCache<String> cache = new SimpleCache<>(PERSIST_TIME, 5, 30 * 1000);
public String read(AssetManager manager, String path) throws IOException {
String str = mCache.get(path);
if (str == null) {
str = FileUtils.readString(manager.open(path));
mCache.put(path, str);
}
return str;
public static String get(final AssetManager assetManager, final String path) {
return cache.get(path, new SimpleCache.Supplier<String>() {
@Override
public String get(String key) {
return PFile.readAsset(assetManager, path);
}
});
}
public static String get(final Activity activity, final String path) {
return get(activity.getAssets(), path);
}
}

View File

@@ -1,9 +1,11 @@
package com.stardust.util;
import android.support.annotation.Keep;
/**
* Created by Stardust on 2017/3/10.
*/
@Keep
public interface Consumer<T> {
void accept(T t);

View File

@@ -1,8 +1,10 @@
package com.stardust.util;
import java.io.File;
import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
/**
* Created by Stardust on 2017/3/31.
@@ -11,12 +13,13 @@ import java.util.Comparator;
public class FileSorter {
public static void sort(File[] files) {
final Collator collator = Collator.getInstance();
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
if (o1.isDirectory() != o2.isDirectory())
return o1.isDirectory() ? Integer.MIN_VALUE : Integer.MAX_VALUE;
return o1.getName().compareTo(o2.getName());
return collator.compare(o1.getName(), o2.getName());
}
});
}

View File

@@ -1,263 +0,0 @@
package com.stardust.util;
import android.content.Context;
import android.database.Cursor;
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.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
/**
* Created by Stardust on 2017/1/23.
*/
public class FileUtils {
private static final int BUFFER_SIZE = 1024 * 100;
public static String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {"_data"};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception ignored) {
} finally {
if (cursor != null)
cursor.close();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getPath(InputStream inputStream) {
if (inputStream instanceof FileInputStream) {
FileInputStream fis = (FileInputStream) inputStream;
try {
Field field = fis.getClass().getDeclaredField("path");
field.setAccessible(true);
return (String) field.get(fis);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
return null;
}
public static boolean createFileIfNotExists(String path) {
ensureFolder(path);
File file = new File(path);
if (!file.exists()) {
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private static boolean ensureFolder(String path) {
int i = path.lastIndexOf("\\");
if (i < 0)
i = path.lastIndexOf("/");
if (i >= 0) {
String folder = path.substring(0, i);
File file = new File(folder);
if (file.exists())
return true;
return file.mkdirs();
} else {
return false;
}
}
public static String readString(File file, String encoding) {
try {
return readString(new FileInputStream(file), encoding);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static String readString(File file) {
return readString(file, "utf-8");
}
public static String readString(InputStream is, String encoding) {
try {
byte[] bytes = new byte[is.available()];
is.read(bytes);
return new String(bytes, encoding);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static String readString(InputStream inputStream) {
return readString(inputStream, "utf-8");
}
public static boolean copy(Context context, int rawId, String path) {
InputStream is = context.getResources().openRawResource(rawId);
return copy(is, path);
}
public static boolean copy(InputStream is, String path) {
if (!ensureFolder(path))
return false;
File file = new File(path);
try {
if (!file.exists())
if (!file.createNewFile())
return false;
FileOutputStream fos = new FileOutputStream(file);
return copy(is, fos);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static boolean copy(InputStream is, OutputStream os) {
byte[] buffer = new byte[BUFFER_SIZE];
try {
while (is.available() > 0) {
int n = is.read(buffer);
os.write(buffer, 0, n);
}
is.close();
os.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static boolean copy(String pathFrom, String pathTo) {
try {
return copy(new FileInputStream(pathFrom), pathTo);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static boolean copyAsset(Context context, String assetFile, String path) {
try {
return copy(context.getAssets().open(assetFile), path);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static String renameWithoutExtension(String path, String newName) {
File file = new File(path);
File newFile = new File(file.getParent(), newName + "." + getExtension(file.getName()));
file.renameTo(newFile);
return newFile.getAbsolutePath();
}
public static String getExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i < 0)
return "";
return fileName.substring(i + 1);
}
public static boolean writeString(String path, String text) {
return writeString(new File(path), text);
}
public static boolean writeString(File file, String text) {
try {
return writeString(new FileOutputStream(file), text);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static boolean writeString(OutputStream outputStream, String text) {
try {
outputStream.write(text.getBytes());
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static String generateNotExistingPath(String path, String extension) {
if (!new File(path + extension).exists())
return path + extension;
int i = 0;
while (true) {
String pathI = path + "(" + i + ")" + extension;
if (!new File(pathI).exists())
return pathI;
i++;
}
}
public static String getNameWithoutExtension(String fileName) {
int a = fileName.lastIndexOf('/');
if (a < 0)
a = fileName.lastIndexOf('\\');
int b = fileName.lastIndexOf('.');
if (a < 0)
a = -1;
if (b < 0)
b = fileName.length();
fileName = fileName.substring(a + 1, b);
return fileName;
}
public static File copyAssetToTmpFile(Context context, String path) {
String extension = getExtension(path);
String name = getNameWithoutExtension(path);
if (name.length() < 5) {
name += name.hashCode();
}
try {
File tmpFile = File.createTempFile(name, "." + extension, context.getCacheDir());
copyAsset(context, path, tmpFile.getPath());
return tmpFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static boolean deleteAll(File file) {
if (file.isFile())
return file.delete();
for (File child : file.listFiles()) {
if (!deleteAll(child))
return false;
}
return file.delete();
}
}

View File

@@ -0,0 +1,118 @@
package com.stardust.util;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Stardust on 2017/4/5.
*/
public class SimpleCache<T> {
public interface Supplier<T> {
T get(String key);
}
private long mPersistTime;
private LimitedHashMap<String, Item<T>> mCache;
private Timer mCacheCheckTimer;
private Supplier<T> mSupplier;
public SimpleCache(long persistTime, int cacheSize, long checkInterval, Supplier<T> supplier) {
mPersistTime = persistTime;
mCache = new LimitedHashMap<>(cacheSize);
mSupplier = supplier == null ? new NullSupplier<T>() : supplier;
mCacheCheckTimer = new Timer();
startCacheCheck(checkInterval);
}
public SimpleCache(long persistTime, int cacheSize, long checkInterval) {
this(persistTime, cacheSize, checkInterval, null);
}
public synchronized void put(String key, T value) {
mCache.put(key, new Item<>(value));
}
public synchronized T get(String key) {
Item<T> item = mCache.get(key);
if (item == null) {
T value = mSupplier.get(key);
if (value != null) {
put(key, value);
}
return value;
}
return item.value;
}
public T get(String key, T defaultValue) {
T value = get(key);
if (value == null) {
value = defaultValue;
}
return value;
}
public T get(String key, Supplier<T> supplier) {
T value = get(key);
if (value == null) {
value = supplier.get(key);
put(key, value);
}
return value;
}
public synchronized void destroy() {
mCacheCheckTimer.cancel();
mCache.clear();
}
private void startCacheCheck(long checkInterval) {
mCacheCheckTimer.schedule(new TimerTask() {
@Override
public void run() {
checkCache();
}
}, 0, checkInterval);
}
private synchronized void checkCache() {
Iterator<Map.Entry<String, Item<T>>> iterator = mCache.entrySet().iterator();
while (iterator.hasNext()) {
if (!iterator.next().getValue().isValid()) {
iterator.remove();
}
}
}
private class Item<T> {
T value;
private long mSaveMillis;
Item(T value) {
mSaveMillis = System.currentTimeMillis();
this.value = value;
}
boolean isValid() {
return System.currentTimeMillis() - mSaveMillis <= mPersistTime;
}
}
private static class NullSupplier<T> implements Supplier<T> {
@Override
public T get(String key) {
return null;
}
}
}