2 * ownCloud Android client application
4 * Copyright (C) 2015 ownCloud Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.activity
;
22 import java
.io
.IOException
;
24 import android
.accounts
.Account
;
25 import android
.accounts
.AccountManager
;
26 import android
.accounts
.AuthenticatorException
;
27 import android
.accounts
.OperationCanceledException
;
28 import android
.content
.BroadcastReceiver
;
29 import android
.content
.Context
;
30 import android
.content
.Intent
;
31 import android
.content
.IntentFilter
;
32 import android
.content
.res
.Resources
.NotFoundException
;
33 import android
.os
.Bundle
;
34 import android
.os
.Parcelable
;
35 import android
.support
.v4
.app
.Fragment
;
36 import android
.support
.v4
.app
.FragmentTransaction
;
37 import android
.support
.v7
.app
.ActionBar
;
38 import android
.util
.Log
;
39 import android
.view
.Menu
;
40 import android
.view
.MenuInflater
;
41 import android
.view
.MenuItem
;
42 import android
.view
.View
;
43 import android
.view
.View
.OnClickListener
;
44 import android
.view
.Window
;
45 import android
.widget
.Button
;
46 import android
.widget
.Toast
;
48 import com
.owncloud
.android
.R
;
49 import com
.owncloud
.android
.datamodel
.OCFile
;
50 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
51 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
52 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
53 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
54 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
55 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
56 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
57 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
58 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
59 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
60 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
61 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
62 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
63 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
64 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
65 import com
.owncloud
.android
.utils
.DisplayUtils
;
66 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
68 public class FolderPickerActivity
extends FileActivity
implements FileFragment
.ContainerActivity
,
69 OnClickListener
, OnEnforceableRefreshListener
{
71 public static final String EXTRA_FOLDER
= UploadFilesActivity
.class.getCanonicalName()
73 public static final String EXTRA_FILE
= UploadFilesActivity
.class.getCanonicalName()
75 //TODO: Think something better
77 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
79 private static final String TAG
= FolderPickerActivity
.class.getSimpleName();
81 private static final String TAG_LIST_OF_FOLDERS
= "LIST_OF_FOLDERS";
83 private boolean mSyncInProgress
= false
;
85 protected Button mCancelBtn
;
86 protected Button mChooseBtn
;
90 protected void onCreate(Bundle savedInstanceState
) {
91 Log_OC
.d(TAG
, "onCreate() start");
92 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
94 super.onCreate(savedInstanceState
);
96 setContentView(R
.layout
.files_folder_picker
);
98 if (savedInstanceState
== null
) {
102 // sets callback listeners for UI elements
106 ActionBar actionBar
= getSupportActionBar();
107 actionBar
.setDisplayShowTitleEnabled(true
);
108 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
109 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
);
110 // always AFTER setContentView(...) ; to work around bug in its implementation
112 // sets message for empty list of folders
115 Log_OC
.d(TAG
, "onCreate() end");
120 protected void onStart() {
122 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
126 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
129 protected void onAccountSet(boolean stateWasRecovered
) {
130 super.onAccountSet(stateWasRecovered
);
131 if (getAccount() != null
) {
135 OCFile folder
= getFile();
136 if (folder
== null
|| !folder
.isFolder()) {
137 // fall back to root folder
138 setFile(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
142 if (!stateWasRecovered
) {
143 OCFileListFragment listOfFolders
= getListOfFilesFragment();
144 listOfFolders
.listDirectory(folder
);
146 startSyncFolderOperation(folder
, false
);
149 updateNavigationElementsInActionBar();
153 private void createFragments() {
154 OCFileListFragment listOfFiles
= new OCFileListFragment();
155 Bundle args
= new Bundle();
156 args
.putBoolean(OCFileListFragment
.ARG_JUST_FOLDERS
, true
);
157 args
.putBoolean(OCFileListFragment
.ARG_ALLOW_CONTEXTUAL_ACTIONS
, false
);
158 listOfFiles
.setArguments(args
);
159 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
160 transaction
.add(R
.id
.fragment_container
, listOfFiles
, TAG_LIST_OF_FOLDERS
);
161 transaction
.commit();
165 * Show a text message on screen view for notifying user if content is
166 * loading or folder is empty
168 private void setBackgroundText() {
169 OCFileListFragment listFragment
= getListOfFilesFragment();
170 if (listFragment
!= null
) {
171 int message
= R
.string
.file_list_loading
;
172 if (!mSyncInProgress
) {
173 // In case folder list is empty
174 message
= R
.string
.file_list_empty_moving
;
176 listFragment
.setMessageForEmptyList(getString(message
));
178 Log
.e(TAG
, "OCFileListFragment is null");
182 protected OCFileListFragment
getListOfFilesFragment() {
183 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FolderPickerActivity
.TAG_LIST_OF_FOLDERS
);
184 if (listOfFiles
!= null
) {
185 return (OCFileListFragment
)listOfFiles
;
187 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
195 * Updates action bar and second fragment, if in dual pane mode.
198 public void onBrowsedDownTo(OCFile directory
) {
200 updateNavigationElementsInActionBar();
202 startSyncFolderOperation(directory
, false
);
207 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
208 long currentSyncTime
= System
.currentTimeMillis();
210 mSyncInProgress
= true
;
212 // perform folder synchronization
213 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
216 getFileOperationsHelper().isSharedSupported(),
220 getApplicationContext()
222 synchFolderOp
.execute(getAccount(), this, null
, null
);
224 setSupportProgressBarIndeterminateVisibility(true
);
230 protected void onResume() {
232 Log_OC
.e(TAG
, "onResume() start");
234 // refresh list of files
235 refreshListOfFilesFragment();
237 // Listen for sync messages
238 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
239 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
240 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
241 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
242 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
243 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
244 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
246 Log_OC
.d(TAG
, "onResume() end");
250 protected void onPause() {
251 Log_OC
.e(TAG
, "onPause() start");
252 if (mSyncBroadcastReceiver
!= null
) {
253 unregisterReceiver(mSyncBroadcastReceiver
);
254 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
255 mSyncBroadcastReceiver
= null
;
258 Log_OC
.d(TAG
, "onPause() end");
263 public boolean onCreateOptionsMenu(Menu menu
) {
264 MenuInflater inflater
= getMenuInflater();
265 inflater
.inflate(R
.menu
.main_menu
, menu
);
266 menu
.findItem(R
.id
.action_upload
).setVisible(false
);
267 menu
.findItem(R
.id
.action_settings
).setVisible(false
);
268 menu
.findItem(R
.id
.action_sync_account
).setVisible(false
);
269 menu
.findItem(R
.id
.action_logger
).setVisible(false
);
270 menu
.findItem(R
.id
.action_sort
).setVisible(false
);
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());
282 getSupportFragmentManager(),
283 CreateFolderDialogFragment
.CREATE_FOLDER_FRAGMENT
287 case android
.R
.id
.home
: {
288 OCFile currentDir
= getCurrentFolder();
289 if(currentDir
!= null
&& currentDir
.getParentId() != 0) {
295 retval
= super.onOptionsItemSelected(item
);
300 protected OCFile
getCurrentFolder() {
301 OCFile file
= getFile();
303 if (file
.isFolder()) {
305 } else if (getStorageManager() != null
) {
306 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
307 return getStorageManager().getFileByPath(parentPath
);
313 protected void refreshListOfFilesFragment() {
314 OCFileListFragment fileListFragment
= getListOfFilesFragment();
315 if (fileListFragment
!= null
) {
316 fileListFragment
.listDirectory();
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
);
332 public void onBackPressed() {
333 OCFileListFragment listOfFiles
= getListOfFilesFragment();
334 if (listOfFiles
!= null
) { // should never be null, indeed
335 int levelsUp
= listOfFiles
.onBrowseUp();
340 setFile(listOfFiles
.getCurrentFile());
341 updateNavigationElementsInActionBar();
345 protected 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
);
353 ?
getString(R
.string
.default_display_name_for_root_folder
)
354 : currentDir
.getFileName()
359 * Set per-view controllers
361 private void initControls(){
362 mCancelBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_cancel
);
363 mCancelBtn
.setOnClickListener(this);
364 mChooseBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_choose
);
365 mChooseBtn
.setOnClickListener(this);
369 public void onClick(View v
) {
370 if (v
== mCancelBtn
) {
372 } else if (v
== mChooseBtn
) {
373 Intent i
= getIntent();
374 Parcelable targetFile
= i
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
376 Intent data
= new Intent();
377 data
.putExtra(EXTRA_FOLDER
, getCurrentFolder());
378 if (targetFile
!= null
) {
379 data
.putExtra(EXTRA_FILE
, targetFile
);
381 setResult(RESULT_OK
, data
);
388 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
389 super.onRemoteOperationFinish(operation
, result
);
391 if (operation
instanceof CreateFolderOperation
) {
392 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
399 * Updates the view associated to the activity after the finish of an operation trying
400 * to create a new folder.
402 * @param operation Creation operation performed.
403 * @param result Result of the creation.
405 private void onCreateFolderOperationFinish(
406 CreateFolderOperation operation
, RemoteOperationResult result
409 if (result
.isSuccess()) {
410 dismissLoadingDialog();
411 refreshListOfFilesFragment();
413 dismissLoadingDialog();
415 Toast msg
= Toast
.makeText(FolderPickerActivity
.this,
416 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
420 } catch (NotFoundException e
) {
421 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
428 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
431 * {@link BroadcastReceiver} to enable syncing feedback in UI
434 public void onReceive(Context context
, Intent intent
) {
436 String event
= intent
.getAction();
437 Log_OC
.d(TAG
, "Received broadcast " + event
);
438 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
439 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
440 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.
441 getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
442 boolean sameAccount
= (getAccount() != null
&&
443 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
447 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
448 mSyncInProgress
= true
;
451 OCFile currentFile
= (getFile() == null
) ? null
:
452 getStorageManager().getFileByPath(getFile().getRemotePath());
453 OCFile currentDir
= (getCurrentFolder() == null
) ? null
:
454 getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
456 if (currentDir
== null
) {
457 // current folder was removed from the server
458 Toast
.makeText( FolderPickerActivity
.this,
460 getString(R
.string
.sync_current_folder_was_removed
),
461 getCurrentFolder().getFileName()),
467 if (currentFile
== null
&& !getFile().isFolder()) {
468 // currently selected file was removed in the server, and now we know it
469 currentFile
= currentDir
;
472 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().
473 equals(synchFolderRemotePath
)) {
474 OCFileListFragment fileListFragment
= getListOfFilesFragment();
475 if (fileListFragment
!= null
) {
476 fileListFragment
.listDirectory(currentDir
);
479 setFile(currentFile
);
482 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
483 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
485 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
487 /// TODO refactor and make common
488 synchResult
!= null
&& !synchResult
.isSuccess() &&
489 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
490 synchResult
.isIdPRedirection() ||
491 (synchResult
.isException() && synchResult
.getException()
492 instanceof AuthenticatorException
))) {
495 OwnCloudClient client
;
496 OwnCloudAccount ocAccount
=
497 new OwnCloudAccount(getAccount(), context
);
498 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
499 removeClientFor(ocAccount
));
501 if (client
!= null
) {
502 OwnCloudCredentials cred
= client
.getCredentials();
504 AccountManager am
= AccountManager
.get(context
);
505 if (cred
.authTokenExpires()) {
506 am
.invalidateAuthToken(
511 am
.clearPassword(getAccount());
515 requestCredentialsUpdate();
517 } catch (AccountNotFoundException e
) {
518 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
523 removeStickyBroadcast(intent
);
524 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
525 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
/*|| mRefreshSharesInProgress*/);
531 } catch (RuntimeException e
) {
532 // avoid app crashes after changing the serial id of RemoteOperationResult
533 // in owncloud library with broadcast notifications pending to process
534 removeStickyBroadcast(intent
);
542 * Shows the information of the {@link OCFile} received as a
543 * parameter in the second fragment.
545 * @param file {@link OCFile} whose details will be shown
548 public void showDetails(OCFile file
) {
556 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
561 public void onRefresh() {
566 public void onRefresh(boolean enforced
) {
567 refreshList(enforced
);
570 private void refreshList(boolean ignoreETag
) {
571 OCFileListFragment listOfFiles
= getListOfFilesFragment();
572 if (listOfFiles
!= null
) {
573 OCFile folder
= listOfFiles
.getCurrentFile();
574 if (folder
!= null
) {
575 startSyncFolderOperation(folder
, ignoreETag
);