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