admin
2020-08-12 cefe2a41db4a275fb1e940a902cb156f1ed68d80
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright © 2012-2013 LiuZhongnan. All rights reserved.
 * 
 * Email:qq81595157@126.com
 * 
 * PROPRIETARY/CONFIDENTIAL.
 */
 
package com.youku.service.download;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
 
import android.annotation.SuppressLint;
import android.os.Environment;
import android.os.StatFs;
 
import com.baseproject.utils.UIUtils;
import com.youku.player.YoukuPlayerConfiguration;
 
/**
 * SDCardManager.SD卡管理,空间数据的获取
 * 
 * @author 刘仲男 qq81595157@126.com
 * @version v3.5
 * @created time 2012-11-5 下午1:16:02
 */
public class SDCardManager {
 
    private String sdPath;
    private long nSDTotalSize;
    private long nSDFreeSize;
 
    public SDCardManager(String sdPath) {
        this.sdPath = sdPath;
        init();
    }
 
    @SuppressWarnings("deprecation")
    private void init() {
        try {
            StatFs statFs = new StatFs(sdPath);
            long totalBlocks = statFs.getBlockCount();// 区域块数
            long availableBlocks = statFs.getAvailableBlocks();// 可利用区域块数
            long blockSize = statFs.getBlockSize();// 每个区域块大小
            nSDTotalSize = totalBlocks * blockSize;
            nSDFreeSize = availableBlocks * blockSize;
        } catch (Exception e) {
 
        }
    }
 
    /**
     * 是否存在
     * 
     * @return
     */
    public boolean exist() {
        return nSDTotalSize == 0 ? false : true;
    }
 
    /**
     * 总空间
     * 
     * @return
     */
    public long getTotalSize() {
        return nSDTotalSize;
    }
 
    /**
     * 剩余空间
     * 
     * @return
     */
    public long getFreeSize() {
        return nSDFreeSize;
    }
 
    /**
     * 优酷视频占有空间大小(/youku/offlinedata/下的文件大小)
     * 
     * @return
     */
    public long getYoukuVideoSpace() {
        try {
            if (sdPath.equals(getDefauleSDCardPath())) {
                // 固定加入p2p所占空间大小100M
//                if (AcceleraterServiceManager.isAccSupported(Youku.context)) {
//                    return getYoukuOfflinedataSpace() + 100 * 1024 * 1024;
//                } else {
                    return getYoukuOfflinedataSpace();
//                }
            } else {
                return getYoukuOfflinedataSpace();
            }
        } catch (Exception e) {
            return getYoukuOfflinedataSpace();
        }
    }
 
    private long getYoukuOfflinedataSpace() {
        File f = new File(sdPath + YoukuPlayerConfiguration.getDownloadPath());
        return f.exists() ? getFileSize(f) : 0;
    }
 
    private long getYoukudiskSpace() {
        File f = new File(sdPath + "/youku/youkudisk/");
        return f.exists() ? getFileSize(f) : 0;
    }
 
    /**
     * 其他程序占有空间大小
     * 
     * @return
     */
    public long getOtherSpace() {
        if (!exist())
            return 0;
        return nSDTotalSize - nSDFreeSize - getYoukuOfflinedataSpace()
                - getYoukudiskSpace();
    }
 
    /**
     * 优酷视频所占空间比例n%
     */
    public int getYoukuProgrss() {
        if (!exist())
            return 0;
        return (int) ((1000 * getYoukuVideoSpace()) / nSDTotalSize);
    }
 
    /**
     * 其他程序所占空间比例n%
     */
    public int getOtherProgrss() {
        if (!exist())
            return 0;
        return (int) ((1000 * getOtherSpace()) / nSDTotalSize);
    }
 
    /**
     * TODO 递归取得文件夹大小
     */
    private static long getFileSize(File f) {
        long size = 0;
        if (f.isDirectory()) {
            File files[] = f.listFiles();
            if (files != null) {
                for (int i = 0, n = files.length; i < n; i++) {
                    if (files[i].isDirectory()) {
                        size = size + getFileSize(files[i]);
                    } else {
                        size = size + files[i].length();
                    }
                }
            }
        } else {
            size = f.length();
        }
        return size;
    }
 
    /** Returns 是否有SD卡 */
    public static boolean hasSDCard() {
        return Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
    }
 
    public static String getDefauleSDCardPath() {
        return hasSDCard() ? Environment.getExternalStorageDirectory()
                .getAbsolutePath() : "";
    }
 
