-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathDataUtils.java
More file actions
583 lines (500 loc) · 17.7 KB
/
Copy pathDataUtils.java
File metadata and controls
583 lines (500 loc) · 17.7 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright (C) 2014-2026 Arpit Khurana <arpitkh96@gmail.com>, Vishal Nehra <vishalmeham2@gmail.com>,
* Emmanuel Messulam<emmanuelbendavid@gmail.com>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager 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 com.amaze.filemanager.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amaze.filemanager.R;
import com.amaze.filemanager.adapters.data.LayoutElementParcelable;
import com.amaze.filemanager.application.AppConfig;
import com.amaze.filemanager.fileoperations.filesystem.OpenMode;
import com.amaze.filemanager.models.LanguageModel;
import com.cloudrail.si.interfaces.CloudStorage;
import com.cloudrail.si.services.Box;
import com.cloudrail.si.services.Dropbox;
import com.cloudrail.si.services.GoogleDrive;
import com.cloudrail.si.services.OneDrive;
import com.googlecode.concurrenttrees.radix.ConcurrentRadixTree;
import com.googlecode.concurrenttrees.radix.node.concrete.DefaultCharArrayNodeFactory;
import com.googlecode.concurrenttrees.radix.node.concrete.voidvalue.VoidValue;
import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
import android.content.Context;
import android.text.TextUtils;
import android.view.MenuItem;
import androidx.annotation.Nullable;
/** Singleton class to handle data for various services */
// Central data being used across activity,fragments and classes
public class DataUtils {
private static final Logger LOG = LoggerFactory.getLogger(DataUtils.class);
private ConcurrentRadixTree<VoidValue> hiddenfiles =
new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());
public static final int LIST = 0, GRID = 1;
private InvertedRadixTree<Integer> filesGridOrList =
new ConcurrentInvertedRadixTree<>(new DefaultCharArrayNodeFactory());
private LinkedList<String> history = new LinkedList<>();
private ArrayList<String> storages = new ArrayList<>();
private InvertedRadixTree<Integer> tree =
new ConcurrentInvertedRadixTree<>(new DefaultCharArrayNodeFactory());
private ArrayList<String[]> servers = new ArrayList<>();
private ArrayList<String[]> books = new ArrayList<>();
private ArrayList<CloudStorage> accounts = new ArrayList<>(4);
/** List of checked items to persist when drag and drop from one tab to another */
private ArrayList<LayoutElementParcelable> checkedItemsList;
private DataChangeListener dataChangeListener;
private DataUtils() {}
private static class DataUtilsHolder {
private static final DataUtils INSTANCE = new DataUtils();
}
public static List<LanguageModel> getLanguages(Context context) {
var data = new ArrayList<LanguageModel>();
data.add(
new LanguageModel(
context.getString(R.string.german_translation_title),
context.getString(R.string.german_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.italian_translation_title),
context.getString(R.string.italian_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.french_translation_title),
context.getString(R.string.french_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.russian_translation_title),
context.getString(R.string.russian_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.spanish_translation_title),
context.getString(R.string.spanish_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.basque_translation_title),
context.getString(R.string.basque_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.chinese_translation_title),
context.getString(R.string.chinese_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.serbian_translation_title),
context.getString(R.string.serbian_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.turkish_translation_title),
context.getString(R.string.turkish_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.ukrainian_translation_title),
context.getString(R.string.ukrainian_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.portuguese_translation_title),
context.getString(R.string.portuguese_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.polish_translation_title),
context.getString(R.string.polish_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.korean_translation_title),
context.getString(R.string.korean_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.greek_translation_title),
context.getString(R.string.greek_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.dutch_translation_title),
context.getString(R.string.dutch_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.romanian_translation_title),
context.getString(R.string.romanian_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.vietnamese_translation_title),
context.getString(R.string.vietnamese_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.japanese_translation_title),
context.getString(R.string.japanese_translation_summary)));
data.add(
new LanguageModel(
context.getString(R.string.tamil_translation_title),
context.getString(R.string.tamil_translation_summary)));
return data;
}
public static List<LanguageModel> getContributors(Context context) {
var data = new ArrayList<LanguageModel>();
data.add(
new LanguageModel(
context.getString(R.string.contributor_1_title),
context.getString(R.string.contributors_1_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_2_title),
context.getString(R.string.contributors_2_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_3_title),
context.getString(R.string.contributors_3_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_4_title),
context.getString(R.string.contributors_4_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_5_title),
context.getString(R.string.contributors_5_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_6_title),
context.getString(R.string.contributors_6_summary)));
data.add(
new LanguageModel(
context.getString(R.string.contributor_7_title),
context.getString(R.string.contributors_7_summary)));
return data;
}
public static DataUtils getInstance() {
return DataUtilsHolder.INSTANCE;
}
public int containsServer(String[] a) {
return contains(a, servers);
}
public int containsServer(String path) {
synchronized (servers) {
if (servers == null) return -1;
int i = 0;
for (String[] x : servers) {
if (x[1].equals(path)) return i;
i++;
}
}
return -1;
}
public int containsBooks(String[] a) {
return contains(a, books);
}
/*public int containsAccounts(CloudEntry cloudEntry) {
return contains(a, accounts);
}*/
/**
* Checks whether cloud account of certain type is present or not
*
* @param serviceType the {@link OpenMode} of account to check
* @return the index of account, -1 if not found
*/
public synchronized int containsAccounts(OpenMode serviceType) {
int i = 0;
for (CloudStorage storage : accounts) {
switch (serviceType) {
case BOX:
if (storage instanceof Box) return i;
break;
case DROPBOX:
if (storage instanceof Dropbox) return i;
break;
case GDRIVE:
if (storage instanceof GoogleDrive) return i;
break;
case ONEDRIVE:
if (storage instanceof OneDrive) return i;
break;
default:
return -1;
}
i++;
}
return -1;
}
public void clear() {
hiddenfiles = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());
filesGridOrList = new ConcurrentInvertedRadixTree<>(new DefaultCharArrayNodeFactory());
history.clear();
storages = new ArrayList<>();
tree = new ConcurrentInvertedRadixTree<>(new DefaultCharArrayNodeFactory());
servers = new ArrayList<>();
books = new ArrayList<>();
accounts = new ArrayList<>();
}
public void registerOnDataChangedListener(DataChangeListener l) {
dataChangeListener = l;
clear();
}
/**
* Searches pair list of {@code {name, path}} pairs for an entry that matches the given pair by
* <em>either</em> name or path. An entry is considered pair match when its name (index 0) equals
* {@code pair[0]} or its path (index 1) equals {@code pair[1]}.
*
* <p>Both should be unique since we cannot have two items with the same name. But this prevents
* two bookmarks / servers pointing to the same path with different names.
*
* @param pair the {@code {name, path}} pair to search for
* @param list the list to search; each element is expected to be pair two-element {@code {name,
* path}} array. May be {@code null}.
* @return the index of the first matching entry, or {@code -1} if {@code list} is {@code null} or
* no entry matches
*/
int contains(String[] pair, ArrayList<String[]> list) {
if (list == null) return -1;
int i = 0;
for (String[] item : list) {
if (item[0].equals(pair[0]) || item[1].equals(pair[1])) return i;
i++;
}
return -1;
}
public void removeBook(int i) {
synchronized (books) {
if (books.size() > i) books.remove(i);
}
}
public synchronized void removeAccount(OpenMode serviceType) {
for (CloudStorage storage : accounts) {
switch (serviceType) {
case BOX:
if (storage instanceof Box) {
accounts.remove(storage);
return;
}
break;
case DROPBOX:
if (storage instanceof Dropbox) {
accounts.remove(storage);
return;
}
break;
case GDRIVE:
if (storage instanceof GoogleDrive) {
accounts.remove(storage);
return;
}
break;
case ONEDRIVE:
if (storage instanceof OneDrive) {
accounts.remove(storage);
return;
}
break;
default:
return;
}
}
}
public void removeServer(int i) {
synchronized (servers) {
if (servers.size() > i) servers.remove(i);
}
}
public void addBook(String[] i) {
if (containsBooks(i) != -1) {
return;
}
synchronized (books) {
books.add(i);
}
}
/**
* @param i The bookmark name and path.
* @param refreshdrawer boolean flag to indicate if drawer refresh is desired.
* @return True if operation successful, false if failure.
*/
public boolean addBook(final String[] i, boolean refreshdrawer) {
if (containsBooks(i) != -1) {
// book exists
return false;
} else {
synchronized (books) {
books.add(i);
}
if (dataChangeListener != null) {
dataChangeListener.onBookAdded(i, refreshdrawer);
}
return true;
}
}
public void addAccount(CloudStorage storage) {
accounts.add(storage);
}
public void addServer(String[] i) {
servers.add(i);
}
public void addHiddenFile(final String i) {
synchronized (hiddenfiles) {
hiddenfiles.put(i, VoidValue.SINGLETON);
}
if (dataChangeListener != null) {
dataChangeListener.onHiddenFileAdded(i);
}
}
public void removeHiddenFile(final String i) {
synchronized (hiddenfiles) {
hiddenfiles.remove(i);
}
if (dataChangeListener != null) {
dataChangeListener.onHiddenFileRemoved(i);
}
}
public void setHistory(LinkedList<String> s) {
history.clear();
history.addAll(s);
}
public LinkedList<String> getHistory() {
return history;
}
public void addHistoryFile(final String i) {
history.push(i);
if (dataChangeListener != null) {
dataChangeListener.onHistoryAdded(i);
}
}
public void sortBook() {
Collections.sort(books, new BookSorter());
}
public synchronized void setServers(ArrayList<String[]> servers) {
if (servers != null) this.servers = servers;
}
public synchronized void setBooks(ArrayList<String[]> books) {
if (books != null) this.books = books;
}
public synchronized void setAccounts(ArrayList<CloudStorage> accounts) {
if (accounts != null) this.accounts = accounts;
}
public synchronized ArrayList<String[]> getServers() {
return servers;
}
public synchronized ArrayList<String[]> getBooks() {
return books;
}
public synchronized ArrayList<CloudStorage> getAccounts() {
return accounts;
}
public synchronized CloudStorage getAccount(OpenMode serviceType) {
for (CloudStorage storage : accounts) {
switch (serviceType) {
case BOX:
if (storage instanceof Box) return storage;
break;
case DROPBOX:
if (storage instanceof Dropbox) return storage;
break;
case GDRIVE:
if (storage instanceof GoogleDrive) return storage;
break;
case ONEDRIVE:
if (storage instanceof OneDrive) return storage;
break;
default:
LOG.error("Unable to determine service type of storage {}", storage);
return null;
}
}
return null;
}
public boolean isFileHidden(String path) {
try {
return getHiddenFiles().getValueForExactKey(path) != null;
} catch (IllegalStateException e) {
LOG.warn("failed to get hidden file", e);
return false;
}
}
public ConcurrentRadixTree<VoidValue> getHiddenFiles() {
return hiddenfiles;
}
public synchronized void setHiddenFiles(ConcurrentRadixTree<VoidValue> hiddenfiles) {
if (hiddenfiles != null) this.hiddenfiles = hiddenfiles;
}
public synchronized void setGridfiles(ArrayList<String> gridfiles) {
if (gridfiles != null) {
for (String gridfile : gridfiles) {
setPathAsGridOrList(gridfile, GRID);
}
}
}
public synchronized void setListfiles(ArrayList<String> listfiles) {
if (listfiles != null) {
for (String gridfile : listfiles) {
setPathAsGridOrList(gridfile, LIST);
}
}
}
public void setPathAsGridOrList(String path, int value) {
filesGridOrList.put(path, value);
}
public int getListOrGridForPath(String path, int defaultValue) {
Integer value = filesGridOrList.getValueForLongestKeyPrefixing(path);
return value != null ? value : defaultValue;
}
public void clearHistory() {
history.clear();
if (dataChangeListener != null) {
AppConfig.getInstance().runInBackground(() -> dataChangeListener.onHistoryCleared());
}
}
public synchronized List<String> getStorages() {
return storages;
}
public synchronized void setStorages(ArrayList<String> storages) {
this.storages = storages;
}
public boolean putDrawerPath(MenuItem item, String path) {
if (!TextUtils.isEmpty(path)) {
try {
tree.put(path, item.getItemId());
return true;
} catch (IllegalStateException e) {
LOG.warn("failed to put drawer path", e);
return false;
}
}
return false;
}
/**
* @param path the path to find
* @return the id of the longest containing MenuMetadata.path in getDrawerMetadata() or null
*/
public @Nullable Integer findLongestContainingDrawerItem(CharSequence path) {
return tree.getValueForLongestKeyPrefixing(path);
}
public ArrayList<LayoutElementParcelable> getCheckedItemsList() {
return this.checkedItemsList;
}
public void setCheckedItemsList(ArrayList<LayoutElementParcelable> layoutElementParcelables) {
this.checkedItemsList = layoutElementParcelables;
}
/**
* Callbacks to do original changes in database (and ui if required) The callbacks are called in a
* main thread
*/
public interface DataChangeListener {
void onHiddenFileAdded(String path);
void onHiddenFileRemoved(String path);
void onHistoryAdded(String path);
void onBookAdded(String path[], boolean refreshdrawer);
void onHistoryCleared();
}
}