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