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 import java
.util
.ArrayList
;
67 public class FolderPickerActivity
extends FileActivity
implements FileFragment
.ContainerActivity
,
68 OnClickListener
, OnEnforceableRefreshListener
{
70 public static final String EXTRA_FOLDER
= UploadFilesActivity
.class.getCanonicalName()
72 public static final String EXTRA_FILE
= UploadFilesActivity
.class.getCanonicalName()
74 public static final String EXTRA_FILES
= 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
;
88 private ProgressBar mProgressBar
;
92 protected void onCreate(Bundle savedInstanceState
) {
93 Log_OC
.d(TAG
, "onCreate() start");
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
);
111 mProgressBar
= (ProgressBar
) findViewById(R
.id
.progressBar
);
112 mProgressBar
.setIndeterminateDrawable(
113 getResources().getDrawable(
114 R
.drawable
.actionbar_progress_indeterminate_horizontal
));
115 mProgressBar
.setIndeterminate(mSyncInProgress
);
116 // always AFTER setContentView(...) ; to work around bug in its implementation
118 // sets message for empty list of folders
121 Log_OC
.d(TAG
, "onCreate() end");
125 protected void onStart() {
130 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
133 protected void onAccountSet(boolean stateWasRecovered
) {
134 super.onAccountSet(stateWasRecovered
);
135 if (getAccount() != null
) {
139 OCFile folder
= getFile();
140 if (folder
== null
|| !folder
.isFolder()) {
141 // fall back to root folder
142 setFile(getStorageManager().getFileByPath(OCFile
.ROOT_PATH
));
146 if (!stateWasRecovered
) {
147 OCFileListFragment listOfFolders
= getListOfFilesFragment();
148 listOfFolders
.listDirectory(folder
, false
);
150 startSyncFolderOperation(folder
, false
);
153 updateNavigationElementsInActionBar();
157 private void createFragments() {
158 OCFileListFragment listOfFiles
= new OCFileListFragment();
159 Bundle args
= new Bundle();
160 args
.putBoolean(OCFileListFragment
.ARG_JUST_FOLDERS
, true
);
161 args
.putBoolean(OCFileListFragment
.ARG_ALLOW_CONTEXTUAL_ACTIONS
, false
);
162 args
.putBoolean(OCFileListFragment
.ARG_HIDE_FAB
, true
);
163 listOfFiles
.setArguments(args
);
164 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
165 transaction
.add(R
.id
.fragment_container
, listOfFiles
, TAG_LIST_OF_FOLDERS
);
166 transaction
.commit();
170 * Show a text message on screen view for notifying user if content is
171 * loading or folder is empty
173 private void setBackgroundText() {
174 OCFileListFragment listFragment
= getListOfFilesFragment();
175 if (listFragment
!= null
) {
176 int message
= R
.string
.file_list_loading
;
177 if (!mSyncInProgress
) {
178 // In case folder list is empty
179 message
= R
.string
.file_list_empty_moving
;
181 listFragment
.setMessageForEmptyList(getString(message
));
183 Log
.e(TAG
, "OCFileListFragment is null");
187 protected OCFileListFragment
getListOfFilesFragment() {
188 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(FolderPickerActivity
.TAG_LIST_OF_FOLDERS
);
189 if (listOfFiles
!= null
) {
190 return (OCFileListFragment
)listOfFiles
;
192 Log_OC
.wtf(TAG
, "Access to unexisting list of files fragment!!");
200 * Updates action bar and second fragment, if in dual pane mode.
203 public void onBrowsedDownTo(OCFile directory
) {
205 updateNavigationElementsInActionBar();
207 startSyncFolderOperation(directory
, false
);
212 public void startSyncFolderOperation(OCFile folder
, boolean ignoreETag
) {
213 long currentSyncTime
= System
.currentTimeMillis();
215 mSyncInProgress
= true
;
217 // perform folder synchronization
218 RemoteOperation synchFolderOp
= new RefreshFolderOperation( folder
,
221 getFileOperationsHelper().isSharedSupported(),
225 getApplicationContext()
227 synchFolderOp
.execute(getAccount(), this, null
, null
);
229 mProgressBar
.setIndeterminate(true
);
235 protected void onResume() {
237 Log_OC
.e(TAG
, "onResume() start");
239 // refresh list of files
240 refreshListOfFilesFragment();
242 // Listen for sync messages
243 IntentFilter syncIntentFilter
= new IntentFilter(FileSyncAdapter
.EVENT_FULL_SYNC_START
);
244 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_END
);
245 syncIntentFilter
.addAction(FileSyncAdapter
.EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED
);
246 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
);
247 syncIntentFilter
.addAction(RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
);
248 mSyncBroadcastReceiver
= new SyncBroadcastReceiver();
249 registerReceiver(mSyncBroadcastReceiver
, syncIntentFilter
);
251 Log_OC
.d(TAG
, "onResume() end");
255 protected void onPause() {
256 Log_OC
.e(TAG
, "onPause() start");
257 if (mSyncBroadcastReceiver
!= null
) {
258 unregisterReceiver(mSyncBroadcastReceiver
);
259 //LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncBroadcastReceiver);
260 mSyncBroadcastReceiver
= null
;
263 Log_OC
.d(TAG
, "onPause() end");
268 public boolean onCreateOptionsMenu(Menu menu
) {
269 MenuInflater inflater
= getMenuInflater();
270 inflater
.inflate(R
.menu
.main_menu
, menu
);
271 menu
.findItem(R
.id
.action_sort
).setVisible(false
);
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());
283 getSupportFragmentManager(),
284 CreateFolderDialogFragment
.CREATE_FOLDER_FRAGMENT
288 case android
.R
.id
.home
: {
289 OCFile currentDir
= getCurrentFolder();
290 if(currentDir
!= null
&& currentDir
.getParentId() != 0) {
296 retval
= super.onOptionsItemSelected(item
);
301 protected OCFile
getCurrentFolder() {
302 OCFile file
= getFile();
304 if (file
.isFolder()) {
306 } else if (getStorageManager() != null
) {
307 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
308 return getStorageManager().getFileByPath(parentPath
);
314 protected void refreshListOfFilesFragment() {
315 OCFileListFragment fileListFragment
= getListOfFilesFragment();
316 if (fileListFragment
!= null
) {
317 fileListFragment
.listDirectory(false
);
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
, false
);
326 setFile(listOfFiles
.getCurrentFile());
327 updateNavigationElementsInActionBar();
328 startSyncFolderOperation(root
, false
);
333 public void onBackPressed() {
334 OCFileListFragment listOfFiles
= getListOfFilesFragment();
335 if (listOfFiles
!= null
) { // should never be null, indeed
336 int levelsUp
= listOfFiles
.onBrowseUp();
341 setFile(listOfFiles
.getCurrentFile());
342 updateNavigationElementsInActionBar();
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
);
354 ?
getString(R
.string
.default_display_name_for_root_folder
)
355 : currentDir
.getFileName()
360 * Set per-view controllers
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);
370 public void onClick(View v
) {
371 if (v
== mCancelBtn
) {
373 } else if (v
== mChooseBtn
) {
374 Intent i
= getIntent();
375 Parcelable targetFile
= i
.getParcelableExtra(FolderPickerActivity
.EXTRA_FILE
);
376 ArrayList
<Parcelable
> targetFiles
= i
.getParcelableArrayListExtra(FolderPickerActivity
.EXTRA_FILES
);
378 Intent data
= new Intent();
379 data
.putExtra(EXTRA_FOLDER
, getCurrentFolder());
380 if (targetFile
!= null
) {
381 data
.putExtra(EXTRA_FILE
, targetFile
);
383 if (targetFiles
!= null
){
384 data
.putParcelableArrayListExtra(EXTRA_FILES
, targetFiles
);
386 setResult(RESULT_OK
, data
);
393 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
394 super.onRemoteOperationFinish(operation
, result
);
396 if (operation
instanceof CreateFolderOperation
) {
397 onCreateFolderOperationFinish((CreateFolderOperation
)operation
, result
);
404 * Updates the view associated to the activity after the finish of an operation trying
405 * to create a new folder.
407 * @param operation Creation operation performed.
408 * @param result Result of the creation.
410 private void onCreateFolderOperationFinish(
411 CreateFolderOperation operation
, RemoteOperationResult result
414 if (result
.isSuccess()) {
415 dismissLoadingDialog();
416 refreshListOfFilesFragment();
418 dismissLoadingDialog();
420 Toast msg
= Toast
.makeText(FolderPickerActivity
.this,
421 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
425 } catch (NotFoundException e
) {
426 Log_OC
.e(TAG
, "Error while trying to show fail message " , e
);
433 private class SyncBroadcastReceiver
extends BroadcastReceiver
{
436 * {@link BroadcastReceiver} to enable syncing feedback in UI
439 public void onReceive(Context context
, Intent intent
) {
441 String event
= intent
.getAction();
442 Log_OC
.d(TAG
, "Received broadcast " + event
);
443 String accountName
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_ACCOUNT_NAME
);
444 String synchFolderRemotePath
= intent
.getStringExtra(FileSyncAdapter
.EXTRA_FOLDER_PATH
);
445 RemoteOperationResult synchResult
= (RemoteOperationResult
)intent
.
446 getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
447 boolean sameAccount
= (getAccount() != null
&&
448 accountName
.equals(getAccount().name
) && getStorageManager() != null
);
452 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
453 mSyncInProgress
= true
;
456 OCFile currentFile
= (getFile() == null
) ? null
:
457 getStorageManager().getFileByPath(getFile().getRemotePath());
458 OCFile currentDir
= (getCurrentFolder() == null
) ? null
:
459 getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
461 if (currentDir
== null
) {
462 // current folder was removed from the server
463 Toast
.makeText( FolderPickerActivity
.this,
465 getString(R
.string
.sync_current_folder_was_removed
),
466 getCurrentFolder().getFileName()),
472 if (currentFile
== null
&& !getFile().isFolder()) {
473 // currently selected file was removed in the server, and now we know it
474 currentFile
= currentDir
;
477 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().
478 equals(synchFolderRemotePath
)) {
479 OCFileListFragment fileListFragment
= getListOfFilesFragment();
480 if (fileListFragment
!= null
) {
481 fileListFragment
.listDirectory(currentDir
, false
);
484 setFile(currentFile
);
487 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) &&
488 !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
490 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
492 /// TODO refactor and make common
493 synchResult
!= null
&& !synchResult
.isSuccess() &&
494 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
495 synchResult
.isIdPRedirection() ||
496 (synchResult
.isException() && synchResult
.getException()
497 instanceof AuthenticatorException
))) {
500 OwnCloudClient client
;
501 OwnCloudAccount ocAccount
=
502 new OwnCloudAccount(getAccount(), context
);
503 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
504 removeClientFor(ocAccount
));
506 if (client
!= null
) {
507 OwnCloudCredentials cred
= client
.getCredentials();
509 AccountManager am
= AccountManager
.get(context
);
510 if (cred
.authTokenExpires()) {
511 am
.invalidateAuthToken(
516 am
.clearPassword(getAccount());
520 requestCredentialsUpdate();
522 } catch (AccountNotFoundException e
) {
523 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
528 removeStickyBroadcast(intent
);
529 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
531 mProgressBar
.setIndeterminate(mSyncInProgress
);
536 } catch (RuntimeException e
) {
537 // avoid app crashes after changing the serial id of RemoteOperationResult
538 // in owncloud library with broadcast notifications pending to process
539 removeStickyBroadcast(intent
);
547 * Shows the information of the {@link OCFile} received as a
548 * parameter in the second fragment.
550 * @param file {@link OCFile} whose details will be shown
553 public void showDetails(OCFile file
) {
561 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
566 public void onRefresh() {
571 public void onRefresh(boolean enforced
) {
572 refreshList(enforced
);
575 private void refreshList(boolean ignoreETag
) {
576 OCFileListFragment listOfFiles
= getListOfFilesFragment();
577 if (listOfFiles
!= null
) {
578 OCFile folder
= listOfFiles
.getCurrentFile();
579 if (folder
!= null
) {
580 startSyncFolderOperation(folder
, ignoreETag
);