From 187570a1ada271ff9dd50d3925ca0521796d5bc0 Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Tue, 8 Feb 2022 17:04:18 +0800 Subject: [PATCH] DocumentsProvider: use cursor extras for error when allowed instead of toast + exception, use cursor extras for error string, as reference recommends. DocumentsUI handles it and displays to user. --- .../safcephfs/CephFSDocumentsProvider.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java index 71e5b8a..71c6fcc 100644 --- a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java +++ b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.database.Cursor; import android.database.MatrixCursor; +import android.os.Bundle; import android.os.CancellationSignal; import android.os.Handler; import android.os.HandlerThread; @@ -14,6 +15,7 @@ import android.os.Process; import android.os.storage.StorageManager; import android.os.StrictMode; import android.preference.PreferenceManager; +import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; import android.provider.DocumentsContract.Root; import android.provider.DocumentsProvider; @@ -119,14 +121,29 @@ public class CephFSDocumentsProvider extends DocumentsProvider { T execute() throws IOException; } + private void throwOrAddErrorExtra(String error, Cursor cursor) { + if (cursor != null) { + Bundle extra = new Bundle(); + extra.putString(DocumentsContract.EXTRA_ERROR, error); + cursor.setExtras(extra); + } else { + toast(error); + throw new IllegalStateException(error); + } + } + private T doCephOperation(CephOperation op) { + return doCephOperation(op, null); + } + + private T doCephOperation(CephOperation op, Cursor cursor) { if (cm == null) { try { cm = setupCeph(); } catch (IOException e) { - String error = "Unable to mount root: " + e.toString(); - toast(error); - throw new IllegalStateException(error); + throwOrAddErrorExtra("Unable to mount root: " + e.getMessage(), + cursor); + return null; } } int r = retries; @@ -147,14 +164,17 @@ public class CephFSDocumentsProvider extends DocumentsProvider { } }.execute(); } catch (IOException e2) { - toast("Unable to remount root: " + e2.toString()); + toast("Unable to remount root: " + e2); } } else { - throw new IllegalStateException("ESHUTDOWN"); + throwOrAddErrorExtra("Operation failed: " + + e.getMessage(), cursor); + return null; } } else { - toast("Operation failed: " + e.getMessage()); - throw new IllegalStateException(e.getMessage()); + throwOrAddErrorExtra("Operation failed: " + e.getMessage(), + cursor); + return null; } } } @@ -323,7 +343,10 @@ public class CephFSDocumentsProvider extends DocumentsProvider { Log.e(APP_NAME, "queryChildDocuments " + parentDocumentId + " not found"); throw new FileNotFoundException(parentDocumentId + " not found"); } - }); + }, result); + if (res == null) { + return result; + } for (String entry : res) { lstatBuildDocumentRow(filename + "/" + entry, entry, parentDocumentId + "/" + entry, result); -- 2.43.0