diff options
Diffstat (limited to 'library/src/main/java/top')
5 files changed, 42 insertions, 22 deletions
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 |