1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.activity
;
21 import android
.accounts
.Account
;
22 import android
.accounts
.AccountManager
;
23 import android
.accounts
.AccountManagerCallback
;
24 import android
.accounts
.AccountManagerFuture
;
25 import android
.accounts
.OperationCanceledException
;
26 import android
.content
.ComponentName
;
27 import android
.content
.Context
;
28 import android
.content
.Intent
;
29 import android
.content
.ServiceConnection
;
30 import android
.os
.Bundle
;
31 import android
.os
.Handler
;
32 import android
.os
.IBinder
;
33 import android
.support
.v4
.app
.Fragment
;
34 import android
.support
.v4
.app
.FragmentManager
;
35 import android
.support
.v4
.app
.FragmentTransaction
;
36 import android
.widget
.Toast
;
38 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
39 import com
.owncloud
.android
.MainApp
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.files
.FileOperationsHelper
;
45 import com
.owncloud
.android
.files
.services
.FileDownloader
;
46 import com
.owncloud
.android
.files
.services
.FileUploader
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
49 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
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
.operations
.CreateShareOperation
;
54 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
56 import com
.owncloud
.android
.services
.OperationsService
;
57 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
58 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
59 import com
.owncloud
.android
.utils
.Log_OC
;
63 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
65 * @author David A. Velasco
67 public class FileActivity
extends SherlockFragmentActivity
68 implements OnRemoteOperationListener
, ComponentsGetter
{
70 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
71 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
72 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
73 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
75 public static final String TAG
= FileActivity
.class.getSimpleName();
77 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
78 private static final String KEY_WAITING_FOR_OP_ID
= "WAITING_FOR_OP_ID";;
81 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
82 private Account mAccount
;
84 /** Main {@link OCFile} handled by the activity.*/
87 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
88 private boolean mRedirectingToSetupAccount
= false
;
90 /** Flag to signal when the value of mAccount was set */
91 private boolean mAccountWasSet
;
93 /** Flag to signal when the value of mAccount was restored from a saved state */
94 private boolean mAccountWasRestored
;
96 /** Flag to signal if the activity is launched by a notification */
97 private boolean mFromNotification
;
99 /** Messages handler associated to the main thread and the life cycle of the activity */
100 private Handler mHandler
;
102 /** Access point to the cached database for the current ownCloud {@link Account} */
103 private FileDataStorageManager mStorageManager
= null
;
105 private FileOperationsHelper mFileOperationsHelper
;
107 private ServiceConnection mOperationsServiceConnection
= null
;
109 private OperationsServiceBinder mOperationsServiceBinder
= null
;
111 protected FileDownloaderBinder mDownloaderBinder
= null
;
112 protected FileUploaderBinder mUploaderBinder
= null
;
113 private ServiceConnection mDownloadServiceConnection
, mUploadServiceConnection
= null
;
117 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
118 * the {@link FileActivity}.
120 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
121 * is requested to create a new one.
124 protected void onCreate(Bundle savedInstanceState
) {
125 super.onCreate(savedInstanceState
);
126 mHandler
= new Handler();
127 mFileOperationsHelper
= new FileOperationsHelper(this);
129 if(savedInstanceState
!= null
) {
130 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
131 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
132 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
133 mFileOperationsHelper
.setOpIdWaitingFor(
134 savedInstanceState
.getLong(KEY_WAITING_FOR_OP_ID
, Long
.MAX_VALUE
)
137 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
138 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
139 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
142 setAccount(account
, savedInstanceState
!= null
);
144 mOperationsServiceConnection
= new OperationsServiceConnection();
145 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
, Context
.BIND_AUTO_CREATE
);
147 mDownloadServiceConnection
= newTransferenceServiceConnection();
148 if (mDownloadServiceConnection
!= null
) {
149 bindService(new Intent(this, FileDownloader
.class), mDownloadServiceConnection
, Context
.BIND_AUTO_CREATE
);
151 mUploadServiceConnection
= newTransferenceServiceConnection();
152 if (mUploadServiceConnection
!= null
) {
153 bindService(new Intent(this, FileUploader
.class), mUploadServiceConnection
, Context
.BIND_AUTO_CREATE
);
160 * Since ownCloud {@link Account}s can be managed from the system setting menu,
161 * the existence of the {@link Account} associated to the instance must be checked
162 * every time it is restarted.
165 protected void onRestart() {
167 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
169 swapToDefaultAccount();
175 protected void onStart() {
178 if (mAccountWasSet
) {
179 onAccountSet(mAccountWasRestored
);
181 if (mOperationsServiceBinder
!= null
) {
182 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
187 protected void onResume() {
190 if (mOperationsServiceBinder
!= null
) {
191 doOnResumeAndBound();
197 protected void onPause() {
198 if (mOperationsServiceBinder
!= null
) {
199 mOperationsServiceBinder
.removeOperationListener(this);
206 protected void onStop() {
208 if (mOperationsServiceBinder
!= null
) {
209 mOperationsServiceBinder
.removeOperationListener(this);
217 protected void onDestroy() {
219 if (mOperationsServiceConnection
!= null
) {
220 unbindService(mOperationsServiceConnection
);
221 mOperationsServiceBinder
= null
;
223 if (mDownloadServiceConnection
!= null
) {
224 unbindService(mDownloadServiceConnection
);
225 mDownloadServiceConnection
= null
;
227 if (mUploadServiceConnection
!= null
) {
228 unbindService(mUploadServiceConnection
);
229 mUploadServiceConnection
= null
;
235 * Sets and validates the ownCloud {@link Account} associated to the Activity.
237 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
239 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
241 * @param account New {@link Account} to set.
242 * @param savedAccount When 'true', account was retrieved from a saved instance state.
244 private void setAccount(Account account
, boolean savedAccount
) {
245 Account oldAccount
= mAccount
;
246 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
249 mAccountWasSet
= true
;
250 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
253 swapToDefaultAccount();
259 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
261 * If no valid ownCloud {@link Account} exists, the the user is requested
262 * to create a new ownCloud {@link Account}.
264 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
266 * @return 'True' if the checked {@link Account} was valid.
268 private void swapToDefaultAccount() {
269 // default to the most recently used account
270 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
271 if (newAccount
== null
) {
272 /// no account available: force account creation
273 createFirstAccount();
274 mRedirectingToSetupAccount
= true
;
275 mAccountWasSet
= false
;
276 mAccountWasRestored
= false
;
279 mAccountWasSet
= true
;
280 mAccountWasRestored
= (newAccount
.equals(mAccount
));
281 mAccount
= newAccount
;
287 * Launches the account creation activity. To use when no ownCloud account is available
289 private void createFirstAccount() {
290 AccountManager am
= AccountManager
.get(getApplicationContext());
291 am
.addAccount(MainApp
.getAccountType(),
296 new AccountCreationCallback(),
305 protected void onSaveInstanceState(Bundle outState
) {
306 super.onSaveInstanceState(outState
);
307 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
308 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
309 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
310 outState
.putLong(KEY_WAITING_FOR_OP_ID
, mFileOperationsHelper
.getOpIdWaitingFor());
315 * Getter for the main {@link OCFile} handled by the activity.
317 * @return Main {@link OCFile} handled by the activity.
319 public OCFile
getFile() {
325 * Setter for the main {@link OCFile} handled by the activity.
327 * @param file Main {@link OCFile} to be handled by the activity.
329 public void setFile(OCFile file
) {
335 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
337 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
339 public Account
getAccount() {
344 * @return Value of mFromNotification: True if the Activity is launched by a notification
346 public boolean fromNotification() {
347 return mFromNotification
;
351 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
353 protected boolean isRedirectingToSetupAccount() {
354 return mRedirectingToSetupAccount
;
358 public OperationsServiceBinder
getOperationsServiceBinder() {
359 return mOperationsServiceBinder
;
362 protected ServiceConnection
newTransferenceServiceConnection() {
368 * Helper class handling a callback from the {@link AccountManager} after the creation of
369 * a new ownCloud {@link Account} finished, successfully or not.
371 * At this moment, only called after the creation of the first account.
373 * @author David A. Velasco
375 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
378 public void run(AccountManagerFuture
<Bundle
> future
) {
379 FileActivity
.this.mRedirectingToSetupAccount
= false
;
380 boolean accountWasSet
= false
;
381 if (future
!= null
) {
384 result
= future
.getResult();
385 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
386 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
387 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
388 setAccount(new Account(name
, type
), false
);
389 accountWasSet
= true
;
391 } catch (OperationCanceledException e
) {
392 Log_OC
.d(TAG
, "Account creation canceled");
394 } catch (Exception e
) {
395 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
399 Log_OC
.e(TAG
, "Account creation callback with null bundle");
401 if (!accountWasSet
) {
402 moveTaskToBack(true
);
410 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
412 * Child classes must grant that state depending on the {@link Account} is updated.
414 protected void onAccountSet(boolean stateWasRecovered
) {
415 if (getAccount() != null
) {
416 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
419 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
424 public FileDataStorageManager
getStorageManager() {
425 return mStorageManager
;
429 public OnRemoteOperationListener
getRemoteOperationListener() {
434 public Handler
getHandler() {
438 public FileOperationsHelper
getFileOperationsHelper() {
439 return mFileOperationsHelper
;
444 * @param operation Removal operation performed.
445 * @param result Result of the removal.
448 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
449 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
451 mFileOperationsHelper
.setOpIdWaitingFor(Long
.MAX_VALUE
);
453 if (operation
instanceof CreateShareOperation
) {
454 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
456 } else if (operation
instanceof UnshareLinkOperation
) {
457 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
462 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
463 dismissLoadingDialog();
464 if (result
.isSuccess()) {
467 Intent sendIntent
= operation
.getSendIntent();
468 startActivity(sendIntent
);
470 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) { // Error --> SHARE_NOT_FOUND
471 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_no_exist
), Toast
.LENGTH_LONG
);
473 } else { // Generic error
474 // Show a Message, operation finished without success
475 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_error
), Toast
.LENGTH_LONG
);
481 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
482 dismissLoadingDialog();
484 if (result
.isSuccess()){
487 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) { // Error --> SHARE_NOT_FOUND
488 Toast t
= Toast
.makeText(this, getString(R
.string
.unshare_link_file_no_exist
), Toast
.LENGTH_LONG
);
490 } else { // Generic error
491 // Show a Message, operation finished without success
492 Toast t
= Toast
.makeText(this, getString(R
.string
.unshare_link_file_error
), Toast
.LENGTH_LONG
);
499 private void updateFileFromDB(){
500 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
507 * Show loading dialog
509 public void showLoadingDialog() {
511 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
512 FragmentManager fm
= getSupportFragmentManager();
513 FragmentTransaction ft
= fm
.beginTransaction();
514 loading
.show(ft
, DIALOG_WAIT_TAG
);
520 * Dismiss loading dialog
522 public void dismissLoadingDialog(){
523 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
525 LoadingDialog loading
= (LoadingDialog
) frag
;
531 private void doOnResumeAndBound() {
532 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
533 long waitingForOpId
= mFileOperationsHelper
.getOpIdWaitingFor();
534 if (waitingForOpId
<= Integer
.MAX_VALUE
) {
535 mOperationsServiceBinder
.dispatchResultIfFinished((int)waitingForOpId
, this);
541 * Implements callback methods for service binding. Passed as a parameter to {
543 private class OperationsServiceConnection
implements ServiceConnection
{
546 public void onServiceConnected(ComponentName component
, IBinder service
) {
547 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
548 Log_OC
.d(TAG
, "Operations service connected");
549 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
550 /*if (!mOperationsServiceBinder.isPerformingBlockingOperation()) {
551 dismissLoadingDialog();
553 doOnResumeAndBound();
562 public void onServiceDisconnected(ComponentName component
) {
563 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
564 Log_OC
.d(TAG
, "Operations service disconnected");
565 mOperationsServiceBinder
= null
;
566 // TODO whatever could be waiting for the service is unbound
573 public FileDownloaderBinder
getFileDownloaderBinder() {
574 return mDownloaderBinder
;
579 public FileUploaderBinder
getFileUploaderBinder() {
580 return mUploaderBinder
;