admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.lcjian.library.util;
 
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
import android.content.Context;
import android.os.Build;
import android.os.storage.StorageManager;
 
/**
 * An {@link android.os.Environment} wrapper that adds
 * support for phones that have multiple external storage directories.
 */
public class Environment extends android.os.Environment {
 
    /**
     * SDCard File List
     */
    private static File[] mStorageList;
 
 
    /**
     * List Of The Potential Volume Daemons
     */
    private static final File[] mVolumeDaemonList = new File[]{
            new File(getRootDirectory(), "etc/vold.fstab"),
            new File(getRootDirectory(), "etc/vold.conf")
    };
 
 
    /**
     * Returns an array of files containing the paths to all of the external
     * storage directories (Emulated/Removable). As a fall back, it reads in the volume daemon file
     * and parses the contents.
     * <p/>
     * <b>Note:</b> This method takes advantage of a hidden method inside {@link StorageManager} which
     * was not introduced until API 14 (ICE_CREAM_SANDWICH/4.0.1).
     * <p/>
     *
     * @param context {@link Context} used to get StorageManager
     */
    public static File[] getExternalStorageList(Context context) {
        mStorageList = null;
        if (mStorageList == null) {
            try {
                mStorageList = (Build.VERSION.SDK_INT >= 14)
                        ? getExternalStorageList((StorageManager) context.getSystemService(Context.STORAGE_SERVICE))
                        : getVolumeDaemonExternalStorageList();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mStorageList;
    }
 
 
    /**
     * Checks to see if more than one external storage directory exists.
     *
     * @param context {@link Context} used to get StorageManager
     */
    public static boolean doesExtraExternalStorageDirectoryExist(Context context) {
        getExternalStorageList(context);
        return mStorageList != null && mStorageList.length >= 2;
    }
 
 
    /**
     * Returns an array of files containing the paths to all of the external
     * storage directories (Emulated/Removable) provided by the volume daemon
     * config file.
     *
     */
    public static File[] getVolumeDaemonExternalStorageList() {
        for (File daemon : mVolumeDaemonList) {
            try {
                if (daemon.exists() && daemon.canRead()) {
                    final String[] stringArray = readFileIntoStringArray(daemon);
                    final List<File> fileList = new ArrayList<File>();
                    if (stringArray != null) {
                        for (String str : stringArray) {
                            final File f = new File(str.split(" ")[2].split(":")[0]);
                            if (!doesFileExistInList(fileList, f)) {
                                fileList.add(f);
                            }
                        }
                    }
                    return (!fileList.isEmpty() ? fileList.toArray(new File[fileList.size()]) : null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        return null;
    }
 
    private static File[] getExternalStorageList(StorageManager storageManager) throws Exception {
        final Method method = storageManager.getClass().getMethod("getVolumePaths");
        final String[] strList = (String[]) method.invoke(storageManager);
        final List<File> fileList = new ArrayList<File>();
 
        for (String path : strList) {
            final File file = new File(path);
            if (!doesFileExistInList(fileList, file)) {
                fileList.add(file);
            }
        }
 
        return (!fileList.isEmpty() ? fileList.toArray(new File[fileList.size()]) : null);
    }
 
    private static boolean doesFileExistInList(List<File> fileList, File newFile) {
        if (newFile == null || fileList == null) {
            // File Is Null Or List Is Null
            return true;
        }
 
        if (!newFile.exists()) {
            // The File Doesn't Exist
            return true;
        }
 
        if (!newFile.isDirectory()) {
            // File Is Not A Directory
            return true;
        }
 
        if (!newFile.canRead()) {
            // Can't Read The File
            return true;
        }
 
        if (newFile.getTotalSpace() <= 0) {
            // File Has No Space
            // Filters Usbdisk Out
            return true;
        }
 
        if (newFile.getName().equalsIgnoreCase("tmp")) {
            // This Folder Showed Up On My Droid X, Filter It Out.
            return true;
        }
 
        if (fileList.contains(newFile)) {
            // File Is In The List
            return true;
        }
 
        // Make Sure The File Isn't In The List As A Link Of Some Sort
        // More Of An In Depth Look
        for (File file : fileList) {
            if (file.getFreeSpace() == newFile.getFreeSpace() && file.getUsableSpace() == newFile.getUsableSpace()) {
                // Same Free/Usable Space
                // Must Be Same Files
                return true;
            }
        }
 
        // File Passed All Of My Tests
        return false;
    }
 
    private static String[] readFileIntoStringArray(File file) {
        Scanner scanner = null;
        try {
            scanner = new Scanner(file);
            final List<String> stringList = new ArrayList<String>();
            while (scanner.hasNext()) {
                final String line = scanner.nextLine();
                if (line != null) {
                    if (line.length() > 0) {
                        if (!line.startsWith("#")) {
                            stringList.add(line);
                        }
                    }
                }
            }
            return !stringList.isEmpty() ? stringList.toArray(new String[stringList.size()]) : null;
        } catch (Exception e) {
            return null;
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }
}