forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviewTextFileFragment.java
More file actions
367 lines (319 loc) · 13.3 KB
/
PreviewTextFileFragment.java
File metadata and controls
367 lines (319 loc) · 13.3 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2019 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.owncloud.android.ui.preview;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.nextcloud.client.account.User;
import com.nextcloud.ui.fileactions.FileAction;
import com.nextcloud.ui.fileactions.FileActionsBottomSheet;
import com.nextcloud.utils.extensions.BundleExtensionsKt;
import com.nextcloud.utils.extensions.FileExtensionsKt;
import com.nmc.android.utils.SearchViewThemeUtils;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.MimeTypeUtil;
import org.mozilla.universalchardet.ReaderFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.FragmentManager;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
public class PreviewTextFileFragment extends PreviewTextFragment {
private static final String EXTRA_FILE = "FILE";
private static final String EXTRA_USER = "USER";
private static final String EXTRA_OPEN_SEARCH = "SEARCH";
private static final String EXTRA_SEARCH_QUERY = "SEARCH_QUERY";
private static final String TAG = PreviewTextFileFragment.class.getSimpleName();
private TextLoadAsyncTask textLoadAsyncTask;
private User user;
public static PreviewTextFileFragment create(User user, OCFile file, boolean openSearch, String searchQuery) {
Bundle args = new Bundle();
args.putParcelable(EXTRA_FILE, file);
args.putParcelable(EXTRA_USER, user);
args.putBoolean(EXTRA_OPEN_SEARCH, openSearch);
args.putString(EXTRA_SEARCH_QUERY, searchQuery);
PreviewTextFileFragment fragment = new PreviewTextFileFragment();
fragment.setArguments(args);
return fragment;
}
/**
* Creates an empty fragment for previews.
* <p>
* MUST BE KEPT: the system uses it when tries to re-instantiate a fragment automatically (for instance, when the
* device is turned a aside).
* <p>
* DO NOT CALL IT: an {@link OCFile} and {@link User} must be provided for a successful construction
*/
public PreviewTextFileFragment() {
super();
user = null;
}
/**
* {@inheritDoc}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
OCFile file = getFile();
Bundle args = getArguments();
if (file == null) {
file = BundleExtensionsKt.getParcelableArgument(args, EXTRA_FILE, OCFile.class);
}
if (user == null) {
user = BundleExtensionsKt.getParcelableArgument(args, EXTRA_USER, User.class);
}
if (args.containsKey(EXTRA_SEARCH_QUERY)) {
searchQuery = args.getString(EXTRA_SEARCH_QUERY);
}
searchOpen = args.getBoolean(EXTRA_OPEN_SEARCH, false);
if (savedInstanceState == null) {
if (file == null) {
throw new IllegalStateException("Instanced with a NULL OCFile");
}
if (user == null) {
throw new IllegalStateException("Instanced with a NULL ownCloud Account");
}
} else {
file = BundleExtensionsKt.getParcelableArgument(savedInstanceState, EXTRA_FILE, OCFile.class);
user = BundleExtensionsKt.getParcelableArgument(savedInstanceState, EXTRA_USER, User.class);
}
handler = new Handler();
setFile(file);
}
/**
* {@inheritDoc}
*/
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
FileExtensionsKt.logFileSize(getFile(), TAG);
outState.putParcelable(PreviewTextFileFragment.EXTRA_FILE, getFile());
outState.putParcelable(PreviewTextFileFragment.EXTRA_USER, user);
super.onSaveInstanceState(outState);
}
@Override
public void loadAndShowTextPreview() {
textLoadAsyncTask = new TextLoadAsyncTask(new WeakReference<>(binding.textPreview),
new WeakReference<>(binding.emptyListProgress));
textLoadAsyncTask.execute(getFile().getStoragePath());
}
/**
* Reads the file to preview and shows its contents. Too critical to be anonymous.
*/
private class TextLoadAsyncTask extends AsyncTask<Object, Void, StringWriter> {
private static final int PARAMS_LENGTH = 1;
private final WeakReference<TextView> textViewReference;
private final WeakReference<FrameLayout> progressViewReference;
private TextLoadAsyncTask(WeakReference<TextView> textView, WeakReference<FrameLayout> progressView) {
textViewReference = textView;
progressViewReference = progressView;
}
@Override
protected void onPreExecute() {
// not used at the moment
}
@Override
protected StringWriter doInBackground(Object... params) {
if (params.length != PARAMS_LENGTH) {
throw new IllegalArgumentException("The parameter to " + TextLoadAsyncTask.class.getName()
+ " must be (1) the file location");
}
String location = (String) params[0];
Scanner scanner = null;
StringWriter source = new StringWriter();
BufferedWriter bufferedWriter = new BufferedWriter(source);
Reader reader = null;
try {
File file = new File(location);
reader = ReaderFactory.createReaderFromFile(file);
scanner = new Scanner(reader);
while (scanner.hasNextLine()) {
bufferedWriter.append(scanner.nextLine());
if (scanner.hasNextLine()) {
bufferedWriter.append("\n");
}
}
bufferedWriter.close();
IOException exc = scanner.ioException();
if (exc != null) {
throw exc;
}
} catch (IOException e) {
Log_OC.e(TAG, e.getMessage(), e);
finish();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log_OC.e(TAG, e.getMessage(), e);
finish();
}
}
if (scanner != null) {
scanner.close();
}
}
return source;
}
@Override
@SuppressFBWarnings("STT")
protected void onPostExecute(final StringWriter stringWriter) {
final TextView textView = textViewReference.get();
if (textView != null) {
originalText = stringWriter.toString();
setText(textView, originalText, getFile(), requireActivity(), false, false, viewThemeUtils);
if (searchView != null) {
searchView.setOnQueryTextListener(PreviewTextFileFragment.this);
if (searchOpen && searchView != null) {
searchView.setQuery(searchQuery, true);
}
}
textView.setVisibility(View.VISIBLE);
}
final FrameLayout progress = progressViewReference.get();
if (progress != null) {
progress.setVisibility(View.GONE);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.custom_menu_placeholder, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.setVisible(true);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setMaxWidth(Integer.MAX_VALUE);
//NMC customization
SearchViewThemeUtils.INSTANCE.themeSearchView(requireActivity(), searchView);
if (searchOpen) {
searchView.setIconified(false);
searchView.setQuery(searchQuery, false);
searchView.clearFocus();
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.custom_menu_placeholder_item) {
final OCFile file = getFile();
if (containerActivity.getStorageManager() != null && file != null) {
// Update the file
final OCFile updatedFile = containerActivity.getStorageManager().getFileById(file.getFileId());
setFile(updatedFile);
final OCFile fileNew = getFile();
if (fileNew != null) {
showFileActions(file);
}
}
return true;
}
return super.onOptionsItemSelected(item);
}
private void showFileActions(OCFile file) {
final var additionalFilter = FileAction.Companion.getFilePreviewActions(getFile());
final FragmentManager fragmentManager = getChildFragmentManager();
FileActionsBottomSheet.newInstance(file, false, additionalFilter)
.setResultListener(fragmentManager, this, this::onFileActionChosen)
.show(fragmentManager, "actions");
}
private void onFileActionChosen(final int itemId) {
if (itemId == R.id.action_send_share_file) {
if (getFile().isSharedWithMe() && !getFile().canReshare()) {
DisplayUtils.showSnackMessage(getView(), R.string.resharing_is_not_allowed);
} else {
containerActivity.getFileOperationsHelper().sendShareFile(getFile());
}
} else if (itemId == R.id.action_send_file) {
containerActivity.getFileOperationsHelper().sendShareFile(getFile(), true);
} else if (itemId == R.id.action_open_file_with) {
openFile();
} else if (itemId == R.id.action_remove_file) {
RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
} else if (itemId == R.id.action_see_details) {
seeDetails();
} else if (itemId == R.id.action_sync_file) {
containerActivity.getFileOperationsHelper().syncFile(getFile());
} else if(itemId == R.id.action_cancel_sync){
containerActivity.getFileOperationsHelper().cancelTransference(getFile());
} else if (itemId == R.id.action_edit) {
containerActivity.getFileOperationsHelper().openFileWithTextEditor(getFile(), getContext());
}
}
/**
* Update the file of the fragment with file value
*
* @param file The new file to set
*/
public void updateFile(OCFile file) {
setFile(file);
}
private void seeDetails() {
containerActivity.showDetails(getFile());
}
/**
* Opens the previewed file with an external application.
*/
private void openFile() {
containerActivity.getFileOperationsHelper().openFile(getFile());
finish();
}
/**
* Helper method to test if an {@link OCFile} can be passed to a {@link PreviewTextFileFragment} to be previewed.
*
* @param file File to test if can be previewed.
* @return 'True' if the file can be handled by the fragment.
*/
public static boolean canBePreviewed(OCFile file) {
final List<String> unsupportedTypes = new LinkedList<>();
unsupportedTypes.add("text/richtext");
unsupportedTypes.add("text/rtf");
unsupportedTypes.add("text/calendar");
unsupportedTypes.add("text/vnd.abc");
unsupportedTypes.add("text/vnd.fmi.flexstor");
unsupportedTypes.add("text/vnd.rn-realtext");
unsupportedTypes.add("text/vnd.wap.wml");
unsupportedTypes.add("text/vnd.wap.wmlscript");
unsupportedTypes.add("text/html");
return file != null && file.isDown() && MimeTypeUtil.isText(file) &&
!unsupportedTypes.contains(file.getMimeType()) &&
!unsupportedTypes.contains(MimeTypeUtil.getMimeTypeFromPath(file.getRemotePath()));
}
@Override
public void onStop() {
super.onStop();
Log_OC.e(TAG, "onStop");
if (textLoadAsyncTask != null) {
textLoadAsyncTask.cancel(true);
}
}
}