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
.os
.Bundle
;
28 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
29 import com
.owncloud
.android
.AccountUtils
;
30 import com
.owncloud
.android
.Log_OC
;
31 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
35 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
37 * @author David A. Velasco
39 public abstract class FileActivity
extends SherlockFragmentActivity
{
41 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
42 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
44 public static final String TAG
= FileActivity
.class.getSimpleName();
47 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
48 private Account mAccount
;
50 /** Main {@link OCFile} handled by the activity.*/
53 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
54 private boolean mRedirectingToSetupAccount
= false
;
58 protected void onCreate(Bundle savedInstanceState
) {
59 super.onCreate(savedInstanceState
);
61 /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
62 if(savedInstanceState
!= null
) {
63 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
64 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
66 mAccount
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
67 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
70 if (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
)) {
77 * Validate the ownCloud {@link Account} associated to the Activity any time it is
78 * started, and if not valid tries to move to a different Account.
81 protected void onStart() {
82 Log_OC
.e(TAG
, "onStart en FileActivity");
84 /// Validate account, and try to fix if wrong
85 if (mAccount
== null
|| !AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
)) {
86 if (!AccountUtils
.accountsAreSetup(getApplicationContext())) {
87 /// no account available: force account creation
90 mRedirectingToSetupAccount
= true
;
93 /// get 'last current account' as default account
94 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
102 * Launches the account creation activity. To use when no ownCloud account is available
104 private void createFirstAccount() {
105 AccountManager am
= AccountManager
.get(getApplicationContext());
106 am
.addAccount(AccountAuthenticator
.ACCOUNT_TYPE
,
107 AccountAuthenticator
.AUTH_TOKEN_TYPE_PASSWORD
,
111 new AccountCreationCallback(),
120 protected void onSaveInstanceState(Bundle outState
) {
121 super.onSaveInstanceState(outState
);
122 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
123 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
128 * Getter for the main {@link OCFile} handled by the activity.
130 * @return Main {@link OCFile} handled by the activity.
132 public OCFile
getFile() {
138 * Setter for the main {@link OCFile} handled by the activity.
140 * @param file Main {@link OCFile} to be handled by the activity.
142 public void setFile(OCFile file
) {
148 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
150 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
152 public Account
getAccount() {
158 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
160 protected boolean isRedirectingToSetupAccount() {
161 return mRedirectingToSetupAccount
;
166 * Helper class handling a callback from the {@link AccountManager} after the creation of
167 * a new ownCloud {@link Account} finished, successfully or not.
169 * At this moment, only called after the creation of the first account.
171 * @author David A. Velasco
173 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
176 public void run(AccountManagerFuture
<Bundle
> future
) {
177 FileActivity
.this.mRedirectingToSetupAccount
= false
;
178 if (future
!= null
) {
181 result
= future
.getResult();
182 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
183 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
184 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
185 FileActivity
.this.mAccount
= new Account(name
, type
);
186 FileActivity
.this.onAccountChanged();
188 } catch (OperationCanceledException e
) {
189 Log_OC
.e(TAG
, "Account creation canceled");
191 } catch (Exception e
) {
192 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
196 Log_OC
.e(TAG
, "Account creation callback with null bundle");
198 if (mAccount
== null
) {
207 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
209 * Child classes must grant that state depending on the {@link Account} is updated.
211 protected abstract void onAccountChanged();