Revert "Android: Improvements to DocumentProvider Added move and rename methods Provider notifies viewer to refresh view when files are changed Bumped up TargetApi annotation for DocumentsContract.buildTreeDocumentUri. Could alternatively use androidx compat class."

This reverts commit 72db30128c.
This commit is contained in:
LibretroAdmin 2024-07-01 18:40:40 -07:00
parent e91f9ab641
commit 5ee1e16595
1 changed files with 7 additions and 53 deletions

View File

@ -11,7 +11,6 @@ import android.graphics.Point;
import android.os.Build; import android.os.Build;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document; import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root; import android.provider.DocumentsContract.Root;
import android.provider.DocumentsProvider; import android.provider.DocumentsProvider;
@ -36,13 +35,11 @@ import java.util.LinkedList;
* offering two different ways of accessing your stored data. This would be confusing for users." * offering two different ways of accessing your stored data. This would be confusing for users."
* - http://developer.android.com/guide/topics/providers/document-provider.html#43 * - http://developer.android.com/guide/topics/providers/document-provider.html#43
*/ */
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.KITKAT)
public class RetroDocumentsProvider extends DocumentsProvider { public class RetroDocumentsProvider extends DocumentsProvider {
private static final String ALL_MIME_TYPES = "*/*"; private static final String ALL_MIME_TYPES = "*/*";
private String DOCUMENTS_AUTHORITY;
// The default columns to return information about a root if no specific // The default columns to return information about a root if no specific
// columns are requested in a query. // columns are requested in a query.
private static final String[] DEFAULT_ROOT_PROJECTION = new String[]{ private static final String[] DEFAULT_ROOT_PROJECTION = new String[]{
@ -92,36 +89,6 @@ public class RetroDocumentsProvider extends DocumentsProvider {
return result; return result;
} }
@Override
public String moveDocument(String sourceDocumentId, String sourceParentDocumentId, String targetParentDocumentId) throws FileNotFoundException {
File sourceFile = getFileForDocId(sourceDocumentId);
File targetParentFile = getFileForDocId(targetParentDocumentId);
File destination = new File(targetParentFile, sourceFile.getName());
boolean success = sourceFile.renameTo(destination);
if(!success){
throw new FileNotFoundException("Failed to move file " + sourceDocumentId + " to " + targetParentDocumentId);
}
getContext().getContentResolver().notifyChange(DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, sourceParentDocumentId), null);
getContext().getContentResolver().notifyChange(DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, targetParentDocumentId), null);
return getDocIdForFile(destination);
}
@Override
public String renameDocument(String documentId, String displayName) throws FileNotFoundException{
File document = getFileForDocId(documentId);
File destination = new File(document.getParentFile(), displayName);
boolean success = document.renameTo(destination);
if(!success){
throw new FileNotFoundException("Failed to rename file " + documentId + " to " + displayName);
}
getContext().getContentResolver().notifyChange(DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, getDocIdForFile(document.getParentFile())), null);
return getDocIdForFile(destination);
}
@Override @Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException { public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
@ -129,7 +96,6 @@ public class RetroDocumentsProvider extends DocumentsProvider {
for (File file : parent.listFiles()) { for (File file : parent.listFiles()) {
includeFile(result, null, file); includeFile(result, null, file);
} }
result.setNotificationUri(getContext().getContentResolver(), DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, parentDocumentId));
return result; return result;
} }
@ -149,7 +115,6 @@ public class RetroDocumentsProvider extends DocumentsProvider {
@Override @Override
public boolean onCreate() { public boolean onCreate() {
DOCUMENTS_AUTHORITY = getContext().getPackageName() + ".documents";
return true; return true;
} }
@ -173,25 +138,14 @@ public class RetroDocumentsProvider extends DocumentsProvider {
} catch (IOException e) { } catch (IOException e) {
throw new FileNotFoundException("Failed to create document with id " + newFile.getPath()); throw new FileNotFoundException("Failed to create document with id " + newFile.getPath());
} }
getContext().getContentResolver().notifyChange(DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, parentDocumentId), null);
return newFile.getPath(); return newFile.getPath();
} }
@Override @Override
public void deleteDocument(String documentId) throws FileNotFoundException { public void deleteDocument(String documentId) throws FileNotFoundException {
File file = getFileForDocId(documentId); File file = getFileForDocId(documentId);
rm_r(file); if (!file.delete()) {
getContext().getContentResolver().notifyChange(DocumentsContract.buildTreeDocumentUri(DOCUMENTS_AUTHORITY, getDocIdForFile(file.getParentFile())), null); throw new FileNotFoundException("Failed to delete document with id " + documentId);
}
void rm_r (File file) throws FileNotFoundException{
if(file.isDirectory()){
for(File child : file.listFiles()) {
rm_r(child);
}
}
if(!file.delete()){
throw new FileNotFoundException("Could not delete file " + file.getPath());
} }
} }
@ -294,11 +248,11 @@ public class RetroDocumentsProvider extends DocumentsProvider {
int flags = 0; int flags = 0;
if (file.isDirectory()) { if (file.isDirectory()) {
if (file.canWrite()) flags |= Document.FLAG_DIR_SUPPORTS_CREATE | Document.FLAG_SUPPORTS_RENAME; if (file.canWrite()) flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
} else { } else if (file.canWrite()) {
if (file.canWrite()) flags |= Document.FLAG_SUPPORTS_WRITE | Document.FLAG_SUPPORTS_RENAME; flags |= Document.FLAG_SUPPORTS_WRITE;
} }
if (file.getParentFile().canWrite()) flags |= Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_MOVE; if (file.getParentFile().canWrite()) flags |= Document.FLAG_SUPPORTS_DELETE;
final String displayName = file.getName(); final String displayName = file.getName();
final String mimeType = getMimeType(file); final String mimeType = getMimeType(file);