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
.Intent
;
27 import android
.os
.Bundle
;
28 import android
.os
.Handler
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v4
.app
.FragmentManager
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.widget
.Toast
;
34 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
35 import com
.owncloud
.android
.MainApp
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.authentication
.AccountUtils
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.FileOperationsHelper
;
41 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
42 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
43 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
44 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
45 import com
.owncloud
.android
.operations
.CreateShareOperation
;
47 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
48 import com
.owncloud
.android
.utils
.Log_OC
;
52 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
54 * @author David A. Velasco
56 public class FileActivity
extends SherlockFragmentActivity
implements OnRemoteOperationListener
{
58 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
59 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
60 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
61 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
63 public static final String TAG
= FileActivity
.class.getSimpleName();
65 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
68 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
69 private Account mAccount
;
71 /** Main {@link OCFile} handled by the activity.*/
74 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
75 private boolean mRedirectingToSetupAccount
= false
;
77 /** Flag to signal when the value of mAccount was set */
78 private boolean mAccountWasSet
;
80 /** Flag to signal when the value of mAccount was restored from a saved state */
81 private boolean mAccountWasRestored
;
83 /** Flag to signal if the activity is launched by a notification */
84 private boolean mFromNotification
;
86 /** Messages handler associated to the main thread and the life cycle of the activity */
87 private Handler mHandler
;
89 /** Access point to the cached database for the current ownCloud {@link Account} */
90 private FileDataStorageManager mStorageManager
= null
;
92 private FileOperationsHelper mFileOperationsHelper
;
96 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
97 * the {@link FileActivity}.
99 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
100 * is requested to create a new one.
103 protected void onCreate(Bundle savedInstanceState
) {
104 super.onCreate(savedInstanceState
);
105 mHandler
= new Handler();
106 mFileOperationsHelper
= new FileOperationsHelper();
108 if(savedInstanceState
!= null
) {
109 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
110 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
111 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
113 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
114 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
115 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
118 setAccount(account
, savedInstanceState
!= null
);
124 * Since ownCloud {@link Account}s can be managed from the system setting menu,
125 * the existence of the {@link Account} associated to the instance must be checked
126 * every time it is restarted.
129 protected void onRestart() {
131 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
133 swapToDefaultAccount();
140 protected void onStart() {
142 if (mAccountWasSet
) {
143 onAccountSet(mAccountWasRestored
);
149 * Sets and validates the ownCloud {@link Account} associated to the Activity.
151 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
153 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
155 * @param account New {@link Account} to set.
156 * @param savedAccount When 'true', account was retrieved from a saved instance state.
158 private void setAccount(Account account
, boolean savedAccount
) {
159 Account oldAccount
= mAccount
;
160 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
163 mAccountWasSet
= true
;
164 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
167 swapToDefaultAccount();
173 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
175 * If no valid ownCloud {@link Account} exists, the the user is requested
176 * to create a new ownCloud {@link Account}.
178 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
180 * @return 'True' if the checked {@link Account} was valid.
182 private void swapToDefaultAccount() {
183 // default to the most recently used account
184 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
185 if (newAccount
== null
) {
186 /// no account available: force account creation
187 createFirstAccount();
188 mRedirectingToSetupAccount
= true
;
189 mAccountWasSet
= false
;
190 mAccountWasRestored
= false
;
193 mAccountWasSet
= true
;
194 mAccountWasRestored
= (newAccount
.equals(mAccount
));
195 mAccount
= newAccount
;
201 * Launches the account creation activity. To use when no ownCloud account is available
203 private void createFirstAccount() {
204 AccountManager am
= AccountManager
.get(getApplicationContext());
205 am
.addAccount(MainApp
.getAccountType(),
210 new AccountCreationCallback(),
219 protected void onSaveInstanceState(Bundle outState
) {
220 super.onSaveInstanceState(outState
);
221 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
222 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
223 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
228 * Getter for the main {@link OCFile} handled by the activity.
230 * @return Main {@link OCFile} handled by the activity.
232 public OCFile
getFile() {
238 * Setter for the main {@link OCFile} handled by the activity.
240 * @param file Main {@link OCFile} to be handled by the activity.
242 public void setFile(OCFile file
) {
248 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
250 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
252 public Account
getAccount() {
257 * @return Value of mFromNotification: True if the Activity is launched by a notification
259 public boolean fromNotification() {
260 return mFromNotification
;
264 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
266 protected boolean isRedirectingToSetupAccount() {
267 return mRedirectingToSetupAccount
;
272 * Helper class handling a callback from the {@link AccountManager} after the creation of
273 * a new ownCloud {@link Account} finished, successfully or not.
275 * At this moment, only called after the creation of the first account.
277 * @author David A. Velasco
279 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
282 public void run(AccountManagerFuture
<Bundle
> future
) {
283 FileActivity
.this.mRedirectingToSetupAccount
= false
;
284 boolean accountWasSet
= false
;
285 if (future
!= null
) {
288 result
= future
.getResult();
289 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
290 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
291 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
292 setAccount(new Account(name
, type
), false
);
293 accountWasSet
= true
;
295 } catch (OperationCanceledException e
) {
296 Log_OC
.d(TAG
, "Account creation canceled");
298 } catch (Exception e
) {
299 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
303 Log_OC
.e(TAG
, "Account creation callback with null bundle");
305 if (!accountWasSet
) {
306 moveTaskToBack(true
);
314 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
316 * Child classes must grant that state depending on the {@link Account} is updated.
318 protected void onAccountSet(boolean stateWasRecovered
) {
319 if (getAccount() != null
) {
320 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
323 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
328 public FileDataStorageManager
getStorageManager() {
329 return mStorageManager
;
333 public OnRemoteOperationListener
getRemoteOperationListener() {
338 public Handler
getHandler() {
342 public FileOperationsHelper
getFileOperationsHelper() {
343 return mFileOperationsHelper
;
348 * @param operation Removal operation performed.
349 * @param result Result of the removal.
352 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
353 Log_OC
.d(TAG
, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
354 if (operation
instanceof CreateShareOperation
) {
355 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
359 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
360 dismissLoadingDialog();
361 if (result
.isSuccess()) {
362 Intent sendIntent
= operation
.getSendIntent();
363 startActivity(sendIntent
);
365 } else if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) { // Error --> SHARE_NOT_FOUND
366 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_no_exist
), Toast
.LENGTH_LONG
);
368 } else { // Generic error
369 // Show a Message, operation finished without success
370 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_file_error
), Toast
.LENGTH_LONG
);
377 * Show loading dialog
379 public void showLoadingDialog() {
381 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
382 FragmentManager fm
= getSupportFragmentManager();
383 FragmentTransaction ft
= fm
.beginTransaction();
384 loading
.show(ft
, DIALOG_WAIT_TAG
);
390 * Dismiss loading dialog
392 public void dismissLoadingDialog(){
393 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
395 LoadingDialog loading
= (LoadingDialog
) frag
;