1818import android .graphics .Point ;
1919import android .net .Uri ;
2020import android .os .AsyncTask ;
21+ import android .os .Binder ;
2122import android .os .Bundle ;
2223import android .os .CancellationSignal ;
2324import android .os .Handler ;
@@ -373,21 +374,26 @@ public String renameDocument(String documentId, String displayName) throws FileN
373374 return null ;
374375 }
375376
376- final var result = new RenameFileOperation (document .getRemotePath (),
377- displayName ,
378- document .getStorageManager ())
379- .execute (document .getClient ());
377+ final long token = Binder .clearCallingIdentity ();
378+ try {
379+ final var result = new RenameFileOperation (document .getRemotePath (),
380+ displayName ,
381+ document .getStorageManager ())
382+ .execute (document .getClient ());
380383
381- if (!result .isSuccess ()) {
382- Log_OC .e (TAG , result .toString ());
383- throw new FileNotFoundException ("Failed to rename document with documentId " + documentId + ": " +
384- result .getException ());
385- }
384+ if (!result .isSuccess ()) {
385+ Log_OC .e (TAG , result .toString ());
386+ throw new FileNotFoundException ("Failed to rename document with documentId " + documentId + ": " +
387+ result .getException ());
388+ }
386389
387- Context context = getNonNullContext ();
388- context .getContentResolver ().notifyChange (toNotifyUri (document .getParent ()), null , false );
390+ Context context = getNonNullContext ();
391+ context .getContentResolver ().notifyChange (toNotifyUri (document .getParent ()), null , false );
389392
390- return null ;
393+ return null ;
394+ } finally {
395+ Binder .restoreCallingIdentity (token );
396+ }
391397 }
392398
393399 @ Override
@@ -405,46 +411,52 @@ public String copyDocument(String sourceDocumentId, String targetParentDocumentI
405411
406412 Document document = toDocument (sourceDocumentId );
407413 FileDataStorageManager storageManager = document .getStorageManager ();
408- final var result = new CopyFileOperation (document .getRemotePath (),
409- targetFolder .getRemotePath (),
410- document .getStorageManager ())
411- .execute (document .getClient ());
412-
413- if (!result .isSuccess ()) {
414- Log_OC .e (TAG , result .toString ());
415- throw new FileNotFoundException ("Failed to copy document with documentId " + sourceDocumentId
416- + " to " + targetParentDocumentId );
417- }
418414
419- Context context = getNonNullContext ();
420- User user = document .getUser ();
415+ final long token = Binder .clearCallingIdentity ();
416+ try {
417+ final var result = new CopyFileOperation (document .getRemotePath (),
418+ targetFolder .getRemotePath (),
419+ document .getStorageManager ())
420+ .execute (document .getClient ());
421421
422- final var updateParent = new RefreshFolderOperation (targetFolder .getFile (),
423- System .currentTimeMillis (),
424- false ,
425- false ,
426- true ,
427- storageManager ,
428- user ,
429- context )
430- .execute (targetFolder .getClient ());
422+ if (!result .isSuccess ()) {
423+ Log_OC .e (TAG , result .toString ());
424+ throw new FileNotFoundException ("Failed to copy document with documentId " + sourceDocumentId
425+ + " to " + targetParentDocumentId );
426+ }
431427
432- if (!updateParent .isSuccess ()) {
433- Log_OC .e (TAG , updateParent .toString ());
434- throw new FileNotFoundException ("Failed to copy document with documentId " + sourceDocumentId
435- + " to " + targetParentDocumentId );
436- }
428+ Context context = getNonNullContext ();
429+ User user = document .getUser ();
430+
431+ final var updateParent = new RefreshFolderOperation (targetFolder .getFile (),
432+ System .currentTimeMillis (),
433+ false ,
434+ false ,
435+ true ,
436+ storageManager ,
437+ user ,
438+ context )
439+ .execute (targetFolder .getClient ());
440+
441+ if (!updateParent .isSuccess ()) {
442+ Log_OC .e (TAG , updateParent .toString ());
443+ throw new FileNotFoundException ("Failed to copy document with documentId " + sourceDocumentId
444+ + " to " + targetParentDocumentId );
445+ }
437446
438- String newPath = targetFolder .getRemotePath () + document .getFile ().getFileName ();
447+ String newPath = targetFolder .getRemotePath () + document .getFile ().getFileName ();
439448
440- if (document .getFile ().isFolder ()) {
441- newPath = newPath + PATH_SEPARATOR ;
442- }
443- Document newFile = new Document (storageManager , newPath );
449+ if (document .getFile ().isFolder ()) {
450+ newPath = newPath + PATH_SEPARATOR ;
451+ }
452+ Document newFile = new Document (storageManager , newPath );
444453
445- context .getContentResolver ().notifyChange (toNotifyUri (targetFolder ), null , false );
454+ context .getContentResolver ().notifyChange (toNotifyUri (targetFolder ), null , false );
446455
447- return newFile .getDocumentId ();
456+ return newFile .getDocumentId ();
457+ } finally {
458+ Binder .restoreCallingIdentity (token );
459+ }
448460 }
449461
450462 @ Override
@@ -467,24 +479,29 @@ public String moveDocument(String sourceDocumentId, String sourceParentDocumentI
467479 return null ;
468480 }
469481
470- final var result = new MoveFileOperation (document .getRemotePath (),
471- targetFolder .getRemotePath (),
472- document .getStorageManager ())
473- .execute (document .getClient ());
482+ final long token = Binder .clearCallingIdentity ();
483+ try {
484+ final var result = new MoveFileOperation (document .getRemotePath (),
485+ targetFolder .getRemotePath (),
486+ document .getStorageManager ())
487+ .execute (document .getClient ());
474488
475- if (!result .isSuccess ()) {
476- Log_OC .e (TAG , result .toString ());
477- throw new FileNotFoundException ("Failed to move document with documentId " + sourceDocumentId
478- + " to " + targetParentDocumentId );
479- }
489+ if (!result .isSuccess ()) {
490+ Log_OC .e (TAG , result .toString ());
491+ throw new FileNotFoundException ("Failed to move document with documentId " + sourceDocumentId
492+ + " to " + targetParentDocumentId );
493+ }
480494
481- Document sourceFolder = toDocument (sourceParentDocumentId );
495+ Document sourceFolder = toDocument (sourceParentDocumentId );
482496
483- Context context = getNonNullContext ();
484- context .getContentResolver ().notifyChange (toNotifyUri (sourceFolder ), null , false );
485- context .getContentResolver ().notifyChange (toNotifyUri (targetFolder ), null , false );
497+ Context context = getNonNullContext ();
498+ context .getContentResolver ().notifyChange (toNotifyUri (sourceFolder ), null , false );
499+ context .getContentResolver ().notifyChange (toNotifyUri (targetFolder ), null , false );
486500
487- return sourceDocumentId ;
501+ return sourceDocumentId ;
502+ } finally {
503+ Binder .restoreCallingIdentity (token );
504+ }
488505 }
489506
490507 @ Override
@@ -533,10 +550,15 @@ public String createDocument(String documentId, String mimeType, String displayN
533550 return null ;
534551 }
535552
536- if (DocumentsContract .Document .MIME_TYPE_DIR .equalsIgnoreCase (mimeType )) {
537- return createFolder (folderDocument , displayName );
538- } else {
539- return createFile (folderDocument , displayName , mimeType );
553+ final long token = Binder .clearCallingIdentity ();
554+ try {
555+ if (DocumentsContract .Document .MIME_TYPE_DIR .equalsIgnoreCase (mimeType )) {
556+ return createFolder (folderDocument , displayName );
557+ } else {
558+ return createFile (folderDocument , displayName , mimeType );
559+ }
560+ } finally {
561+ Binder .restoreCallingIdentity (token );
540562 }
541563 }
542564
@@ -675,18 +697,24 @@ public void deleteDocument(String documentId) throws FileNotFoundException {
675697 recursiveRevokePermission (document );
676698
677699 OCFile file = document .getStorageManager ().getFileByPath (document .getRemotePath ());
678- final var result = new RemoveFileOperation (file ,
679- false ,
680- document .getUser (),
681- true ,
682- context ,
683- document .getStorageManager ())
684- .execute (document .getClient ());
685700
686- if (!result .isSuccess ()) {
687- throw new FileNotFoundException ("Failed to delete document with documentId " + documentId );
701+ final long token = Binder .clearCallingIdentity ();
702+ try {
703+ final var result = new RemoveFileOperation (file ,
704+ false ,
705+ document .getUser (),
706+ true ,
707+ context ,
708+ document .getStorageManager ())
709+ .execute (document .getClient ());
710+
711+ if (!result .isSuccess ()) {
712+ throw new FileNotFoundException ("Failed to delete document with documentId " + documentId );
713+ }
714+ context .getContentResolver ().notifyChange (toNotifyUri (parentFolder ), null , false );
715+ } finally {
716+ Binder .restoreCallingIdentity (token );
688717 }
689- context .getContentResolver ().notifyChange (toNotifyUri (parentFolder ), null , false );
690718 }
691719
692720 private void recursiveRevokePermission (Document document ) {
0 commit comments