Merge branch 'develop' into share_link__unshare_file
[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 import android.os.Handler;
29 import android.support.v4.app.Fragment;
30 import android.support.v4.app.FragmentManager;
31 import android.support.v4.app.FragmentTransaction;
32 import android.widget.Toast;
33
34 import com.actionbarsherlock.app.SherlockFragmentActivity;
35 import com.owncloud.android.MainApp;
36 import com.owncloud.android.R;
37 import com.owncloud.android.authentication.AccountUtils;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.FileOperationsHelper;
41 import com.owncloud.android.lib.operations.common.OnRemoteOperationListener;
42 import com.owncloud.android.lib.operations.common.RemoteOperation;
43 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
44 import com.owncloud.android.lib.operations.common.RemoteOperationResult.ResultCode;
45 import com.owncloud.android.operations.CreateShareOperation;
46 import com.owncloud.android.operations.UnshareLinkOperation;
47
48 import com.owncloud.android.ui.dialog.LoadingDialog;
49 import com.owncloud.android.utils.Log_OC;
50
51
52 /**
53 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
54 *
55 * @author David A. Velasco
56 */
57 public class FileActivity extends SherlockFragmentActivity implements OnRemoteOperationListener {
58
59 public static final String EXTRA_FILE = "com.owncloud.android.ui.activity.FILE";
60 public static final String EXTRA_ACCOUNT = "com.owncloud.android.ui.activity.ACCOUNT";
61 public static final String EXTRA_WAITING_TO_PREVIEW = "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
62 public static final String EXTRA_FROM_NOTIFICATION= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
63
64 public static final String TAG = FileActivity.class.getSimpleName();
65
66 private static final String DIALOG_WAIT_TAG = "DIALOG_WAIT";
67
68
69 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
70 private Account mAccount;
71
72 /** Main {@link OCFile} handled by the activity.*/
73 private OCFile mFile;
74
75 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
76 private boolean mRedirectingToSetupAccount = false;
77
78 /** Flag to signal when the value of mAccount was set */
79 private boolean mAccountWasSet;
80
81 /** Flag to signal when the value of mAccount was restored from a saved state */
82 private boolean mAccountWasRestored;
83
84 /** Flag to signal if the activity is launched by a notification */
85 private boolean mFromNotification;
86
87 /** Messages handler associated to the main thread and the life cycle of the activity */
88 private Handler mHandler;
89
90 /** Access point to the cached database for the current ownCloud {@link Account} */
91 private FileDataStorageManager mStorageManager = null;
92
93 private FileOperationsHelper mFileOperationsHelper;
94
95
96 /**
97 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
98 * the {@link FileActivity}.
99 *
100 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
101 * is requested to create a new one.
102 */
103 @Override
104 protected void onCreate(Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
106 mHandler = new Handler();
107 mFileOperationsHelper = new FileOperationsHelper();
108 Account account;
109 if(savedInstanceState != null) {
110 account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
111 mFile = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
112 mFromNotification = savedInstanceState.getBoolean(FileActivity.EXTRA_FROM_NOTIFICATION);
113 } else {
114 account = getIntent().getParcelableExtra(FileActivity.EXTRA_ACCOUNT);
115 mFile = getIntent().getParcelableExtra(FileActivity.EXTRA_FILE);
116 mFromNotification = getIntent().getBooleanExtra(FileActivity.EXTRA_FROM_NOTIFICATION, false);
117 }
118
119 setAccount(account, savedInstanceState != null);
120
121 }
122
123
124 /**
125 * Since ownCloud {@link Account}s can be managed from the system setting menu,
126 * the existence of the {@link Account} associated to the instance must be checked
127 * every time it is restarted.
128 */
129 @Override
130 protected void onRestart() {
131 super.onRestart();
132 boolean validAccount = (mAccount != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), mAccount.name));
133 if (!validAccount) {
134 swapToDefaultAccount();
135 }
136
137 }
138
139
140 @Override
141 protected void onStart() {
142 super.onStart();
143 if (mAccountWasSet) {
144 onAccountSet(mAccountWasRestored);
145 }
146 }
147
148
149 /**
150 * Sets and validates the ownCloud {@link Account} associated to the Activity.
151 *
152 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
153 *
154 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
155 *
156 * @param account New {@link Account} to set.
157 * @param savedAccount When 'true', account was retrieved from a saved instance state.
158 */
159 private void setAccount(Account account, boolean savedAccount) {
160 Account oldAccount = mAccount;
161 boolean validAccount = (account != null && AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), account.name));
162 if (validAccount) {
163 mAccount = account;
164 mAccountWasSet = true;
165 mAccountWasRestored = (savedAccount || mAccount.equals(oldAccount));
166
167 } else {
168 swapToDefaultAccount();
169 }
170 }
171
172
173 /**
174 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
175 *
176 * If no valid ownCloud {@link Account} exists, the the user is requested
177 * to create a new ownCloud {@link Account}.
178 *
179 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
180 *
181 * @return 'True' if the checked {@link Account} was valid.
182 */
183 private void swapToDefaultAccount() {
184 // default to the most recently used account
185 Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
186 if (newAccount == null) {
187 /// no account available: force account creation
188 createFirstAccount();
189 mRedirectingToSetupAccount = true;
190 mAccountWasSet = false;
191 mAccountWasRestored = false;
192
193 } else {
194 mAccountWasSet = true;
195 mAccountWasRestored = (newAccount.equals(mAccount));
196 mAccount = newAccount;
197 }
198 }
199
200
201 /**
202 * Launches the account creation activity. To use when no ownCloud account is available
203 */
204 private void createFirstAccount() {
205 AccountManager am = AccountManager.get(getApplicationContext());
206 am.addAccount(MainApp.getAccountType(),
207 null,
208 null,
209 null,
210 this,
211 new AccountCreationCallback(),
212 null);
213 }
214
215
216 /**
217 * {@inheritDoc}
218 */
219 @Override
220 protected void onSaveInstanceState(Bundle outState) {
221 super.onSaveInstanceState(outState);
222 outState.putParcelable(FileActivity.EXTRA_FILE, mFile);
223 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
224 outState.putBoolean(FileActivity.EXTRA_FROM_NOTIFICATION, mFromNotification);
225 }
226
227
228 /**
229 * Getter for the main {@link OCFile} handled by the activity.
230 *
231 * @return Main {@link OCFile} handled by the activity.
232 */
233 public OCFile getFile() {
234 return mFile;
235 }
236
237
238 /**
239 * Setter for the main {@link OCFile} handled by the activity.
240 *
241 * @param file Main {@link OCFile} to be handled by the activity.
242 */
243 public void setFile(OCFile file) {
244 mFile = file;
245 }
246
247
248 /**
249 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
250 *
251 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
252 */
253 public Account getAccount() {
254 return mAccount;
255 }
256
257 /**
258 * @return Value of mFromNotification: True if the Activity is launched by a notification
259 */
260 public boolean fromNotification() {
261 return mFromNotification;
262 }
263
264 /**
265 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
266 */
267 protected boolean isRedirectingToSetupAccount() {
268 return mRedirectingToSetupAccount;
269 }
270
271
272 /**
273 * Helper class handling a callback from the {@link AccountManager} after the creation of
274 * a new ownCloud {@link Account} finished, successfully or not.
275 *
276 * At this moment, only called after the creation of the first account.
277 *
278 * @author David A. Velasco
279 */
280 public class AccountCreationCallback implements AccountManagerCallback<Bundle> {
281
282 @Override
283 public void run(AccountManagerFuture<Bundle> future) {
284 FileActivity.this.mRedirectingToSetupAccount = false;
285 boolean accountWasSet = false;
286 if (future != null) {
287 try {
288 Bundle result;
289 result = future.getResult();
290 String name = result.getString(AccountManager.KEY_ACCOUNT_NAME);
291 String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE);
292 if (AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), name)) {
293 setAccount(new Account(name, type), false);
294 accountWasSet = true;
295 }
296 } catch (OperationCanceledException e) {
297 Log_OC.d(TAG, "Account creation canceled");
298
299 } catch (Exception e) {
300 Log_OC.e(TAG, "Account creation finished in exception: ", e);
301 }
302
303 } else {
304 Log_OC.e(TAG, "Account creation callback with null bundle");
305 }
306 if (!accountWasSet) {
307 moveTaskToBack(true);
308 }
309 }
310
311 }
312
313
314 /**
315 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
316 *
317 * Child classes must grant that state depending on the {@link Account} is updated.
318 */
319 protected void onAccountSet(boolean stateWasRecovered) {
320 if (getAccount() != null) {
321 mStorageManager = new FileDataStorageManager(getAccount(), getContentResolver());
322
323 } else {
324 Log_OC.wtf(TAG, "onAccountChanged was called with NULL account associated!");
325 }
326 }
327
328
329 public FileDataStorageManager getStorageManager() {
330 return mStorageManager;
331 }
332
333
334 public OnRemoteOperationListener getRemoteOperationListener() {
335 return this;
336 }
337
338
339 public Handler getHandler() {
340 return mHandler;
341 }
342
343 public FileOperationsHelper getFileOperationsHelper() {
344 return mFileOperationsHelper;
345 }
346
347 /**
348 *
349 * @param operation Removal operation performed.
350 * @param result Result of the removal.
351 */
352 @Override
353 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
354 Log_OC.d(TAG, "Received result of operation in FileActivity - common behaviour for all the FileActivities ");
355 if (operation instanceof CreateShareOperation) {
356 onCreateShareOperationFinish((CreateShareOperation) operation, result);
357
358 } else if (operation instanceof UnshareLinkOperation) {
359 onUnshareLinkOperationFinish((UnshareLinkOperation)operation, result);
360
361 }
362 }
363
364 private void onCreateShareOperationFinish(CreateShareOperation operation, RemoteOperationResult result) {
365 dismissLoadingDialog();
366 if (result.isSuccess()) {
367 Intent sendIntent = operation.getSendIntent();
368 startActivity(sendIntent);
369
370 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
371 Toast t = Toast.makeText(this, getString(R.string.share_link_file_no_exist), Toast.LENGTH_LONG);
372 t.show();
373 } else { // Generic error
374 // Show a Message, operation finished without success
375 Toast t = Toast.makeText(this, getString(R.string.share_link_file_error), Toast.LENGTH_LONG);
376 t.show();
377 }
378 }
379
380
381 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation, RemoteOperationResult result) {
382 dismissLoadingDialog();
383
384 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
385 Toast t = Toast.makeText(this, getString(R.string.unshare_link_file_no_exist), Toast.LENGTH_LONG);
386 t.show();
387 } else if (!result.isSuccess()){ // Generic error
388 // Show a Message, operation finished without success
389 Toast t = Toast.makeText(this, getString(R.string.unshare_link_file_error), Toast.LENGTH_LONG);
390 t.show();
391 }
392
393 }
394
395 /**
396 * Show loading dialog
397 */
398 public void showLoadingDialog() {
399 // Construct dialog
400 LoadingDialog loading = new LoadingDialog(getResources().getString(R.string.wait_a_moment));
401 FragmentManager fm = getSupportFragmentManager();
402 FragmentTransaction ft = fm.beginTransaction();
403 loading.show(ft, DIALOG_WAIT_TAG);
404
405 }
406
407
408 /**
409 * Dismiss loading dialog
410 */
411 public void dismissLoadingDialog(){
412 Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG);
413 if (frag != null) {
414 LoadingDialog loading = (LoadingDialog) frag;
415 loading.dismiss();
416 }
417 }
418
419 }