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
.util
.Log
;
38 import android
.view
.View
;
39 import android
.view
.View
.OnClickListener
;
40 import android
.widget
.Button
;
41 import android
.widget
.Toast
;
43 import com
.actionbarsherlock
.app
.ActionBar
;
44 import com
.actionbarsherlock
.view
.Menu
;
45 import com
.actionbarsherlock
.view
.MenuInflater
;
46 import com
.actionbarsherlock
.view
.MenuItem
;
47 import com
.actionbarsherlock
.view
.Window
;
48 import com
.owncloud
.android
.MainApp
;
49 import com
.owncloud
.android
.R
;
50 import com
.owncloud
.android
.datamodel
.OCFile
;
51 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
52 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
53 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
54 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
55 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
56 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
57 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
58 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
59 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
60 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
61 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
62 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
63 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
64 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
65 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
66 import com
.owncloud
.android
.utils
.DisplayUtils
;
67 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
69 public class FolderPickerActivity
extends FileActivity
implements FileFragment
.ContainerActivity
,
70 OnClickListener
, OnEnforceableRefreshListener
{
72 public static final String EXTRA_FOLDER
= UploadFilesActivity
.class.getCanonicalName()
74 public static final String EXTRA_FILE
= UploadFilesActivity
.class.getCanonicalName()
76 //TODO: Think something better
78 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
80 private static final String TAG
= FolderPickerActivity
.class.getSimpleName();
82 private static final String TAG_LIST_OF_FOLDERS
= "LIST_OF_FOLDERS";
84 private boolean mSyncInProgress
= false
;
86 protected Button mCancelBtn
;
87 protected Button mChooseBtn
;
91 protected void onCreate(Bundle savedInstanceState
) {
92 Log_OC
.d(TAG
, "onCreate() start");
93 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
95 super.onCreate(savedInstanceState
);
97 setContentView(R
.layout
.files_folder_picker
);
99 if (savedInstanceState
== null
) {
103 // sets callback listeners for UI elements
107 ActionBar actionBar
= getSupportActionBar();
108 actionBar
.setDisplayShowTitleEnabled(true
);
109 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
110 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
);
111 // always AFTER setContentView(...) ; to work around bug in its implementation
113 // sets message for empty list of folders
116 Log_OC
.d(TAG
, "onCreate() end");
121 protected void onStart() {
123 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
127 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
130 protected void onAccountSet(boolean stateWasRecovered
) {
131 super.onAccountSet(stateWasRecovered
);
132 if (getAccount() != null
) {
136 OCFile folder
= getFile();
137 if (folder
== null
|| !folder
.isFolder()) {
138 // fall back to root folder
139 setFile(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
143 if (!stateWasRecovered
) {
144 OCFileListFragment listOfFolders
= getListOfFilesFragment();
145 listOfFolders
.listDirectory(folder
/*, false*/);
147 startSyncFolderOperation(folder
, false
);
150 updateNavigationElementsInActionBar();
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();
166 * Show a text message on screen view for notifying user if content is
167 * loading or folder is empty
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
;
177 listFragment
.setMessageForEmptyList(getString(message
));
179 Log
.e(TAG
, "OCFileListFragment is null");
183 protected OCFileListFragment
getListOfFilesFragment() {
184 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FolderPickerActivity
.TAG_LIST_OF_FOLDERS
);
185 if (listOfFiles
!= null
) {
186 return (OCFileListFragment
)listOfFiles
;
188 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
196 * Updates action bar and second fragment, if in dual pane mode.
199 public void onBrowsedDownTo(OCFile directory
) {
201 updateNavigationElementsInActionBar();
203 startSyncFolderOperation(directory
, false
);
208 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
209 long currentSyncTime
= System
.currentTimeMillis();
211 mSyncInProgress
= true
;
213 // perform folder synchronization
214 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
217 getFileOperationsHelper().isSharedSupported(),
221 getApplicationContext()
223 synchFolderOp
.execute(getAccount(), this, null
, null
);
225 setSupportProgressBarIndeterminateVisibility(true
);
231 protected void onResume() {
233 Log_OC
.e(TAG
, "onResume() start");
235 // refresh list of files
236 refreshListOfFilesFragment();
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(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
243 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
244 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
245 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
247 Log_OC
.d(TAG
, "onResume() end");
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
;
259 Log_OC
.d(TAG
, "onPause() end");
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_sort
).setVisible(false
);
273 public boolean onOptionsItemSelected(MenuItem item
) {
274 boolean retval
= true
;
275 switch (item
.getItemId()) {
276 case R
.id
.action_create_dir
: {
277 CreateFolderDialogFragment dialog
=
278 CreateFolderDialogFragment
.newInstance(getCurrentFolder());
280 getSupportFragmentManager(),
281 CreateFolderDialogFragment
.CREATE_FOLDER_FRAGMENT
285 case android
.R
.id
.home
: {
286 OCFile currentDir
= getCurrentFolder();
287 if(currentDir
!= null
&& currentDir
.getParentId() != 0) {
293 retval
= super.onOptionsItemSelected(item
);
298 protected OCFile
getCurrentFolder() {
299 OCFile file
= getFile();
301 if (file
.isFolder()) {
303 } else if (getStorageManager() != null
) {
304 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
305 return getStorageManager().getFileByPath(parentPath
);
311 protected void refreshListOfFilesFragment() {
312 OCFileListFragment fileListFragment
= getListOfFilesFragment();
313 if (fileListFragment
!= null
) {
314 fileListFragment
.listDirectory();
315 // TODO Enable when "On Device" is recovered ?
316 // fileListFragment.listDirectory(false);
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 // TODO Enable when "On Device" is recovered ?
326 // listOfFiles.listDirectory(root, false);
327 setFile(listOfFiles
.getCurrentFile());
328 updateNavigationElementsInActionBar();
329 startSyncFolderOperation(root
, false
);
334 public void onBackPressed() {
335 OCFileListFragment listOfFiles
= getListOfFilesFragment();
336 if (listOfFiles
!= null
) { // should never be null, indeed
337 int levelsUp
= listOfFiles
.onBrowseUp();
342 setFile(listOfFiles
.getCurrentFile());
343 updateNavigationElementsInActionBar();
347 protected void updateNavigationElementsInActionBar() {
348 ActionBar actionBar
= getSupportActionBar();
349 OCFile currentDir
= getCurrentFolder();
350 boolean atRoot
= (currentDir
== null
|| currentDir
.getParentId() == 0);
351 actionBar
.setDisplayHomeAsUpEnabled(!atRoot
);
352 actionBar
.setHomeButtonEnabled(!atRoot
);
355 ?
getString(R
.string
.default_display_name_for_root_folder
)
356 : currentDir
.getFileName()
361 * Set per-view controllers
363 private void initControls(){
364 mCancelBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_cancel
);
365 mCancelBtn
.setOnClickListener(this);
366 mChooseBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_choose
);
367 mChooseBtn
.setOnClickListener(this);
371 public void onClick(View v
) {
372 if (v
== mCancelBtn
) {
374 } else if (v
== mChooseBtn
) {
375 Intent i
= getIntent();
376 Parcelable targetFile
= i
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
378 Intent data
= new Intent();
379 data
.putExtra(EXTRA_FOLDER
, getCurrentFolder());
380 if (targetFile
!= null
) {
381 data
.putExtra(EXTRA_FILE
, targetFile
);
383 setResult(RESULT_OK
, data
);
390 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
391 super.onRemoteOperationFinish(operation
, result
);
393 if (operation
instanceof CreateFolderOperation
) {
394 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
401 * Updates the view associated to the activity after the finish of an operation trying
402 * to create a new folder.
404 * @param operation Creation operation performed.
405 * @param result Result of the creation.
407 private void onCreateFolderOperationFinish(
408 CreateFolderOperation operation
, RemoteOperationResult result
411 if (result
.isSuccess()) {
412 dismissLoadingDialog();
413 refreshListOfFilesFragment();
415 dismissLoadingDialog();
417 Toast msg
= Toast
.makeText(FolderPickerActivity
.this,
418 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
422 } catch (NotFoundException e
) {
423 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
430 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
433 * {@link BroadcastReceiver} to enable syncing feedback in UI
436 public void onReceive(Context context
, Intent intent
) {
438 String event
= intent
.getAction();
439 Log_OC
.d(TAG
, "Received broadcast " + event
);
440 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
441 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
442 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.
443 getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
444 boolean sameAccount
= (getAccount() != null
&&
445 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
449 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
450 mSyncInProgress
= true
;
453 OCFile currentFile
= (getFile() == null
) ? null
:
454 getStorageManager().getFileByPath(getFile().getRemotePath());
455 OCFile currentDir
= (getCurrentFolder() == null
) ? null
:
456 getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
458 if (currentDir
== null
) {
459 // current folder was removed from the server
460 Toast
.makeText( FolderPickerActivity
.this,
462 getString(R
.string
.sync_current_folder_was_removed
),
463 getCurrentFolder().getFileName()),
469 if (currentFile
== null
&& !getFile().isFolder()) {
470 // currently selected file was removed in the server, and now we know it
471 currentFile
= currentDir
;
474 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().
475 equals(synchFolderRemotePath
)) {
476 OCFileListFragment fileListFragment
= getListOfFilesFragment();
477 if (fileListFragment
!= null
) {
478 fileListFragment
.listDirectory(currentDir
);
479 // TODO Enable when "On Device" is recovered ?
480 // fileListFragment.listDirectory(currentDir, false);
483 setFile(currentFile
);
486 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
487 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
489 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
491 /// TODO refactor and make common
492 synchResult
!= null
&& !synchResult
.isSuccess() &&
493 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
494 synchResult
.isIdPRedirection() ||
495 (synchResult
.isException() && synchResult
.getException()
496 instanceof AuthenticatorException
))) {
499 OwnCloudClient client
;
500 OwnCloudAccount ocAccount
=
501 new OwnCloudAccount(getAccount(), context
);
502 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
503 removeClientFor(ocAccount
));
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());
519 requestCredentialsUpdate();
521 } catch (AccountNotFoundException e
) {
522 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
527 removeStickyBroadcast(intent
);
528 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
529 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
/*|| mRefreshSharesInProgress*/);
535 } catch (RuntimeException e
) {
536 // avoid app crashes after changing the serial id of RemoteOperationResult
537 // in owncloud library with broadcast notifications pending to process
538 removeStickyBroadcast(intent
);
546 * Shows the information of the {@link OCFile} received as a
547 * parameter in the second fragment.
549 * @param file {@link OCFile} whose details will be shown
552 public void showDetails(OCFile file
) {
560 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
565 public void onRefresh() {
570 public void onRefresh(boolean enforced
) {
571 refreshList(enforced
);
574 private void refreshList(boolean ignoreETag
) {
575 OCFileListFragment listOfFiles
= getListOfFilesFragment();
576 if (listOfFiles
!= null
) {
577 OCFile folder
= listOfFiles
.getCurrentFile();
578 if (folder
!= null
) {
579 startSyncFolderOperation(folder
, ignoreETag
);