-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathAppConfig.java
More file actions
316 lines (268 loc) · 10.1 KB
/
AppConfig.java
File metadata and controls
316 lines (268 loc) · 10.1 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
/*
* 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.application;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import org.acra.ACRA;
import org.acra.attachment.DefaultAttachmentProvider;
import org.acra.config.CoreConfiguration;
import org.acra.config.CoreConfigurationBuilder;
import org.acra.data.StringFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amaze.filemanager.BuildConfig;
import com.amaze.filemanager.R;
import com.amaze.filemanager.crashreport.ErrorActivity;
import com.amaze.filemanager.database.ExplorerDatabase;
import com.amaze.filemanager.database.UtilitiesDatabase;
import com.amaze.filemanager.database.UtilsHandler;
import com.amaze.filemanager.fileoperations.exceptions.ShellNotRunningException;
import com.amaze.filemanager.fileoperations.filesystem.OpenMode;
import com.amaze.filemanager.filesystem.HybridFile;
import com.amaze.filemanager.filesystem.ssh.CustomSshJConfig;
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants;
import com.amaze.filemanager.ui.provider.UtilitiesProvider;
import com.amaze.filemanager.utils.ScreenUtils;
import com.amaze.trashbin.TrashBin;
import com.amaze.trashbin.TrashBinConfig;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.StrictMode;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;
import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import jcifs.Config;
import jcifs.smb.SmbException;
public class AppConfig extends GlideApplication {
private static final Logger log = LoggerFactory.getLogger(AppConfig.class);
private UtilitiesProvider utilsProvider;
private UtilsHandler utilsHandler;
private WeakReference<Context> mainActivityContext;
private static ScreenUtils screenUtils;
private static AppConfig instance;
private UtilitiesDatabase utilitiesDatabase;
private ExplorerDatabase explorerDatabase;
private TrashBinConfig trashBinConfig;
private TrashBin trashBin;
private static final String TRASH_BIN_BASE_PATH =
Environment.getExternalStorageDirectory().getPath() + File.separator + ".AmazeData";
public UtilitiesProvider getUtilsProvider() {
return utilsProvider;
}
@Override
public void onCreate() {
super.onCreate();
AppCompatDelegate.setCompatVectorFromResourcesEnabled(
true); // selector in srcCompat isn't supported without this
instance = this;
CustomSshJConfig.init();
explorerDatabase = ExplorerDatabase.initialize(this);
utilitiesDatabase = UtilitiesDatabase.initialize(this);
utilsProvider = new UtilitiesProvider(this);
utilsHandler = new UtilsHandler(this, utilitiesDatabase);
runInBackground(Config::registerSmbURLHandler);
// disabling file exposure method check for api n+
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
initACRA();
}
@Override
public void onTerminate() {
super.onTerminate();
}
/**
* Post a runnable to handler. Use this in case we don't have any restriction to execute after
* this runnable is executed, and {@link #runInBackground(Runnable)} in case we need to execute
* something after execution in background
*/
public void runInBackground(Runnable runnable) {
Completable.fromRunnable(runnable).subscribeOn(Schedulers.io()).subscribe();
}
/**
* Shows a toast message
*
* @param context Any context belonging to this application
* @param message The message to show
*/
public static void toast(Context context, @StringRes int message) {
// this is a static method so it is easier to call,
// as the context checking and casting is done for you
if (context == null) return;
if (!(context instanceof Application)) {
context = context.getApplicationContext();
}
if (context instanceof Application) {
final Context c = context;
final @StringRes int m = message;
getInstance().runInApplicationThread(() -> Toast.makeText(c, m, Toast.LENGTH_LONG).show());
}
}
/**
* Shows a toast message
*
* @param context Any context belonging to this application
* @param message The message to show
*/
public static void toast(Context context, String message) {
// this is a static method so it is easier to call,
// as the context checking and casting is done for you
if (context == null) return;
if (!(context instanceof Application)) {
context = context.getApplicationContext();
}
if (context instanceof Application) {
final Context c = context;
final String m = message;
getInstance().runInApplicationThread(() -> Toast.makeText(c, m, Toast.LENGTH_LONG).show());
}
}
/**
* Run a {@link Runnable} in the main application thread
*
* @param r {@link Runnable} to run
*/
public void runInApplicationThread(@NonNull Runnable r) {
Completable.fromRunnable(r).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
}
/**
* Convenience method to run a {@link Callable} in the main application thread. Use when the
* callable's return value is not processed.
*
* @param c {@link Callable} to run
*/
public void runInApplicationThread(@NonNull Callable<Void> c) {
Completable.fromCallable(c).subscribeOn(AndroidSchedulers.mainThread()).subscribe();
}
public static synchronized AppConfig getInstance() {
return instance;
}
public UtilsHandler getUtilsHandler() {
return utilsHandler;
}
public void setMainActivityContext(@NonNull Activity activity) {
mainActivityContext = new WeakReference<>(activity);
screenUtils = new ScreenUtils(activity);
}
public ScreenUtils getScreenUtils() {
return screenUtils;
}
@Nullable
public Context getMainActivityContext() {
return mainActivityContext.get();
}
public ExplorerDatabase getExplorerDatabase() {
return explorerDatabase;
}
public UtilitiesDatabase getUtilitiesDatabase() {
return utilitiesDatabase;
}
/**
* Called in {@link #attachBaseContext(Context)} after calling the {@code super} method. Should be
* overridden if MultiDex is enabled, since it has to be initialized before ACRA.
*/
protected void initACRA() {
if (ACRA.isACRASenderServiceProcess()) {
return;
}
try {
final CoreConfiguration acraConfig =
new CoreConfigurationBuilder()
.withBuildConfigClass(BuildConfig.class)
.withReportFormat(StringFormat.JSON)
.withAttachmentUriProvider(DefaultAttachmentProvider.class)
.withSendReportsInDevMode(true)
.build();
ACRA.init(this, acraConfig);
// Refer to ACRA's constructor for exact exception(s) thrown
} catch (IllegalStateException ace) {
log.warn("failed to initialize ACRA", ace);
ErrorActivity.reportError(
this,
ace,
null,
ErrorActivity.ErrorInfo.make(
ErrorActivity.ERROR_UNKNOWN,
"Could not initialize ACRA crash report",
R.string.app_ui_crash));
}
}
public TrashBin getTrashBinInstance() {
if (trashBin == null) {
trashBin =
new TrashBin(
getApplicationContext(),
true,
getTrashBinConfig(),
s -> {
runInBackground(
() -> {
HybridFile file = new HybridFile(OpenMode.TRASH_BIN, s);
try {
file.delete(getMainActivityContext(), false);
} catch (ShellNotRunningException | SmbException e) {
log.warn("failed to delete file in trash bin cleanup", e);
}
});
return true;
},
null);
}
return trashBin;
}
private TrashBinConfig getTrashBinConfig() {
if (trashBinConfig == null) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int days =
sharedPrefs.getInt(
PreferencesConstants.KEY_TRASH_BIN_RETENTION_DAYS,
TrashBinConfig.RETENTION_DAYS_INFINITE);
long bytes =
sharedPrefs.getLong(
PreferencesConstants.KEY_TRASH_BIN_RETENTION_BYTES,
TrashBinConfig.RETENTION_BYTES_INFINITE);
int numOfFiles =
sharedPrefs.getInt(
PreferencesConstants.KEY_TRASH_BIN_RETENTION_NUM_OF_FILES,
TrashBinConfig.RETENTION_NUM_OF_FILES);
int intervalHours =
sharedPrefs.getInt(
PreferencesConstants.KEY_TRASH_BIN_CLEANUP_INTERVAL_HOURS,
TrashBinConfig.INTERVAL_CLEANUP_HOURS);
trashBinConfig =
new TrashBinConfig(
TRASH_BIN_BASE_PATH, days, bytes, numOfFiles, intervalHours, false, true);
}
return trashBinConfig;
}
}