@SuppressLint("NewApi") private void listRoots() { currentDir = null; items.clear(); HashSet<String> paths = new HashSet<>(); String defaultPath = Environment.getExternalStorageDirectory().getPath(); boolean isDefaultPathRemovable = Build.VERSION.SDK_INT >= 9 && Environment.isExternalStorageRemovable(); String defaultPathState = Environment.getExternalStorageState(); if (defaultPathState.equals(Environment.MEDIA_MOUNTED) || defaultPathState.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { ListItem ext = new ListItem(); if (Build.VERSION.SDK_INT < 9 || Environment.isExternalStorageRemovable()) { ext.title = LocaleController.getString("SdCard", R.string.SdCard); ext.icon = R.drawable.ic_external_storage; } else { ext.title = LocaleController.getString("InternalStorage", R.string.InternalStorage); ext.icon = R.drawable.ic_storage; } ext.subtitle = getRootSubtitle(defaultPath); ext.file = Environment.getExternalStorageDirectory(); items.add(ext); paths.add(defaultPath); } BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader("/proc/mounts")); String line; while ((line = bufferedReader.readLine()) != null) { if (line.contains("vfat") || line.contains("/mnt")) { FileLog.e("tmessages", line); StringTokenizer tokens = new StringTokenizer(line, " "); String unused = tokens.nextToken(); String path = tokens.nextToken(); if (paths.contains(path)) { continue; } if (line.contains("/dev/block/vold")) { if (!line.contains("/mnt/secure") && !line.contains("/mnt/asec") && !line.contains("/mnt/obb") && !line.contains("/dev/mapper") && !line.contains("tmpfs")) { if (!new File(path).isDirectory()) { int index = path.lastIndexOf('/'); if (index != -1) { String newPath = "/storage/" + path.substring(index + 1); if (new File(newPath).isDirectory()) { path = newPath; } } } paths.add(path); try { ListItem item = new ListItem(); if (path.toLowerCase().contains("sd")) { item.title = LocaleController.getString("SdCard", R.string.SdCard); } else { item.title = LocaleController.getString("ExternalStorage", R.string.ExternalStorage); } item.icon = R.drawable.ic_external_storage; item.subtitle = getRootSubtitle(path); item.file = new File(path); items.add(item); } catch (Exception e) { FileLog.e("tmessages", e); } } } } } } catch (Exception e) { FileLog.e("tmessages", e); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (Exception e) { FileLog.e("tmessages", e); } } } ListItem fs = new ListItem(); fs.title = "/"; fs.subtitle = LocaleController.getString("SystemRoot", R.string.SystemRoot); fs.icon = R.drawable.ic_directory; fs.file = new File("/"); items.add(fs); try { File telegramPath = new File(Environment.getExternalStorageDirectory(), "Telegram"); if (telegramPath.exists()) { fs = new ListItem(); fs.title = "Telegram"; fs.subtitle = telegramPath.toString(); fs.icon = R.drawable.ic_directory; fs.file = telegramPath; items.add(fs); } } catch (Exception e) { FileLog.e("tmessages", e); } fs = new ListItem(); fs.title = LocaleController.getString("Gallery", R.string.Gallery); fs.subtitle = LocaleController.getString("GalleryInfo", R.string.GalleryInfo); fs.icon = R.drawable.ic_storage_gallery; fs.file = null; items.add(fs); AndroidUtilities.clearDrawableAnimation(listView); scrolling = true; listAdapter.notifyDataSetChanged(); }
private boolean listFiles(File dir) { if (!dir.canRead()) { if (dir.getAbsolutePath().startsWith(Environment.getExternalStorageDirectory().toString()) || dir.getAbsolutePath().startsWith("/sdcard") || dir.getAbsolutePath().startsWith("/mnt/sdcard")) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { currentDir = dir; items.clear(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_SHARED.equals(state)) { emptyView.setText(LocaleController.getString("UsbActive", R.string.UsbActive)); } else { emptyView.setText(LocaleController.getString("NotMounted", R.string.NotMounted)); } AndroidUtilities.clearDrawableAnimation(listView); scrolling = true; listAdapter.notifyDataSetChanged(); return true; } } showErrorBox(LocaleController.getString("AccessError", R.string.AccessError)); return false; } emptyView.setText(LocaleController.getString("NoFiles", R.string.NoFiles)); File[] files; try { files = dir.listFiles(); } catch (Exception e) { showErrorBox(e.getLocalizedMessage()); return false; } if (files == null) { showErrorBox(LocaleController.getString("UnknownError", R.string.UnknownError)); return false; } currentDir = dir; items.clear(); Arrays.sort( files, new Comparator<File>() { @Override public int compare(File lhs, File rhs) { if (lhs.isDirectory() != rhs.isDirectory()) { return lhs.isDirectory() ? -1 : 1; } return lhs.getName().compareToIgnoreCase(rhs.getName()); /*long lm = lhs.lastModified(); long rm = lhs.lastModified(); if (lm == rm) { return 0; } else if (lm > rm) { return -1; } else { return 1; }*/ } }); for (File file : files) { if (file.getName().startsWith(".")) { continue; } ListItem item = new ListItem(); item.title = file.getName(); item.file = file; if (file.isDirectory()) { item.icon = R.drawable.ic_directory; item.subtitle = LocaleController.getString("Folder", R.string.Folder); } else { String fname = file.getName(); String[] sp = fname.split("\\."); item.ext = sp.length > 1 ? sp[sp.length - 1] : "?"; item.subtitle = AndroidUtilities.formatFileSize(file.length()); fname = fname.toLowerCase(); if (fname.endsWith(".jpg") || fname.endsWith(".png") || fname.endsWith(".gif") || fname.endsWith(".jpeg")) { item.thumb = file.getAbsolutePath(); } } items.add(item); } ListItem item = new ListItem(); item.title = ".."; if (history.size() > 0) { HistoryEntry entry = history.get(history.size() - 1); if (entry.dir == null) { item.subtitle = LocaleController.getString("Folder", R.string.Folder); } else { item.subtitle = entry.dir.toString(); } } else { item.subtitle = LocaleController.getString("Folder", R.string.Folder); } item.icon = R.drawable.ic_directory; item.file = null; items.add(0, item); AndroidUtilities.clearDrawableAnimation(listView); scrolling = true; listAdapter.notifyDataSetChanged(); return true; }