-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Fix install logic
- Loading branch information
Showing
24 changed files
with
182 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/main/java/androidx/documentfile/provider/DocumentUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package androidx.documentfile.provider; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.provider.DocumentsContract; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DocumentUtils { | ||
public interface IFileFilter { | ||
boolean accept(String name); | ||
} | ||
private static TreeDocumentFile findFile(Context context, TreeDocumentFile file, String name) { | ||
final ContentResolver resolver = context.getContentResolver(); | ||
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(file.getUri(), DocumentsContract.getDocumentId(file.getUri())); | ||
Cursor c = null; | ||
try { | ||
c = resolver.query(childrenUri, new String[] {DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME}, null, null, null); | ||
while (c.moveToNext()) { | ||
final String documentName = c.getString(1); | ||
if (TextUtils.equals(name, documentName)) { | ||
final String documentId = c.getString(0); | ||
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(file.getUri(), documentId); | ||
return new TreeDocumentFile(file, context, documentUri); | ||
} | ||
} | ||
} catch (Exception e) { | ||
Log.w("DocumentUtils", "Failed query: " + e); | ||
} finally { | ||
if (c != null) { | ||
c.close(); | ||
} | ||
} | ||
return null; | ||
} | ||
public static DocumentFile findFile(Context context, DocumentFile documentFile, String name) { | ||
if (documentFile instanceof TreeDocumentFile) | ||
return findFile(context, (TreeDocumentFile)documentFile, name); | ||
return documentFile.findFile(name); | ||
} | ||
private static List<DocumentFile> filterFiles(Context context, TreeDocumentFile file, IFileFilter filter) { | ||
ContentResolver resolver = context.getContentResolver(); | ||
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(file.getUri(), DocumentsContract.getDocumentId(file.getUri())); | ||
List<DocumentFile> filtered = new ArrayList<>(); | ||
Cursor c = null; | ||
try { | ||
c = resolver.query(childrenUri, new String[] {DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME}, null, null, null); | ||
while (c.moveToNext()) { | ||
String documentName = c.getString(1); | ||
String documentId = c.getString(0); | ||
Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(file.getUri(), documentId); | ||
TreeDocumentFile child = new TreeDocumentFile(file, context, documentUri); | ||
if (child.isDirectory()) | ||
filtered.addAll(filterFiles(context, child, filter)); | ||
else if (filter.accept(documentName)) | ||
filtered.add(child); | ||
} | ||
} catch (Exception e) { | ||
Log.w("DocumentUtils", "Failed query: " + e); | ||
} finally { | ||
if (c != null) { | ||
c.close(); | ||
} | ||
} | ||
return filtered; | ||
} | ||
public static List<DocumentFile> filterFiles(Context context, DocumentFile documentFile, IFileFilter filter) { | ||
if (documentFile instanceof TreeDocumentFile) | ||
return filterFiles(context, (TreeDocumentFile)documentFile, filter); | ||
List<DocumentFile> filtered = new ArrayList<>(); | ||
DocumentFile[] files = documentFile.listFiles(); | ||
if (files != null) { | ||
for (DocumentFile file : files) { | ||
if (filter.accept(file.getName())) | ||
filtered.add(file); | ||
} | ||
} | ||
return filtered; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.