Server or connection errors in authorization require handled as in server check
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.ui.activity;
20
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;
27
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;
33
34 /**
35 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
36 *
37 * @author David A. Velasco
38 */
39 public abstract class FileActivity extends SherlockFragmentActivity {
40
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";
43
44 public static final String TAG = FileActivity.class.getSimpleName();
45
46
47 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
48 private Account mAccount;
49
50 /** Main {@link OCFile} handled by the activity.*/
51 private OCFile mFile;
52
53 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
54 private boolean mRedirectingToSetupAccount = false;
55
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60
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);
65 } else {
66 mAccount = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
67 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
68 }
69
70 if (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name)) {
71 onAccountChanged();
72 }
73 }
74
75
76 /**
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.
79 */
80 @Override
81 protected void onStart() {
82 Log_OC.e(TAG, "onStart en FileActivity");
83 super.onStart();
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
88 mAccount = null;
89 createFirstAccount();
90 mRedirectingToSetupAccount = true;
91
92 } else {
93 /// get 'last current account' as default account
94 mAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
95 onAccountChanged();
96 }
97 }
98 }
99
100
101 /**
102 * Launches the account creation activity. To use when no ownCloud account is available
103 */
104 private void createFirstAccount() {
105 AccountManager am = AccountManager.get(getApplicationContext());
106 am.addAccount(AccountAuthenticator.ACCOUNT_TYPE,
107 AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD,
108 null,
109 null,
110 this,
111 new AccountCreationCallback(),
112 null);
113 }
114
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 protected void onSaveInstanceState(Bundle outState) {
121 super.onSaveInstanceState(outState);
122 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
123 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
124 }
125
126
127 /**
128 * Getter for the main {@link OCFile} handled by the activity.
129 *
130 * @return Main {@link OCFile} handled by the activity.
131 */
132 public OCFile getFile() {
133 return mFile;
134 }
135
136
137 /**
138 * Setter for the main {@link OCFile} handled by the activity.
139 *
140 * @param file Main {@link OCFile} to be handled by the activity.
141 */
142 public void setFile(OCFile file) {
143 mFile = file;
144 }
145
146
147 /**
148 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
149 *
150 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
151 */
152 public Account getAccount() {
153 return mAccount;
154 }
155
156
157 /**
158 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
159 */
160 protected boolean isRedirectingToSetupAccount() {
161 return mRedirectingToSetupAccount;
162 }
163
164
165 /**
166 * Helper class handling a callback from the {@link AccountManager} after the creation of
167 * a new ownCloud {@link Account} finished, successfully or not.
168 *
169 * At this moment, only called after the creation of the first account.
170 *
171 * @author David A. Velasco
172 */
173 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
174
175 @Override
176 public void run(AccountManagerFuture<Bundle> future) {
177 FileActivity.this.mRedirectingToSetupAccount = false;
178 if (future != null) {
179 try {
180 Bundle result;
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();
187 }
188 } catch (OperationCanceledException e) {
189 Log_OC.e(TAG, "Account creation canceled");
190
191 } catch (Exception e) {
192 Log_OC.e(TAG, "Account creation finished in exception: ", e);
193 }
194
195 } else {
196 Log_OC.e(TAG, "Account creation callback with null bundle");
197 }
198 if (mAccount == null) {
199 finish();
200 }
201 }
202
203 }
204
205
206 /**
207 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
208 *
209 * Child classes must grant that state depending on the {@link Account} is updated.
210 */
211 protected abstract void onAccountChanged();
212 }