Fixed. App crash when setting a path without slash
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / MoveActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.activity;
19
20 import java.io.IOException;
21
22 import android.accounts.Account;
23 import android.accounts.AccountManager;
24 import android.accounts.AuthenticatorException;
25 import android.accounts.OperationCanceledException;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.content.res.Resources.NotFoundException;
31 import android.os.Bundle;
32 import android.support.v4.app.Fragment;
33 import android.support.v4.app.FragmentTransaction;
34 import android.support.v4.widget.SwipeRefreshLayout;
35 import android.util.Log;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.widget.Button;
39 import android.widget.Toast;
40
41 import com.actionbarsherlock.app.ActionBar;
42 import com.actionbarsherlock.view.Menu;
43 import com.actionbarsherlock.view.MenuInflater;
44 import com.actionbarsherlock.view.MenuItem;
45 import com.actionbarsherlock.view.Window;
46 import com.owncloud.android.R;
47 import com.owncloud.android.datamodel.OCFile;
48 import com.owncloud.android.lib.common.OwnCloudAccount;
49 import com.owncloud.android.lib.common.OwnCloudClient;
50 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
51 import com.owncloud.android.lib.common.OwnCloudCredentials;
52 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
53 import com.owncloud.android.lib.common.operations.RemoteOperation;
54 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
55 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
56 import com.owncloud.android.operations.CreateFolderOperation;
57 import com.owncloud.android.operations.SynchronizeFolderOperation;
58 import com.owncloud.android.syncadapter.FileSyncAdapter;
59 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
60 import com.owncloud.android.ui.fragment.FileFragment;
61 import com.owncloud.android.ui.fragment.OCFileListFragment;
62 import com.owncloud.android.utils.DisplayUtils;
63 import com.owncloud.android.utils.ErrorMessageAdapter;
64 import com.owncloud.android.lib.common.utils.Log_OC;
65
66 public class MoveActivity extends HookActivity implements FileFragment.ContainerActivity,
67 OnClickListener, SwipeRefreshLayout.OnRefreshListener {
68
69 public static final String EXTRA_CURRENT_FOLDER = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CURRENT_FOLDER";
70 public static final String EXTRA_TARGET_FILE = UploadFilesActivity.class.getCanonicalName() + "EXTRA_TARGET_FILE";
71
72 public static final int RESULT_OK_AND_MOVE = 1;
73
74 private SyncBroadcastReceiver mSyncBroadcastReceiver;
75
76 private static final String TAG = MoveActivity.class.getSimpleName();
77
78 private static final String TAG_LIST_OF_FOLDERS = "LIST_OF_FOLDERS";
79
80 private boolean mSyncInProgress = false;
81
82 private Button mCancelBtn;
83 private Button mChooseBtn;
84
85
86 @Override
87 protected void onCreate(Bundle savedInstanceState) {
88 Log_OC.d(TAG, "onCreate() start");
89 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
90
91 super.onCreate(savedInstanceState);
92
93 setContentView(R.layout.files_move);
94
95 if (savedInstanceState == null) {
96 createFragments();
97 }
98
99 // sets callback listeners for UI elements
100 initControls();
101
102 // Action bar setup
103 ActionBar actionBar = getSupportActionBar();
104 actionBar.setDisplayShowTitleEnabled(true);
105 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
106 setSupportProgressBarIndeterminateVisibility(mSyncInProgress);
107 // always AFTER setContentView(...) ; to work around bug in its implementation
108
109 // sets message for empty list of folders
110 setBackgroundText();
111
112 Log_OC.d(TAG, "onCreate() end");
113
114 }
115
116 @Override
117 protected void onStart() {
118 super.onStart();
119 getSupportActionBar().setIcon(DisplayUtils.getSeasonalIconId());
120 }
121
122 @Override
123 protected void onDestroy() {
124 super.onDestroy();
125 }
126
127 /**
128 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
129 */
130 @Override
131 protected void onAccountSet(boolean stateWasRecovered) {
132 super.onAccountSet(stateWasRecovered);
133 if (getAccount() != null) {
134
135 updateFileFromDB();
136
137 OCFile folder = getFile();
138 if (folder == null || !folder.isFolder()) {
139 // fall back to root folder
140 setFile(getStorageManager().getFileByPath(OCFile.ROOT_PATH));
141 folder = getFile();
142 }
143
144 if (!stateWasRecovered) {
145 OCFileListFragment listOfFolders = getListOfFilesFragment();
146 listOfFolders.listDirectory(folder);
147
148 startSyncFolderOperation(folder, false);
149 }
150
151 updateNavigationElementsInActionBar();
152 }
153 }
154
155 private void createFragments() {
156 OCFileListFragment listOfFiles = new OCFileListFragment();
157 Bundle args = new Bundle();
158 args.putBoolean(OCFileListFragment.ARG_JUST_FOLDERS, true);
159 args.putBoolean(OCFileListFragment.ARG_ALLOW_CONTEXTUAL_ACTIONS, false);
160 listOfFiles.setArguments(args);
161 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
162 transaction.add(R.id.fragment_container, listOfFiles, TAG_LIST_OF_FOLDERS);
163 transaction.commit();
164 }
165
166 /**
167 * Show a text message on screen view for notifying user if content is
168 * loading or folder is empty
169 */
170 private void setBackgroundText() {
171 OCFileListFragment listFragment = getListOfFilesFragment();
172 if (listFragment != null) {
173 int message = R.string.file_list_loading;
174 if (!mSyncInProgress) {
175 // In case folder list is empty
176 message = R.string.file_list_empty_moving;
177 }
178 listFragment.setMessageForEmptyList(getString(message));
179 } else {
180 Log.e(TAG, "OCFileListFragment is null");
181 }
182 }
183
184 private OCFileListFragment getListOfFilesFragment() {
185 Fragment listOfFiles = getSupportFragmentManager().findFragmentByTag(MoveActivity.TAG_LIST_OF_FOLDERS);
186 if (listOfFiles != null) {
187 return (OCFileListFragment)listOfFiles;
188 }
189 Log_OC.wtf(TAG, "Access to unexisting list of files fragment!!");
190 return null;
191 }
192
193
194 /**
195 * {@inheritDoc}
196 *
197 * Updates action bar and second fragment, if in dual pane mode.
198 */
199 @Override
200 public void onBrowsedDownTo(OCFile directory) {
201 setFile(directory);
202 updateNavigationElementsInActionBar();
203 // Sync Folder
204 startSyncFolderOperation(directory, false);
205
206 }
207
208
209 public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) {
210 long currentSyncTime = System.currentTimeMillis();
211
212 mSyncInProgress = true;
213
214 // perform folder synchronization
215 RemoteOperation synchFolderOp = new SynchronizeFolderOperation( folder,
216 currentSyncTime,
217 false,
218 getFileOperationsHelper().isSharedSupported(),
219 ignoreETag,
220 getStorageManager(),
221 getAccount(),
222 getApplicationContext()
223 );
224 synchFolderOp.execute(getAccount(), this, null, null);
225
226 setSupportProgressBarIndeterminateVisibility(true);
227
228 setBackgroundText();
229 }
230
231 @Override
232 protected void onResume() {
233 super.onResume();
234 Log_OC.e(TAG, "onResume() start");
235
236 // refresh list of files
237 refreshListOfFilesFragment();
238
239 // Listen for sync messages
240 IntentFilter syncIntentFilter = new IntentFilter(FileSyncAdapter.EVENT_FULL_SYNC_START);
241 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_END);
242 syncIntentFilter.addAction(FileSyncAdapter.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED);
243 syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED);
244 syncIntentFilter.addAction(SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED);
245 mSyncBroadcastReceiver = new SyncBroadcastReceiver();
246 registerReceiver(mSyncBroadcastReceiver, syncIntentFilter);
247
248 Log_OC.d(TAG, "onResume() end");
249 }
250
251 @Override
252 protected void onPause() {
253 Log_OC.e(TAG, "onPause() start");
254 if (mSyncBroadcastReceiver != null) {
255 unregisterReceiver(mSyncBroadcastReceiver);
256 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
257 mSyncBroadcastReceiver = null;
258 }
259
260 Log_OC.d(TAG, "onPause() end");
261 super.onPause();
262 }
263
264 @Override
265 public boolean onCreateOptionsMenu(Menu menu) {
266 MenuInflater inflater = getSherlock().getMenuInflater();
267 inflater.inflate(R.menu.main_menu, menu);
268 menu.findItem(R.id.action_upload).setVisible(false);
269 menu.findItem(R.id.action_settings).setVisible(false);
270 menu.findItem(R.id.action_sync_account).setVisible(false);
271 return true;
272 }
273
274 @Override
275 public boolean onOptionsItemSelected(MenuItem item) {
276 boolean retval = true;
277 switch (item.getItemId()) {
278 case R.id.action_create_dir: {
279 CreateFolderDialogFragment dialog =
280 CreateFolderDialogFragment.newInstance(getCurrentFolder());
281 dialog.show(
282 getSupportFragmentManager(),
283 CreateFolderDialogFragment.CREATE_FOLDER_FRAGMENT
284 );
285 break;
286 }
287 case android.R.id.home: {
288 OCFile currentDir = getCurrentFolder();
289 if(currentDir != null && currentDir.getParentId() != 0) {
290 onBackPressed();
291 }
292 break;
293 }
294 default:
295 retval = super.onOptionsItemSelected(item);
296 }
297 return retval;
298 }
299
300 private OCFile getCurrentFolder() {
301 OCFile file = getFile();
302 if (file != null) {
303 if (file.isFolder()) {
304 return file;
305 } else if (getStorageManager() != null) {
306 String parentPath = file.getRemotePath().substring(0, file.getRemotePath().lastIndexOf(file.getFileName()));
307 return getStorageManager().getFileByPath(parentPath);
308 }
309 }
310 return null;
311 }
312
313 protected void refreshListOfFilesFragment() {
314 OCFileListFragment fileListFragment = getListOfFilesFragment();
315 if (fileListFragment != null) {
316 fileListFragment.listDirectory();
317 }
318 }
319
320 public void browseToRoot() {
321 OCFileListFragment listOfFiles = getListOfFilesFragment();
322 if (listOfFiles != null) { // should never be null, indeed
323 OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH);
324 listOfFiles.listDirectory(root);
325 setFile(listOfFiles.getCurrentFile());
326 updateNavigationElementsInActionBar();
327 startSyncFolderOperation(root, false);
328 }
329 }
330
331 @Override
332 public void onBackPressed() {
333 OCFileListFragment listOfFiles = getListOfFilesFragment();
334 if (listOfFiles != null) { // should never be null, indeed
335 int levelsUp = listOfFiles.onBrowseUp();
336 if (levelsUp == 0) {
337 finish();
338 return;
339 }
340 setFile(listOfFiles.getCurrentFile());
341 updateNavigationElementsInActionBar();
342 }
343 }
344
345 private void updateNavigationElementsInActionBar() {
346 ActionBar actionBar = getSupportActionBar();
347 OCFile currentDir = getCurrentFolder();
348 boolean atRoot = (currentDir == null || currentDir.getParentId() == 0);
349 actionBar.setDisplayHomeAsUpEnabled(!atRoot);
350 actionBar.setHomeButtonEnabled(!atRoot);
351 actionBar.setTitle(
352 atRoot
353 ? getString(R.string.default_display_name_for_root_folder)
354 : currentDir.getFileName()
355 );
356 }
357
358 /**
359 * Set per-view controllers
360 */
361 private void initControls(){
362 mCancelBtn = (Button) findViewById(R.id.move_files_btn_cancel);
363 mCancelBtn.setOnClickListener(this);
364 mChooseBtn = (Button) findViewById(R.id.move_files_btn_choose);
365 mChooseBtn.setOnClickListener(this);
366 }
367
368 @Override
369 public void onClick(View v) {
370 if (v == mCancelBtn) {
371 finish();
372 } else if (v == mChooseBtn) {
373 Intent i = getIntent();
374 OCFile targetFile = (OCFile) i.getParcelableExtra(MoveActivity.EXTRA_TARGET_FILE);
375
376 Intent data = new Intent();
377 data.putExtra(EXTRA_CURRENT_FOLDER, getCurrentFolder());
378 data.putExtra(EXTRA_TARGET_FILE, targetFile);
379 setResult(RESULT_OK_AND_MOVE, data);
380 finish();
381 }
382 }
383
384
385 @Override
386 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
387 super.onRemoteOperationFinish(operation, result);
388
389 if (operation instanceof CreateFolderOperation) {
390 onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
391
392 }
393 }
394
395
396 /**
397 * Updates the view associated to the activity after the finish of an operation trying
398 * to create a new folder.
399 *
400 * @param operation Creation operation performed.
401 * @param result Result of the creation.
402 */
403 private void onCreateFolderOperationFinish(
404 CreateFolderOperation operation, RemoteOperationResult result
405 ) {
406
407 if (result.isSuccess()) {
408 dismissLoadingDialog();
409 refreshListOfFilesFragment();
410 } else {
411 dismissLoadingDialog();
412 try {
413 Toast msg = Toast.makeText(MoveActivity.this,
414 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
415 Toast.LENGTH_LONG);
416 msg.show();
417
418 } catch (NotFoundException e) {
419 Log_OC.e(TAG, "Error while trying to show fail message " , e);
420 }
421 }
422 }
423
424
425
426 private class SyncBroadcastReceiver extends BroadcastReceiver {
427
428 /**
429 * {@link BroadcastReceiver} to enable syncing feedback in UI
430 */
431 @Override
432 public void onReceive(Context context, Intent intent) {
433 try {
434 String event = intent.getAction();
435 Log_OC.d(TAG, "Received broadcast " + event);
436 String accountName = intent.getStringExtra(FileSyncAdapter.EXTRA_ACCOUNT_NAME);
437 String synchFolderRemotePath = intent.getStringExtra(FileSyncAdapter.EXTRA_FOLDER_PATH);
438 RemoteOperationResult synchResult = (RemoteOperationResult)intent.getSerializableExtra(FileSyncAdapter.EXTRA_RESULT);
439 boolean sameAccount = (getAccount() != null && accountName.equals(getAccount().name) && getStorageManager() != null);
440
441 if (sameAccount) {
442
443 if (FileSyncAdapter.EVENT_FULL_SYNC_START.equals(event)) {
444 mSyncInProgress = true;
445
446 } else {
447 OCFile currentFile = (getFile() == null) ? null : getStorageManager().getFileByPath(getFile().getRemotePath());
448 OCFile currentDir = (getCurrentFolder() == null) ? null : getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
449
450 if (currentDir == null) {
451 // current folder was removed from the server
452 Toast.makeText( MoveActivity.this,
453 String.format(getString(R.string.sync_current_folder_was_removed), getCurrentFolder().getFileName()),
454 Toast.LENGTH_LONG)
455 .show();
456 browseToRoot();
457
458 } else {
459 if (currentFile == null && !getFile().isFolder()) {
460 // currently selected file was removed in the server, and now we know it
461 currentFile = currentDir;
462 }
463
464 if (synchFolderRemotePath != null && currentDir.getRemotePath().equals(synchFolderRemotePath)) {
465 OCFileListFragment fileListFragment = getListOfFilesFragment();
466 if (fileListFragment != null) {
467 fileListFragment.listDirectory(currentDir);
468 }
469 }
470 setFile(currentFile);
471 }
472
473 mSyncInProgress = (!FileSyncAdapter.EVENT_FULL_SYNC_END.equals(event) && !SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_SHARES_SYNCED.equals(event));
474
475 if (SynchronizeFolderOperation.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED.
476 equals(event) &&
477 /// TODO refactor and make common
478 synchResult != null && !synchResult.isSuccess() &&
479 (synchResult.getCode() == ResultCode.UNAUTHORIZED ||
480 synchResult.isIdPRedirection() ||
481 (synchResult.isException() && synchResult.getException()
482 instanceof AuthenticatorException))) {
483
484 OwnCloudClient client = null;
485 try {
486 OwnCloudAccount ocAccount =
487 new OwnCloudAccount(getAccount(), context);
488 client = (OwnCloudClientManagerFactory.getDefaultSingleton().
489 removeClientFor(ocAccount));
490 // TODO get rid of these exceptions
491 } catch (AccountNotFoundException e) {
492 e.printStackTrace();
493 } catch (AuthenticatorException e) {
494 e.printStackTrace();
495 } catch (OperationCanceledException e) {
496 e.printStackTrace();
497 } catch (IOException e) {
498 e.printStackTrace();
499 }
500
501 if (client != null) {
502 OwnCloudCredentials cred = client.getCredentials();
503 if (cred != null) {
504 AccountManager am = AccountManager.get(context);
505 if (cred.authTokenExpires()) {
506 am.invalidateAuthToken(
507 getAccount().type,
508 cred.getAuthToken()
509 );
510 } else {
511 am.clearPassword(getAccount());
512 }
513 }
514 }
515
516 requestCredentialsUpdate();
517
518 }
519 }
520 removeStickyBroadcast(intent);
521 Log_OC.d(TAG, "Setting progress visibility to " + mSyncInProgress);
522 setSupportProgressBarIndeterminateVisibility(mSyncInProgress /*|| mRefreshSharesInProgress*/);
523
524 setBackgroundText();
525
526 }
527
528 } catch (RuntimeException e) {
529 // avoid app crashes after changing the serial id of RemoteOperationResult
530 // in owncloud library with broadcast notifications pending to process
531 removeStickyBroadcast(intent);
532 }
533 }
534 }
535
536
537
538 /**
539 * Shows the information of the {@link OCFile} received as a
540 * parameter in the second fragment.
541 *
542 * @param file {@link OCFile} whose details will be shown
543 */
544 @Override
545 public void showDetails(OCFile file) {
546
547 }
548
549 /**
550 * {@inheritDoc}
551 */
552 @Override
553 public void onTransferStateChanged(OCFile file, boolean downloading, boolean uploading) {
554
555 }
556
557
558 @Override
559 public void onRefresh() {
560 OCFileListFragment listOfFiles = getListOfFilesFragment();
561 if (listOfFiles != null) {
562 OCFile folder = listOfFiles.getCurrentFile();
563 if (folder != null) {
564 startSyncFolderOperation(folder, true);
565 }
566 }
567 }
568
569 }