-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathFileOperationsHelper.java
More file actions
245 lines (213 loc) · 8.97 KB
/
FileOperationsHelper.java
File metadata and controls
245 lines (213 loc) · 8.97 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* openCloud Android client application
*
* @author masensio
* @author David A. Velasco
* @author Juan Carlos González Cabrero
* @author David González Verdugo
* @author Shashvat Kedia
* @author David Crespo Rios
* @author Aitor Ballesteros Pavón
*
* Copyright (C) 2024 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
* <p>
* 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.
* <p>
* 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 eu.opencloud.android.ui.helpers;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import eu.opencloud.android.R;
import eu.opencloud.android.domain.files.model.OCFile;
import eu.opencloud.android.domain.sharing.shares.model.OCShare;
import eu.opencloud.android.lib.common.accounts.AccountUtils;
import eu.opencloud.android.presentation.common.ShareSheetHelper;
import eu.opencloud.android.presentation.sharing.ShareActivity;
import eu.opencloud.android.services.OperationsService;
import eu.opencloud.android.ui.activity.FileActivity;
import eu.opencloud.android.usecases.synchronization.SynchronizeFileUseCase;
import eu.opencloud.android.usecases.synchronization.SynchronizeFolderUseCase;
import eu.opencloud.android.utils.MimetypeIconUtil;
import eu.opencloud.android.utils.UriUtilsKt;
import kotlin.Lazy;
import org.jetbrains.annotations.NotNull;
import timber.log.Timber;
import static org.koin.java.KoinJavaComponent.inject;
public class FileOperationsHelper {
private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
private FileActivity mFileActivity;
/// Identifier of operation in progress which result shouldn't be lost
private long mWaitingForOpId = Long.MAX_VALUE;
public FileOperationsHelper(FileActivity fileActivity) {
mFileActivity = fileActivity;
}
private Intent getIntentForSavedMimeType(Uri data, String type, boolean hasWritePermission) {
Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
intentForSavedMimeType.setDataAndType(data, type);
int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
if (hasWritePermission) {
flags = flags | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
}
intentForSavedMimeType.setFlags(flags);
return intentForSavedMimeType;
}
public void openFile(OCFile ocFile) {
if (ocFile != null) {
String finalMimeType = MimetypeIconUtil.getBestMimeTypeForOpen(ocFile.getMimeType(), ocFile.getFileName());
Intent intentForSavedMimeType = getIntentForSavedMimeType(UriUtilsKt.INSTANCE.getExposedFileUriForOCFile(mFileActivity, ocFile),
finalMimeType, ocFile.getHasWritePermission());
openFileWithIntent(intentForSavedMimeType);
} else {
Timber.e("Trying to open a NULL OCFile");
}
}
private void openFileWithIntent(Intent openFileWithIntent) {
try {
mFileActivity.startActivity(Intent.createChooser(openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)));
} catch (ActivityNotFoundException anfe) {
mFileActivity.showSnackMessage(mFileActivity.getString(
R.string.file_list_no_app_for_file_type
));
}
}
/**
* Show dialog to allow the user to choose an app to send the private link of an {@link OCFile},
* or copy it to clipboard.
*
* @param file @param file {@link OCFile} which will be shared with internal users
*/
public void copyOrSendPrivateLink(OCFile file) {
// Parse remoteId
String privateLink = file.getPrivateLink();
if (privateLink == null || privateLink.isEmpty()) {
mFileActivity.showSnackMessage(
mFileActivity.getString(R.string.file_private_link_error)
);
return;
}
shareLink(privateLink);
}
/**
* Show dialog to allow the user to choose an app to send the link of an {@link OCShare},
* or copy it to clipboard.
*
* @param share {@link OCShare} which link will be sent to the app chosen by the user.
*/
public void copyOrSendPublicLink(OCShare share) {
String link = share.getShareLink();
if (link.length() <= 0) {
mFileActivity.showSnackMessage(
mFileActivity.getString(R.string.share_no_link_in_this_share)
);
return;
}
shareLink(link);
}
/**
* Show an instance of {@link eu.opencloud.android.domain.sharing.shares.model.ShareType} for sharing or unsharing
* the {@link OCFile} received as parameter.
*
* @param file File to share or unshare.
*/
public void showShareFile(OCFile file) {
Intent intent = new Intent(mFileActivity, ShareActivity.class);
intent.putExtra(FileActivity.EXTRA_FILE, file);
intent.putExtra(FileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
mFileActivity.startActivity(intent);
}
/**
* Request the synchronization of a file or folder with the OC server, including its contents.
*
* @param file The file or folder to synchronize
* DEPRECATED: Use the usecases within a viewmodel instead!
*/
@Deprecated
public void syncFile(OCFile file) {
if (!file.isFolder()) {
@NotNull Lazy<SynchronizeFileUseCase> synchronizeFileUseCaseLazy = inject(SynchronizeFileUseCase.class);
synchronizeFileUseCaseLazy.getValue().invoke(
new SynchronizeFileUseCase.Params(file)
);
} else {
@NotNull Lazy<SynchronizeFolderUseCase> synchronizeFolderUseCaseLazy = inject(SynchronizeFolderUseCase.class);
synchronizeFolderUseCaseLazy.getValue().invoke(
new SynchronizeFolderUseCase.Params(
file.getRemotePath(),
file.getOwner(),
file.getSpaceId(),
SynchronizeFolderUseCase.SyncFolderMode.SYNC_FOLDER_RECURSIVELY,
false)
);
}
}
public long getOpIdWaitingFor() {
return mWaitingForOpId;
}
public void setOpIdWaitingFor(long waitingForOpId) {
mWaitingForOpId = waitingForOpId;
}
/**
* Starts a check of the currently stored credentials for the given account.
*
* @param account OC account which credentials will be checked.
*/
public void checkCurrentCredentials(Account account) {
Intent service = new Intent(mFileActivity, OperationsService.class);
service.setAction(OperationsService.ACTION_CHECK_CURRENT_CREDENTIALS);
service.putExtra(OperationsService.EXTRA_ACCOUNT, account);
mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
mFileActivity.showLoadingDialog(R.string.wait_checking_credentials);
}
/**
* Share link with other apps
*
* @param link link to share
*/
private void shareLink(String link) {
Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
intentToShareLink.setType("text/plain");
String displayName = AccountManager.get(mFileActivity.getApplicationContext()).getUserData(
mFileActivity.getAccount(),
AccountUtils.Constants.KEY_DISPLAY_NAME
);
if (displayName != null) {
intentToShareLink.putExtra(
Intent.EXTRA_SUBJECT,
mFileActivity.getString(
R.string.subject_user_shared_with_you,
displayName,
mFileActivity.getFile().getFileName()
)
);
} else {
intentToShareLink.putExtra(
Intent.EXTRA_SUBJECT,
mFileActivity.getString(
R.string.subject_shared_with_you,
mFileActivity.getFile().getFileName()
)
);
}
String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
Intent shareSheetIntent = new ShareSheetHelper().getShareSheetIntent(
intentToShareLink,
mFileActivity.getApplicationContext(),
R.string.activity_chooser_title,
packagesToExclude
);
mFileActivity.startActivity(shareSheetIntent);
}
}