forked from owncloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileActivity.java
More file actions
489 lines (427 loc) · 17.9 KB
/
FileActivity.java
File metadata and controls
489 lines (427 loc) · 17.9 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
/**
* ownCloud Android client application
*
* @author David A. Velasco
* @author David González Verdugo
* @author Christian Schabesberger
* @author Abel García de Prada
* @author Aitor Ballesteros Pavón
* @author Jorge Aguado Recio
* <p>
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2025 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 com.owncloud.android.ui.activity;
import android.accounts.Account;
import android.accounts.AuthenticatorException;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Parcelable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.snackbar.Snackbar;
import com.owncloud.android.R;
import com.owncloud.android.domain.files.model.FileListOption;
import com.owncloud.android.domain.files.model.OCFile;
import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.presentation.authentication.AccountUtils;
import com.owncloud.android.presentation.authentication.AuthenticatorConstants;
import com.owncloud.android.presentation.authentication.LoginActivity;
import com.owncloud.android.services.OperationsService;
import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.SslUntrustedCertDialog;
import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter;
import com.owncloud.android.ui.helpers.FileOperationsHelper;
import kotlin.Unit;
import timber.log.Timber;
import static com.owncloud.android.extensions.ActivityExtKt.showSnackbarWithAction;
/**
* Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
*/
public class FileActivity extends DrawerActivity
implements OnRemoteOperationListener, ComponentsGetter, SslUntrustedCertDialog.OnSslUntrustedCertListener {
public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
public static final String EXTRA_FROM_NOTIFICATION =
"com.owncloud.android.ui.activity.FROM_NOTIFICATION";
public static final String EXTRA_FILE_LIST_OPTION = "EXTRA_FILE_LIST_OPTION";
private static final String KEY_WAITING_FOR_OP_ID = "WAITING_FOR_OP_ID";
private static final String KEY_ACTION_BAR_TITLE = "ACTION_BAR_TITLE";
// go to a high number, since the low numbers are used by android
public static final int REQUEST_CODE__UPDATE_CREDENTIALS = 0;
public static final int REQUEST_CODE__LAST_SHARED = REQUEST_CODE__UPDATE_CREDENTIALS;
protected static final long DELAY_TO_REQUEST_OPERATIONS_LATER = 200;
/* Dialog tags */
private static final String DIALOG_UNTRUSTED_CERT = "DIALOG_UNTRUSTED_CERT";
private static final String DIALOG_CERT_NOT_SAVED = "DIALOG_CERT_NOT_SAVED";
/**
* Main {@link OCFile} handled by the activity.
*/
private OCFile mFile;
/**
* Flag to signal if the activity is launched by a notification
*/
private boolean mFromNotification;
/**
* Messages handler associated to the main thread and the life cycle of the activity
*/
private Handler mHandler;
private FileOperationsHelper mFileOperationsHelper;
private ServiceConnection mOperationsServiceConnection = null;
private OperationsServiceBinder mOperationsServiceBinder = null;
private boolean mResumed = false;
/**
* Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
* the {@link FileActivity}.
* <p>
* Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
* is requested to create a new one.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
mFileOperationsHelper = new FileOperationsHelper(this);
Account account = null;
if (savedInstanceState != null) {
mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
mFileOperationsHelper.setOpIdWaitingFor(
savedInstanceState.getLong(KEY_WAITING_FOR_OP_ID, Long.MAX_VALUE)
);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(savedInstanceState.getString(KEY_ACTION_BAR_TITLE));
}
} else {
account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION,
false);
}
AccountUtils.updateAccountVersion(this); // best place, before any access to AccountManager
// or database
setAccount(account, savedInstanceState != null);
mOperationsServiceConnection = new OperationsServiceConnection();
bindService(new Intent(this, OperationsService.class), mOperationsServiceConnection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
mResumed = true;
if (mOperationsServiceBinder != null) {
doOnResumeAndBound();
}
}
@Override
protected void onPause() {
if (mOperationsServiceBinder != null) {
mOperationsServiceBinder.removeOperationListener(this);
}
mResumed = false;
super.onPause();
}
@Override
protected void onDestroy() {
if (mOperationsServiceConnection != null) {
unbindService(mOperationsServiceConnection);
mOperationsServiceBinder = null;
}
super.onDestroy();
}
/**
* {@inheritDoc}
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
outState.putLong(KEY_WAITING_FOR_OP_ID, mFileOperationsHelper.getOpIdWaitingFor());
if (getSupportActionBar() != null && getSupportActionBar().getTitle() != null) {
// Null check in case the actionbar is used in ActionBar.NAVIGATION_MODE_LIST
// since it doesn't have a title then
outState.putString(KEY_ACTION_BAR_TITLE, getSupportActionBar().getTitle().toString());
}
}
/**
* Getter for the main {@link OCFile} handled by the activity.
*
* @return Main {@link OCFile} handled by the activity.
*/
public OCFile getFile() {
return mFile;
}
/**
* Setter for the main {@link OCFile} handled by the activity.
*
* @param file Main {@link OCFile} to be handled by the activity.
*/
public void setFile(OCFile file) {
mFile = file;
}
/**
* @return Value of mFromNotification: True if the Activity is launched by a notification
*/
public boolean fromNotification() {
return mFromNotification;
}
public OperationsServiceBinder getOperationsServiceBinder() {
return mOperationsServiceBinder;
}
public OnRemoteOperationListener getRemoteOperationListener() {
return this;
}
public Handler getHandler() {
return mHandler;
}
public FileOperationsHelper getFileOperationsHelper() {
return mFileOperationsHelper;
}
/**
* @param operation Operation performed.
* @param result Result of the removal.
*/
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
Timber.d("Received result of operation in FileActivity - common behaviour for all the FileActivities");
mFileOperationsHelper.setOpIdWaitingFor(Long.MAX_VALUE);
dismissLoadingDialog();
if (!result.isSuccess() && (
result.getCode() == ResultCode.UNAUTHORIZED ||
(result.isException() && result.getException() instanceof AuthenticatorException)
)) {
requestCredentialsUpdate();
if (result.getCode() == ResultCode.UNAUTHORIZED) {
showSnackMessage(
ErrorMessageAdapter.Companion.getResultMessage(result, getResources())
);
}
} else if (!result.isSuccess() && ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(result.getCode())) {
showUntrustedCertDialog(result);
}
}
protected void showRequestAccountChangeNotice(String errorMessage, boolean mustChange) {
if (mustChange) {
new AlertDialog.Builder(this)
.setTitle(R.string.auth_failure_snackbar_action)
.setMessage(errorMessage)
.setPositiveButton(android.R.string.yes, (dialog, which) -> requestCredentialsUpdate())
.setIcon(R.drawable.common_error_grey)
.setCancelable(false)
.show();
} else {
showSnackbarWithAction(
this,
errorMessage,
getString(R.string.auth_oauth_failure_snackbar_action),
() -> {
requestCredentialsUpdate();
return Unit.INSTANCE;
},
Snackbar.LENGTH_INDEFINITE,
android.R.id.content
);
}
}
protected void showRequestRegainAccess() {
showSnackbarWithAction(
this,
getString(R.string.auth_oauth_failure),
getString(R.string.auth_oauth_failure_snackbar_action),
() -> {
requestCredentialsUpdate();
return Unit.INSTANCE;
},
Snackbar.LENGTH_INDEFINITE,
android.R.id.content
);
}
/**
* Invalidates the credentials stored for the current OC account and requests new credentials to the user,
* navigating to {@link LoginActivity}
* <p>
* Equivalent to call requestCredentialsUpdate(null);
*/
protected void requestCredentialsUpdate() {
requestCredentialsUpdate(null);
}
/**
* Invalidates the credentials stored for the given OC account and requests new credentials to the user,
* navigating to {@link LoginActivity}
*
* @param account Stored OC account to request credentials update for. If null, current account will
* be used.
*/
protected void requestCredentialsUpdate(Account account) {
if (account == null) {
account = getAccount();
}
/// request credentials to user
Intent updateAccountCredentials = new Intent(this, LoginActivity.class);
updateAccountCredentials.putExtra(AuthenticatorConstants.EXTRA_ACCOUNT, account);
updateAccountCredentials.putExtra(
AuthenticatorConstants.EXTRA_ACTION,
AuthenticatorConstants.ACTION_UPDATE_EXPIRED_TOKEN);
updateAccountCredentials.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(updateAccountCredentials);
}
/**
* Show untrusted cert dialog
*/
public void showUntrustedCertDialog(RemoteOperationResult result) {
// Show a dialog with the certificate info
FragmentManager fm = getSupportFragmentManager();
SslUntrustedCertDialog dialog = (SslUntrustedCertDialog) fm.findFragmentByTag(DIALOG_UNTRUSTED_CERT);
if (dialog == null) {
dialog = SslUntrustedCertDialog.newInstanceForFullSslError(
(CertificateCombinedException) result.getException());
FragmentTransaction ft = fm.beginTransaction();
dialog.show(ft, DIALOG_UNTRUSTED_CERT);
}
}
/**
* Show untrusted cert dialog
*/
public void showUntrustedCertDialogForThrowable(Throwable throwable) {
// Show a dialog with the certificate info
FragmentManager fm = getSupportFragmentManager();
SslUntrustedCertDialog dialog = (SslUntrustedCertDialog) fm.findFragmentByTag(DIALOG_UNTRUSTED_CERT);
if (dialog == null) {
dialog = SslUntrustedCertDialog.newInstanceForFullSslError(
(CertificateCombinedException) throwable);
FragmentTransaction ft = fm.beginTransaction();
dialog.show(ft, DIALOG_UNTRUSTED_CERT);
}
}
protected void updateFileFromDB() {
OCFile file = getFile();
if (file != null) {
file = getStorageManager().getFileByPath(file.getRemotePath(), file.getSpaceId());
setFile(file);
}
}
private void doOnResumeAndBound() {
mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler);
long waitingForOpId = mFileOperationsHelper.getOpIdWaitingFor();
if (waitingForOpId <= Integer.MAX_VALUE) {
boolean wait = mOperationsServiceBinder.dispatchResultIfFinished((int) waitingForOpId,
this);
if (!wait) {
dismissLoadingDialog();
}
}
}
/**
* Implements callback methods for service binding. Passed as a parameter to {
*/
private class OperationsServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName component, IBinder service) {
if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
Timber.d("Operations service connected");
mOperationsServiceBinder = (OperationsServiceBinder) service;
if (mResumed) {
doOnResumeAndBound();
}
}
}
@Override
public void onServiceDisconnected(ComponentName component) {
if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) {
Timber.d("Operations service disconnected");
mOperationsServiceBinder = null;
// TODO whatever could be waiting for the service is unbound
}
}
}
@Override
public void restart() {
Intent i = new Intent(this, FileDisplayActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra(EXTRA_FILE_LIST_OPTION, (Parcelable) FileListOption.ALL_FILES);
startActivity(i);
}
@Override
public void navigateToOption(FileListOption fileListOption) {
Intent intent;
switch (fileListOption) {
case ALL_FILES:
restart();
break;
case SPACES_LIST:
intent = new Intent(this, FileDisplayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EXTRA_FILE_LIST_OPTION, (Parcelable) FileListOption.SPACES_LIST);
startActivity(intent);
break;
case SHARED_BY_LINK:
intent = new Intent(this, FileDisplayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EXTRA_FILE_LIST_OPTION, (Parcelable) FileListOption.SHARED_BY_LINK);
startActivity(intent);
break;
case AV_OFFLINE:
intent = new Intent(this, FileDisplayActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EXTRA_FILE_LIST_OPTION, (Parcelable) FileListOption.AV_OFFLINE);
startActivity(intent);
break;
}
}
protected OCFile getCurrentDir() {
OCFile file = getFile();
if (file != null) {
if (file.isFolder()) {
return file;
} else if (getStorageManager() != null) {
String parentPath = file.getParentRemotePath();
return getStorageManager().getFileByPath(parentPath, file.getSpaceId());
}
}
return null;
}
/* OnSslUntrustedCertListener methods */
@Override
public void onSavedCertificate() {
// Nothing to do in this context
}
@Override
public void onFailedSavingCertificate() {
ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(
R.string.ssl_validator_not_saved, new String[]{}, 0, android.R.string.ok, -1, -1
);
dialog.show(getSupportFragmentManager(), DIALOG_CERT_NOT_SAVED);
}
@Override
public void onCancelCertificate() {
// nothing to do
}
}