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
.net
.Uri
;
28 import android
.os
.Bundle
;
29 import android
.webkit
.MimeTypeMap
;
31 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
32 import com
.owncloud
.android
.Log_OC
;
33 import com
.owncloud
.android
.MainApp
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.authentication
.AccountUtils
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import eu
.alefzero
.webdav
.WebdavUtils
;
42 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
44 * @author David A. Velasco
46 public abstract class FileActivity
extends SherlockFragmentActivity
{
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";
52 public static final String TAG
= FileActivity
.class.getSimpleName();
55 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
56 private Account mAccount
;
58 /** Main {@link OCFile} handled by the activity.*/
61 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
62 private boolean mRedirectingToSetupAccount
= false
;
64 /** Flag to signal when the value of mAccount was set */
65 private boolean mAccountWasSet
;
67 /** Flag to signal when the value of mAccount was restored from a saved state */
68 private boolean mAccountWasRestored
;
72 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
73 * the {@link FileActivity}.
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.
79 protected void onCreate(Bundle savedInstanceState
) {
80 super.onCreate(savedInstanceState
);
83 if(savedInstanceState
!= null
) {
84 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
85 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
87 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
88 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
91 setAccount(account
, savedInstanceState
!= null
);
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.
101 protected void onRestart() {
103 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
105 swapToDefaultAccount();
112 protected void onStart() {
114 if (mAccountWasSet
) {
115 onAccountSet(mAccountWasRestored
);
121 * Sets and validates the ownCloud {@link Account} associated to the Activity.
123 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
125 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
127 * @param account New {@link Account} to set.
128 * @param savedAccount When 'true', account was retrieved from a saved instance state.
130 private void setAccount(Account account
, boolean savedAccount
) {
131 Account oldAccount
= mAccount
;
132 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
135 mAccountWasSet
= true
;
136 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
139 swapToDefaultAccount();
145 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
147 * If no valid ownCloud {@link Account} exists, the the user is requested
148 * to create a new ownCloud {@link Account}.
150 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
152 * @return 'True' if the checked {@link Account} was valid.
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
;
165 mAccountWasSet
= true
;
166 mAccountWasRestored
= (newAccount
.equals(mAccount
));
167 mAccount
= newAccount
;
173 * Launches the account creation activity. To use when no ownCloud account is available
175 private void createFirstAccount() {
176 AccountManager am
= AccountManager
.get(getApplicationContext());
177 am
.addAccount(MainApp
.getAccountType(),
182 new AccountCreationCallback(),
191 protected void onSaveInstanceState(Bundle outState
) {
192 super.onSaveInstanceState(outState
);
193 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
194 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
199 * Getter for the main {@link OCFile} handled by the activity.
201 * @return Main {@link OCFile} handled by the activity.
203 public OCFile
getFile() {
209 * Setter for the main {@link OCFile} handled by the activity.
211 * @param file Main {@link OCFile} to be handled by the activity.
213 public void setFile(OCFile file
) {
219 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
221 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
223 public Account
getAccount() {
229 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
231 protected boolean isRedirectingToSetupAccount() {
232 return mRedirectingToSetupAccount
;
237 * Helper class handling a callback from the {@link AccountManager} after the creation of
238 * a new ownCloud {@link Account} finished, successfully or not.
240 * At this moment, only called after the creation of the first account.
242 * @author David A. Velasco
244 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
247 public void run(AccountManagerFuture
<Bundle
> future
) {
248 FileActivity
.this.mRedirectingToSetupAccount
= false
;
249 boolean accountWasSet
= false
;
250 if (future
!= null
) {
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
;
260 } catch (OperationCanceledException e
) {
261 Log_OC
.d(TAG
, "Account creation canceled");
263 } catch (Exception e
) {
264 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
268 Log_OC
.e(TAG
, "Account creation callback with null bundle");
270 if (!accountWasSet
) {
271 moveTaskToBack(true
);
279 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
281 * Child classes must grant that state depending on the {@link Account} is updated.
283 protected abstract void onAccountSet(boolean stateWasRecovered
);
287 public void openFile(OCFile file
) {
289 String storagePath
= file
.getStoragePath();
290 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
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
);
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
);
306 Intent chooserIntent
= null
;
307 if (intentForGuessedMimeType
!= null
) {
308 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
310 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
313 startActivity(chooserIntent
);
316 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");