From 6e3f75a9c55de4e9b2c731ec70ced08901aa7862 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Fri, 15 Sep 2017 20:28:56 +0800 Subject: refactor: use AbstractWrapper --- library/src/main/java/android/os/Environment.java | 7 +- .../trumeet/redirectstorage/RedirectStorage.java | 127 ++------------------- .../redirectstorage/wrapper/AbstractWrapper.java | 58 ++++++++++ .../wrapper/UserEnvironmentWrapperMarshmallow.java | 91 +++++++++++++++ 4 files changed, 166 insertions(+), 117 deletions(-) create mode 100644 library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java create mode 100644 library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java diff --git a/library/src/main/java/android/os/Environment.java b/library/src/main/java/android/os/Environment.java index f2da89b..f529268 100644 --- a/library/src/main/java/android/os/Environment.java +++ b/library/src/main/java/android/os/Environment.java @@ -1,7 +1,5 @@ package android.os; -import android.os.storage.StorageVolume; - import java.io.File; /** @@ -67,6 +65,11 @@ public class Environment { public static class UserEnvironment { private final int mUserId; + + /** + * For Lollipop + + * @param userId uid + */ public UserEnvironment(int userId) { mUserId = userId; } diff --git a/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java b/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java index c887e00..038cfe2 100644 --- a/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java +++ b/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java @@ -4,8 +4,8 @@ import android.os.Environment; import java.io.File; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; + +import top.trumeet.redirectstorage.wrapper.AbstractWrapper; /** * Created by Trumeet on 2017/9/8. @@ -40,7 +40,7 @@ public class RedirectStorage { */ public static void disable () { try { - UserEnvironmentWrapper wrapper = + AbstractWrapper wrapper = getInstalledWrapper(); if (wrapper != null) wrapper.setEnable(false); @@ -55,7 +55,7 @@ public class RedirectStorage { */ public static File getRealPath () { try { - UserEnvironmentWrapper wrapper = getInstalledWrapper(); + AbstractWrapper wrapper = getInstalledWrapper(); if (wrapper != null) return wrapper.getRealExternalStorageDirectory(); return Environment.getExternalStorageDirectory(); @@ -67,7 +67,7 @@ public class RedirectStorage { public static boolean isEnable () { try { - UserEnvironmentWrapper wrapper = getInstalledWrapper(); + AbstractWrapper wrapper = getInstalledWrapper(); return wrapper != null && wrapper.isEnable(); } catch (Exception e) { throw new RuntimeException(e); @@ -92,7 +92,7 @@ public class RedirectStorage { * 获取已安装的 Wrapper * @return 已安装的 Wrapper */ - private static UserEnvironmentWrapper getInstalledWrapper () + private static AbstractWrapper getInstalledWrapper () throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException{ return getInstalledWrapper(getCurrentUserField()); @@ -103,11 +103,11 @@ public class RedirectStorage { * @param field CurrentUserField * @return 已安装的 Wrapper */ - private static UserEnvironmentWrapper getInstalledWrapper (Field field) + private static AbstractWrapper getInstalledWrapper (Field field) throws IllegalAccessException { Environment.UserEnvironment o = (Environment.UserEnvironment) field.get(null); - return o == null || !(o instanceof UserEnvironmentWrapper) - ? null : (UserEnvironmentWrapper) o; + return o == null || !(o instanceof AbstractWrapper) + ? null : (AbstractWrapper) o; } /** @@ -125,7 +125,7 @@ public class RedirectStorage { private static void invokeEnvironmentSdcardMethod(String target) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException { Field sCurrentUserField = getCurrentUserField(); - UserEnvironmentWrapper wrapper = getInstalledWrapper(sCurrentUserField); + AbstractWrapper wrapper = getInstalledWrapper(sCurrentUserField); if (wrapper != null) { // Update current wrapper wrapper.setCustomPath(target); @@ -142,111 +142,8 @@ public class RedirectStorage { } catch (Exception e) { e.printStackTrace(); } - sCurrentUserField.set(null, new UserEnvironmentWrapper(o, - user - , target)); - } - } - - private static class UserEnvironmentWrapper extends Environment.UserEnvironment { - - private final Environment.UserEnvironment mBase; - private String mCustomPath; - private boolean mEnable = true; - - public boolean isEnable() { - return mEnable; - } - - public void setEnable(boolean mEnable) { - this.mEnable = mEnable; - } - - public String getCustomPath() { - return mCustomPath; - } - - public void setCustomPath(String mCustomPath) { - this.mCustomPath = mCustomPath; - } - - public UserEnvironmentWrapper(Environment.UserEnvironment ue, - int userId, String customPath) { - super(userId); - mBase = ue; - mCustomPath = customPath; - } - - @Override - public File[] getExternalDirs() { - return buildExternalDirs(mEnable); - } - - private File[] buildExternalDirs (boolean mEnable) { - File[] dirs = mBase.getExternalDirs(); - if (!mEnable) - return dirs; - if (dirs == null || dirs.length == 0) - return dirs; - List list = new ArrayList<>(dirs.length); - for (File file : dirs) { - list.add(new File(file.getAbsolutePath() + mCustomPath)); - } - return list.toArray(new File[list.size()]); - } - - public File getRealExternalStorageDirectory () { - return buildExternalDirs(false)[0]; - } - - @Override - public File getExternalStorageDirectory() { - return mBase.getExternalStorageDirectory(); - } - - @Override - public File getExternalStoragePublicDirectory(String type) { - return mBase.getExternalStoragePublicDirectory(type); - } - - @Override - public File[] buildExternalStoragePublicDirs(String type) { - return mBase.buildExternalStoragePublicDirs(type); - } - - @Override - public File[] buildExternalStorageAndroidDataDirs() { - return mBase.buildExternalStorageAndroidDataDirs(); - } - - @Override - public File[] buildExternalStorageAndroidObbDirs() { - return mBase.buildExternalStorageAndroidObbDirs(); - } - - @Override - public File[] buildExternalStorageAppDataDirs(String packageName) { - return mBase.buildExternalStorageAppDataDirs(packageName); - } - - @Override - public File[] buildExternalStorageAppMediaDirs(String packageName) { - return mBase.buildExternalStorageAppMediaDirs(packageName); - } - - @Override - public File[] buildExternalStorageAppObbDirs(String packageName) { - return mBase.buildExternalStorageAppObbDirs(packageName); - } - - @Override - public File[] buildExternalStorageAppFilesDirs(String packageName) { - return mBase.buildExternalStorageAppFilesDirs(packageName); - } - - @Override - public File[] buildExternalStorageAppCacheDirs(String packageName) { - return mBase.buildExternalStorageAppCacheDirs(packageName); + sCurrentUserField.set(null, AbstractWrapper.getWrapper(o, + target, user)); } } } diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java new file mode 100644 index 0000000..2a3d2ce --- /dev/null +++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java @@ -0,0 +1,58 @@ +package top.trumeet.redirectstorage.wrapper; + +import android.os.Build; +import android.os.Environment; + +import java.io.File; + +/** + * Created by Trumeet on 2017/9/15. + */ + +public abstract class AbstractWrapper extends Environment.UserEnvironment { + public static AbstractWrapper getWrapper (Environment.UserEnvironment base, + String customPath, + Integer userId) { + checkNonNull(base); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + checkNonNull(userId); + return new UserEnvironmentWrapperMarshmallow(base, + userId, customPath); + } + return null; + } + + private static void checkNonNull (Object object) { + if (object == null) + throw new NullPointerException(); + } + + final Environment.UserEnvironment mBase; + String mCustomPath; + boolean mEnable = true; + + public AbstractWrapper(Environment.UserEnvironment ue, + int userId, String customPath) { + super(userId); + mBase = ue; + mCustomPath = customPath; + } + + public boolean isEnable() { + return mEnable; + } + + public void setEnable(boolean mEnable) { + this.mEnable = mEnable; + } + + public String getCustomPath() { + return mCustomPath; + } + + public void setCustomPath(String mCustomPath) { + this.mCustomPath = mCustomPath; + } + + public abstract File getRealExternalStorageDirectory (); +} diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java new file mode 100644 index 0000000..6565631 --- /dev/null +++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java @@ -0,0 +1,91 @@ +package top.trumeet.redirectstorage.wrapper; + +import android.os.Environment; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Trumeet on 2017/9/15. + * Wrapper for marshmallow+ + */ + +class UserEnvironmentWrapperMarshmallow extends AbstractWrapper { + + UserEnvironmentWrapperMarshmallow(Environment.UserEnvironment ue, int userId, String customPath) { + super(ue, userId, customPath); + } + + @Override + public File[] getExternalDirs() { + return buildExternalDirs(mEnable); + } + + private File[] buildExternalDirs (boolean mEnable) { + File[] dirs = mBase.getExternalDirs(); + if (!mEnable) + return dirs; + if (dirs == null || dirs.length == 0) + return dirs; + List list = new ArrayList<>(dirs.length); + for (File file : dirs) { + list.add(new File(file.getAbsolutePath() + mCustomPath)); + } + return list.toArray(new File[list.size()]); + } + + public File getRealExternalStorageDirectory () { + return buildExternalDirs(false)[0]; + } + + @Override + public File getExternalStorageDirectory() { + return mBase.getExternalStorageDirectory(); + } + + @Override + public File getExternalStoragePublicDirectory(String type) { + return mBase.getExternalStoragePublicDirectory(type); + } + + @Override + public File[] buildExternalStoragePublicDirs(String type) { + return mBase.buildExternalStoragePublicDirs(type); + } + + @Override + public File[] buildExternalStorageAndroidDataDirs() { + return mBase.buildExternalStorageAndroidDataDirs(); + } + + @Override + public File[] buildExternalStorageAndroidObbDirs() { + return mBase.buildExternalStorageAndroidObbDirs(); + } + + @Override + public File[] buildExternalStorageAppDataDirs(String packageName) { + return mBase.buildExternalStorageAppDataDirs(packageName); + } + + @Override + public File[] buildExternalStorageAppMediaDirs(String packageName) { + return mBase.buildExternalStorageAppMediaDirs(packageName); + } + + @Override + public File[] buildExternalStorageAppObbDirs(String packageName) { + return mBase.buildExternalStorageAppObbDirs(packageName); + } + + @Override + public File[] buildExternalStorageAppFilesDirs(String packageName) { + return mBase.buildExternalStorageAppFilesDirs(packageName); + } + + @Override + public File[] buildExternalStorageAppCacheDirs(String packageName) { + return mBase.buildExternalStorageAppCacheDirs(packageName); + } +} \ No newline at end of file -- cgit v1.2.3