aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrumeet <liangyuteng12345@gmail.com>2017-09-30 20:03:29 +0800
committerTrumeet <liangyuteng12345@gmail.com>2017-09-30 20:03:29 +0800
commit266894af1c1cf4c82addd22f2635a07ec2f1506d (patch)
tree3b3f98346823d3ac1391228e15887c4c48b05ee0
parent37bd9772688c6228a8d265b41e216e7985e17408 (diff)
downloadRedirectStorage-266894af1c1cf4c82addd22f2635a07ec2f1506d.tar
RedirectStorage-266894af1c1cf4c82addd22f2635a07ec2f1506d.tar.gz
RedirectStorage-266894af1c1cf4c82addd22f2635a07ec2f1506d.tar.bz2
RedirectStorage-266894af1c1cf4c82addd22f2635a07ec2f1506d.zip
feat: support custom callbackHEAD1.2master
-rw-r--r--README.md20
-rw-r--r--app/src/main/java/top/trumeet/redirectstorage/simple/MainActivity.java12
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/PathCallback.java19
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java13
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java20
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java6
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java6
7 files changed, 61 insertions, 35 deletions
diff --git a/README.md b/README.md
index 0862cb1..23104f7 100644
--- a/README.md
+++ b/README.md
@@ -8,12 +8,12 @@
* Add it in your root build.gradle at the end of repositories:
```
- allprojects {
- repositories {
- ...
- maven { url 'https://jitpack.io' }
- }
- }
+allprojects {
+ repositories {
+ ...
+ maven { url 'https://jitpack.io' }
+ }
+}
```
* 查詢最新版本
@@ -23,9 +23,9 @@
* 添加依賴
```
- dependencies {
- compile 'com.github.Trumeet:RedirectStorage:<最新版本號>'
- }
+dependencies {
+ compile 'com.github.Trumeet:RedirectStorage:<最新版本號>'
+}
```
# Usage
@@ -35,7 +35,7 @@
```java
// 启用
-enable(String pathSuffix) // pathSuffix 是 加在原目录后面的后缀,详情见 JavaDoc
+enable(PathCallback callback) // 当需要 Hack 路径的时候的 Callback
// 禁用
disable()
diff --git a/app/src/main/java/top/trumeet/redirectstorage/simple/MainActivity.java b/app/src/main/java/top/trumeet/redirectstorage/simple/MainActivity.java
index 193cb0c..adb493f 100644
--- a/app/src/main/java/top/trumeet/redirectstorage/simple/MainActivity.java
+++ b/app/src/main/java/top/trumeet/redirectstorage/simple/MainActivity.java
@@ -8,6 +8,9 @@ import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
+import java.io.File;
+
+import top.trumeet.redirectstorage.PathCallback;
import top.trumeet.redirectstorage.RedirectStorage;
public class MainActivity extends Activity {
@@ -25,9 +28,12 @@ public class MainActivity extends Activity {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
- RedirectStorage.enable(getExternalFilesDir(null)
- .getAbsolutePath().substring(RedirectStorage.getRealPath().getAbsolutePath()
- .length()));
+ RedirectStorage.enable(new PathCallback() {
+ @Override
+ public File onModify(File original) {
+ return getFilesDir();
+ }
+ });
} else {
RedirectStorage.disable();
}
diff --git a/library/src/main/java/top/trumeet/redirectstorage/PathCallback.java b/library/src/main/java/top/trumeet/redirectstorage/PathCallback.java
new file mode 100644
index 0000000..a3b71df
--- /dev/null
+++ b/library/src/main/java/top/trumeet/redirectstorage/PathCallback.java
@@ -0,0 +1,19 @@
+package top.trumeet.redirectstorage;
+
+import java.io.File;
+
+/**
+ * Created by Trumeet on 2017/9/30.
+ * Callback to modify path
+ * @author Trumeet
+ */
+
+public interface PathCallback {
+ /**
+ * 当需要修改目录时调用,您可以返回修改过的 File。
+ * 当 Disabled 或获取真实路径的时候它不会被调用。
+ * @param original Framework 获得的 File
+ * @return 你修改的 File
+ */
+ File onModify (File original);
+}
diff --git a/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java b/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java
index ed330df..8c59ed7 100644
--- a/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java
+++ b/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java
@@ -28,12 +28,11 @@ public class RedirectStorage {
/**
* 安装并启用。
- * @param pathSuffix 合并到原目录后面的路径。比如说要重定向到
- * SD卡/ABC,那么传递 ABC。
+ * @param callback 当修改路径时的 Callback
*/
- public static void enable (String pathSuffix) {
+ public static void enable (PathCallback callback) {
try {
- invokeEnvironmentSdcardMethod(pathSuffix);
+ invokeEnvironmentSdcardMethod(callback);
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -126,13 +125,13 @@ public class RedirectStorage {
}
}
- private static void invokeEnvironmentSdcardMethod(String target)
+ private static void invokeEnvironmentSdcardMethod(PathCallback callback)
throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Field sCurrentUserField = getCurrentUserField();
AbstractWrapper wrapper = getInstalledWrapper(sCurrentUserField);
if (wrapper != null) {
// Update current wrapper
- wrapper.setCustomPath(target);
+ wrapper.setCallback(callback);
wrapper.setEnable(true);
} else {
// Install new wrapper
@@ -147,7 +146,7 @@ public class RedirectStorage {
e.printStackTrace();
}
AbstractWrapper abstractWrapper = AbstractWrapper.getWrapper(o,
- target, user);
+ callback, user);
if (abstractWrapper == null) {
Log.e(TAG, "Can not create wrapper, it looks like not support your ROM: " +
Build.VERSION.SDK_INT);
diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java
index e67c144..800ebf1 100644
--- a/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java
+++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java
@@ -7,6 +7,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
+import top.trumeet.redirectstorage.PathCallback;
+
/**
* Created by Trumeet on 2017/9/15.
* @author Trumeet
@@ -14,7 +16,7 @@ import java.util.List;
public abstract class AbstractWrapper extends Environment.UserEnvironment {
public static AbstractWrapper getWrapper (Environment.UserEnvironment base,
- String customPath,
+ PathCallback customPath,
Integer userId) {
checkNonNull(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@@ -36,14 +38,14 @@ public abstract class AbstractWrapper extends Environment.UserEnvironment {
}
final Environment.UserEnvironment mBase;
- String mCustomPath;
+ PathCallback callback;
boolean mEnable = true;
public AbstractWrapper(Environment.UserEnvironment ue,
- int userId, String customPath) {
+ int userId, PathCallback callback) {
super(userId);
mBase = ue;
- mCustomPath = customPath;
+ this.callback = callback;
}
public boolean isEnable() {
@@ -54,12 +56,8 @@ public abstract class AbstractWrapper extends Environment.UserEnvironment {
this.mEnable = mEnable;
}
- public String getCustomPath() {
- return mCustomPath;
- }
-
- public void setCustomPath(String mCustomPath) {
- this.mCustomPath = mCustomPath;
+ public void setCallback (PathCallback callback) {
+ this.callback = callback;
}
public abstract File getRealExternalStorageDirectory ();
@@ -69,7 +67,7 @@ public abstract class AbstractWrapper extends Environment.UserEnvironment {
return dirs;
List<File> list = new ArrayList<>(dirs.length);
for (File file : dirs) {
- list.add(new File(file.getAbsolutePath() + mCustomPath));
+ list.add(callback.onModify(file));
}
return list.toArray(new File[list.size()]);
}
diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java
index fb3450e..942d1b9 100644
--- a/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java
+++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java
@@ -6,6 +6,8 @@ import android.os.Environment;
import java.io.File;
+import top.trumeet.redirectstorage.PathCallback;
+
/**
* Created by Trumeet on 2017/9/15.
* @author Trumeet
@@ -13,8 +15,8 @@ import java.io.File;
@TargetApi(Build.VERSION_CODES.KITKAT)
class WrapperKK extends AbstractWrapper {
- WrapperKK(Environment.UserEnvironment ue, int userId, String customPath) {
- super(ue, userId, customPath);
+ WrapperKK(Environment.UserEnvironment ue, int userId, PathCallback callback) {
+ super(ue, userId, callback);
}
@Override
diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java
index bc6a698..059e5b5 100644
--- a/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java
+++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java
@@ -6,6 +6,8 @@ import android.os.Environment;
import java.io.File;
+import top.trumeet.redirectstorage.PathCallback;
+
/**
* Created by Trumeet on 2017/9/15.
* Wrapper for marshmallow+
@@ -14,8 +16,8 @@ import java.io.File;
@TargetApi(Build.VERSION_CODES.M)
class WrapperMM extends AbstractWrapper {
- WrapperMM(Environment.UserEnvironment ue, int userId, String customPath) {
- super(ue, userId, customPath);
+ WrapperMM(Environment.UserEnvironment ue, int userId, PathCallback callback) {
+ super(ue, userId, callback);
}
@Override