diff options
author | Trumeet <liangyuteng12345@gmail.com> | 2017-09-15 20:55:18 +0800 |
---|---|---|
committer | Trumeet <liangyuteng12345@gmail.com> | 2017-09-15 20:55:18 +0800 |
commit | 14a18bb15c2fcb9d0249d99912382ccb814f5d4f (patch) | |
tree | 0b7ccf431e1862f22188f7b2272909adf2926193 | |
parent | 38ee4ec1c63fe1341338f9ac6010cf15c5f85c52 (diff) | |
download | RedirectStorage-14a18bb15c2fcb9d0249d99912382ccb814f5d4f.tar RedirectStorage-14a18bb15c2fcb9d0249d99912382ccb814f5d4f.tar.gz RedirectStorage-14a18bb15c2fcb9d0249d99912382ccb814f5d4f.tar.bz2 RedirectStorage-14a18bb15c2fcb9d0249d99912382ccb814f5d4f.zip |
feat: may support KitKat and after1.1_test
-rw-r--r-- | library/build.gradle | 2 | ||||
-rw-r--r-- | library/src/main/java/android/os/Environment.java | 18 | ||||
-rw-r--r-- | library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java | 20 | ||||
-rw-r--r-- | library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java | 102 | ||||
-rw-r--r-- | library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java (renamed from library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java) | 17 |
5 files changed, 146 insertions, 13 deletions
diff --git a/library/build.gradle b/library/build.gradle index bfa8e20..d3fd352 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { - minSdkVersion 21 + minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" diff --git a/library/src/main/java/android/os/Environment.java b/library/src/main/java/android/os/Environment.java index f529268..67faa77 100644 --- a/library/src/main/java/android/os/Environment.java +++ b/library/src/main/java/android/os/Environment.java @@ -108,5 +108,23 @@ public class Environment { public File[] buildExternalStorageAppCacheDirs(String packageName) { throw new RuntimeException("Stub!"); } + public File[] getExternalDirsForVold() { + throw new RuntimeException("Stub!"); + } + public File[] getExternalDirsForApp() { + throw new RuntimeException("Stub!"); + } + public File getMediaDir() { + throw new RuntimeException("Stub!"); + } + public File[] buildExternalStorageAppDataDirsForVold(String packageName) { + throw new RuntimeException("Stub!"); + } + public File[] buildExternalStorageAppMediaDirsForVold(String packageName) { + throw new RuntimeException("Stub!"); + } + public File[] buildExternalStorageAppObbDirsForVold(String packageName) { + throw new RuntimeException("Stub!"); + } } } 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 2a3d2ce..e67c144 100644 --- a/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java +++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java @@ -4,9 +4,12 @@ import android.os.Build; import android.os.Environment; import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * Created by Trumeet on 2017/9/15. + * @author Trumeet */ public abstract class AbstractWrapper extends Environment.UserEnvironment { @@ -16,7 +19,12 @@ public abstract class AbstractWrapper extends Environment.UserEnvironment { checkNonNull(base); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { checkNonNull(userId); - return new UserEnvironmentWrapperMarshmallow(base, + return new WrapperMM(base, + userId, customPath); + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + checkNonNull(userId); + return new WrapperKK(base, userId, customPath); } return null; @@ -55,4 +63,14 @@ public abstract class AbstractWrapper extends Environment.UserEnvironment { } public abstract File getRealExternalStorageDirectory (); + + public final File[] convertDirs (File... 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()]); + } } diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java new file mode 100644 index 0000000..58d1d65 --- /dev/null +++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperKK.java @@ -0,0 +1,102 @@ +package top.trumeet.redirectstorage.wrapper; + +import android.annotation.TargetApi; +import android.os.Build; +import android.os.Environment; + +import java.io.File; + +/** + * Created by Trumeet on 2017/9/15. + * @author Trumeet + */ + +@TargetApi(Build.VERSION_CODES.KITKAT) +class WrapperKK extends AbstractWrapper { + WrapperKK(Environment.UserEnvironment ue, int userId, String customPath) { + super(ue, userId, customPath); + } + + @Override + public File getRealExternalStorageDirectory() { + return null; + } + + private File convert (File dir) { + if (!mEnable) + return dir; + return new File(convertDirs(dir)[0].getAbsolutePath()); + } + + private File[] convert (File[] dirs) { + if (!mEnable) + return dirs; + return convertDirs(dirs); + } + + @Deprecated + public File getExternalStorageDirectory() { + return convert(mBase.getExternalStorageDirectory()); + } + + @Deprecated + public File getExternalStoragePublicDirectory(String type) { + return buildExternalStoragePublicDirs(type)[0]; + } + + public File[] getExternalDirsForVold() { + return convert(mBase.getExternalDirsForVold()); + } + + public File[] getExternalDirsForApp() { + return convert(mBase.getExternalDirsForApp()); + } + + public File getMediaDir() { + return convert(mBase.getMediaDir()); + } + + public File[] buildExternalStoragePublicDirs(String type) { + return convert(mBase.buildExternalStoragePublicDirs(type)); + } + + public File[] buildExternalStorageAndroidDataDirs() { + return convert(mBase.buildExternalStorageAndroidDataDirs()); + } + + public File[] buildExternalStorageAndroidObbDirs() { + return convert(mBase.buildExternalStorageAndroidObbDirs()); + } + + public File[] buildExternalStorageAppDataDirs(String packageName) { + return convert(mBase.buildExternalStorageAppDataDirs(packageName)); + } + + public File[] buildExternalStorageAppDataDirsForVold(String packageName) { + return convert(mBase.buildExternalStorageAppDataDirsForVold(packageName)); + } + + public File[] buildExternalStorageAppMediaDirs(String packageName) { + return convert(mBase.buildExternalStorageAppMediaDirs(packageName)); + } + + public File[] buildExternalStorageAppMediaDirsForVold(String packageName) { + return convert(mBase.buildExternalStorageAppMediaDirsForVold(packageName)); + } + + public File[] buildExternalStorageAppObbDirs(String packageName) { + return convert(mBase.buildExternalStorageAppObbDirs(packageName)); + } + + public File[] buildExternalStorageAppObbDirsForVold(String packageName) { + return convert(mBase.buildExternalStorageAppObbDirsForVold(packageName)); + } + + public File[] buildExternalStorageAppFilesDirs(String packageName) { + return convert(mBase.buildExternalStorageAppFilesDirs(packageName)); + } + + public File[] buildExternalStorageAppCacheDirs(String packageName) { + return convert(mBase.buildExternalStorageAppCacheDirs(packageName)); + } +} diff --git a/library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java index 6565631..bc6a698 100644 --- a/library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java +++ b/library/src/main/java/top/trumeet/redirectstorage/wrapper/WrapperMM.java @@ -1,19 +1,20 @@ package top.trumeet.redirectstorage.wrapper; +import android.annotation.TargetApi; +import android.os.Build; 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 { +@TargetApi(Build.VERSION_CODES.M) +class WrapperMM extends AbstractWrapper { - UserEnvironmentWrapperMarshmallow(Environment.UserEnvironment ue, int userId, String customPath) { + WrapperMM(Environment.UserEnvironment ue, int userId, String customPath) { super(ue, userId, customPath); } @@ -26,13 +27,7 @@ class UserEnvironmentWrapperMarshmallow extends AbstractWrapper { 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()]); + return convertDirs(dirs); } public File getRealExternalStorageDirectory () { |