1 package org.easyrpg.player;
2 
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Scanner;
10 
11 import android.os.Environment;
12 
13 // Author and Source of this class: http://stackoverflow.com/a/15612964/540909
14 
15 public class ExternalStorage {
16     public static final String SD_CARD = "Internal Storage";
17     public static final String EXTERNAL_SD_CARD = "SD Card";
18 
19     /**
20      * @return A map of all storage locations available
21      */
getAllStorageLocations()22     public static Map<String, File> getAllStorageLocations() {
23         Map<String, File> map = new LinkedHashMap<String, File>(10);
24 
25         List<String> mMounts = new ArrayList<String>(10);
26         List<String> mVold = new ArrayList<String>(10);
27         mMounts.add("/mnt/sdcard");
28         mVold.add("/mnt/sdcard");
29 
30         try {
31             File mountFile = new File("/proc/mounts");
32             if(mountFile.exists()){
33                 Scanner scanner = new Scanner(mountFile);
34                 while (scanner.hasNext()) {
35                     String line = scanner.nextLine();
36                     if (line.startsWith("/dev/block/vold/")) {
37                         String[] lineElements = line.split(" ");
38                         String element = lineElements[1];
39 
40                         // don't add the default mount path
41                         // it's already in the list.
42                         if (!element.equals("/mnt/sdcard"))
43                             mMounts.add(element);
44                     }
45                 }
46             }
47         } catch (Exception e) {
48         }
49 
50         try {
51             File voldFile = new File("/system/etc/vold.fstab");
52             if(voldFile.exists()){
53                 Scanner scanner = new Scanner(voldFile);
54                 while (scanner.hasNext()) {
55                     String line = scanner.nextLine();
56                     if (line.startsWith("dev_mount")) {
57                         String[] lineElements = line.split(" ");
58                         String element = lineElements[2];
59 
60                         if (element.contains(":"))
61                             element = element.substring(0, element.indexOf(":"));
62                         if (!element.equals("/mnt/sdcard"))
63                             mVold.add(element);
64                     }
65                 }
66             }
67         } catch (Exception e) {
68         }
69 
70 
71         for (int i = 0; i < mMounts.size(); i++) {
72             String mount = mMounts.get(i);
73             if (!mVold.contains(mount))
74                 mMounts.remove(i--);
75         }
76         mVold.clear();
77 
78         List<String> mountHash = new ArrayList<String>(10);
79 
80         for(String mount : mMounts){
81             File root = new File(mount);
82             if (root.exists() && root.isDirectory() && root.canRead()) {
83                 File[] list = root.listFiles();
84                 String hash = "[";
85                 if(list!=null){
86                     for(File f : list){
87                         hash += f.getName().hashCode()+":"+f.length()+", ";
88                     }
89                 }
90                 hash += "]";
91                 if(!mountHash.contains(hash)){
92                     String key = SD_CARD + " " + map.size();
93                     if (map.size() == 0) {
94                         key = SD_CARD;
95                     } else if (map.size() == 1) {
96                         key = EXTERNAL_SD_CARD;
97                     }
98                     mountHash.add(hash);
99                     map.put(key, root);
100                 }
101             }
102         }
103 
104         mMounts.clear();
105 
106         if(map.isEmpty()){
107                  map.put(SD_CARD, Environment.getExternalStorageDirectory());
108         }
109         return map;
110     }
111 }