-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix crash during text file creation #5442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 386 | ||
| 385 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import android.annotation.SuppressLint; | ||
| import android.app.Activity; | ||
| import android.app.Dialog; | ||
| import android.content.Context; | ||
| import android.content.DialogInterface; | ||
| import android.content.Intent; | ||
| import android.graphics.PorterDuff; | ||
|
|
@@ -241,35 +242,36 @@ protected String doInBackground(Void... voids) { | |
| new DirectEditingCreateFileRemoteOperation(path, | ||
| creator.getEditor(), | ||
| creator.getId(), | ||
| template.getTitle()) | ||
| .execute(client); | ||
|
|
||
| if (result.isSuccess()) { | ||
| // get file | ||
| RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path) | ||
| .execute(client); | ||
|
|
||
| if (newFileResult.isSuccess()) { | ||
| OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0)); | ||
|
|
||
| if (chooseTemplateDialogFragmentWeakReference.get() != null) { | ||
| FileDataStorageManager storageManager = new FileDataStorageManager( | ||
| user.toPlatformAccount(), | ||
| chooseTemplateDialogFragmentWeakReference.get().requireContext().getContentResolver()); | ||
| storageManager.saveFile(temp); | ||
| file = storageManager.getFileByPath(path); | ||
|
|
||
| return result.getData().get(0).toString(); | ||
| } | ||
| else { | ||
| return ""; | ||
| } | ||
| } else { | ||
| return ""; | ||
| } | ||
| } else { | ||
| template.getTitle()).execute(client); | ||
| if (!result.isSuccess()) { | ||
| return ""; | ||
| } | ||
|
|
||
| RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path).execute(client); | ||
| if (!newFileResult.isSuccess()) { | ||
| return ""; | ||
| } | ||
|
|
||
| OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| final ChooseTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get(); | ||
| if (fragment == null) { | ||
| return ""; | ||
| } | ||
|
|
||
| final Context context = fragment.getContext(); | ||
| if (context == null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also "isAdded" or "isDetached", which we can use, or? getContext is: return mHost == null ? null : mHost.getContext() So isAdded might even the more extensive check, or?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you can use that, as it calls However, because subsequent call to Because the action run here is not related to UI elements displayed on the screen, we can rely on Android API is not a pinnacle of software engineering, and
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. |
||
| // fragment has been detached | ||
| return ""; | ||
| } | ||
|
|
||
| FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(), | ||
| context.getContentResolver()); | ||
| storageManager.saveFile(temp); | ||
| file = storageManager.getFileByPath(path); | ||
|
|
||
| return result.getData().get(0).toString(); | ||
|
|
||
| } catch (ClientFactory.CreationException e) { | ||
| Log_OC.e(TAG, "Error creating file from template!", e); | ||
| return ""; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty common error I see. Both activities and fragments are passed as weak references to avoid leaks, but in case of fragment we must check if it has been detached.