Skip to content

Commit

Permalink
v0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
郑晓勇 committed May 23, 2017
1 parent 0c9133c commit e8d8275
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tiny/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

ext {
upload_group_id = 'com.zxy.android'
upload_version = '0.0.5'
upload_version = '0.0.6'

site_url = 'https://github.com/Sunzxyong/Tiny'
git_url = 'https://github.com/Sunzxyong/Tiny.git'
Expand Down
19 changes: 18 additions & 1 deletion tiny/src/main/java/com/zxy/tiny/Tiny.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.net.Uri;
import android.text.TextUtils;

import com.zxy.tiny.common.ApplicationLoader;
import com.zxy.tiny.common.TinyException;
import com.zxy.tiny.core.CompressEngine;
import com.zxy.tiny.core.CompressKit;
Expand Down Expand Up @@ -43,6 +44,12 @@ public Tiny debug(boolean isDebug) {
return this;
}

/**
* Initialization is not must.
*
* @param application
*/
@Deprecated
public void init(Application application) {
if (application == null)
throw new TinyException.IllegalArgumentException("application can not be null!");
Expand All @@ -55,7 +62,7 @@ public boolean isDebug() {

public Application getApplication() {
if (mApplication == null)
throw new TinyException.UnsupportedOperationException("must initialize the 'Tiny' framework before use!");
mApplication = ApplicationLoader.get();
return mApplication;
}

Expand Down Expand Up @@ -107,6 +114,16 @@ public synchronized CompressEngine source(int[] resIds) {
return new CompressEngine().source(resIds);
}

public synchronized boolean clearCompressDirectory() {
File dir = FileKit.getDefaultFileCompressDirectory();
try {
return FileKit.clearDirectory(dir);
} catch (Exception e) {
//for android 6.0+,permission request
}
return false;
}

public static class BitmapCompressOptions {

/**
Expand Down
37 changes: 37 additions & 0 deletions tiny/src/main/java/com/zxy/tiny/common/ApplicationLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.zxy.tiny.common;

import android.app.Application;

import java.lang.reflect.Method;

/**
* Created by zhengxiaoyong on 2017/5/23.
*/
public class ApplicationLoader {

private static Application sApplication;

public static Application get() {
return sApplication != null ? sApplication : getApplication();
}

private static Application getApplication() {
Application application = null;
Method method;
try {
method = Class.forName("android.app.AppGlobals").getDeclaredMethod("getInitialApplication");
method.setAccessible(true);
application = (Application) method.invoke(null);
} catch (Exception e) {
try {
method = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication");
method.setAccessible(true);
application = (Application) method.invoke(null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
return application;
}

}
22 changes: 22 additions & 0 deletions tiny/src/main/java/com/zxy/tiny/core/FileKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.text.TextUtils;

import com.zxy.tiny.Tiny;
import com.zxy.tiny.common.Logger;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -91,4 +92,25 @@ public static File[] wrap(String[] filePaths) {
}
return files;
}

public static boolean clearDirectory(File dir) {
if (dir == null || !dir.isDirectory() || !dir.exists())
return false;
File[] files = dir.listFiles();
int length = files.length;
for (int i = 0; i < length; i++) {
File file = files[i];
if (file == null)
continue;
if (file.isFile() && file.exists()) {
boolean result = file.delete();
Logger.e(file.getName() + (result ? " delete success!" : " delete failed!"));
continue;
}
if (file.isDirectory() && file.exists()) {
clearDirectory(file);
}
}
return true;
}
}

0 comments on commit e8d8275

Please sign in to comment.