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