public FileInfo getRecentDir() { for (int i = 0; i < mRoot.dirCount(); i++) { if (mRoot.getDir(i).isRecentDir()) return mRoot.getDir(i); } L.w("Recent books directory not found!"); return null; }
public FileInfo getDownloadDirectory() { for (int i = 0; i < mRoot.dirCount(); i++) { FileInfo item = mRoot.getDir(i); if (!item.isSpecialDir() && !item.isArchive) { if (!item.isListed) listDirectory(item); FileInfo books = item.findItemByPathName(item.pathname + "/Books"); if (books == null) books = item.findItemByPathName(item.pathname + "/books"); if (books != null && books.exists()) return books; File dir = new File(item.getPathName()); if (dir.isDirectory()) { if (!dir.canWrite()) Log.w("cr3", "Directory " + dir + " is readonly"); File f = new File(dir, "Books"); if (f.mkdirs() || f.isDirectory()) { books = new FileInfo(f); books.parent = item; item.addDir(books); books.isScanned = true; books.isListed = true; return books; } } } } try { throw new Exception("download directory not found and cannot be created"); } catch (Exception e) { Log.e("cr3", "download directory is not found!!!", e); } return null; }
public FileInfo getOPDSRoot() { for (int i = 0; i < mRoot.dirCount(); i++) { if (mRoot.getDir(i).isOPDSRoot()) return mRoot.getDir(i); } L.w("OPDS root directory not found!"); return null; }
private FileInfo findRoot(String pathname) { String normalized = engine.getPathCorrector().normalizeIfPossible(pathname); for (int i = 0; i < mRoot.dirCount(); i++) { FileInfo dir = mRoot.getDir(i); if (normalized.equals(engine.getPathCorrector().normalizeIfPossible(dir.getPathName()))) return dir; } return null; }
/** * List directories in subtree, limited by runtime and depth; remove empty branches (w/o books). * * @param root is directory to start with * @param maxDepth is maximum depth * @param limitTs is limit for android.os.SystemClock.uptimeMillis() * @return true if completed, false if stopped by limit. */ private boolean listSubtree(FileInfo root, int maxDepth, long limitTs) { long ts = android.os.SystemClock.uptimeMillis(); if (ts > limitTs || maxDepth <= 0) return false; listDirectory(root); for (int i = root.dirCount() - 1; i >= -0; i--) { boolean res = listSubtree(root.getDir(i), maxDepth - 1, limitTs); if (!res) return false; } if (mHideEmptyDirs) root.removeEmptyDirs(); return true; }
/** * Lists all directories from root to directory of specified file, returns found directory. * * @param file * @param root * @return */ private FileInfo findParentInternal(FileInfo file, FileInfo root) { if (root == null || file == null || root.isRecentDir()) return null; if (!root.isRootDir() && !file.getPathName().startsWith(root.getPathName())) return null; // to list all directories starting root dir if (root.isDirectory && !root.isSpecialDir()) listDirectory(root); for (int i = 0; i < root.dirCount(); i++) { FileInfo found = findParentInternal(file, root.getDir(i)); if (found != null) return found; } for (int i = 0; i < root.fileCount(); i++) { if (root.getFile(i).getPathName().equals(file.getPathName())) return root; if (root.getFile(i).getPathName().startsWith(file.getPathName() + "@/")) return root; } return null; }
public FileInfo setSearchResults(FileInfo[] results) { FileInfo existingResults = null; for (int i = 0; i < mRoot.dirCount(); i++) { FileInfo dir = mRoot.getDir(i); if (dir.isSearchDir()) { existingResults = dir; dir.clear(); break; } } if (existingResults == null) { FileInfo dir = new FileInfo(); dir.isDirectory = true; dir.pathname = FileInfo.SEARCH_RESULT_DIR_TAG; dir.filename = mActivity.getResources().getString(R.string.dir_search_results); dir.parent = mRoot; dir.isListed = true; dir.isScanned = true; mRoot.addDir(dir); existingResults = dir; } for (FileInfo item : results) existingResults.addFile(item); return existingResults; }
/** * For all files in directory, retrieve metadata from DB or scan and save into DB. Call in GUI * thread only! * * @param baseDir is directory with files to lookup/scan; file items will be updated with info * from file metadata or DB * @param readyCallback is Runable to call when operation is finished or stopped (will be called * in GUI thread) * @param control allows to stop long operation */ private void scanDirectoryFiles( final CRDBService.LocalBinder db, final FileInfo baseDir, final ScanControl control, final Engine.ProgressControl progress, final Runnable readyCallback) { // GUI thread BackgroundThread.ensureGUI(); log.d("scanDirectoryFiles(" + baseDir.getPathName() + ") "); // store list of files to scan ArrayList<String> pathNames = new ArrayList<String>(); for (int i = 0; i < baseDir.fileCount(); i++) { pathNames.add(baseDir.getFile(i).getPathName()); } if (pathNames.size() == 0) { readyCallback.run(); return; } // list all subdirectories for (int i = 0; i < baseDir.dirCount(); i++) { if (control.isStopped()) break; listDirectory(baseDir.getDir(i)); } // load book infos for files db.loadFileInfos( pathNames, new CRDBService.FileInfoLoadingCallback() { @Override public void onFileInfoListLoaded(ArrayList<FileInfo> list) { log.v("onFileInfoListLoaded"); // GUI thread final ArrayList<FileInfo> filesForParsing = new ArrayList<FileInfo>(); ArrayList<FileInfo> filesForSave = new ArrayList<FileInfo>(); Map<String, FileInfo> mapOfFilesFoundInDb = new HashMap<String, FileInfo>(); for (FileInfo f : list) mapOfFilesFoundInDb.put(f.getPathName(), f); for (int i = 0; i < baseDir.fileCount(); i++) { FileInfo item = baseDir.getFile(i); FileInfo fromDB = mapOfFilesFoundInDb.get(item.getPathName()); if (fromDB != null) { // use DB value baseDir.setFile(i, fromDB); } else { // not found in DB if (item.format.canParseProperties()) { filesForParsing.add(new FileInfo(item)); } else { filesForSave.add(new FileInfo(item)); } } } if (filesForSave.size() > 0) { db.saveFileInfos(filesForSave); } if (filesForParsing.size() == 0 || control.isStopped()) { readyCallback.run(); return; } // scan files in Background thread BackgroundThread.instance() .postBackground( new Runnable() { @Override public void run() { // Background thread final ArrayList<FileInfo> filesForSave = new ArrayList<FileInfo>(); try { int count = filesForParsing.size(); for (int i = 0; i < count; i++) { if (control.isStopped()) break; progress.setProgress(i * 10000 / count); FileInfo item = filesForParsing.get(i); engine.scanBookProperties(item); filesForSave.add(item); } } catch (Exception e) { L.e("Exception while scanning", e); } progress.hide(); // jump to GUI thread BackgroundThread.instance() .postGUI( new Runnable() { @Override public void run() { // GUI thread try { if (filesForSave.size() > 0) { db.saveFileInfos(filesForSave); } for (FileInfo file : filesForSave) baseDir.setFile(file); } catch (Exception e) { L.e("Exception while scanning", e); } // call finish handler readyCallback.run(); } }); } }); } }); }