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 android
.accounts
.Account
;
23 import android
.accounts
.AccountManager
;
24 import android
.accounts
.AuthenticatorException
;
25 import android
.content
.BroadcastReceiver
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.content
.IntentFilter
;
29 import android
.content
.res
.Resources
.NotFoundException
;
30 import android
.os
.Bundle
;
31 import android
.os
.Parcelable
;
32 import android
.support
.v4
.app
.Fragment
;
33 import android
.support
.v4
.app
.FragmentTransaction
;
34 import android
.support
.v7
.app
.ActionBar
;
35 import android
.util
.Log
;
36 import android
.view
.Menu
;
37 import android
.view
.MenuInflater
;
38 import android
.view
.MenuItem
;
39 import android
.view
.View
;
40 import android
.view
.View
.OnClickListener
;
41 import android
.view
.Window
;
42 import android
.widget
.Button
;
43 import android
.widget
.ProgressBar
;
44 import android
.widget
.Toast
;
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
.lib
.common
.utils
.Log_OC
;
57 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
58 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
59 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
60 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
61 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
62 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
63 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
65 public class FolderPickerActivity
extends FileActivity
implements FileFragment
.ContainerActivity
,
66 OnClickListener
, OnEnforceableRefreshListener
{
68 public static final String EXTRA_FOLDER
= UploadFilesActivity
.class.getCanonicalName()
70 public static final String EXTRA_FILE
= UploadFilesActivity
.class.getCanonicalName()
72 //TODO: Think something better
74 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
76 private static final String TAG
= FolderPickerActivity
.class.getSimpleName();
78 private static final String TAG_LIST_OF_FOLDERS
= "LIST_OF_FOLDERS";
80 private boolean mSyncInProgress
= false
;
82 protected Button mCancelBtn
;
83 protected Button mChooseBtn
;
84 private ProgressBar mProgressBar
;
88 protected void onCreate(Bundle savedInstanceState
) {
89 Log_OC
.d(TAG
, "onCreate() start");
91 super.onCreate(savedInstanceState
);
93 setContentView(R
.layout
.files_folder_picker
);
95 if (savedInstanceState
== null
) {
99 // sets callback listeners for UI elements
103 ActionBar actionBar
= getSupportActionBar();
104 actionBar
.setDisplayShowTitleEnabled(true
);
105 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
107 mProgressBar
= (ProgressBar
) findViewById(R
.id
.progressBar
);
108 mProgressBar
.setIndeterminateDrawable(
109 getResources().getDrawable(
110 R
.drawable
.actionbar_progress_indeterminate_horizontal
));
111 mProgressBar
.setIndeterminate(mSyncInProgress
);
112 // always AFTER setContentView(...) ; to work around bug in its implementation
114 // sets message for empty list of folders
117 Log_OC
.d(TAG
, "onCreate() end");
121 protected void onStart() {
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
/*, false*/);
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 mProgressBar
.setIndeterminate(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_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();
313 // TODO Enable when "On Device" is recovered ?
314 // fileListFragment.listDirectory(false);
318 public void browseToRoot() {
319 OCFileListFragment listOfFiles
= getListOfFilesFragment();
320 if (listOfFiles
!= null
) { // should never be null, indeed
321 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
322 listOfFiles
.listDirectory(root
);
323 // TODO Enable when "On Device" is recovered ?
324 // listOfFiles.listDirectory(root, false);
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
);
477 // TODO Enable when "On Device" is recovered ?
478 // fileListFragment.listDirectory(currentDir, false);
481 setFile(currentFile
);
484 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
485 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
487 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
489 /// TODO refactor and make common
490 synchResult
!= null
&& !synchResult
.isSuccess() &&
491 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
492 synchResult
.isIdPRedirection() ||
493 (synchResult
.isException() && synchResult
.getException()
494 instanceof AuthenticatorException
))) {
497 OwnCloudClient client
;
498 OwnCloudAccount ocAccount
=
499 new OwnCloudAccount(getAccount(), context
);
500 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
501 removeClientFor(ocAccount
));
503 if (client
!= null
) {
504 OwnCloudCredentials cred
= client
.getCredentials();
506 AccountManager am
= AccountManager
.get(context
);
507 if (cred
.authTokenExpires()) {
508 am
.invalidateAuthToken(
513 am
.clearPassword(getAccount());
517 requestCredentialsUpdate();
519 } catch (AccountNotFoundException e
) {
520 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
525 removeStickyBroadcast(intent
);
526 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
528 mProgressBar
.setIndeterminate(mSyncInProgress
);
533 } catch (RuntimeException e
) {
534 // avoid app crashes after changing the serial id of RemoteOperationResult
535 // in owncloud library with broadcast notifications pending to process
536 removeStickyBroadcast(intent
);
544 * Shows the information of the {@link OCFile} received as a
545 * parameter in the second fragment.
547 * @param file {@link OCFile} whose details will be shown
550 public void showDetails(OCFile file
) {
558 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
563 public void onRefresh() {
568 public void onRefresh(boolean enforced
) {
569 refreshList(enforced
);
572 private void refreshList(boolean ignoreETag
) {
573 OCFileListFragment listOfFiles
= getListOfFilesFragment();
574 if (listOfFiles
!= null
) {
575 OCFile folder
= listOfFiles
.getCurrentFile();
576 if (folder
!= null
) {
577 startSyncFolderOperation(folder
, ignoreETag
);