    /**
     * 获得外部存储路径
     * 
     * @return /mnt/sdcard或者/storage/extSdCard等多种名称
     */
    @SuppressLint("NewApi")
    public static ArrayList<SDCardInfo> getExternalStorageDirectory() {
        ArrayList<SDCardInfo> list = new ArrayList<SDCardInfo>();
        if (UIUtils.hasKitKat()) {
            // OS 4.4 以上读取外置 SD 卡
            final File[] externalFiles = YoukuPlayerConfiguration.context
                    .getExternalFilesDirs(null);
            if (null != externalFiles) {
                SDCardInfo info = new SDCardInfo();
                info.path = getDefauleSDCardPath();
                info.isExternal = false;
                list.add(info);
                if (externalFiles.length > 1 && (null != externalFiles[1])) {
                    SDCardInfo externalInfo = new SDCardInfo();
                    externalInfo.path = externalFiles[1].getAbsolutePath();
                    externalInfo.isExternal = true;
                    list.add(externalInfo);
                }
            }
            return list;
        } else {
            Runtime runtime = Runtime.getRuntime();
            Process proc;
            try {
                proc = runtime.exec("mount");
                // InputStream is = Youku.context.getAssets().open("mount.txt");
                InputStream is = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                String line;
                // String mount = new String();
                BufferedReader br = new BufferedReader(isr);
                String defauleSDCardPath = getDefauleSDCardPath();
                //用于盛放已经判断过的路径,省时高效
                HashMap<String,Integer> tempPath = new HashMap<String,Integer>();
                while ((line = br.readLine()) != null) {
//                     Logger.d("nathan2", line);
                    // "fat"为不可卸载的;"fuse"为可卸载的;但是有的手机不适用,所以统一去处理;storage为一些三星手机的存储路径标识
                    if (line.contains("fat") || line.contains("fuse")
                            || line.contains("storage")) {
                        if (line.contains("secure") || line.contains("asec")
                                || line.contains("firmware")
                                || line.contains("shell")
                                || line.contains("obb")
                                || line.contains("legacy")
                                || line.contains("data")
                                || line.contains("tmpfs")) {
 
                            continue;
                        }
                        String columns[] = line.split(" ");
                        for (int i = 0; i < columns.length; i++) {
                            String path = columns[i];
                            //一加手机的"/dev/fuse"是假地址
                            if (path.contains("/") && !path.contains("data") && !path.contains("Data")
                                    && !path.contains("/dev/fuse")) {
                                try {
                                    if (tempPath.containsKey(path)) {
                                        continue;
                                    } else {
                                        tempPath.put(path, 0);
                                        SDCardManager m = new SDCardManager(path);
                                        if (m.getTotalSize() >= 1024 * 1024 * 1024) {
                                            SDCardInfo info = new SDCardInfo();
                                            info.path = columns[i];
                                            if (info.path.equals(defauleSDCardPath)) {
                                                info.isExternal = false;
                                            } else {
                                                info.isExternal = true;
                                            }
                                            list.add(info);
                                        }
                                    }
                                } catch (Exception e) {
                                    continue;
                                }
                            }
                        }
                    }
                }
                tempPath.clear();
                if (list.size() == 1) {
                    if (!defauleSDCardPath.equals(list.get(0).path)) {
                        SDCardInfo info = new SDCardInfo();
                        info.path = defauleSDCardPath;
                        info.isExternal = false;
                        list.add(info);
                    } else {
                        YoukuPlayerConfiguration.savePreference("download_file_path",
                                defauleSDCardPath);
                    }
                } else if (list.size() == 0) {
                    if (defauleSDCardPath != null
                            && defauleSDCardPath.length() != 0) {
                        SDCardInfo info = new SDCardInfo();
                        info.path = defauleSDCardPath;
                        info.isExternal = false;
                        list.add(info);
                    }
                }
                if (list.size() > 1) {
                    Set<SDCardInfo> s = new TreeSet<SDCardInfo>(
                            new Comparator<SDCardInfo>() {
 
                                @Override
                                public int compare(SDCardInfo o1, SDCardInfo o2) {
                                    return o1.path.compareTo(o2.path);
                                }
 
                            });
                    s.addAll(list);
                    list = new ArrayList<SDCardInfo>(s);
                }
                return list;
            } catch (IOException e) {
 
            }
            return null;
 
        }
    }
 
    public static class SDCardInfo {
 
        /** 路径/mnt/sdcard或者/storage/extSdCard等多种名称 */
        public String path;
 
        /** 是否是外部存储 */
        public boolean isExternal;
    }
}