src/main/java/de/blau/android/util/SavingHelper.java
/**
     * Exports an Exportable asynchronously, displaying a toast on success or failure
     * 
     * @param ctx context for the toast
     * @param exportable the exportable
     * @param uri Uri to write to
     */
Exports an Exportable asynchronously, displaying a toast on success or failure

@param ctx context for the toast
@param exportable the exportable
@param uri Uri to write to
Exports an Exportable asynchronously displaying a toast on success or failure @param ctx context for the toast @param exportable the exportable @param uri Uri to write to
public static void asyncExport(@NonNull final Context ctx, @NonNull final Exportable exportable, @NonNull Uri uri) {
        new ExecutorTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void param) {
                try (OutputStream outputStream = ctx.getContentResolver().openOutputStream(uri)) {
                    exportable.export(outputStream);
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Export failed - " + uri.toString());
                    return false;
                }
                return true;
            }
            @Override
            protected void onPostExecute(Boolean result) {
                try {
                    if (!result) { 
                        Snack.toastTopError(ctx, R.string.toast_export_failed);
                    } else {
                        Log.i(DEBUG_TAG, "Successful export to " + uri);
                        Snack.toastTopInfo(ctx, ctx.getResources().getString(R.string.toast_export_success, uri.getPath()));
                    }
                } catch (Exception | Error ignored) { 
                    Log.e(DEBUG_TAG, "Toast in asyncExport.onPostExecute failed with " + ignored.getMessage());
                }
            }
        }.execute();
    }
public static void asyncExport ( @ NonNull final Context ctx , @ NonNull final Exportable exportable , @ NonNull Uri uri ) { new ExecutorTask < Void , Void , Boolean > ( ) { @ Override protected Boolean doInBackground ( Void param ) { try ( OutputStream outputStream = ctx . getContentResolver ( ) . openOutputStream ( uri ) ) { exportable . export ( outputStream ) ; } catch ( Exception e ) { Log . e ( DEBUG_TAG , "Export failed - " + uri . toString ( ) ) ; return false ; } return true ; } @ Override protected void onPostExecute ( Boolean result ) { try { if ( ! result ) { Snack . toastTopError ( ctx , R . string . toast_export_failed ) ; } else { Log . i ( DEBUG_TAG , "Successful export to " + uri ) ; Snack . toastTopInfo ( ctx , ctx . getResources ( ) . getString ( R . string . toast_export_success , uri . getPath ( ) ) ) ; } } catch ( Exception | Error ignored ) { Log . e ( DEBUG_TAG , "Toast in asyncExport.onPostExecute failed with " + ignored . getMessage ( ) ) ; } } } . execute ( ) ; }
Exports an Exportable asynchronously, displaying a toast on success or failure
Exports an Exportable asynchronously displaying a toast on success or failure
// NOSONAR result can't be null // NOSONAR crashing is not an option