diff --git a/src/org/geometerplus/fbreader/Paths.java b/src/org/geometerplus/fbreader/Paths.java index 20af9cb56..49f2cd3eb 100644 --- a/src/org/geometerplus/fbreader/Paths.java +++ b/src/org/geometerplus/fbreader/Paths.java @@ -79,6 +79,63 @@ public abstract class Paths { } } + private static void addDirToList(List list, String candidate) { + if (candidate == null || !candidate.startsWith("/")) { + return; + } + for (int count = 0; count < 5; ++count) { + while (candidate.endsWith("/")) { + candidate = candidate.substring(0, candidate.length() - 1); + } + final File f = new File(candidate); + try { + final String canonical = f.getCanonicalPath(); + if (canonical.equals(candidate)) { + break; + } + candidate = canonical; + } catch (Throwable t) { + return; + } + } + while (candidate.endsWith("/")) { + candidate = candidate.substring(0, candidate.length() - 1); + } + if (!"".equals(candidate) && !list.contains(candidate) && new File(candidate).canRead()) { + list.add(candidate); + } + } + + public static List allCardDirectories() { + final List dirs = new LinkedList(); + dirs.add(cardDirectory()); + addDirToList(dirs, System.getenv("SECONDARY_STORAGE")); + /* + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader("/system/etc/vold.fstab")); + String line; + while ((line = reader.readLine()) != null) { + final int hashIndex = line.indexOf("#"); + if (hashIndex >= 0) { + line = line.substring(0, hashIndex); + } + final String[] parts = line.split("\\s+"); + if (parts.length >= 5) { + addDirToList(dirs, parts[2]); + } + } + } catch (Throwable e) { + } finally { + try { + reader.close(); + } catch (Throwable t) { + } + } + */ + return dirs; + } + public static String cardDirectory() { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { return Environment.getExternalStorageDirectory().getPath(); @@ -102,11 +159,9 @@ public abstract class Paths { } } catch (Throwable e) { } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - } + try { + reader.close(); + } catch (Throwable t) { } } @@ -137,7 +192,7 @@ public abstract class Paths { } public static List bookPath() { - final List path = new ArrayList(Paths.BookPathOption.getValue()); + final List path = new ArrayList(BookPathOption.getValue()); final String downloadsDirectory = DownloadsDirectoryOption.getValue(); if (!"".equals(downloadsDirectory) && !path.contains(downloadsDirectory)) { path.add(downloadsDirectory); diff --git a/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java b/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java index 0b1f427b4..ab313a30a 100644 --- a/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java +++ b/src/org/geometerplus/fbreader/library/FileFirstLevelTree.java @@ -19,6 +19,8 @@ package org.geometerplus.fbreader.library; +import java.util.List; + import org.geometerplus.zlibrary.core.filesystem.ZLFile; import org.geometerplus.zlibrary.core.resources.ZLResource; @@ -43,22 +45,32 @@ public class FileFirstLevelTree extends FirstLevelTree { public void waitForOpening() { clear(); for (String dir : Paths.BookPathOption.getValue()) { - addChild(dir, "fileTreeLibrary", dir); + addChild(dir, resource().getResource("fileTreeLibrary").getValue(), dir); + } + addChild("/", "fileTreeRoot"); + final List cards = Paths.allCardDirectories(); + if (cards.size() == 1) { + addChild(cards.get(0), "fileTreeCard"); + } else { + final ZLResource res = resource().getResource("fileTreeCard"); + final String title = res.getResource("withIndex").getValue(); + final String summary = res.getResource("summary").getValue(); + int index = 0; + for (String dir : cards) { + addChild(dir, title.replaceAll("%s", String.valueOf(++index)), summary); + } } - addChild("/", "fileTreeRoot", null); - addChild(Paths.cardDirectory(), "fileTreeCard", null); } - private void addChild(String path, String resourceKey, String summary) { + private void addChild(String path, String title, String summary) { final ZLFile file = ZLFile.createFileByPath(path); if (file != null) { - final ZLResource resource = resource().getResource(resourceKey); - new FileTree( - this, - file, - resource.getValue(), - summary != null ? summary : resource.getResource("summary").getValue() - ); + new FileTree(this, file, title, summary); } } + + private void addChild(String path, String resourceKey) { + final ZLResource resource = resource().getResource(resourceKey); + addChild(path, resource.getValue(), resource.getResource("summary").getValue()); + } }