1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.activity
;
20 import java
.io
.IOException
;
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
;
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
;
65 public class FolderPickerActivity
extends FileActivity
implements FileFragment
.ContainerActivity
,
66 OnClickListener
, OnEnforceableRefreshListener
{
68 public static final String EXTRA_CURRENT_FOLDER
= UploadFilesActivity
.class.getCanonicalName()
69 + ".EXTRA_CURRENT_FOLDER";
70 public static final String EXTRA_TARGET_FILE
= UploadFilesActivity
.class.getCanonicalName()
71 + "EXTRA_TARGET_FILE";
73 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
75 private static final String TAG
= FolderPickerActivity
.class.getSimpleName();
77 private static final String TAG_LIST_OF_FOLDERS
= "LIST_OF_FOLDERS";
79 private boolean mSyncInProgress
= false
;
81 protected Button mCancelBtn
;
82 protected Button mChooseBtn
;
86 protected void onCreate(Bundle savedInstanceState
) {
87 Log_OC
.d(TAG
, "onCreate() start");
88 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
90 super.onCreate(savedInstanceState
);
92 setContentView(R
.layout
.files_folder_picker
);
94 if (savedInstanceState
== null
) {
98 // sets callback listeners for UI elements
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
108 // sets message for empty list of folders
111 Log_OC
.d(TAG
, "onCreate() end");
116 protected void onStart() {
118 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
122 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
125 protected void onAccountSet(boolean stateWasRecovered
) {
126 super.onAccountSet(stateWasRecovered
);
127 if (getAccount() != null
) {
131 OCFile folder
= getFile();
132 if (folder
== null
|| !folder
.isFolder()) {
133 // fall back to root folder
134 setFile(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
138 if (!stateWasRecovered
) {
139 OCFileListFragment listOfFolders
= getListOfFilesFragment();
140 listOfFolders
.listDirectory(folder
);
142 startSyncFolderOperation(folder
, false
);
145 updateNavigationElementsInActionBar();
149 private void createFragments() {
150 OCFileListFragment listOfFiles
= new OCFileListFragment();
151 Bundle args
= new Bundle();
152 args
.putBoolean(OCFileListFragment
.ARG_JUST_FOLDERS
, true
);
153 args
.putBoolean(OCFileListFragment
.ARG_ALLOW_CONTEXTUAL_ACTIONS
, false
);
154 listOfFiles
.setArguments(args
);
155 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
156 transaction
.add(R
.id
.fragment_container
, listOfFiles
, TAG_LIST_OF_FOLDERS
);
157 transaction
.commit();
161 * Show a text message on screen view for notifying user if content is
162 * loading or folder is empty
164 private void setBackgroundText() {
165 OCFileListFragment listFragment
= getListOfFilesFragment();
166 if (listFragment
!= null
) {
167 int message
= R
.string
.file_list_loading
;
168 if (!mSyncInProgress
) {
169 // In case folder list is empty
170 message
= R
.string
.file_list_empty_moving
;
172 listFragment
.setMessageForEmptyList(getString(message
));
174 Log
.e(TAG
, "OCFileListFragment is null");
178 protected OCFileListFragment
getListOfFilesFragment() {
179 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FolderPickerActivity
.TAG_LIST_OF_FOLDERS
);
180 if (listOfFiles
!= null
) {
181 return (OCFileListFragment
)listOfFiles
;
183 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
191 * Updates action bar and second fragment, if in dual pane mode.
194 public void onBrowsedDownTo(OCFile directory
) {
196 updateNavigationElementsInActionBar();
198 startSyncFolderOperation(directory
, false
);
203 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
204 long currentSyncTime
= System
.currentTimeMillis();
206 mSyncInProgress
= true
;
208 // perform folder synchronization
209 RemoteOperation synchFolderOp
= new SynchronizeFolderOperation( folder
,
212 getFileOperationsHelper().isSharedSupported(),
216 getApplicationContext()
218 synchFolderOp
.execute(getAccount(), this, null
, null
);
220 setSupportProgressBarIndeterminateVisibility(true
);
226 protected void onResume() {
228 Log_OC
.e(TAG
, "onResume() start");
230 // refresh list of files
231 refreshListOfFilesFragment();
233 // Listen for sync messages
234 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
235 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
236 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
237 syncIntentFilter
.addAction(SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
238 syncIntentFilter
.addAction(SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
239 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
240 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
242 Log_OC
.d(TAG
, "onResume() end");
246 protected void onPause() {
247 Log_OC
.e(TAG
, "onPause() start");
248 if (mSyncBroadcastReceiver
!= null
) {
249 unregisterReceiver(mSyncBroadcastReceiver
);
250 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
251 mSyncBroadcastReceiver
= null
;
254 Log_OC
.d(TAG
, "onPause() end");
259 public boolean onCreateOptionsMenu(Menu menu
) {
260 MenuInflater inflater
= getSherlock().getMenuInflater();
261 inflater
.inflate(R
.menu
.main_menu
, menu
);
262 menu
.findItem(R
.id
.action_upload
).setVisible(false
);
263 menu
.findItem(R
.id
.action_settings
).setVisible(false
);
264 menu
.findItem(R
.id
.action_sync_account
).setVisible(false
);
265 menu
.findItem(R
.id
.action_logger
).setVisible(false
);
266 menu
.findItem(R
.id
.action_sort
).setVisible(false
);
271 public boolean onOptionsItemSelected(MenuItem item
) {
272 boolean retval
= true
;
273 switch (item
.getItemId()) {
274 case R
.id
.action_create_dir
: {
275 CreateFolderDialogFragment dialog
=
276 CreateFolderDialogFragment
.newInstance(getCurrentFolder());
278 getSupportFragmentManager(),
279 CreateFolderDialogFragment
.CREATE_FOLDER_FRAGMENT
283 case android
.R
.id
.home
: {
284 OCFile currentDir
= getCurrentFolder();
285 if(currentDir
!= null
&& currentDir
.getParentId() != 0) {
291 retval
= super.onOptionsItemSelected(item
);
296 protected OCFile
getCurrentFolder() {
297 OCFile file
= getFile();
299 if (file
.isFolder()) {
301 } else if (getStorageManager() != null
) {
302 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
303 return getStorageManager().getFileByPath(parentPath
);
309 protected void refreshListOfFilesFragment() {
310 OCFileListFragment fileListFragment
= getListOfFilesFragment();
311 if (fileListFragment
!= null
) {
312 fileListFragment
.listDirectory();
316 public void browseToRoot() {
317 OCFileListFragment listOfFiles
= getListOfFilesFragment();
318 if (listOfFiles
!= null
) { // should never be null, indeed
319 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
320 listOfFiles
.listDirectory(root
);
321 setFile(listOfFiles
.getCurrentFile());
322 updateNavigationElementsInActionBar();
323 startSyncFolderOperation(root
, false
);
328 public void onBackPressed() {
329 OCFileListFragment listOfFiles
= getListOfFilesFragment();
330 if (listOfFiles
!= null
) { // should never be null, indeed
331 int levelsUp
= listOfFiles
.onBrowseUp();
336 setFile(listOfFiles
.getCurrentFile());
337 updateNavigationElementsInActionBar();
341 protected void updateNavigationElementsInActionBar() {
342 ActionBar actionBar
= getSupportActionBar();
343 OCFile currentDir
= getCurrentFolder();
344 boolean atRoot
= (currentDir
== null
|| currentDir
.getParentId() == 0);
345 actionBar
.setDisplayHomeAsUpEnabled(!atRoot
);
346 actionBar
.setHomeButtonEnabled(!atRoot
);
349 ?
getString(R
.string
.default_display_name_for_root_folder
)
350 : currentDir
.getFileName()
355 * Set per-view controllers
357 private void initControls(){
358 mCancelBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_cancel
);
359 mCancelBtn
.setOnClickListener(this);
360 mChooseBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_choose
);
361 mChooseBtn
.setOnClickListener(this);
365 public void onClick(View v
) {
366 if (v
== mCancelBtn
) {
368 } else if (v
== mChooseBtn
) {
369 Intent i
= getIntent();
370 OCFile targetFile
= (OCFile
) i
.getParcelableExtra(FolderPickerActivity
.EXTRA_TARGET_FILE
);
372 Intent data
= new Intent();
373 data
.putExtra(EXTRA_CURRENT_FOLDER
, getCurrentFolder());
374 data
.putExtra(EXTRA_TARGET_FILE
, targetFile
);
375 setResult(RESULT_OK
, data
);
382 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
383 super.onRemoteOperationFinish(operation
, result
);
385 if (operation
instanceof CreateFolderOperation
) {
386 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
393 * Updates the view associated to the activity after the finish of an operation trying
394 * to create a new folder.
396 * @param operation Creation operation performed.
397 * @param result Result of the creation.
399 private void onCreateFolderOperationFinish(
400 CreateFolderOperation operation
, RemoteOperationResult result
403 if (result
.isSuccess()) {
404 dismissLoadingDialog();
405 refreshListOfFilesFragment();
407 dismissLoadingDialog();
409 Toast msg
= Toast
.makeText(FolderPickerActivity
.this,
410 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
414 } catch (NotFoundException e
) {
415 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
422 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
425 * {@link BroadcastReceiver} to enable syncing feedback in UI
428 public void onReceive(Context context
, Intent intent
) {
430 String event
= intent
.getAction();
431 Log_OC
.d(TAG
, "Received broadcast " + event
);
432 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
433 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
434 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.
435 getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
436 boolean sameAccount
= (getAccount() != null
&&
437 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
441 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
442 mSyncInProgress
= true
;
445 OCFile currentFile
= (getFile() == null
) ? null
:
446 getStorageManager().getFileByPath(getFile().getRemotePath());
447 OCFile currentDir
= (getCurrentFolder() == null
) ? null
:
448 getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
450 if (currentDir
== null
) {
451 // current folder was removed from the server
452 Toast
.makeText( FolderPickerActivity
.this,
454 getString(R
.string
.sync_current_folder_was_removed
),
455 getCurrentFolder().getFileName()),
461 if (currentFile
== null
&& !getFile().isFolder()) {
462 // currently selected file was removed in the server, and now we know it
463 currentFile
= currentDir
;
466 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().
467 equals(synchFolderRemotePath
)) {
468 OCFileListFragment fileListFragment
= getListOfFilesFragment();
469 if (fileListFragment
!= null
) {
470 fileListFragment
.listDirectory(currentDir
);
473 setFile(currentFile
);
476 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
477 !SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
479 if (SynchronizeFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
481 /// TODO refactor and make common
482 synchResult
!= null
&& !synchResult
.isSuccess() &&
483 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
484 synchResult
.isIdPRedirection() ||
485 (synchResult
.isException() && synchResult
.getException()
486 instanceof AuthenticatorException
))) {
488 OwnCloudClient client
= null
;
490 OwnCloudAccount ocAccount
=
491 new OwnCloudAccount(getAccount(), context
);
492 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
493 removeClientFor(ocAccount
));
494 // TODO get rid of these exceptions
495 } catch (AccountNotFoundException e
) {
497 } catch (AuthenticatorException e
) {
499 } catch (OperationCanceledException e
) {
501 } catch (IOException e
) {
505 if (client
!= null
) {
506 OwnCloudCredentials cred
= client
.getCredentials();
508 AccountManager am
= AccountManager
.get(context
);
509 if (cred
.authTokenExpires()) {
510 am
.invalidateAuthToken(
515 am
.clearPassword(getAccount());
520 requestCredentialsUpdate();
524 removeStickyBroadcast(intent
);
525 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
526 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
/*|| mRefreshSharesInProgress*/);
532 } catch (RuntimeException e
) {
533 // avoid app crashes after changing the serial id of RemoteOperationResult
534 // in owncloud library with broadcast notifications pending to process
535 removeStickyBroadcast(intent
);
543 * Shows the information of the {@link OCFile} received as a
544 * parameter in the second fragment.
546 * @param file {@link OCFile} whose details will be shown
549 public void showDetails(OCFile file
) {
557 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
562 public void onRefresh() {
567 public void onRefresh(boolean enforced
) {
568 refreshList(enforced
);
571 private void refreshList(boolean ignoreETag
) {
572 OCFileListFragment listOfFiles
= getListOfFilesFragment();
573 if (listOfFiles
!= null
) {
574 OCFile folder
= listOfFiles
.getCurrentFile();
575 if (folder
!= null
) {
576 startSyncFolderOperation(folder
, ignoreETag
);