1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
.activity
;
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
;
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
;
48 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
49 import com
.owncloud
.android
.utils
.Log_OC
;
53 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
55 * @author David A. Velasco
57 public class FileActivity
extends SherlockFragmentActivity
implements OnRemoteOperationListener
{
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";
64 public static final String TAG
= FileActivity
.class.getSimpleName();
66 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
69 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
70 private Account mAccount
;
72 /** Main {@link OCFile} handled by the activity.*/
75 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
76 private boolean mRedirectingToSetupAccount
= false
;
78 /** Flag to signal when the value of mAccount was set */
79 private boolean mAccountWasSet
;
81 /** Flag to signal when the value of mAccount was restored from a saved state */
82 private boolean mAccountWasRestored
;
84 /** Flag to signal if the activity is launched by a notification */
85 private boolean mFromNotification
;
87 /** Messages handler associated to the main thread and the life cycle of the activity */
88 private Handler mHandler
;
90 /** Access point to the cached database for the current ownCloud {@link Account} */
91 private FileDataStorageManager mStorageManager
= null
;
93 private FileOperationsHelper mFileOperationsHelper
;
97 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
98 * the {@link FileActivity}.
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.
104 protected void onCreate(Bundle savedInstanceState
) {
105 super.onCreate(savedInstanceState
);
106 mHandler
= new Handler();
107 mFileOperationsHelper
= new FileOperationsHelper();
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
);
114 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
115 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
116 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
119 setAccount(account
, savedInstanceState
!= null
);
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.
130 protected void onRestart() {
132 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
134 swapToDefaultAccount();
141 protected void onStart() {
143 if (mAccountWasSet
) {
144 onAccountSet(mAccountWasRestored
);
150 * Sets and validates the ownCloud {@link Account} associated to the Activity.
152 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
154 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
156 * @param account New {@link Account} to set.
157 * @param savedAccount When 'true', account was retrieved from a saved instance state.
159 private void setAccount(Account account
, boolean savedAccount
) {
160 Account oldAccount
= mAccount
;
161 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
164 mAccountWasSet
= true
;
165 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
168 swapToDefaultAccount();
174 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
176 * If no valid ownCloud {@link Account} exists, the the user is requested
177 * to create a new ownCloud {@link Account}.
179 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
181 * @return 'True' if the checked {@link Account} was valid.
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
;
194 mAccountWasSet
= true
;
195 mAccountWasRestored
= (newAccount
.equals(mAccount
));
196 mAccount
= newAccount
;
202 * Launches the account creation activity. To use when no ownCloud account is available
204 private void createFirstAccount() {
205 AccountManager am
= AccountManager
.get(getApplicationContext());
206 am
.addAccount(MainApp
.getAccountType(),
211 new AccountCreationCallback(),
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
);
229 * Getter for the main {@link OCFile} handled by the activity.
231 * @return Main {@link OCFile} handled by the activity.
233 public OCFile
getFile() {
239 * Setter for the main {@link OCFile} handled by the activity.
241 * @param file Main {@link OCFile} to be handled by the activity.
243 public void setFile(OCFile file
) {
249 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
251 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
253 public Account
getAccount() {
258 * @return Value of mFromNotification: True if the Activity is launched by a notification
260 public boolean fromNotification() {
261 return mFromNotification
;
265 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
267 protected boolean isRedirectingToSetupAccount() {
268 return mRedirectingToSetupAccount
;
273 * Helper class handling a callback from the {@link AccountManager} after the creation of
274 * a new ownCloud {@link Account} finished, successfully or not.
276 * At this moment, only called after the creation of the first account.
278 * @author David A. Velasco
280 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
283 public void run(AccountManagerFuture
<Bundle
> future
) {
284 FileActivity
.this.mRedirectingToSetupAccount
= false
;
285 boolean accountWasSet
= false
;
286 if (future
!= null
) {
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
;
296 } catch (OperationCanceledException e
) {
297 Log_OC
.d(TAG
, "Account creation canceled");
299 } catch (Exception e
) {
300 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
304 Log_OC
.e(TAG
, "Account creation callback with null bundle");
306 if (!accountWasSet
) {
307 moveTaskToBack(true
);
315 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
317 * Child classes must grant that state depending on the {@link Account} is updated.
319 protected void onAccountSet(boolean stateWasRecovered
) {
320 if (getAccount() != null
) {
321 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
324 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
329 public FileDataStorageManager
getStorageManager() {
330 return mStorageManager
;
334 public OnRemoteOperationListener
getRemoteOperationListener() {
339 public Handler
getHandler() {
343 public FileOperationsHelper
getFileOperationsHelper() {
344 return mFileOperationsHelper
;
349 * @param operation Removal operation performed.
350 * @param result Result of the removal.
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
);
358 } else if (operation
instanceof UnshareLinkOperation
) {
359 onUnshareLinkOperationFinish((UnshareLinkOperation
)operation
, result
);
364 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
365 dismissLoadingDialog();
366 if (result
.isSuccess()) {
367 Intent sendIntent
= operation
.getSendIntent();
368 startActivity(sendIntent
);
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
);
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
);
381 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
382 dismissLoadingDialog();
384 if (!result
.isSuccess()){ // Generic error
385 // Show a Message, operation finished without success
386 Toast t
= Toast
.makeText(this, getString(R
.string
.unshare_link_file_error
), Toast
.LENGTH_LONG
);
393 * Show loading dialog
395 public void showLoadingDialog() {
397 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
398 FragmentManager fm
= getSupportFragmentManager();
399 FragmentTransaction ft
= fm
.beginTransaction();
400 loading
.show(ft
, DIALOG_WAIT_TAG
);
406 * Dismiss loading dialog
408 public void dismissLoadingDialog(){
409 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
411 LoadingDialog loading
= (LoadingDialog
) frag
;