Added HookActivity for integration of external SDKs
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDisplayActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.ui.activity;
20
21 import java.io.File;
22
23 import android.accounts.Account;
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.app.ProgressDialog;
27 import android.content.BroadcastReceiver;
28 import android.content.ComponentName;
29 import android.content.ContentResolver;
30 import android.content.Context;
31 import android.content.DialogInterface;
32 import android.content.Intent;
33 import android.content.IntentFilter;
34 import android.content.ServiceConnection;
35 import android.content.SharedPreferences;
36 import android.content.SyncRequest;
37 import android.content.res.Resources.NotFoundException;
38 import android.database.Cursor;
39 import android.net.Uri;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.os.IBinder;
43 import android.preference.PreferenceManager;
44 import android.provider.MediaStore;
45 import android.support.v4.app.Fragment;
46 import android.support.v4.app.FragmentManager;
47 import android.support.v4.app.FragmentTransaction;
48 import android.support.v4.content.LocalBroadcastManager;
49 import android.util.Log;
50 import android.view.View;
51 import android.view.ViewGroup;
52 import android.widget.ArrayAdapter;
53 import android.widget.TextView;
54 import android.widget.Toast;
55
56 import com.actionbarsherlock.app.ActionBar;
57 import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
58 import com.actionbarsherlock.view.Menu;
59 import com.actionbarsherlock.view.MenuInflater;
60 import com.actionbarsherlock.view.MenuItem;
61 import com.actionbarsherlock.view.Window;
62 import com.owncloud.android.MainApp;
63 import com.owncloud.android.R;
64 import com.owncloud.android.datamodel.FileDataStorageManager;
65 import com.owncloud.android.datamodel.OCFile;
66 import com.owncloud.android.files.services.FileDownloader;
67 import com.owncloud.android.files.services.FileObserverService;
68 import com.owncloud.android.files.services.FileUploader;
69 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
70 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
71 import com.owncloud.android.operations.CreateFolderOperation;
72
73 import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
74 import com.owncloud.android.lib.operations.common.RemoteOperation;
75 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
76 import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
77
78 import com.owncloud.android.operations.RemoveFileOperation;
79 import com.owncloud.android.operations.RenameFileOperation;
80 import com.owncloud.android.operations.SynchronizeFileOperation;
81 import com.owncloud.android.operations.SynchronizeFolderOperation;
82 import com.owncloud.android.services.OperationsService;
83 import com.owncloud.android.syncadapter.FileSyncAdapter;
84 import com.owncloud.android.ui.dialog.EditNameDialog;
85 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
86 import com.owncloud.android.ui.dialog.LoadingDialog;
87 import com.owncloud.android.ui.dialog.SslValidatorDialog;
88 import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener;
89 import com.owncloud.android.ui.fragment.FileDetailFragment;
90 import com.owncloud.android.ui.fragment.FileFragment;
91 import com.owncloud.android.ui.fragment.OCFileListFragment;
92 import com.owncloud.android.ui.preview.PreviewImageActivity;
93 import com.owncloud.android.ui.preview.PreviewMediaFragment;
94 import com.owncloud.android.ui.preview.PreviewVideoActivity;
95 import com.owncloud.android.utils.DisplayUtils;
96 import com.owncloud.android.utils.Log_OC;
97
98
99 /**
100 * Displays, what files the user has available in his ownCloud.
101 *
102 * @author Bartek Przybylski
103 * @author David A. Velasco
104 */
105
106 public class FileDisplayActivity extends HookActivity implements
107 OCFileListFragment.ContainerActivity, FileDetailFragment.ContainerActivity, OnNavigationListener, OnSslValidatorListener, OnRemoteOperationListener, EditNameDialogListener {
108
109 private ArrayAdapter<String> mDirectories;
110
111 /** Access point to the cached database for the current ownCloud {@link Account} */
112 private FileDataStorageManager mStorageManager = null;
113
114 private SyncBroadcastReceiver mSyncBroadcastReceiver;
115 private UploadFinishReceiver mUploadFinishReceiver;
116 private DownloadFinishReceiver mDownloadFinishReceiver;
117 private OperationsServiceReceiver mOperationsServiceReceiver;
118 private FileDownloaderBinder mDownloaderBinder = null;
119 private FileUploaderBinder mUploaderBinder = null;
120 private ServiceConnection mDownloadConnection = null, mUploadConnection = null;
121 private RemoteOperationResult mLastSslUntrustedServerResult = null;
122
123 private boolean mDualPane;
124 private View mLeftFragmentContainer;
125 private View mRightFragmentContainer;
126
127 private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
128 private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
129 private static final String KEY_REFRESH_SHARES_IN_PROGRESS = "SHARES_IN_PROGRESS";
130
131 public static final int DIALOG_SHORT_WAIT = 0;
132 private static final int DIALOG_CHOOSE_UPLOAD_SOURCE = 1;
133 private static final int DIALOG_SSL_VALIDATOR = 2;
134 private static final int DIALOG_CERT_NOT_SAVED = 3;
135
136 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
137
138 public static final String ACTION_DETAILS = "com.owncloud.android.ui.activity.action.DETAILS";
139
140 private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
141 private static final int ACTION_SELECT_MULTIPLE_FILES = 2;
142
143 private static final String TAG = FileDisplayActivity.class.getSimpleName();
144
145 private static final String TAG_LIST_OF_FILES = "LIST_OF_FILES";
146 private static final String TAG_SECOND_FRAGMENT = "SECOND_FRAGMENT";
147
148 private OCFile mWaitingToPreview;
149 private Handler mHandler;
150
151 private boolean mSyncInProgress = false;
152 private boolean mRefreshSharesInProgress = false;
153
154 @Override
155 protected void onCreate(Bundle savedInstanceState) {
156 Log_OC.d(TAG, "onCreate() start");
157 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
158
159 super.onCreate(savedInstanceState); // this calls onAccountChanged() when ownCloud Account is valid
160
161 mHandler = new Handler();
162
163 /// bindings to transference services
164 mUploadConnection = new ListServiceConnection();
165 mDownloadConnection = new ListServiceConnection();
166 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
167 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
168
169 // PIN CODE request ; best location is to decide, let's try this first
170 if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN) && savedInstanceState == null) {
171 requestPinCode();
172 } else if (getIntent().getAction() == null && savedInstanceState == null) {
173 requestPinCode();
174 }
175
176 /// file observer
177 Intent observer_intent = new Intent(this, FileObserverService.class);
178 observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);
179 startService(observer_intent);
180
181 /// Load of saved instance state
182 if(savedInstanceState != null) {
183 mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW);
184 mSyncInProgress = savedInstanceState.getBoolean(KEY_SYNC_IN_PROGRESS);
185 mRefreshSharesInProgress = savedInstanceState.getBoolean(KEY_REFRESH_SHARES_IN_PROGRESS);
186
187 } else {
188 mWaitingToPreview = null;
189 mSyncInProgress = false;
190 mRefreshSharesInProgress = false;
191 }
192
193 /// USER INTERFACE
194
195 // Inflate and set the layout view
196 setContentView(R.layout.files);
197 mDualPane = getResources().getBoolean(R.bool.large_land_layout);
198 mLeftFragmentContainer = findViewById(R.id.left_fragment_container);
199 mRightFragmentContainer = findViewById(R.id.right_fragment_container);
200 if (savedInstanceState == null) {
201 createMinFragments();
202 }
203
204 // Action bar setup
205 mDirectories = new CustomArrayAdapter<String>(this, R.layout.sherlock_spinner_dropdown_item);
206 getSupportActionBar().setHomeButtonEnabled(true); // mandatory since Android ICS, according to the official documentation
207 setSupportProgressBarIndeterminateVisibility(mSyncInProgress || mRefreshSharesInProgress); // always AFTER setContentView(...) ; to work around bug in its implementation
208
209 Log_OC.d(TAG, "onCreate() end");
210 }
211
212 @Override
213 protected void onStart() {
214 super.onStart();
215 getSupportActionBar().setIcon(DisplayUtils.getSeasonalIconId());
216 }
217
218 @Override
219 protected void onDestroy() {
220 super.onDestroy();
221 if (mDownloadConnection != null)
222 unbindService(mDownloadConnection);
223 if (mUploadConnection != null)
224 unbindService(mUploadConnection);
225 }
226
227
228 /**
229 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
230 */
231 @Override
232 protected void onAccountSet(boolean stateWasRecovered) {
233 if (getAccount() != null) {
234 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
235
236 /// Check whether the 'main' OCFile handled by the Activity is contained in the current Account
237 OCFile file = getFile();
238 // get parent from path
239 String parentPath = "";
240 if (file != null) {
241 if (file.isDown() && file.getLastSyncDateForProperties() == 0) {
242 // upload in progress - right now, files are not inserted in the local cache until the upload is successful
243 // get parent from path
244 parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
245 if (mStorageManager.getFileByPath(parentPath) == null)
246 file = null; // not able to know the directory where the file is uploading
247 } else {
248 file = mStorageManager.getFileByPath(file.getRemotePath()); // currentDir = null if not in the current Account
249 }
250 }
251 if (file == null) {
252 // fall back to root folder
253 file = mStorageManager.getFileByPath(OCFile.ROOT_PATH); // never returns null
254 }
255 setFile(file);
256 setNavigationListWithFolder(file);
257
258 if (!stateWasRecovered) {
259 Log_OC.e(TAG, "Initializing Fragments in onAccountChanged..");
260 initFragmentsWithFile();
261 if (file.isFolder()) {
262 startSyncFolderOperation(file);
263 }
264
265 } else {
266 updateFragmentsVisibility(!file.isFolder());
267 updateNavigationElementsInActionBar(file.isFolder() ? null : file);
268 }
269
270
271 } else {
272 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
273 }
274 }
275
276
277 private void setNavigationListWithFolder(OCFile file) {
278 mDirectories.clear();
279 OCFile fileIt = file;
280 String parentPath;
281 while(fileIt != null && fileIt.getFileName() != OCFile.ROOT_PATH) {
282 if (fileIt.isFolder()) {
283 mDirectories.add(fileIt.getFileName());
284 }
285 //fileIt = mStorageManager.getFileById(fileIt.getParentId());
286 // get parent from path
287 parentPath = fileIt.getRemotePath().substring(0, fileIt.getRemotePath().lastIndexOf(fileIt.getFileName()));
288 fileIt = mStorageManager.getFileByPath(parentPath);
289 }
290 mDirectories.add(OCFile.PATH_SEPARATOR);
291 }
292
293
294 private void createMinFragments() {
295 OCFileListFragment listOfFiles = new OCFileListFragment();
296 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
297 transaction.add(R.id.left_fragment_container, listOfFiles, TAG_LIST_OF_FILES);
298 transaction.commit();
299 }
300
301 private void initFragmentsWithFile() {
302 if (getAccount() != null && getFile() != null) {
303 /// First fragment
304 OCFileListFragment listOfFiles = getListOfFilesFragment();
305 if (listOfFiles != null) {
306 listOfFiles.listDirectory(getCurrentDir());
307 } else {
308 Log.e(TAG, "Still have a chance to lose the initializacion of list fragment >(");
309 }
310
311 /// Second fragment
312 OCFile file = getFile();
313 Fragment secondFragment = chooseInitialSecondFragment(file);
314 if (secondFragment != null) {
315 setSecondFragment(secondFragment);
316 updateFragmentsVisibility(true);
317 updateNavigationElementsInActionBar(file);
318
319 } else {
320 cleanSecondFragment();
321 }
322
323 } else {
324 Log.wtf(TAG, "initFragments() called with invalid NULLs!");
325 if (getAccount() == null) {
326 Log.wtf(TAG, "\t account is NULL");
327 }
328 if (getFile() == null) {
329 Log.wtf(TAG, "\t file is NULL");
330 }
331 }
332 }
333
334 private Fragment chooseInitialSecondFragment(OCFile file) {
335 Fragment secondFragment = null;
336 if (file != null && !file.isFolder()) {
337 if (file.isDown() && PreviewMediaFragment.canBePreviewed(file)
338 && file.getLastSyncDateForProperties() > 0 // temporal fix
339 ) {
340 int startPlaybackPosition = getIntent().getIntExtra(PreviewVideoActivity.EXTRA_START_POSITION, 0);
341 boolean autoplay = getIntent().getBooleanExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, true);
342 secondFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay);
343
344 } else {
345 secondFragment = new FileDetailFragment(file, getAccount());
346 }
347 }
348 return secondFragment;
349 }
350
351
352 /**
353 * Replaces the second fragment managed by the activity with the received as
354 * a parameter.
355 *
356 * Assumes never will be more than two fragments managed at the same time.
357 *
358 * @param fragment New second Fragment to set.
359 */
360 private void setSecondFragment(Fragment fragment) {
361 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
362 transaction.replace(R.id.right_fragment_container, fragment, TAG_SECOND_FRAGMENT);
363 transaction.commit();
364 }
365
366
367 private void updateFragmentsVisibility(boolean existsSecondFragment) {
368 if (mDualPane) {
369 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
370 mLeftFragmentContainer.setVisibility(View.VISIBLE);
371 }
372 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
373 mRightFragmentContainer.setVisibility(View.VISIBLE);
374 }
375
376 } else if (existsSecondFragment) {
377 if (mLeftFragmentContainer.getVisibility() != View.GONE) {
378 mLeftFragmentContainer.setVisibility(View.GONE);
379 }
380 if (mRightFragmentContainer.getVisibility() != View.VISIBLE) {
381 mRightFragmentContainer.setVisibility(View.VISIBLE);
382 }
383
384 } else {
385 if (mLeftFragmentContainer.getVisibility() != View.VISIBLE) {
386 mLeftFragmentContainer.setVisibility(View.VISIBLE);
387 }
388 if (mRightFragmentContainer.getVisibility() != View.GONE) {
389 mRightFragmentContainer.setVisibility(View.GONE);
390 }
391 }
392 }
393
394
395 private OCFileListFragment getListOfFilesFragment() {
396 Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(FileDisplayActivity.TAG_LIST_OF_FILES);
397 if (listOfFiles != null) {
398 return (OCFileListFragment)listOfFiles;
399 }
400 Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
401 return null;
402 }
403
404 protected FileFragment getSecondFragment() {
405 Fragment second = getSupportFragmentManager().findFragmentByTag(FileDisplayActivity.TAG_SECOND_FRAGMENT);
406 if (second != null) {
407 return (FileFragment)second;
408 }
409 return null;
410 }
411
412 public void cleanSecondFragment() {
413 Fragment second = getSecondFragment();
414 if (second != null) {
415 FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
416 tr.remove(second);
417 tr.commit();
418 }
419 updateFragmentsVisibility(false);
420 updateNavigationElementsInActionBar(null);
421 }
422
423 protected void refeshListOfFilesFragment() {
424 OCFileListFragment fileListFragment = getListOfFilesFragment();
425 if (fileListFragment != null) {
426 fileListFragment.listDirectory();
427 }
428 }
429
430 protected void refreshSecondFragment(String downloadEvent, String downloadedRemotePath, boolean success) {
431 FileFragment secondFragment = getSecondFragment();
432 boolean waitedPreview = (mWaitingToPreview != null && mWaitingToPreview.getRemotePath().equals(downloadedRemotePath));
433 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
434 FileDetailFragment detailsFragment = (FileDetailFragment) secondFragment;
435 OCFile fileInFragment = detailsFragment.getFile();
436 if (fileInFragment != null && !downloadedRemotePath.equals(fileInFragment.getRemotePath())) {
437 // the user browsed to other file ; forget the automatic preview
438 mWaitingToPreview = null;
439
440 } else if (downloadEvent.equals(FileDownloader.getDownloadAddedMessage())) {
441 // grant that the right panel updates the progress bar
442 detailsFragment.listenForTransferProgress();
443 detailsFragment.updateFileDetails(true, false);
444
445 } else if (downloadEvent.equals(FileDownloader.getDownloadFinishMessage())) {
446 // update the right panel
447 boolean detailsFragmentChanged = false;
448 if (waitedPreview) {
449 if (success) {
450 mWaitingToPreview = mStorageManager.getFileById(mWaitingToPreview.getFileId()); // update the file from database, for the local storage path
451 if (PreviewMediaFragment.canBePreviewed(mWaitingToPreview)) {
452 startMediaPreview(mWaitingToPreview, 0, true);
453 detailsFragmentChanged = true;
454 } else {
455 openFile(mWaitingToPreview);
456 }
457 }
458 mWaitingToPreview = null;
459 }
460 if (!detailsFragmentChanged) {
461 detailsFragment.updateFileDetails(false, (success));
462 }
463 }
464 }
465 }
466
467
468 @Override
469 public boolean onCreateOptionsMenu(Menu menu) {
470 MenuInflater inflater = getSherlock().getMenuInflater();
471 inflater.inflate(R.menu.main_menu, menu);
472 return true;
473 }
474
475 @Override
476 public boolean onOptionsItemSelected(MenuItem item) {
477 boolean retval = true;
478 switch (item.getItemId()) {
479 case R.id.action_create_dir: {
480 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.uploader_info_dirname), "", -1, -1, this);
481 dialog.show(getSupportFragmentManager(), "createdirdialog");
482 break;
483 }
484 case R.id.action_sync_account: {
485 startSynchronization();
486 break;
487 }
488 case R.id.action_upload: {
489 showDialog(DIALOG_CHOOSE_UPLOAD_SOURCE);
490 break;
491 }
492 case R.id.action_settings: {
493 Intent settingsIntent = new Intent(this, Preferences.class);
494 startActivity(settingsIntent);
495 break;
496 }
497 case android.R.id.home: {
498 FileFragment second = getSecondFragment();
499 OCFile currentDir = getCurrentDir();
500 if((currentDir != null && currentDir.getParentId() != 0) ||
501 (second != null && second.getFile() != null)) {
502 onBackPressed();
503
504 }
505 break;
506 }
507 default:
508 retval = super.onOptionsItemSelected(item);
509 }
510 return retval;
511 }
512
513 private void startSynchronization() {
514 Log_OC.e(TAG, "Got to start sync");
515 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
516 Log_OC.e(TAG, "Canceling all syncs for " + MainApp.getAuthority());
517 ContentResolver.cancelSync(null, MainApp.getAuthority()); // cancel the current synchronizations of any ownCloud account
518 Bundle bundle = new Bundle();
519 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
520 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
521 Log_OC.e(TAG, "Requesting sync for " + getAccount().name + " at " + MainApp.getAuthority());
522 ContentResolver.requestSync(
523 getAccount(),
524 MainApp.getAuthority(), bundle);
525 } else {
526 Log_OC.e(TAG, "Requesting sync for " + getAccount().name + " at " + MainApp.getAuthority() + " with new API");
527 SyncRequest.Builder builder = new SyncRequest.Builder();
528 builder.setSyncAdapter(getAccount(), MainApp.getAuthority());
529 builder.setExpedited(true);
530 builder.setManual(true);
531 builder.syncOnce();
532 SyncRequest request = builder.build();
533 ContentResolver.requestSync(request);
534 }
535 }
536
537
538 @Override
539 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
540 if (itemPosition != 0) {
541 String targetPath = "";
542 for (int i=itemPosition; i < mDirectories.getCount() - 1; i++) {
543 targetPath = mDirectories.getItem(i) + OCFile.PATH_SEPARATOR + targetPath;
544 }
545 targetPath = OCFile.PATH_SEPARATOR + targetPath;
546 OCFile targetFolder = mStorageManager.getFileByPath(targetPath);
547 if (targetFolder != null) {
548 browseTo(targetFolder);
549 }
550
551 // the next operation triggers a new call to this method, but it's necessary to
552 // ensure that the name exposed in the action bar is the current directory when the
553 // user selected it in the navigation list
554 if (getSupportActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST && itemPosition != 0)
555 getSupportActionBar().setSelectedNavigationItem(0);
556 }
557 return true;
558 }
559
560 /**
561 * Called, when the user selected something for uploading
562 */
563 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
564 super.onActivityResult(requestCode, resultCode, data);
565
566 if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
567 requestSimpleUpload(data, resultCode);
568
569 } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) {
570 requestMultipleUpload(data, resultCode);
571
572 }
573 }
574
575 private void requestMultipleUpload(Intent data, int resultCode) {
576 String[] filePaths = data.getStringArrayExtra(UploadFilesActivity.EXTRA_CHOSEN_FILES);
577 if (filePaths != null) {
578 String[] remotePaths = new String[filePaths.length];
579 String remotePathBase = "";
580 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
581 remotePathBase += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
582 }
583 if (!remotePathBase.endsWith(OCFile.PATH_SEPARATOR))
584 remotePathBase += OCFile.PATH_SEPARATOR;
585 for (int j = 0; j< remotePaths.length; j++) {
586 remotePaths[j] = remotePathBase + (new File(filePaths[j])).getName();
587 }
588
589 Intent i = new Intent(this, FileUploader.class);
590 i.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
591 i.putExtra(FileUploader.KEY_LOCAL_FILE, filePaths);
592 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
593 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
594 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
595 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
596 startService(i);
597
598 } else {
599 Log_OC.d(TAG, "User clicked on 'Update' with no selection");
600 Toast t = Toast.makeText(this, getString(R.string.filedisplay_no_file_selected), Toast.LENGTH_LONG);
601 t.show();
602 return;
603 }
604 }
605
606
607 private void requestSimpleUpload(Intent data, int resultCode) {
608 String filepath = null;
609 try {
610 Uri selectedImageUri = data.getData();
611
612 String filemanagerstring = selectedImageUri.getPath();
613 String selectedImagePath = getPath(selectedImageUri);
614
615 if (selectedImagePath != null)
616 filepath = selectedImagePath;
617 else
618 filepath = filemanagerstring;
619
620 } catch (Exception e) {
621 Log_OC.e(TAG, "Unexpected exception when trying to read the result of Intent.ACTION_GET_CONTENT", e);
622 e.printStackTrace();
623
624 } finally {
625 if (filepath == null) {
626 Log_OC.e(TAG, "Couldnt resolve path to file");
627 Toast t = Toast.makeText(this, getString(R.string.filedisplay_unexpected_bad_get_content), Toast.LENGTH_LONG);
628 t.show();
629 return;
630 }
631 }
632
633 Intent i = new Intent(this, FileUploader.class);
634 i.putExtra(FileUploader.KEY_ACCOUNT,
635 getAccount());
636 String remotepath = new String();
637 for (int j = mDirectories.getCount() - 2; j >= 0; --j) {
638 remotepath += OCFile.PATH_SEPARATOR + mDirectories.getItem(j);
639 }
640 if (!remotepath.endsWith(OCFile.PATH_SEPARATOR))
641 remotepath += OCFile.PATH_SEPARATOR;
642 remotepath += new File(filepath).getName();
643
644 i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
645 i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
646 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
647 if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
648 i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
649 startService(i);
650 }
651
652 @Override
653 public void onBackPressed() {
654 OCFileListFragment listOfFiles = getListOfFilesFragment();
655 if (mDualPane || getSecondFragment() == null) {
656 if (listOfFiles != null) { // should never be null, indeed
657 if (mDirectories.getCount() <= 1) {
658 finish();
659 return;
660 }
661 int levelsUp = listOfFiles.onBrowseUp();
662 for (int i=0; i < levelsUp && mDirectories.getCount() > 1 ; i++) {
663 popDirname();
664 }
665 }
666 }
667 if (listOfFiles != null) { // should never be null, indeed
668 setFile(listOfFiles.getCurrentFile());
669 }
670 cleanSecondFragment();
671
672 }
673
674 @Override
675 protected void onSaveInstanceState(Bundle outState) {
676 // responsibility of restore is preferred in onCreate() before than in onRestoreInstanceState when there are Fragments involved
677 Log_OC.e(TAG, "onSaveInstanceState() start");
678 super.onSaveInstanceState(outState);
679 outState.putParcelable(FileDisplayActivity.KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
680 outState.putBoolean(FileDisplayActivity.KEY_SYNC_IN_PROGRESS, mSyncInProgress);
681 outState.putBoolean(FileDisplayActivity.KEY_REFRESH_SHARES_IN_PROGRESS, mRefreshSharesInProgress);
682
683 Log_OC.d(TAG, "onSaveInstanceState() end");
684 }
685
686
687
688 @Override
689 protected void onResume() {
690 super.onResume();
691 Log_OC.e(TAG, "onResume() start");
692
693 // Listen for sync messages
694 IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
695 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
696 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FOLDER_SIZE_SYNCED);
697 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FOLDER_CONTENTS_SYNCED);
698 syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED);
699 mSyncBroadcastReceiver = new SyncBroadcastReceiver();
700 //registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
701 LocalBroadcastManager.getInstance(this).registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
702
703 // Listen for upload messages
704 IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage());
705 mUploadFinishReceiver = new UploadFinishReceiver();
706 registerReceiver(mUploadFinishReceiver, uploadIntentFilter);
707
708 // Listen for download messages
709 IntentFilter downloadIntentFilter = new IntentFilter(FileDownloader.getDownloadAddedMessage());
710 downloadIntentFilter.addAction(FileDownloader.getDownloadFinishMessage());
711 mDownloadFinishReceiver = new DownloadFinishReceiver();
712 registerReceiver(mDownloadFinishReceiver, downloadIntentFilter);
713
714 // Listen for messages from the OperationsService
715 IntentFilter operationsIntentFilter = new IntentFilter(OperationsService.ACTION_OPERATION_ADDED);
716 operationsIntentFilter.addAction(OperationsService.ACTION_OPERATION_FINISHED);
717 mOperationsServiceReceiver = new OperationsServiceReceiver();
718 LocalBroadcastManager.getInstance(this).registerReceiver(mOperationsServiceReceiver, operationsIntentFilter);
719
720 Log_OC.d(TAG, "onResume() end");
721 }
722
723
724 @Override
725 protected void onPause() {
726 super.onPause();
727 Log_OC.e(TAG, "onPause() start");
728 if (mSyncBroadcastReceiver != null) {
729 //unregisterReceiver(mSyncBroadcastReceiver);
730 LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
731 mSyncBroadcastReceiver = null;
732 }
733 if (mUploadFinishReceiver != null) {
734 unregisterReceiver(mUploadFinishReceiver);
735 mUploadFinishReceiver = null;
736 }
737 if (mDownloadFinishReceiver != null) {
738 unregisterReceiver(mDownloadFinishReceiver);
739 mDownloadFinishReceiver = null;
740 }
741 if (mOperationsServiceReceiver != null) {
742 LocalBroadcastManager.getInstance(this).unregisterReceiver(mOperationsServiceReceiver);
743 mOperationsServiceReceiver = null;
744 }
745 Log_OC.d(TAG, "onPause() end");
746 }
747
748
749 @Override
750 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
751 if (id == DIALOG_SSL_VALIDATOR && mLastSslUntrustedServerResult != null) {
752 ((SslValidatorDialog)dialog).updateResult(mLastSslUntrustedServerResult);
753 }
754 }
755
756
757 @Override
758 protected Dialog onCreateDialog(int id) {
759 Dialog dialog = null;
760 AlertDialog.Builder builder;
761 switch (id) {
762 case DIALOG_SHORT_WAIT: {
763 ProgressDialog working_dialog = new ProgressDialog(this);
764 working_dialog.setMessage(getResources().getString(
765 R.string.wait_a_moment));
766 working_dialog.setIndeterminate(true);
767 working_dialog.setCancelable(false);
768 dialog = working_dialog;
769 break;
770 }
771 case DIALOG_CHOOSE_UPLOAD_SOURCE: {
772
773 String[] items = null;
774
775 String[] allTheItems = { getString(R.string.actionbar_upload_files),
776 getString(R.string.actionbar_upload_from_apps),
777 getString(R.string.actionbar_failed_instant_upload) };
778
779 String[] commonItems = { getString(R.string.actionbar_upload_files),
780 getString(R.string.actionbar_upload_from_apps) };
781
782 if (InstantUploadActivity.IS_ENABLED)
783 items = allTheItems;
784 else
785 items = commonItems;
786
787 builder = new AlertDialog.Builder(this);
788 builder.setTitle(R.string.actionbar_upload);
789 builder.setItems(items, new DialogInterface.OnClickListener() {
790 public void onClick(DialogInterface dialog, int item) {
791 if (item == 0) {
792 // if (!mDualPane) {
793 Intent action = new Intent(FileDisplayActivity.this, UploadFilesActivity.class);
794 action.putExtra(UploadFilesActivity.EXTRA_ACCOUNT, FileDisplayActivity.this.getAccount());
795 startActivityForResult(action, ACTION_SELECT_MULTIPLE_FILES);
796 // } else {
797 // TODO create and handle new fragment
798 // LocalFileListFragment
799 // }
800 } else if (item == 1) {
801 Intent action = new Intent(Intent.ACTION_GET_CONTENT);
802 action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE);
803 startActivityForResult(Intent.createChooser(action, getString(R.string.upload_chooser_title)),
804 ACTION_SELECT_CONTENT_FROM_APPS);
805 } else if (item == 2 && InstantUploadActivity.IS_ENABLED) {
806 Intent action = new Intent(FileDisplayActivity.this, InstantUploadActivity.class);
807 action.putExtra(FileUploader.KEY_ACCOUNT, FileDisplayActivity.this.getAccount());
808 startActivity(action);
809 }
810 }
811 });
812 dialog = builder.create();
813 break;
814 }
815 case DIALOG_SSL_VALIDATOR: {
816 dialog = SslValidatorDialog.newInstance(this, mLastSslUntrustedServerResult, this);
817 break;
818 }
819 case DIALOG_CERT_NOT_SAVED: {
820 builder = new AlertDialog.Builder(this);
821 builder.setMessage(getResources().getString(R.string.ssl_validator_not_saved));
822 builder.setCancelable(false);
823 builder.setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
824 @Override
825 public void onClick(DialogInterface dialog, int which) {
826 dialog.dismiss();
827 };
828 });
829 dialog = builder.create();
830 break;
831 }
832 default:
833 dialog = null;
834 }
835
836 return dialog;
837 }
838
839
840 /**
841 * Show loading dialog
842 */
843 public void showLoadingDialog() {
844 // Construct dialog
845 LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
846 FragmentManager fm = getSupportFragmentManager();
847 FragmentTransaction ft = fm.beginTransaction();
848 loading.show(ft, DIALOG_WAIT_TAG);
849
850 }
851
852 /**
853 * Dismiss loading dialog
854 */
855 public void dismissLoadingDialog(){
856 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
857 if (frag != null) {
858 LoadingDialog loading = (LoadingDialog) frag;
859 loading.dismiss();
860 }
861 }
862
863
864 /**
865 * Translates a content URI of an image to a physical path
866 * on the disk
867 * @param uri The URI to resolve
868 * @return The path to the image or null if it could not be found
869 */
870 public String getPath(Uri uri) {
871 String[] projection = { MediaStore.Images.Media.DATA };
872 Cursor cursor = managedQuery(uri, projection, null, null, null);
873 if (cursor != null) {
874 int column_index = cursor
875 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
876 cursor.moveToFirst();
877 return cursor.getString(column_index);
878 }
879 return null;
880 }
881
882 /**
883 * Pushes a directory to the drop down list
884 * @param directory to push
885 * @throws IllegalArgumentException If the {@link OCFile#isFolder()} returns false.
886 */
887 public void pushDirname(OCFile directory) {
888 if(!directory.isFolder()){
889 throw new IllegalArgumentException("Only directories may be pushed!");
890 }
891 mDirectories.insert(directory.getFileName(), 0);
892 setFile(directory);
893 }
894
895 /**
896 * Pops a directory name from the drop down list
897 * @return True, unless the stack is empty
898 */
899 public boolean popDirname() {
900 mDirectories.remove(mDirectories.getItem(0));
901 return !mDirectories.isEmpty();
902 }
903
904 // Custom array adapter to override text colors
905 private class CustomArrayAdapter<T> extends ArrayAdapter<T> {
906
907 public CustomArrayAdapter(FileDisplayActivity ctx, int view) {
908 super(ctx, view);
909 }
910
911 public View getView(int position, View convertView, ViewGroup parent) {
912 View v = super.getView(position, convertView, parent);
913
914 ((TextView) v).setTextColor(getResources().getColorStateList(
915 android.R.color.white));
916
917 fixRoot((TextView) v );
918 return v;
919 }
920
921 public View getDropDownView(int position, View convertView,
922 ViewGroup parent) {
923 View v = super.getDropDownView(position, convertView, parent);
924
925 ((TextView) v).setTextColor(getResources().getColorStateList(
926 android.R.color.white));
927
928 fixRoot((TextView) v );
929 return v;
930 }
931
932 private void fixRoot(TextView v) {
933 if (v.getText().equals(OCFile.PATH_SEPARATOR)) {
934 v.setText(R.string.default_display_name_for_root_folder);
935 }
936 }
937
938 }
939
940 private class SyncBroadcastReceiver extends BroadcastReceiver {
941
942 /**
943 * {@link BroadcastReceiver} to enable syncing feedback in UI
944 */
945 @Override
946 public void onReceive(Context context, Intent intent) {
947 String event = intent.getAction();
948 String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
949 String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
950 RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncAdapter.EXTRA_RESULT);
951 boolean sameAccount = (getAccount() != null && accountName.equals(getAccount().name) && mStorageManager != null);
952
953 if (sameAccount) {
954
955 if (!FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
956
957 OCFile currentFile = (getFile() == null) ? null : mStorageManager.getFileByPath(getFile().getRemotePath());
958 OCFile currentDir = (getCurrentDir() == null) ? null : mStorageManager.getFileByPath(getCurrentDir().getRemotePath());
959
960 if (currentDir == null) {
961 // current folder was removed from the server
962 Toast.makeText( FileDisplayActivity.this,
963 String.format(getString(R.string.sync_current_folder_was_removed), mDirectories.getItem(0)),
964 Toast.LENGTH_LONG)
965 .show();
966 browseToRoot();
967
968 } else {
969 if (currentFile == null && !getFile().isFolder()) {
970 // currently selected file was removed in the server, and now we know it
971 cleanSecondFragment();
972 currentFile = currentDir;
973 }
974
975 if (synchFolderRemotePath != null && currentDir.getRemotePath().equals(synchFolderRemotePath)) {
976 OCFileListFragment fileListFragment = getListOfFilesFragment();
977 if (fileListFragment != null) {
978 fileListFragment.listDirectory(currentDir);
979 }
980 }
981 setFile(currentFile);
982 }
983
984 mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) &&
985 !SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED.equals(event) &&
986 (synchResult == null || synchResult.isSuccess())) ;
987
988 if (synchResult != null &&
989 synchResult.isSuccess() &&
990 (SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SYNCED.equals(event) ||
991 FileSyncAdapter.EVENT_FOLDER_CONTENTS_SYNCED.equals(event)
992 )
993 ) {
994 startGetShares();
995 }
996
997 }
998 //removeStickyBroadcast(intent);
999 setSupportProgressBarIndeterminateVisibility(mSyncInProgress || mRefreshSharesInProgress);
1000 }
1001
1002 if (synchResult != null) {
1003 if (synchResult.getCode().equals(RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED)) {
1004 mLastSslUntrustedServerResult = synchResult;
1005 showDialog(DIALOG_SSL_VALIDATOR);
1006 }
1007 }
1008 }
1009 }
1010
1011
1012 private class UploadFinishReceiver extends BroadcastReceiver {
1013 /**
1014 * Once the file upload has finished -> update view
1015 * @author David A. Velasco
1016 * {@link BroadcastReceiver} to enable upload feedback in UI
1017 */
1018 @Override
1019 public void onReceive(Context context, Intent intent) {
1020 String uploadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1021 String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
1022 boolean sameAccount = getAccount() != null && accountName.equals(getAccount().name);
1023 OCFile currentDir = getCurrentDir();
1024 boolean isDescendant = (currentDir != null) && (uploadedRemotePath != null) && (uploadedRemotePath.startsWith(currentDir.getRemotePath()));
1025 if (sameAccount && isDescendant) {
1026 refeshListOfFilesFragment();
1027 }
1028 }
1029
1030 }
1031
1032
1033 /**
1034 * Class waiting for broadcast events from the {@link FielDownloader} service.
1035 *
1036 * Updates the UI when a download is started or finished, provided that it is relevant for the
1037 * current folder.
1038 */
1039 private class DownloadFinishReceiver extends BroadcastReceiver {
1040 @Override
1041 public void onReceive(Context context, Intent intent) {
1042 boolean sameAccount = isSameAccount(context, intent);
1043 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
1044 boolean isDescendant = isDescendant(downloadedRemotePath);
1045
1046 if (sameAccount && isDescendant) {
1047 refeshListOfFilesFragment();
1048 refreshSecondFragment(intent.getAction(), downloadedRemotePath, intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false));
1049 }
1050
1051 removeStickyBroadcast(intent);
1052 }
1053
1054 private boolean isDescendant(String downloadedRemotePath) {
1055 OCFile currentDir = getCurrentDir();
1056 return (currentDir != null && downloadedRemotePath != null && downloadedRemotePath.startsWith(currentDir.getRemotePath()));
1057 }
1058
1059 private boolean isSameAccount(Context context, Intent intent) {
1060 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
1061 return (accountName != null && getAccount() != null && accountName.equals(getAccount().name));
1062 }
1063 }
1064
1065
1066 /**
1067 * Class waiting for broadcast events from the {@link OperationsService}.
1068 *
1069 * Updates the list of files when a get for shares is finished; at this moment the refresh of shares is the only
1070 * operation performed in {@link OperationsService}.
1071 *
1072 * In the future will handle the progress or finalization of all the operations performed in {@link OperationsService},
1073 * probably all the operations associated to the app model.
1074 */
1075 private class OperationsServiceReceiver extends BroadcastReceiver {
1076
1077 @Override
1078 public void onReceive(Context context, Intent intent) {
1079 if (OperationsService.ACTION_OPERATION_ADDED.equals(intent.getAction())) {
1080
1081 } else if (OperationsService.ACTION_OPERATION_FINISHED.equals(intent.getAction())) {
1082 mRefreshSharesInProgress = false;
1083
1084 Account account = intent.getParcelableExtra(OperationsService.EXTRA_ACCOUNT);
1085 RemoteOperationResult getSharesResult = (RemoteOperationResult)intent.getSerializableExtra(OperationsService.EXTRA_RESULT);
1086 if (getAccount() != null && account.name.equals(getAccount().name)
1087 && mStorageManager != null
1088 ) {
1089 refeshListOfFilesFragment();
1090 }
1091 if ((getSharesResult != null) &&
1092 RemoteOperationResult.ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED.equals(getSharesResult.getCode())) {
1093 mLastSslUntrustedServerResult = getSharesResult;
1094 showDialog(DIALOG_SSL_VALIDATOR);
1095 }
1096
1097 setSupportProgressBarIndeterminateVisibility(mRefreshSharesInProgress || mSyncInProgress);
1098 }
1099
1100 }
1101
1102 }
1103
1104
1105 /**
1106 * {@inheritDoc}
1107 */
1108 @Override
1109 public FileDataStorageManager getStorageManager() {
1110 return mStorageManager;
1111 }
1112
1113
1114 public void browseToRoot() {
1115 OCFileListFragment listOfFiles = getListOfFilesFragment();
1116 if (listOfFiles != null) { // should never be null, indeed
1117 while (mDirectories.getCount() > 1) {
1118 popDirname();
1119 }
1120 OCFile root = mStorageManager.getFileByPath(OCFile.ROOT_PATH);
1121 listOfFiles.listDirectory(root);
1122 setFile(listOfFiles.getCurrentFile());
1123 startSyncFolderOperation(root);
1124 }
1125 cleanSecondFragment();
1126 }
1127
1128
1129 public void browseTo(OCFile folder) {
1130 if (folder == null || !folder.isFolder()) {
1131 throw new IllegalArgumentException("Trying to browse to invalid folder " + folder);
1132 }
1133 OCFileListFragment listOfFiles = getListOfFilesFragment();
1134 if (listOfFiles != null) {
1135 setNavigationListWithFolder(folder);
1136 listOfFiles.listDirectory(folder);
1137 setFile(listOfFiles.getCurrentFile());
1138 startSyncFolderOperation(folder);
1139 } else {
1140 Log_OC.e(TAG, "Unexpected null when accessing list fragment");
1141 }
1142 cleanSecondFragment();
1143 }
1144
1145
1146 /**
1147 * {@inheritDoc}
1148 *
1149 * Updates action bar and second fragment, if in dual pane mode.
1150 */
1151 @Override
1152 public void onBrowsedDownTo(OCFile directory) {
1153 pushDirname(directory);
1154 cleanSecondFragment();
1155
1156 // Sync Folder
1157 startSyncFolderOperation(directory);
1158
1159 }
1160
1161 /**
1162 * Opens the image gallery showing the image {@link OCFile} received as parameter.
1163 *
1164 * @param file Image {@link OCFile} to show.
1165 */
1166 @Override
1167 public void startImagePreview(OCFile file) {
1168 Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class);
1169 showDetailsIntent.putExtra(EXTRA_FILE, file);
1170 showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
1171 startActivity(showDetailsIntent);
1172 }
1173
1174 /**
1175 * Stars the preview of an already down media {@link OCFile}.
1176 *
1177 * @param file Media {@link OCFile} to preview.
1178 * @param startPlaybackPosition Media position where the playback will be started, in milliseconds.
1179 * @param autoplay When 'true', the playback will start without user interactions.
1180 */
1181 @Override
1182 public void startMediaPreview(OCFile file, int startPlaybackPosition, boolean autoplay) {
1183 Fragment mediaFragment = new PreviewMediaFragment(file, getAccount(), startPlaybackPosition, autoplay);
1184 setSecondFragment(mediaFragment);
1185 updateFragmentsVisibility(true);
1186 updateNavigationElementsInActionBar(file);
1187 setFile(file);
1188 }
1189
1190 /**
1191 * Requests the download of the received {@link OCFile} , updates the UI
1192 * to monitor the download progress and prepares the activity to preview
1193 * or open the file when the download finishes.
1194 *
1195 * @param file {@link OCFile} to download and preview.
1196 */
1197 @Override
1198 public void startDownloadForPreview(OCFile file) {
1199 Fragment detailFragment = new FileDetailFragment(file, getAccount());
1200 setSecondFragment(detailFragment);
1201 mWaitingToPreview = file;
1202 requestForDownload();
1203 updateFragmentsVisibility(true);
1204 updateNavigationElementsInActionBar(file);
1205 setFile(file);
1206 }
1207
1208
1209 /**
1210 * Shows the information of the {@link OCFile} received as a
1211 * parameter in the second fragment.
1212 *
1213 * @param file {@link OCFile} whose details will be shown
1214 */
1215 @Override
1216 public void showDetails(OCFile file) {
1217 Fragment detailFragment = new FileDetailFragment(file, getAccount());
1218 setSecondFragment(detailFragment);
1219 updateFragmentsVisibility(true);
1220 updateNavigationElementsInActionBar(file);
1221 setFile(file);
1222 }
1223
1224
1225 /**
1226 * TODO
1227 */
1228 private void updateNavigationElementsInActionBar(OCFile chosenFile) {
1229 ActionBar actionBar = getSupportActionBar();
1230 if (chosenFile == null || mDualPane) {
1231 // only list of files - set for browsing through folders
1232 OCFile currentDir = getCurrentDir();
1233 boolean noRoot = (currentDir != null && currentDir.getParentId() != 0);
1234 actionBar.setDisplayHomeAsUpEnabled(noRoot);
1235 actionBar.setDisplayShowTitleEnabled(!noRoot);
1236 if (!noRoot) {
1237 actionBar.setTitle(getString(R.string.default_display_name_for_root_folder));
1238 }
1239 actionBar.setNavigationMode(!noRoot ? ActionBar.NAVIGATION_MODE_STANDARD : ActionBar.NAVIGATION_MODE_LIST);
1240 actionBar.setListNavigationCallbacks(mDirectories, this); // assuming mDirectories is updated
1241
1242 } else {
1243 actionBar.setDisplayHomeAsUpEnabled(true);
1244 actionBar.setDisplayShowTitleEnabled(true);
1245 actionBar.setTitle(chosenFile.getFileName());
1246 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
1247 }
1248 }
1249
1250
1251 /**
1252 * {@inheritDoc}
1253 */
1254 @Override
1255 public void onFileStateChanged() {
1256 refeshListOfFilesFragment();
1257 updateNavigationElementsInActionBar(getSecondFragment().getFile());
1258 }
1259
1260
1261 /**
1262 * {@inheritDoc}
1263 */
1264 @Override
1265 public FileDownloaderBinder getFileDownloaderBinder() {
1266 return mDownloaderBinder;
1267 }
1268
1269
1270 /**
1271 * {@inheritDoc}
1272 */
1273 @Override
1274 public FileUploaderBinder getFileUploaderBinder() {
1275 return mUploaderBinder;
1276 }
1277
1278
1279 /** Defines callbacks for service binding, passed to bindService() */
1280 private class ListServiceConnection implements ServiceConnection {
1281
1282 @Override
1283 public void onServiceConnected(ComponentName component, IBinder service) {
1284 if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
1285 Log_OC.d(TAG, "Download service connected");
1286 mDownloaderBinder = (FileDownloaderBinder) service;
1287 if (mWaitingToPreview != null) {
1288 requestForDownload();
1289 }
1290
1291 } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
1292 Log_OC.d(TAG, "Upload service connected");
1293 mUploaderBinder = (FileUploaderBinder) service;
1294 } else {
1295 return;
1296 }
1297 // a new chance to get the mDownloadBinder through getFileDownloadBinder() - THIS IS A MESS
1298 OCFileListFragment listOfFiles = getListOfFilesFragment();
1299 if (listOfFiles != null) {
1300 listOfFiles.listDirectory();
1301 }
1302 FileFragment secondFragment = getSecondFragment();
1303 if (secondFragment != null && secondFragment instanceof FileDetailFragment) {
1304 FileDetailFragment detailFragment = (FileDetailFragment)secondFragment;
1305 detailFragment.listenForTransferProgress();
1306 detailFragment.updateFileDetails(false, false);
1307 }
1308 }
1309
1310 @Override
1311 public void onServiceDisconnected(ComponentName component) {
1312 if (component.equals(new ComponentName(FileDisplayActivity.this, FileDownloader.class))) {
1313 Log_OC.d(TAG, "Download service disconnected");
1314 mDownloaderBinder = null;
1315 } else if (component.equals(new ComponentName(FileDisplayActivity.this, FileUploader.class))) {
1316 Log_OC.d(TAG, "Upload service disconnected");
1317 mUploaderBinder = null;
1318 }
1319 }
1320 };
1321
1322
1323
1324 /**
1325 * Launch an intent to request the PIN code to the user before letting him use the app
1326 */
1327 private void requestPinCode() {
1328 boolean pinStart = false;
1329 SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
1330 pinStart = appPrefs.getBoolean("set_pincode", false);
1331 if (pinStart) {
1332 Intent i = new Intent(getApplicationContext(), PinCodeActivity.class);
1333 i.putExtra(PinCodeActivity.EXTRA_ACTIVITY, "FileDisplayActivity");
1334 startActivity(i);
1335 }
1336 }
1337
1338
1339 @Override
1340 public void onSavedCertificate() {
1341 startSyncFolderOperation(getCurrentDir());
1342 }
1343
1344
1345 @Override
1346 public void onFailedSavingCertificate() {
1347 showDialog(DIALOG_CERT_NOT_SAVED);
1348 }
1349
1350
1351 /**
1352 * Updates the view associated to the activity after the finish of some operation over files
1353 * in the current account.
1354 *
1355 * @param operation Removal operation performed.
1356 * @param result Result of the removal.
1357 */
1358 @Override
1359 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
1360 if (operation instanceof RemoveFileOperation) {
1361 onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
1362
1363 } else if (operation instanceof RenameFileOperation) {
1364 onRenameFileOperationFinish((RenameFileOperation)operation, result);
1365
1366 } else if (operation instanceof SynchronizeFileOperation) {
1367 onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);
1368
1369 } else if (operation instanceof CreateFolderOperation) {
1370 onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
1371 }
1372 }
1373
1374
1375 /**
1376 * Updates the view associated to the activity after the finish of an operation trying to remove a
1377 * file.
1378 *
1379 * @param operation Removal operation performed.
1380 * @param result Result of the removal.
1381 */
1382 private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
1383 dismissLoadingDialog();
1384 if (result.isSuccess()) {
1385 Toast msg = Toast.makeText(this, R.string.remove_success_msg, Toast.LENGTH_LONG);
1386 msg.show();
1387 OCFile removedFile = operation.getFile();
1388 getSecondFragment();
1389 FileFragment second = getSecondFragment();
1390 if (second != null && removedFile.equals(second.getFile())) {
1391 cleanSecondFragment();
1392 }
1393 if (mStorageManager.getFileById(removedFile.getParentId()).equals(getCurrentDir())) {
1394 refeshListOfFilesFragment();
1395 }
1396
1397 } else {
1398 Toast msg = Toast.makeText(this, R.string.remove_fail_msg, Toast.LENGTH_LONG);
1399 msg.show();
1400 if (result.isSslRecoverableException()) {
1401 mLastSslUntrustedServerResult = result;
1402 showDialog(DIALOG_SSL_VALIDATOR);
1403 }
1404 }
1405 }
1406
1407 /**
1408 * Updates the view associated to the activity after the finish of an operation trying create a new folder
1409 *
1410 * @param operation Creation operation performed.
1411 * @param result Result of the creation.
1412 */
1413 private void onCreateFolderOperationFinish(CreateFolderOperation operation, RemoteOperationResult result) {
1414 if (result.isSuccess()) {
1415 dismissLoadingDialog();
1416 refeshListOfFilesFragment();
1417
1418 } else {
1419 dismissLoadingDialog();
1420 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
1421 Toast.makeText(FileDisplayActivity.this, R.string.filename_forbidden_characters, Toast.LENGTH_LONG).show();
1422 } else {
1423 try {
1424 Toast msg = Toast.makeText(FileDisplayActivity.this, R.string.create_dir_fail_msg, Toast.LENGTH_LONG);
1425 msg.show();
1426
1427 } catch (NotFoundException e) {
1428 Log_OC.e(TAG, "Error while trying to show fail message " , e);
1429 }
1430 }
1431 }
1432 }
1433
1434
1435 /**
1436 * Updates the view associated to the activity after the finish of an operation trying to rename a
1437 * file.
1438 *
1439 * @param operation Renaming operation performed.
1440 * @param result Result of the renaming.
1441 */
1442 private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {
1443 dismissLoadingDialog();
1444 OCFile renamedFile = operation.getFile();
1445 if (result.isSuccess()) {
1446 if (mDualPane) {
1447 FileFragment details = getSecondFragment();
1448 if (details != null && details instanceof FileDetailFragment && renamedFile.equals(details.getFile()) ) {
1449 ((FileDetailFragment) details).updateFileDetails(renamedFile, getAccount());
1450 }
1451 }
1452 if (mStorageManager.getFileById(renamedFile.getParentId()).equals(getCurrentDir())) {
1453 refeshListOfFilesFragment();
1454 }
1455
1456 } else {
1457 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
1458 Toast msg = Toast.makeText(this, R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
1459 msg.show();
1460 // TODO throw again the new rename dialog
1461 } if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
1462 Toast msg = Toast.makeText(this, R.string.filename_forbidden_characters, Toast.LENGTH_LONG);
1463 msg.show();
1464 } else {
1465 Toast msg = Toast.makeText(this, R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
1466 msg.show();
1467 if (result.isSslRecoverableException()) {
1468 mLastSslUntrustedServerResult = result;
1469 showDialog(DIALOG_SSL_VALIDATOR);
1470 }
1471 }
1472 }
1473 }
1474
1475
1476 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {
1477 dismissLoadingDialog();
1478 OCFile syncedFile = operation.getLocalFile();
1479 if (!result.isSuccess()) {
1480 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
1481 Intent i = new Intent(this, ConflictsResolveActivity.class);
1482 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, syncedFile);
1483 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, getAccount());
1484 startActivity(i);
1485
1486 }
1487
1488 } else {
1489 if (operation.transferWasRequested()) {
1490 refeshListOfFilesFragment();
1491 onTransferStateChanged(syncedFile, true, true);
1492
1493 } else {
1494 Toast msg = Toast.makeText(this, R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG);
1495 msg.show();
1496 }
1497 }
1498 }
1499
1500
1501 /**
1502 * {@inheritDoc}
1503 */
1504 @Override
1505 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
1506 if (mDualPane) {
1507 FileFragment details = getSecondFragment();
1508 if (details != null && details instanceof FileDetailFragment && file.equals(details.getFile()) ) {
1509 if (downloading || uploading) {
1510 ((FileDetailFragment)details).updateFileDetails(file, getAccount());
1511 } else {
1512 ((FileDetailFragment)details).updateFileDetails(false, true);
1513 }
1514 }
1515 }
1516 }
1517
1518
1519 public void onDismiss(EditNameDialog dialog) {
1520 if (dialog.getResult()) {
1521 String newDirectoryName = dialog.getNewFilename().trim();
1522 Log_OC.d(TAG, "'create directory' dialog dismissed with new name " + newDirectoryName);
1523 if (newDirectoryName.length() > 0) {
1524 String path = getCurrentDir().getRemotePath();
1525
1526 // Create directory
1527 path += newDirectoryName + OCFile.PATH_SEPARATOR;
1528 RemoteOperation operation = new CreateFolderOperation(path, false, mStorageManager);
1529 operation.execute( getAccount(),
1530 FileDisplayActivity.this,
1531 FileDisplayActivity.this,
1532 mHandler,
1533 FileDisplayActivity.this);
1534
1535 showLoadingDialog();
1536 }
1537 }
1538 }
1539
1540
1541 private void requestForDownload() {
1542 Account account = getAccount();
1543 if (!mDownloaderBinder.isDownloading(account, mWaitingToPreview)) {
1544 Intent i = new Intent(this, FileDownloader.class);
1545 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
1546 i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
1547 startService(i);
1548 }
1549 }
1550
1551
1552 private OCFile getCurrentDir() {
1553 OCFile file = getFile();
1554 if (file != null) {
1555 if (file.isFolder()) {
1556 return file;
1557 } else if (mStorageManager != null) {
1558 String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
1559 return mStorageManager.getFileByPath(parentPath);
1560 }
1561 }
1562 return null;
1563 }
1564
1565 public void startSyncFolderOperation(OCFile folder) {
1566 long currentSyncTime = System.currentTimeMillis();
1567
1568 mSyncInProgress = true;
1569
1570 // perform folder synchronization
1571 RemoteOperation synchFolderOp = new SynchronizeFolderOperation( folder,
1572 currentSyncTime,
1573 false,
1574 getStorageManager(),
1575 getAccount(),
1576 getApplicationContext()
1577 );
1578 synchFolderOp.execute(getAccount(), this, null, null, this);
1579
1580 setSupportProgressBarIndeterminateVisibility(true);
1581 }
1582
1583
1584 private void startGetShares() {
1585 // Get shared files/folders
1586 Intent intent = new Intent(this, OperationsService.class);
1587 intent.putExtra(OperationsService.EXTRA_ACCOUNT, getAccount());
1588 startService(intent);
1589
1590 mRefreshSharesInProgress = true;
1591 }
1592
1593 }