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 android
.accounts
.Account
;
21 import android
.accounts
.AccountManager
;
22 import android
.accounts
.AuthenticatorException
;
23 import android
.content
.BroadcastReceiver
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.content
.IntentFilter
;
27 import android
.content
.res
.Resources
.NotFoundException
;
28 import android
.os
.Bundle
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v4
.app
.FragmentTransaction
;
31 import android
.support
.v4
.widget
.SwipeRefreshLayout
;
32 import android
.support
.v7
.app
.ActionBar
;
33 import android
.util
.Log
;
34 import android
.view
.Menu
;
35 import android
.view
.MenuInflater
;
36 import android
.view
.MenuItem
;
37 import android
.view
.View
;
38 import android
.view
.View
.OnClickListener
;
39 import android
.view
.Window
;
40 import android
.widget
.Button
;
41 import android
.widget
.Toast
;
43 import com
.owncloud
.android
.R
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
45 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
46 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
47 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
48 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
49 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
50 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
51 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
52 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
53 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
54 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
55 import com
.owncloud
.android
.operations
.RefreshFolderOperation
;
56 import com
.owncloud
.android
.syncadapter
.FileSyncAdapter
;
57 import com
.owncloud
.android
.ui
.dialog
.CreateFolderDialogFragment
;
58 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
59 import com
.owncloud
.android
.ui
.fragment
.OCFileListFragment
;
60 import com
.owncloud
.android
.utils
.DisplayUtils
;
61 import com
.owncloud
.android
.utils
.ErrorMessageAdapter
;
64 public class CopyActivity
extends HookActivity
implements FileFragment
.ContainerActivity
,
65 OnClickListener
, SwipeRefreshLayout
.OnRefreshListener
{
67 public static final String EXTRA_CURRENT_FOLDER
= UploadFilesActivity
.class.getCanonicalName() + ".EXTRA_CURRENT_FOLDER";
68 public static final String EXTRA_TARGET_FILE
= UploadFilesActivity
.class.getCanonicalName() + "EXTRA_TARGET_FILE";
70 public static final int RESULT_OK_AND_COPY
= 1;
72 private SyncBroadcastReceiver mSyncBroadcastReceiver
;
74 private static final String TAG
= CopyActivity
.class.getSimpleName();
76 private static final String TAG_LIST_OF_FOLDERS
= "LIST_OF_FOLDERS";
78 private boolean mSyncInProgress
= false
;
80 private Button mCancelBtn
;
81 private Button mChooseBtn
;
85 protected void onCreate(Bundle savedInstanceState
) {
86 Log_OC
.d(TAG
, "onCreate() start");
87 requestWindowFeature(Window
.FEATURE_INDETERMINATE_PROGRESS
);
89 super.onCreate(savedInstanceState
);
91 setContentView(R
.layout
.files_folder_picker
);
93 if (savedInstanceState
== null
) {
97 // sets callback listeners for UI elements
101 ActionBar actionBar
= getSupportActionBar();
102 actionBar
.setDisplayShowTitleEnabled(true
);
103 actionBar
.setNavigationMode(ActionBar
.NAVIGATION_MODE_STANDARD
);
104 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
);
105 // always AFTER setContentView(...) ; to work around bug in its implementation
107 // sets message for empty list of folders
110 Log_OC
.d(TAG
, "onCreate() end");
115 protected void onStart() {
117 getSupportActionBar().setIcon(DisplayUtils
.getSeasonalIconId());
121 protected void onDestroy() {
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 private OCFileListFragment
getListOfFilesFragment() {
183 Fragment listOfFiles
= getSupportFragmentManager().findFragmentByTag(CopyActivity
.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_sync_account
).setVisible(false
);
272 public boolean onOptionsItemSelected(MenuItem item
) {
273 boolean retval
= true
;
274 switch (item
.getItemId()) {
275 case R
.id
.action_create_dir
: {
276 CreateFolderDialogFragment dialog
=
277 CreateFolderDialogFragment
.newInstance(getCurrentFolder());
279 getSupportFragmentManager(),
280 CreateFolderDialogFragment
.CREATE_FOLDER_FRAGMENT
284 case android
.R
.id
.home
: {
285 OCFile currentDir
= getCurrentFolder();
286 if (currentDir
!= null
&& currentDir
.getParentId() != 0) {
292 retval
= super.onOptionsItemSelected(item
);
297 private OCFile
getCurrentFolder() {
298 OCFile file
= getFile();
300 if (file
.isFolder()) {
302 } else if (getStorageManager() != null
) {
303 String parentPath
= file
.getRemotePath().substring(0, file
.getRemotePath().lastIndexOf(file
.getFileName()));
304 return getStorageManager().getFileByPath(parentPath
);
310 protected void refreshListOfFilesFragment() {
311 OCFileListFragment fileListFragment
= getListOfFilesFragment();
312 if (fileListFragment
!= null
) {
313 fileListFragment
.listDirectory();
317 public void browseToRoot() {
318 OCFileListFragment listOfFiles
= getListOfFilesFragment();
319 if (listOfFiles
!= null
) { // should never be null, indeed
320 OCFile root
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
321 listOfFiles
.listDirectory(root
);
322 setFile(listOfFiles
.getCurrentFile());
323 updateNavigationElementsInActionBar();
324 startSyncFolderOperation(root
, false
);
329 public void onBackPressed() {
330 OCFileListFragment listOfFiles
= getListOfFilesFragment();
331 if (listOfFiles
!= null
) { // should never be null, indeed
332 int levelsUp
= listOfFiles
.onBrowseUp();
337 setFile(listOfFiles
.getCurrentFile());
338 updateNavigationElementsInActionBar();
342 private void updateNavigationElementsInActionBar() {
343 ActionBar actionBar
= getSupportActionBar();
344 OCFile currentDir
= getCurrentFolder();
345 boolean atRoot
= (currentDir
== null
|| currentDir
.getParentId() == 0);
346 actionBar
.setDisplayHomeAsUpEnabled(!atRoot
);
347 actionBar
.setHomeButtonEnabled(!atRoot
);
350 ?
getString(R
.string
.default_display_name_for_root_folder
)
351 : currentDir
.getFileName()
356 * Set per-view controllers
358 private void initControls() {
359 mCancelBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_cancel
);
360 mCancelBtn
.setOnClickListener(this);
361 mChooseBtn
= (Button
) findViewById(R
.id
.folder_picker_btn_choose
);
362 mChooseBtn
.setOnClickListener(this);
366 public void onClick(View v
) {
367 if (v
== mCancelBtn
) {
369 } else if (v
== mChooseBtn
) {
370 Intent i
= getIntent();
371 OCFile targetFile
= i
.getParcelableExtra(CopyActivity
.EXTRA_TARGET_FILE
);
373 Intent data
= new Intent();
374 data
.putExtra(EXTRA_CURRENT_FOLDER
, getCurrentFolder());
375 data
.putExtra(EXTRA_TARGET_FILE
, targetFile
);
376 setResult(RESULT_OK_AND_COPY
, data
);
383 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
384 super.onRemoteOperationFinish(operation
, result
);
386 if (operation
instanceof CreateFolderOperation
) {
387 onCreateFolderOperationFinish((CreateFolderOperation
) operation
, result
);
394 * Updates the view associated to the activity after the finish of an operation trying
395 * to create a new folder.
397 * @param operation Creation operation performed.
398 * @param result Result of the creation.
400 private void onCreateFolderOperationFinish(
401 CreateFolderOperation operation
, RemoteOperationResult result
404 if (result
.isSuccess()) {
405 dismissLoadingDialog();
406 refreshListOfFilesFragment();
408 dismissLoadingDialog();
410 Toast msg
= Toast
.makeText(CopyActivity
.this,
411 ErrorMessageAdapter
.getErrorCauseMessage(result
, operation
, getResources()),
415 } catch (NotFoundException e
) {
416 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
.getSerializableExtra(FileSyncAdapter
.EXTRA_RESULT
);
435 boolean sameAccount
= (getAccount() != null
&& accountName
.equals(getAccount().name
) && getStorageManager() != null
);
439 if (FileSyncAdapter
.EVENT_FULL_SYNC_START
.equals(event
)) {
440 mSyncInProgress
= true
;
443 OCFile currentFile
= (getFile() == null
) ? null
: getStorageManager().getFileByPath(getFile().getRemotePath());
444 OCFile currentDir
= (getCurrentFolder() == null
) ? null
: getStorageManager().getFileByPath(getCurrentFolder().getRemotePath());
446 if (currentDir
== null
) {
447 // current folder was removed from the server
448 Toast
.makeText(CopyActivity
.this,
449 String
.format(getString(R
.string
.sync_current_folder_was_removed
), getCurrentFolder().getFileName()),
455 if (currentFile
== null
&& !getFile().isFolder()) {
456 // currently selected file was removed in the server, and now we know it
457 currentFile
= currentDir
;
460 if (synchFolderRemotePath
!= null
&& currentDir
.getRemotePath().equals(synchFolderRemotePath
)) {
461 OCFileListFragment fileListFragment
= getListOfFilesFragment();
462 if (fileListFragment
!= null
) {
463 fileListFragment
.listDirectory(currentDir
);
466 setFile(currentFile
);
469 mSyncInProgress
= (!FileSyncAdapter
.EVENT_FULL_SYNC_END
.equals(event
) && !RefreshFolderOperation
.EVENT_SINGLE_FOLDER_SHARES_SYNCED
.equals(event
));
471 if (RefreshFolderOperation
.EVENT_SINGLE_FOLDER_CONTENTS_SYNCED
.
473 /// TODO refactor and make common
474 synchResult
!= null
&& !synchResult
.isSuccess() &&
475 (synchResult
.getCode() == ResultCode
.UNAUTHORIZED
||
476 synchResult
.isIdPRedirection() ||
477 (synchResult
.isException() && synchResult
.getException()
478 instanceof AuthenticatorException
))) {
481 OwnCloudClient client
;
482 OwnCloudAccount ocAccount
=
483 new OwnCloudAccount(getAccount(), context
);
484 client
= (OwnCloudClientManagerFactory
.getDefaultSingleton().
485 removeClientFor(ocAccount
));
487 if (client
!= null
) {
488 OwnCloudCredentials cred
= client
.getCredentials();
490 AccountManager am
= AccountManager
.get(context
);
491 if (cred
.authTokenExpires()) {
492 am
.invalidateAuthToken(
497 am
.clearPassword(getAccount());
501 requestCredentialsUpdate();
503 } catch (AccountNotFoundException e
) {
504 Log_OC
.e(TAG
, "Account " + getAccount() + " was removed!", e
);
509 removeStickyBroadcast(intent
);
510 Log_OC
.d(TAG
, "Setting progress visibility to " + mSyncInProgress
);
511 setSupportProgressBarIndeterminateVisibility(mSyncInProgress
/*|| mRefreshSharesInProgress*/);
517 } catch (RuntimeException e
) {
518 // avoid app crashes after changing the serial id of RemoteOperationResult
519 // in owncloud library with broadcast notifications pending to process
520 removeStickyBroadcast(intent
);
527 * Shows the information of the {@link OCFile} received as a
528 * parameter in the second fragment.
530 * @param file {@link OCFile} whose details will be shown
533 public void showDetails(OCFile file
) {
541 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
547 public void onRefresh() {
548 OCFileListFragment listOfFiles
= getListOfFilesFragment();
549 if (listOfFiles
!= null
) {
550 OCFile folder
= listOfFiles
.getCurrentFile();
551 if (folder
!= null
) {
552 startSyncFolderOperation(folder
, true
);