OC-2489: Add GetRemoteSharedFilesOperation
[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
91 Account account;
92 if(savedInstanceState != null) {
93 account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
94 mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
95 mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
96 } else {
97 account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
98 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
99 mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
100 }
101
102 setAccount(account, savedInstanceState != null);
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
154
155 /**
156 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
157 *
158 * If no valid ownCloud {@link Account} exists, the the user is requested
159 * to create a new ownCloud {@link Account}.
160 *
161 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
162 *
163 * @return 'True' if the checked {@link Account} was valid.
164 */
165 private void swapToDefaultAccount() {
166 // default to the most recently used account
167 AccountManager accountManager = AccountManager.get(this);
168 Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
169 if (newAccount == null) {
170 /// no account available: force account creation
171 createFirstAccount();
172 mRedirectingToSetupAccount = true;
173 mAccountWasSet = false;
174 mAccountWasRestored = false;
175
176 } else {
177 mAccountWasSet = true;
178 mAccountWasRestored = (newAccount.equals(mAccount));
179 mAccount = newAccount;
180 }
181 setIsSharedSupported( Boolean.getBoolean(accountManager.getUserData(mAccount, OwnCloudAccount.Constants.KEY_SUPPORTS_SHARE_API)));
182 }
183
184
185 /**
186 * Launches the account creation activity. To use when no ownCloud account is available
187 */
188 private void createFirstAccount() {
189 AccountManager am = AccountManager.get(getApplicationContext());
190 am.addAccount(MainApp.getAccountType(),
191 null,
192 null,
193 null,
194 this,
195 new AccountCreationCallback(),
196 null);
197 }
198
199
200 /**
201 * {@inheritDoc}
202 */
203 @Override
204 protected void onSaveInstanceState(Bundle outState) {
205 super.onSaveInstanceState(outState);
206 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
207 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
208 outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
209 }
210
211
212 /**
213 * Getter for the main {@link OCFile} handled by the activity.
214 *
215 * @return Main {@link OCFile} handled by the activity.
216 */
217 public OCFile getFile() {
218 return mFile;
219 }
220
221
222 /**
223 * Setter for the main {@link OCFile} handled by the activity.
224 *
225 * @param file Main {@link OCFile} to be handled by the activity.
226 */
227 public void setFile(OCFile file) {
228 mFile = file;
229 }
230
231
232 /**
233 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
234 *
235 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
236 */
237 public Account getAccount() {
238 return mAccount;
239 }
240
241 /**
242 * @return Value of mFromNotification: True if the Activity is launched by a notification
243 */
244 public boolean fromNotification() {
245 return mFromNotification;
246 }
247
248 /**
249 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
250 */
251 protected boolean isRedirectingToSetupAccount() {
252 return mRedirectingToSetupAccount;
253 }
254
255
256 /**
257 * @return 'True' if the server supports the Share API
258 */
259 public boolean isSharedSupported() {
260 return mIsSharedSupported;
261 }
262
263
264 public void setIsSharedSupported(boolean mIsSharedSupported) {
265 this.mIsSharedSupported = mIsSharedSupported;
266 }
267
268 /**
269 * Helper class handling a callback from the {@link AccountManager} after the creation of
270 * a new ownCloud {@link Account} finished, successfully or not.
271 *
272 * At this moment, only called after the creation of the first account.
273 *
274 * @author David A. Velasco
275 */
276 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
277
278 @Override
279 public void run(AccountManagerFuture<Bundle> future) {
280 FileActivity.this.mRedirectingToSetupAccount = false;
281 boolean accountWasSet = false;
282 if (future != null) {
283 try {
284 Bundle result;
285 result = future.getResult();
286 String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
287 String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
288 if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
289 setAccount(new Account(name, type), false);
290 accountWasSet = true;
291 }
292 } catch (OperationCanceledException e) {
293 Log_OC.d(TAG, "Account creation canceled");
294
295 } catch (Exception e) {
296 Log_OC.e(TAG, "Account creation finished in exception: ", e);
297 }
298
299 } else {
300 Log_OC.e(TAG, "Account creation callback with null bundle");
301 }
302 if (!accountWasSet) {
303 moveTaskToBack(true);
304 }
305 }
306
307 }
308
309
310 /**
311 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
312 *
313 * Child classes must grant that state depending on the {@link Account} is updated.
314 */
315 protected abstract void onAccountSet(boolean stateWasRecovered);
316
317
318
319 public void openFile(OCFile file) {
320 if (file != null) {
321 String storagePath = file.getStoragePath();
322 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
323
324 Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
325 intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
326 intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
327
328 Intent intentForGuessedMimeType = null;
329 if (storagePath.lastIndexOf('.') >= 0) {
330 String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
331 if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
332 intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
333 intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
334 intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
335 }
336 }
337
338 Intent chooserIntent = null;
339 if (intentForGuessedMimeType != null) {
340 chooserIntent = Intent.createChooser(intentForGuessedMimeType, getString(R.string.actionbar_open_with));
341 } else {
342 chooserIntent = Intent.createChooser(intentForSavedMimeType, getString(R.string.actionbar_open_with));
343 }
344
345 startActivity(chooserIntent);
346
347 } else {
348 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
349 }
350 }
351
352 }