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