aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrumeet <liangyuteng12345@gmail.com>2017-09-15 20:28:56 +0800
committerTrumeet <liangyuteng12345@gmail.com>2017-09-15 20:28:56 +0800
commit6e3f75a9c55de4e9b2c731ec70ced08901aa7862 (patch)
tree238714a3de3919320b7448bfd60750d895cc8f95
parentb8c9ff8b8d04a1107405bbcd6539eec05f8a1b4e (diff)
downloadRedirectStorage-6e3f75a9c55de4e9b2c731ec70ced08901aa7862.tar
RedirectStorage-6e3f75a9c55de4e9b2c731ec70ced08901aa7862.tar.gz
RedirectStorage-6e3f75a9c55de4e9b2c731ec70ced08901aa7862.tar.bz2
RedirectStorage-6e3f75a9c55de4e9b2c731ec70ced08901aa7862.zip
refactor: use AbstractWrapper
-rw-r--r--library/src/main/java/android/os/Environment.java7
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/RedirectStorage.java127
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java58
-rw-r--r--library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java91
4 files changed, 166 insertions, 117 deletions
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<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));
}
}
}
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<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);
+ }
+} \ No newline at end of file