diff options
Diffstat (limited to 'library/src/main/java/top/trumeet/redirectstorage/wrapper')
-rw-r--r-- | library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java | 58 | ||||
-rw-r--r-- | library/src/main/java/top/trumeet/redirectstorage/wrapper/UserEnvironmentWrapperMarshmallow.java | 91 |
2 files changed, 149 insertions, 0 deletions
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 |