1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 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
.lib
.operations
.common
.OnRemoteOperationListener
;
46 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
47 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
48 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
49 import com
.owncloud
.android
.operations
.CreateShareOperation
;
50 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
52 import com
.owncloud
.android
.services
.OperationsService
;
53 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
54 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
55 import com
.owncloud
.android
.utils
.Log_OC
;
59 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
61 * @author David A. Velasco
63 public class FileActivity
extends SherlockFragmentActivity
implements OnRemoteOperationListener
{
65 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
66 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
67 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
68 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
70 public static final String TAG
= FileActivity
.class.getSimpleName();
72 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
75 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
76 private Account mAccount
;
78 /** Main {@link OCFile} handled by the activity.*/
81 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
82 private boolean mRedirectingToSetupAccount
= false
;
84 /** Flag to signal when the value of mAccount was set */
85 private boolean mAccountWasSet
;
87 /** Flag to signal when the value of mAccount was restored from a saved state */
88 private boolean mAccountWasRestored
;
90 /** Flag to signal if the activity is launched by a notification */
91 private boolean mFromNotification
;
93 /** Messages handler associated to the main thread and the life cycle of the activity */
94 private Handler mHandler
;
96 /** Access point to the cached database for the current ownCloud {@link Account} */
97 private FileDataStorageManager mStorageManager
= null
;
99 private FileOperationsHelper mFileOperationsHelper
;
101 private ServiceConnection mOperationsServiceConnection
= null
;
103 private OperationsServiceBinder mOperationsServiceBinder
= null
;
107 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
108 * the {@link FileActivity}.
110 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
111 * is requested to create a new one.
114 protected void onCreate(Bundle savedInstanceState
) {
115 super.onCreate(savedInstanceState
);
116 mHandler
= new Handler();
117 mFileOperationsHelper
= new FileOperationsHelper();
119 if(savedInstanceState
!= null
) {
120 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
121 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
122 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
124 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
125 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
126 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
129 setAccount(account
, savedInstanceState
!= null
);
131 mOperationsServiceConnection
= new OperationsServiceConnection();
132 bindService(new Intent(this, OperationsService
.class), mOperationsServiceConnection
, Context
.BIND_AUTO_CREATE
);
137 * Since ownCloud {@link Account}s can be managed from the system setting menu,
138 * the existence of the {@link Account} associated to the instance must be checked
139 * every time it is restarted.
142 protected void onRestart() {
144 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
146 swapToDefaultAccount();
152 protected void onStart() {
154 if (mAccountWasSet
) {
155 onAccountSet(mAccountWasRestored
);
161 protected void onStop() {
163 if (mOperationsServiceBinder
!= null
) {
164 mOperationsServiceBinder
.removeOperationListener(this);
165 mOperationsServiceBinder
= null
;
171 protected void onDestroy() {
173 if (mOperationsServiceConnection
!= null
)
174 unbindService(mOperationsServiceConnection
);
179 * Sets and validates the ownCloud {@link Account} associated to the Activity.
181 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
183 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
185 * @param account New {@link Account} to set.
186 * @param savedAccount When 'true', account was retrieved from a saved instance state.
188 private void setAccount(Account account
, boolean savedAccount
) {
189 Account oldAccount
= mAccount
;
190 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
193 mAccountWasSet
= true
;
194 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
197 swapToDefaultAccount();
203 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
205 * If no valid ownCloud {@link Account} exists, the the user is requested
206 * to create a new ownCloud {@link Account}.
208 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
210 * @return 'True' if the checked {@link Account} was valid.
212 private void swapToDefaultAccount() {
213 // default to the most recently used account
214 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
215 if (newAccount
== null
) {
216 /// no account available: force account creation
217 createFirstAccount();
218 mRedirectingToSetupAccount
= true
;
219 mAccountWasSet
= false
;
220 mAccountWasRestored
= false
;
223 mAccountWasSet
= true
;
224 mAccountWasRestored
= (newAccount
.equals(mAccount
));
225 mAccount
= newAccount
;
231 * Launches the account creation activity. To use when no ownCloud account is available
233 private void createFirstAccount() {
234 AccountManager am
= AccountManager
.get(getApplicationContext());
235 am
.addAccount(MainApp
.getAccountType(),
240 new AccountCreationCallback(),
249 protected void onSaveInstanceState(Bundle outState
) {
250 super.onSaveInstanceState(outState
);
251 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
252 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
253 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
258 * Getter for the main {@link OCFile} handled by the activity.
260 * @return Main {@link OCFile} handled by the activity.
262 public OCFile
getFile() {
268 * Setter for the main {@link OCFile} handled by the activity.
270 * @param file Main {@link OCFile} to be handled by the activity.
272 public void setFile(OCFile file
) {
278 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
280 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
282 public Account
getAccount() {
287 * @return Value of mFromNotification: True if the Activity is launched by a notification
289 public boolean fromNotification() {
290 return mFromNotification
;
294 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
296 protected boolean isRedirectingToSetupAccount() {
297 return mRedirectingToSetupAccount
;
302 * Helper class handling a callback from the {@link AccountManager} after the creation of
303 * a new ownCloud {@link Account} finished, successfully or not.
305 * At this moment, only called after the creation of the first account.
307 * @author David A. Velasco
309 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
312 public void run(AccountManagerFuture
<Bundle
> future
) {
313 FileActivity
.this.mRedirectingToSetupAccount
= false
;
314 boolean accountWasSet
= false
;
315 if (future
!= null
) {
318 result
= future
.getResult();
319 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
320 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
321 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
322 setAccount(new Account(name
, type
), false
);
323 accountWasSet
= true
;
325 } catch (OperationCanceledException e
) {
326 Log_OC
.d(TAG
, "Account creation canceled");
328 } catch (Exception e
) {
329 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
333 Log_OC
.e(TAG
, "Account creation callback with null bundle");
335 if (!accountWasSet
) {
336 moveTaskToBack(true
);
344 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
346 * Child classes must grant that state depending on the {@link Account} is updated.
348 protected void onAccountSet(boolean stateWasRecovered
) {
349 if (getAccount() != null
) {
350 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
353 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
358 public FileDataStorageManager
getStorageManager() {
359 return mStorageManager
;
363 public OnRemoteOperationListener
getRemoteOperationListener() {
368 public Handler
getHandler() {
372 public FileOperationsHelper
getFileOperationsHelper() {
373 return mFileOperationsHelper
;
378 * @param operation Removal operation performed.
379 * @param result Result of the removal.
382 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
383 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
384 if (operation
instanceof CreateShareOperation
) {
385 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
387 } else if (operation
instanceof UnshareLinkOperation
) {
388 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
393 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
394 dismissLoadingDialog();
395 if (result
.isSuccess()) {
398 Intent sendIntent
= operation
.getSendIntent();
399 startActivity(sendIntent
);
401 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) { // Error --> SHARE_NOT_FOUND
402 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_no_exist
), Toast
.LENGTH_LONG
);
404 } else { // Generic error
405 // Show a Message, operation finished without success
406 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_error
), Toast
.LENGTH_LONG
);
412 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
413 dismissLoadingDialog();
415 if (result
.isSuccess()){
418 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) { // Error --> SHARE_NOT_FOUND
419 Toast t
= Toast
.makeText(this, getString(R
.string
.unshare_link_file_no_exist
), Toast
.LENGTH_LONG
);
421 } else { // Generic error
422 // Show a Message, operation finished without success
423 Toast t
= Toast
.makeText(this, getString(R
.string
.unshare_link_file_error
), Toast
.LENGTH_LONG
);
430 private void updateFileFromDB(){
431 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
438 * Show loading dialog
440 public void showLoadingDialog() {
442 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
443 FragmentManager fm
= getSupportFragmentManager();
444 FragmentTransaction ft
= fm
.beginTransaction();
445 loading
.show(ft
, DIALOG_WAIT_TAG
);
451 * Dismiss loading dialog
453 public void dismissLoadingDialog(){
454 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
456 LoadingDialog loading
= (LoadingDialog
) frag
;
463 * Implements callback methods for service binding. Passed as a parameter to {
465 private class OperationsServiceConnection
implements ServiceConnection
{
468 public void onServiceConnected(ComponentName component
, IBinder service
) {
469 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
470 Log_OC
.d(TAG
, "Operations service connected");
471 mOperationsServiceBinder
= (OperationsServiceBinder
) service
;
472 mOperationsServiceBinder
.addOperationListener(FileActivity
.this, mHandler
);
473 if (!mOperationsServiceBinder
.isPerformingBlockingOperation()) {
474 dismissLoadingDialog();
484 public void onServiceDisconnected(ComponentName component
) {
485 if (component
.equals(new ComponentName(FileActivity
.this, OperationsService
.class))) {
486 Log_OC
.d(TAG
, "Operations service disconnected");
487 mOperationsServiceBinder
= null
;
488 // TODO whatever could be waiting for the service is unbound