aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/top/trumeet/redirectstorage/wrapper/AbstractWrapper.java
blob: e67c1444d3149cbeab40ec1863959048af821ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package top.trumeet.redirectstorage.wrapper;

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 {
    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 WrapperMM(base,
                    userId, customPath);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            checkNonNull(userId);
            return new WrapperKK(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 ();

    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()]);
    }
}