Skip to content

Commit 9e565aa

Browse files
committed
Updating the sample / demo application sources
1 parent 85d1004 commit 9e565aa

9 files changed

Lines changed: 108 additions & 50 deletions

File tree

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/BasicFileProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ public void selectBaseDirectoryByType(FileChooserBuilder.BaseFolderPathType base
179179
}
180180
}
181181

182+
public boolean enterNextSubfolder(String subfolderPath) {
183+
File nextSubfolder = new File(baseDirPath, subfolderPath);
184+
boolean status = nextSubfolder.exists() && nextSubfolder.isDirectory();
185+
if(status) {
186+
baseDirPath = nextSubfolder;
187+
}
188+
return status;
189+
}
190+
182191
@Override
183192
public boolean onCreate() {
184193
if(fileProviderStaticInst == null) {

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/DisplayAdapters.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,30 @@ public boolean onLongClick(View v) {
201201
DisplayTypes.FileType fileItem = getFileItemForView(v);
202202
if(fileItem != null && fileItem.isDirectory()) {
203203
// Recursively descend into the clicked directory location:
204-
DisplayTypes.DirectoryResultContext nextFolder = fileItem.getParentFolderContext();
205-
if(nextFolder == null) {
204+
DisplayTypes.DirectoryResultContext nextFolderParent = fileItem.getParentFolderContext();
205+
if(nextFolderParent == null) {
206206
return false;
207207
}
208208
int fileItemPosIndex = getPositionForView(v);
209-
DisplayTypes.DirectoryResultContext workingFolder = DisplayFragments.getInstance().pathHistoryStack.peek();
210-
DisplayFragments.getInstance().pathHistoryStack.push(nextFolder);
209+
DisplayTypes.DirectoryResultContext workingFolder;
210+
if(DisplayFragments.getInstance().pathHistoryStack.empty()) {
211+
workingFolder = null;
212+
}
213+
else {
214+
workingFolder = DisplayFragments.getInstance().pathHistoryStack.peek();
215+
}
216+
nextFolderParent.clearDirectoryContentsList();
217+
DisplayFragments.getInstance().pathHistoryStack.push(nextFolderParent);
211218
if(workingFolder == null) {
212-
nextFolder.loadNextFolderAtIndex(fileItemPosIndex, true);
219+
DisplayTypes.DirectoryResultContext nextFolder = nextFolderParent.loadNextFolderAtIndex(fileItemPosIndex, true);
220+
nextFolder = DisplayTypes.DirectoryResultContext.probeAtCursoryFolderQuery(nextFolder.getCWDBasePath());
221+
DisplayFragments.getInstance().pathHistoryStack.push(nextFolder);
213222
DisplayFragments.getInstance().descendIntoNextDirectory(true);
214223
}
215224
else {
216-
nextFolder.loadNextFolderAtIndex(fileItemPosIndex, false);
225+
DisplayTypes.DirectoryResultContext nextFolder = nextFolderParent.loadNextFolderAtIndex(fileItemPosIndex, false);
226+
nextFolder = DisplayTypes.DirectoryResultContext.probeAtCursoryFolderQuery(nextFolder.getCWDBasePath());
227+
DisplayFragments.getInstance().pathHistoryStack.push(nextFolder);
217228
DisplayFragments.getInstance().descendIntoNextDirectory(false);
218229
}
219230
String displayRecurseMsg = String.format(Locale.getDefault(), "Descending recursively into DIR \"%s\".", fileItem.getBaseName());
@@ -257,20 +268,18 @@ public static class OnSelectListener implements CompoundButton.OnCheckedChangeLi
257268
private static String LOGTAG = OnSelectListener.class.getSimpleName();
258269

259270
public static boolean performNewFileItemClick(CheckBox cbView, DisplayTypes.FileType fileItem) {
260-
Log.i(LOGTAG, String.format(Locale.getDefault(), "INIT PERFORM CLICK: (selected, max allowed) = (%d, %d)",
271+
Log.d(LOGTAG, String.format(Locale.getDefault(), "INIT PERFORM CLICK: (selected, max allowed) = (%d, %d)",
261272
DisplayFragments.getInstance().curSelectionCount, DisplayFragments.getInstance().maxAllowedSelections));
262273
if(cbView == null || fileItem == null) {
263274
return false;
264275
}
265276
boolean isDir = fileItem.isDirectory();
266277
if(!isDir && !DisplayFragments.getInstance().allowSelectFiles) {
267-
Log.i(LOGTAG, "Blocking FILE item selection I");
268278
cbView.setChecked(false);
269279
cbView.jumpDrawablesToCurrentState(); // No animations
270280
return false;
271281
}
272282
else if(isDir && !DisplayFragments.getInstance().allowSelectFolders) {
273-
Log.i(LOGTAG, "Blocking DIR item selection II");
274283
cbView.setChecked(false);
275284
cbView.jumpDrawablesToCurrentState();
276285
return false;
@@ -288,8 +297,7 @@ else if(isDir && !DisplayFragments.getInstance().allowSelectFolders) {
288297
cbView.setEnabled(true);
289298
DisplayFragments.getInstance().activeSelectionsList.remove(fileItem);
290299
DisplayFragments.getInstance().curSelectionCount--;
291-
Log.i(LOGTAG, "DE-Selected next checkbox (file item)");
292-
Log.i(LOGTAG, String.format(Locale.getDefault(), "RETURNING PERFORM CLICK: (selected, max allowed) = (%d, %d)",
300+
Log.d(LOGTAG, String.format(Locale.getDefault(), "RETURNING PERFORM CLICK: (selected, max allowed) = (%d, %d)",
293301
DisplayFragments.getInstance().curSelectionCount, DisplayFragments.getInstance().maxAllowedSelections));
294302
return true;
295303
}
@@ -303,8 +311,7 @@ else if(DisplayFragments.getInstance().curSelectionCount >= DisplayFragments.get
303311
cbView.setEnabled(true);
304312
DisplayFragments.getInstance().activeSelectionsList.add(fileItem);
305313
DisplayFragments.getInstance().curSelectionCount++;
306-
Log.i(LOGTAG, "Selected next checkbox (file item)");
307-
Log.i(LOGTAG, String.format(Locale.getDefault(), "RETURNING PERFORM CLICK: (selected, max allowed) = (%d, %d)",
314+
Log.d(LOGTAG, String.format(Locale.getDefault(), "RETURNING PERFORM CLICK: (selected, max allowed) = (%d, %d)",
308315
DisplayFragments.getInstance().curSelectionCount, DisplayFragments.getInstance().maxAllowedSelections));
309316
return true;
310317
}

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/DisplayFragments.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void setCwdFolderContext(DisplayTypes.DirectoryResultContext nextCwdCtx)
8686
public List<String> fileItemBasePathsList = new ArrayList<String>();
8787
public Stack<DisplayTypes.DirectoryResultContext> pathHistoryStack;
8888

89-
public static final int DEFAULT_VIEWPORT_FILE_ITEMS_COUNT = 25; // set large enough to overfill the window on first load
89+
public static final int DEFAULT_VIEWPORT_FILE_ITEMS_COUNT = 50; // set large enough to overfill the window on first load
9090
private int viewportMaxFileItemsCount = DEFAULT_VIEWPORT_FILE_ITEMS_COUNT;
9191
public int fileItemDisplayHeight = 0;
9292

@@ -170,14 +170,22 @@ public void descendIntoNextDirectory(boolean initNewFileTree) {
170170
FileChooserException.GenericRuntimeErrorException rte = new FileChooserException.GenericRuntimeErrorException("Empty context for folder history ( no more history ??? )");
171171
FileChooserActivity.getInstance().postSelectedFilesActivityResult(rte);
172172
}
173-
DisplayTypes.DirectoryResultContext nextFolder = pathHistoryStack.peek();
173+
DisplayTypes.DirectoryResultContext nextFolder = pathHistoryStack.pop();
174174
if(nextFolder != null) {
175175

176+
// Stop the prefetch thread for the current directory:
177+
FileChooserActivity.getInstance().stopPrefetchFileUpdatesThread();
178+
176179
// Completely clear out the previously displayed contents:
177180
FileChooserRecyclerView mainRV = getMainRecyclerView();
181+
DisplayAdapters.FileListAdapter rvAdapter = (DisplayAdapters.FileListAdapter) mainRV.getAdapter();
182+
int priorAdapterCount = rvAdapter.getItemCount();
183+
rvAdapter.reloadDataSets(new ArrayList<String>(), new ArrayList<DisplayTypes.FileType>(), false);
184+
rvAdapter.notifyItemRangeRemoved(0, priorAdapterCount);
185+
rvAdapter.notifyDataSetChanged();
178186
mainRV.removeAllViews();
179187
mainRV.removeAllViewsInLayout();
180-
// ??? Need to also clear out the adapter contents ???
188+
mainRV.invalidate();
181189

182190
// Descend into the next directory:
183191
lastFileDataStartIndex = 0;
@@ -189,6 +197,9 @@ public void descendIntoNextDirectory(boolean initNewFileTree) {
189197
DisplayFragments.FolderNavigationFragment.dirsOneBackText.setText(folderHistoryOneBackPath);
190198
DisplayFragments.FolderNavigationFragment.dirsTwoBackText.setText(folderHistoryTwoBackPath);
191199

200+
// Restart the prefetch thread for the current directory:
201+
FileChooserActivity.getInstance().startPrefetchFileUpdatesThread();
202+
192203
}
193204
else {
194205
Log.i(LOGTAG, "descendIntoNextDirectory: CWD Ctx is NULL!");

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/DisplayTypes.java

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public DirectoryResultContext(MatrixCursor mcResult, String parentFolderDocId, S
8080

8181
public int getFolderChildCount() { return folderMaxChildCount; }
8282

83+
public String getCWDBasePath() {
84+
return FileUtils.getFileBaseNameFromPath(activeCWDAbsPath);
85+
}
86+
8387
public void computeDirectoryContents(int startIndexPos, int maxIndexPos,
8488
int trimFromFrontCount, int trimFromBackCount, int newItemsCount,
8589
boolean updateGlobalIndices) {
@@ -96,12 +100,12 @@ public void computeDirectoryContents(int startIndexPos, int maxIndexPos,
96100
if(newItemsCount > 0) {
97101
fpInst.setFilesStartIndex(maxIndexPos + 1 - Math.abs(newItemsCount));
98102
fpInst.setFilesListLength(Math.abs(newItemsCount));
99-
Log.i(LOGTAG, "REQUESTING start index = " + (maxIndexPos + 1 - Math.abs(newItemsCount)) + ", LEN = " + Math.abs(newItemsCount));
103+
Log.d(LOGTAG, "REQUESTING start index = " + (maxIndexPos + 1 - Math.abs(newItemsCount)) + ", LEN = " + Math.abs(newItemsCount));
100104
}
101105
else {
102106
fpInst.setFilesStartIndex(startIndexPos);
103107
fpInst.setFilesListLength(Math.abs(newItemsCount));
104-
Log.i(LOGTAG, "REQUESTING start index = " + startIndexPos + ", LEN = " + Math.abs(newItemsCount));
108+
Log.d(LOGTAG, "REQUESTING start index = " + startIndexPos + ", LEN = " + Math.abs(newItemsCount));
105109
}
106110
int initStartIndexPos = startIndexPos;
107111
try {
@@ -132,16 +136,16 @@ public void computeDirectoryContents(int startIndexPos, int maxIndexPos,
132136
}
133137
if(updateGlobalIndices) {
134138
int resultSizeDiff = Math.abs(newItemsCount) - mcRowIdx;
135-
Log.i(LOGTAG, String.format(Locale.getDefault(), "UPDATING GLOBAL INDICES: [%d, %d] -> [%d, %d]",
139+
Log.d(LOGTAG, String.format(Locale.getDefault(), "UPDATING GLOBAL INDICES: [%d, %d] -> [%d, %d]",
136140
DisplayFragments.getInstance().lastFileDataStartIndex, DisplayFragments.getInstance().lastFileDataEndIndex,
137141
initStartIndexPos, maxIndexPos - resultSizeDiff));
138142
DisplayFragments.getInstance().lastFileDataStartIndex = initStartIndexPos;
139143
DisplayFragments.getInstance().lastFileDataEndIndex = maxIndexPos - resultSizeDiff;
140144
}
141145
setNextDirectoryContents(filesDataList);
142-
Log.i(LOGTAG, "computeDirectoryContents: PRINTING NEXT (truncated) folder contents list:");
146+
Log.d(LOGTAG, "computeDirectoryContents: PRINTING NEXT (truncated) folder contents list:");
143147
for(int fcidx = Math.max(0, directoryContentsList.size() - 3); fcidx < directoryContentsList.size(); fcidx++) {
144-
Log.i(LOGTAG, String.format(Locale.getDefault(), " [#%02d => %02d ACTUAL Idx] FILE BASE NAME => \"%s\" ... ", fcidx + 1,
148+
Log.d(LOGTAG, String.format(Locale.getDefault(), " [#%02d => %02d ACTUAL Idx] FILE BASE NAME => \"%s\" ... ", fcidx + 1,
145149
fcidx + 1 + DisplayFragments.getInstance().lastFileDataStartIndex, directoryContentsList.get(fcidx).getBaseName()));
146150
}
147151
}
@@ -167,30 +171,24 @@ public DirectoryResultContext loadNextFolderAtIndex(int posIndex, boolean initNe
167171
BasicFileProvider fpInst = BasicFileProvider.getInstance();
168172
DisplayFragments.updateFolderHistoryPaths(FileUtils.getFileBaseNameFromPath(activeCWDAbsPath), initNewFileTree);
169173
try {
170-
fpInst.updateQueryFilesList(); // cancel any previously pending noUpdate requests
171-
FileType requestedFileItem = directoryContentsList.get(posIndex);
172-
String nextActiveDocId = requestedFileItem.getFileProviderDocumentId();
173-
MatrixCursor nextDirCursor = (MatrixCursor) fpInst.queryChildDocuments(nextActiveDocId, BasicFileProvider.DEFAULT_DOCUMENT_PROJECTION, "");
174-
clearDirectoryContentsList();
175-
DirectoryResultContext nextFolderCtx = new DirectoryResultContext(nextDirCursor, requestedFileItem.getFileProviderDocumentId(), requestedFileItem.getAbsolutePath());
176-
return nextFolderCtx;
174+
// Load the document ID for the current position in case it is out of range:
175+
computeDirectoryContents(posIndex, posIndex);
176+
FileType selectedFileItem = directoryContentsList.get(0);
177+
return probeAtCursoryFolderQuery(selectedFileItem.getBaseName());
178+
//String nextActiveDocId = selectedFileItem.getFileProviderDocumentId();
179+
//MatrixCursor nextDirCursor = (MatrixCursor) fpInst.queryChildDocuments(nextActiveDocId, BasicFileProvider.DEFAULT_DOCUMENT_PROJECTION, "");
180+
//clearDirectoryContentsList();
181+
//DirectoryResultContext nextFolderCtx = new DirectoryResultContext(nextDirCursor, selectedFileItem.getFileProviderDocumentId(), selectedFileItem.getAbsolutePath());
182+
//return nextFolderCtx;
177183
} catch(Exception ioe) {
178184
ioe.printStackTrace();
179185
DisplayFragments.getInstance().pathHistoryStack.pop();
180186
return null;
181187
}
182188
}
183189

184-
public static DirectoryResultContext probeAtCursoryFolderQuery(FileChooserBuilder.BaseFolderPathType baseFolderChoice) {
190+
private static DirectoryResultContext probeAtCursoryFolderQueryGetNext() {
185191
BasicFileProvider fpInst = BasicFileProvider.getInstance();
186-
if(fpInst == null) {
187-
return null;
188-
}
189-
else {
190-
fpInst.setCustomFileFilter(DisplayFragments.getInstance().localFilesListFilter);
191-
fpInst.setCustomFolderSort(DisplayFragments.getInstance().localFilesListSortFunc);
192-
}
193-
fpInst.selectBaseDirectoryByType(baseFolderChoice);
194192
try {
195193
fpInst.updateQueryFilesList(); // cancel any previously pending noUpdate requests
196194
MatrixCursor cursoryProbe = (MatrixCursor) fpInst.queryRoots(BasicFileProvider.DEFAULT_ROOT_PROJECTION);
@@ -208,6 +206,35 @@ public static DirectoryResultContext probeAtCursoryFolderQuery(FileChooserBuilde
208206
}
209207
}
210208

209+
public static DirectoryResultContext probeAtCursoryFolderQuery(FileChooserBuilder.BaseFolderPathType baseFolderChoice) {
210+
BasicFileProvider fpInst = BasicFileProvider.getInstance();
211+
if(fpInst == null) {
212+
return null;
213+
}
214+
else {
215+
fpInst.setCustomFileFilter(DisplayFragments.getInstance().localFilesListFilter);
216+
fpInst.setCustomFolderSort(DisplayFragments.getInstance().localFilesListSortFunc);
217+
}
218+
fpInst.selectBaseDirectoryByType(baseFolderChoice);
219+
return probeAtCursoryFolderQueryGetNext();
220+
}
221+
222+
public static DirectoryResultContext probeAtCursoryFolderQuery(String nextSubfolderPath) {
223+
BasicFileProvider fpInst = BasicFileProvider.getInstance();
224+
if(fpInst == null) {
225+
return null;
226+
}
227+
else {
228+
fpInst.setCustomFileFilter(DisplayFragments.getInstance().localFilesListFilter);
229+
fpInst.setCustomFolderSort(DisplayFragments.getInstance().localFilesListSortFunc);
230+
}
231+
if(!fpInst.enterNextSubfolder(nextSubfolderPath)) {
232+
return null;
233+
}
234+
Log.i(LOGTAG, "ENTERING subfolder \"" + nextSubfolderPath + "\" ...");
235+
return probeAtCursoryFolderQueryGetNext();
236+
}
237+
211238
}
212239

213240
public static class FileType {

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/FileChooserActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public class FileChooserActivity extends AppCompatActivity implements EasyPermis
6363

6464
private PrefetchFilesUpdater prefetchFilesUpdaterInst;
6565
public void startPrefetchFileUpdatesThread() {
66+
if(prefetchFilesUpdaterInst.isAlive()) {
67+
prefetchFilesUpdaterInst.interrupt();
68+
}
69+
prefetchFilesUpdaterInst = new PrefetchFilesUpdater();
6670
prefetchFilesUpdaterInst.start();
6771
}
6872
public void stopPrefetchFileUpdatesThread() { prefetchFilesUpdaterInst.interrupt(); }

AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/PrefetchFilesUpdater.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,19 @@ public UpdateDataStruct(UpdateDataType dataType, List<String> fileNames, List<Di
114114
}
115115

116116
private int BalancedBufferSize;
117-
private boolean isInit;
118117
private int topBufferSize;
119118
private int bottomBufferSize;
119+
private boolean isInit;
120120

121121
public PrefetchFilesUpdater() {
122122

123123
// Set a sane default with some scroll buffer space:
124124
// My testing Android phone comfortably fits 12-15 layout items.
125-
// Let's buffer in 25 by default to not have to restruct the scroller
125+
// Let's buffer in 35 by default to not have to restruct the scroller
126126
// and default fling velocities too much:
127-
BalancedBufferSize = 25;
128-
isInit = false;
127+
BalancedBufferSize = 35;
129128
topBufferSize = bottomBufferSize = 0;
129+
isInit = false;
130130

131131
setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
132132
public void uncaughtException(Thread threadRef, Throwable ex) {
@@ -262,7 +262,7 @@ protected void onPreExecute() {
262262
firstVisibleIndex = getLayoutFirstVisibleItemIndex();
263263
lastVisibleIndex = getLayoutLastVisibleItemIndex();
264264

265-
Log.i(LOGTAG, String.format(Locale.getDefault(),
265+
Log.d(LOGTAG, String.format(Locale.getDefault(),
266266
"PrefetchUpdaterThread: CHECK FOR UPDATES: Visible[%d, %d] ;; ToBalance[%d, %d] (%d, %d);; ItemsCount = %d, DirLen = %d",
267267
firstVisibleIndex, lastVisibleIndex,
268268
balanceTopCount, balanceBottomCount,
@@ -291,7 +291,7 @@ protected void onPostExecute(Long result) {
291291

292292
if(updateDataType.equals(UpdateDataStruct.UpdateDataType.APPEND_DATA_TO_BOTTOM)) {
293293

294-
Log.i(LOGTAG, String.format(Locale.getDefault(),
294+
Log.d(LOGTAG, String.format(Locale.getDefault(),
295295
"POSTING update to RecyclerView: APPEND #%d data items to BOTTOM, RM #%d from TOP",
296296
itemsAppendedCount, itemsPrunedCount));
297297
int prevFirstVisibleIndex = firstVisibleIndex;
@@ -314,7 +314,7 @@ protected void onPostExecute(Long result) {
314314
}
315315
else if(updateDataType.equals(UpdateDataStruct.UpdateDataType.PREPEND_DATA_AT_TOP)) { // Prepend items to top, trim the extra from the bottom:
316316

317-
Log.i(LOGTAG, String.format(Locale.getDefault(),
317+
Log.d(LOGTAG, String.format(Locale.getDefault(),
318318
"POSTING update to RecyclerView: PREPEND #%d data items to TOP, RM #%02d from BOTTOM",
319319
itemsAppendedCount, itemsPrunedCount));
320320
int prevFirstVisibleIndex = firstVisibleIndex;
214 KB
Loading

0 commit comments

Comments
 (0)