aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java')
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java127
1 files changed, 12 insertions, 115 deletions
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<File> 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));
}
}
}