-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DataUtils.java
439 lines (359 loc) · 12.9 KB
/
DataUtils.java
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
/*
* DataUtils.java
*
* Copyright (C) 2016-2018 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 android.support.annotation.Nullable;
import android.view.MenuItem;
import com.amaze.filemanager.ui.views.drawer.MenuMetadata;
import com.amaze.filemanager.utils.application.AppConfig;
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 java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
* Singleton class to handle data for various services
*/
//Central data being used across activity,fragments and classes
public class DataUtils {
public static final int DELETE = 0, COPY = 1, MOVE = 2, NEW_FOLDER = 3,
RENAME = 4, NEW_FILE = 5, EXTRACT = 6, COMPRESS = 7, SAVE_FILE = 8;
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 HashMap<MenuItem, MenuMetadata> menuMetadataMap = new HashMap<>();//Faster HashMap<Integer, V>
private ArrayList<String[]> servers = new ArrayList<>();
private ArrayList<String[]> books = new ArrayList<>();
private ArrayList<CloudStorage> accounts = new ArrayList<>(4);
private DataChangeListener dataChangeListener;
private static DataUtils sDataUtils;
public static DataUtils getInstance() {
if (sDataUtils == null) {
sDataUtils = new DataUtils();
}
return sDataUtils;
}
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());
menuMetadataMap.clear();
servers = new ArrayList<>();
books = new ArrayList<>();
accounts = new ArrayList<>();
}
public void registerOnDataChangedListener(DataChangeListener l) {
dataChangeListener = l;
clear();
}
int contains(String a, ArrayList<String[]> b) {
int i = 0;
for (String[] x : b) {
if (x[1].equals(a)) return i;
i++;
}
return -1;
}
int contains(String[] a, ArrayList<String[]> b) {
if (b == null) return -1;
int i = 0;
for (String[] x : b) {
if (x[0].equals(a[0]) && x[1].equals(a[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) {
synchronized (books) {
books.add(i);
}
}
public void addBook(final String[] i, boolean refreshdrawer) {
synchronized (books) {
books.add(i);
}
if (refreshdrawer && dataChangeListener != null) {
AppConfig.runInBackground(() -> dataChangeListener.onBookAdded(i, 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) {
AppConfig.runInBackground(() -> dataChangeListener.onHiddenFileAdded(i));
}
}
public void removeHiddenFile(final String i) {
synchronized (hiddenfiles) {
hiddenfiles.remove(i);
}
if (dataChangeListener != null) {
AppConfig.runInBackground(() -> 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) {
AppConfig.runInBackground(() -> 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:
return null;
}
}
return null;
}
public boolean isFileHidden(String path) {
return getHiddenFiles().getValueForExactKey(path) != null;
}
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.runInBackground(() -> dataChangeListener.onHistoryCleared());
}
}
public synchronized List<String> getStorages() {
return storages;
}
public synchronized void setStorages(ArrayList<String> storages) {
this.storages = storages;
}
public MenuMetadata getDrawerMetadata(MenuItem item) {
return menuMetadataMap.get(item);
}
public void putDrawerMetadata(MenuItem item, MenuMetadata metadata) {
menuMetadataMap.put(item, metadata);
if(metadata.path != null) tree.put(metadata.path, item.getItemId());
}
/**
* @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);
}
/**
* Callbacks to do original changes in database (and ui if required)
* The callbacks are called in a background thread
*/
public interface DataChangeListener {
void onHiddenFileAdded(String path);
void onHiddenFileRemoved(String path);
void onHistoryAdded(String path);
void onBookAdded(String path[], boolean refreshdrawer);
void onHistoryCleared();
}
}