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