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
;
29 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
30 import com
.owncloud
.android
.AccountUtils
;
31 import com
.owncloud
.android
.Log_OC
;
32 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
36 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
38 * @author David A. Velasco
40 public abstract class FileActivity
extends SherlockFragmentActivity
{
42 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
43 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
45 public static final String TAG
= FileActivity
.class.getSimpleName();
48 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
49 private Account mAccount
;
51 /** Main {@link OCFile} handled by the activity.*/
54 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
55 private boolean mRedirectingToSetupAccount
= false
;
59 public void onCreate(Bundle savedInstanceState
) {
60 super.onCreate(savedInstanceState
);
62 /// Load of saved instance state: keep this always before initDataFromCurrentAccount()
63 if(savedInstanceState
!= null
) {
64 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
65 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
67 mAccount
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
68 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
71 if (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
)) {
78 public void onStart() {
79 Log_OC
.e(TAG
, "onStart en FileActivity");
81 /// Validate account, and try to fix if wrong
82 if (mAccount
== null
|| !AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
)) {
83 if (!AccountUtils
.accountsAreSetup(getApplicationContext())) {
84 /// no account available: force account creation
87 mRedirectingToSetupAccount
= true
;
90 /// get 'last current account' as default account
91 mAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
99 * Launches the account creation activity. To use when no ownCloud account is available
101 private void createFirstAccountOldStyle() {
102 Intent intent
= new Intent(android
.provider
.Settings
.ACTION_ADD_ACCOUNT
);
103 intent
.putExtra(android
.provider
.Settings
.EXTRA_AUTHORITIES
, new String
[] { AccountAuthenticator
.AUTHORITY
});
104 startActivity(intent
);
110 private void createFirstAccount() {
111 AccountManager am
= AccountManager
.get(getApplicationContext());
112 am
.addAccount(AccountAuthenticator
.ACCOUNT_TYPE
,
113 AccountAuthenticator
.AUTH_TOKEN_TYPE_PASSWORD
,
117 new AccountCreationCallback(),
123 protected void onSaveInstanceState(Bundle outState
) {
124 super.onSaveInstanceState(outState
);
125 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
126 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
131 * Getter for the main {@link OCFile} handled by the activity.
133 * @return Main {@link OCFile} handled by the activity.
135 public OCFile
getFile() {
141 * Setter for the main {@link OCFile} handled by the activity.
143 * @param file Main {@link OCFile} to be handled by the activity.
145 public void setFile(OCFile file
) {
151 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
153 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
155 public Account
getAccount() {
161 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
163 protected boolean isRedirectingToSetupAccount() {
164 return mRedirectingToSetupAccount
;
169 * Helper class handling a callback from the {@link AccountManager} after the creation of
170 * a new ownCloud {@link Account} finished, successfully or not.
172 * At this moment, only called after the creation of the first account.
174 * @author David A. Velasco
176 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
179 public void run(AccountManagerFuture
<Bundle
> future
) {
180 FileActivity
.this.mRedirectingToSetupAccount
= false
;
181 if (future
!= null
) {
184 result
= future
.getResult();
185 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
186 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
187 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
188 FileActivity
.this.mAccount
= new Account(name
, type
);
189 FileActivity
.this.onAccountChanged();
191 } catch (OperationCanceledException e
) {
192 Log_OC
.e(TAG
, "Account creation canceled");
194 } catch (Exception e
) {
195 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
199 Log_OC
.e(TAG
, "Account creation callback with null bundle");
201 if (mAccount
== null
) {
210 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
212 * Child classes must grant that state depending on the {@link Account} is updated.
214 protected abstract void onAccountChanged();