-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAttachmentTask.java
More file actions
86 lines (70 loc) · 2.83 KB
/
AttachmentTask.java
File metadata and controls
86 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (C) 2013-2020 Federico Iosue (federico@iosue.it)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.feio.android.omninotes.async;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;
import androidx.fragment.app.Fragment;
import it.feio.android.omninotes.OmniNotes;
import it.feio.android.omninotes.models.Attachment;
import it.feio.android.omninotes.models.listeners.OnAttachingFileListener;
import it.feio.android.omninotes.utils.StorageHelper;
import java.lang.ref.WeakReference;
public class AttachmentTask extends AsyncTask<Void, Void, Attachment> {
private final WeakReference<Fragment> mFragmentWeakReference;
private OnAttachingFileListener mOnAttachingFileListener;
private Uri uri;
private String fileName;
public AttachmentTask(Fragment mFragment, Uri uri,
OnAttachingFileListener mOnAttachingFileListener) {
this(mFragment, uri, null, mOnAttachingFileListener);
}
public AttachmentTask(Fragment mFragment, Uri uri, String fileName,
OnAttachingFileListener mOnAttachingFileListener) {
mFragmentWeakReference = new WeakReference<>(mFragment);
this.uri = uri;
this.fileName = TextUtils.isEmpty(fileName) ? "" : fileName;
this.mOnAttachingFileListener = mOnAttachingFileListener;
}
@Override
protected Attachment doInBackground(Void... params) {
Attachment attachment = StorageHelper.createAttachmentFromUri(OmniNotes.getAppContext(), uri);
attachment.setName(this.fileName);
return attachment;
}
@Override
protected void onPostExecute(Attachment mAttachment) {
if (isAlive()) {
if (mAttachment != null) {
mOnAttachingFileListener.onAttachingFileFinished(mAttachment);
} else {
mOnAttachingFileListener.onAttachingFileErrorOccurred(null);
}
} else {
if (mAttachment != null) {
StorageHelper.delete(OmniNotes.getAppContext(), mAttachment.getUri().getPath());
}
}
}
private boolean isAlive() {
return mFragmentWeakReference != null
&& mFragmentWeakReference.get() != null
&& mFragmentWeakReference.get().isAdded()
&& mFragmentWeakReference.get().getActivity() != null
&& !mFragmentWeakReference.get().getActivity().isFinishing();
}
}