Merge pull request #330 from LukeOwncloud/update_jackrabbit
[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.net.Uri;
28 import android.os.Bundle;
29 import android.webkit.MimeTypeMap;
30
31 import com.actionbarsherlock.app.SherlockFragmentActivity;
32 import com.owncloud.android.MainApp;
33 import com.owncloud.android.R;
34 import com.owncloud.android.authentication.AccountUtils;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
37 import com.owncloud.android.utils.Log_OC;
38
39
40 /**
41 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
42 *
43 * @author David A. Velasco
44 */
45 public abstract class FileActivity extends SherlockFragmentActivity {
46
47 public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
48 public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
49 public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
50 public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
51
52 public static final String TAG = FileActivity.class.getSimpleName();
53
54
55 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
56 private Account mAccount;
57
58 /** Main {@link OCFile} handled by the activity.*/
59 private OCFile mFile;
60
61 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
62 private boolean mRedirectingToSetupAccount = false;
63
64 /** Flag to signal when the value of mAccount was set */
65 private boolean mAccountWasSet;
66
67 /** Flag to signal when the value of mAccount was restored from a saved state */
68 private boolean mAccountWasRestored;
69
70 /** Flag to signal if the activity is launched by a notification */
71 private boolean mFromNotification;
72
73
74 /**
75 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
76 * the {@link FileActivity}.
77 *
78 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
79 * is requested to create a new one.
80 */
81 @Override
82 protected void onCreate(Bundle savedInstanceState) {
83 super.onCreate(savedInstanceState);
84
85 Account account;
86 if(savedInstanceState != null) {
87 account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
88 mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
89 mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
90 } else {
91 account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
92 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
93 mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
94 }
95
96 setAccount(account, savedInstanceState != null);
97 }
98
99
100 /**
101 * Since ownCloud {@link Account}s can be managed from the system setting menu,
102 * the existence of the {@link Account} associated to the instance must be checked
103 * every time it is restarted.
104 */
105 @Override
106 protected void onRestart() {
107 super.onRestart();
108 boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
109 if (!validAccount) {
110 swapToDefaultAccount();
111 }
112
113 }
114
115
116 @Override
117 protected void onStart() {
118 super.onStart();
119 if (mAccountWasSet) {
120 onAccountSet(mAccountWasRestored);
121 }
122 }
123
124
125 /**
126 * Sets and validates the ownCloud {@link Account} associated to the Activity.
127 *
128 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
129 *
130 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
131 *
132 * @param account New {@link Account} to set.
133 * @param savedAccount When 'true', account was retrieved from a saved instance state.
134 */
135 private void setAccount(Account account, boolean savedAccount) {
136 Account oldAccount = mAccount;
137 boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
138 if (validAccount) {
139 mAccount = account;
140 mAccountWasSet = true;
141 mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
142
143 } else {
144 swapToDefaultAccount();
145 }
146 }
147
148
149 /**
150 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
151 *
152 * If no valid ownCloud {@link Account} exists, the the user is requested
153 * to create a new ownCloud {@link Account}.
154 *
155 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
156 *
157 * @return 'True' if the checked {@link Account} was valid.
158 */
159 private void swapToDefaultAccount() {
160 // default to the most recently used account
161 Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
162 if (newAccount == null) {
163 /// no account available: force account creation
164 createFirstAccount();
165 mRedirectingToSetupAccount = true;
166 mAccountWasSet = false;
167 mAccountWasRestored = false;
168
169 } else {
170 mAccountWasSet = true;
171 mAccountWasRestored = (newAccount.equals(mAccount));
172 mAccount = newAccount;
173 }
174 }
175
176
177 /**
178 * Launches the account creation activity. To use when no ownCloud account is available
179 */
180 private void createFirstAccount() {
181 AccountManager am = AccountManager.get(getApplicationContext());
182 am.addAccount(MainApp.getAccountType(),
183 null,
184 null,
185 null,
186 this,
187 new AccountCreationCallback(),
188 null);
189 }
190
191
192 /**
193 * {@inheritDoc}
194 */
195 @Override
196 protected void onSaveInstanceState(Bundle outState) {
197 super.onSaveInstanceState(outState);
198 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
199 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
200 outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
201 }
202
203
204 /**
205 * Getter for the main {@link OCFile} handled by the activity.
206 *
207 * @return Main {@link OCFile} handled by the activity.
208 */
209 public OCFile getFile() {
210 return mFile;
211 }
212
213
214 /**
215 * Setter for the main {@link OCFile} handled by the activity.
216 *
217 * @param file Main {@link OCFile} to be handled by the activity.
218 */
219 public void setFile(OCFile file) {
220 mFile = file;
221 }
222
223
224 /**
225 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
226 *
227 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
228 */
229 public Account getAccount() {
230 return mAccount;
231 }
232
233 /**
234 * @return Value of mFromNotification: True if the Activity is launched by a notification
235 */
236 public boolean fromNotification() {
237 return mFromNotification;
238 }
239
240 /**
241 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
242 */
243 protected boolean isRedirectingToSetupAccount() {
244 return mRedirectingToSetupAccount;
245 }
246
247
248 /**
249 * Helper class handling a callback from the {@link AccountManager} after the creation of
250 * a new ownCloud {@link Account} finished, successfully or not.
251 *
252 * At this moment, only called after the creation of the first account.
253 *
254 * @author David A. Velasco
255 */
256 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
257
258 @Override
259 public void run(AccountManagerFuture<Bundle> future) {
260 FileActivity.this.mRedirectingToSetupAccount = false;
261 boolean accountWasSet = false;
262 if (future != null) {
263 try {
264 Bundle result;
265 result = future.getResult();
266 String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
267 String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
268 if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
269 setAccount(new Account(name, type), false);
270 accountWasSet = true;
271 }
272 } catch (OperationCanceledException e) {
273 Log_OC.d(TAG, "Account creation canceled");
274
275 } catch (Exception e) {
276 Log_OC.e(TAG, "Account creation finished in exception: ", e);
277 }
278
279 } else {
280 Log_OC.e(TAG, "Account creation callback with null bundle");
281 }
282 if (!accountWasSet) {
283 moveTaskToBack(true);
284 }
285 }
286
287 }
288
289
290 /**
291 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
292 *
293 * Child classes must grant that state depending on the {@link Account} is updated.
294 */
295 protected abstract void onAccountSet(boolean stateWasRecovered);
296
297
298
299 public void openFile(OCFile file) {
300 if (file != null) {
301 String storagePath = file.getStoragePath();
302 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
303
304 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
305 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
306 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
307
308 Intent intentForGuessedMimeType = null;
309 if (storagePath.lastIndexOf('.') >= 0) {
310 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
311 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
312 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
313 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
314 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
315 }
316 }
317
318 Intent chooserIntent = null;
319 if (intentForGuessedMimeType != null) {
320 chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with));
321 } else {
322 chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with));
323 }
324
325 startActivity(chooserIntent);
326
327 } else {
328 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
329 }
330 }
331
332 }