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
.R
;
34 import com
.owncloud
.android
.authentication
.AccountAuthenticator
;
35 import com
.owncloud
.android
.authentication
.AccountUtils
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import eu
.alefzero
.webdav
.WebdavUtils
;
41 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
43 * @author David A. Velasco
45 public abstract class FileActivity
extends SherlockFragmentActivity
{
47 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
48 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
49 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
51 public static final String TAG
= FileActivity
.class.getSimpleName();
54 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
55 private Account mAccount
;
57 /** Main {@link OCFile} handled by the activity.*/
60 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
61 private boolean mRedirectingToSetupAccount
= false
;
63 /** Flag to signal when the value of mAccount was set */
64 private boolean mAccountWasSet
;
66 /** Flag to signal when the value of mAccount was restored from a saved state */
67 private boolean mAccountWasRestored
;
71 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
72 * the {@link FileActivity}.
74 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
75 * is requested to create a new one.
78 protected void onCreate(Bundle savedInstanceState
) {
79 super.onCreate(savedInstanceState
);
82 if(savedInstanceState
!= null
) {
83 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
84 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
86 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
87 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
90 setAccount(account
, savedInstanceState
!= null
);
95 * Since ownCloud {@link Account}s can be managed from the system setting menu,
96 * the existence of the {@link Account} associated to the instance must be checked
97 * every time it is restarted.
100 protected void onRestart() {
102 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
104 swapToDefaultAccount();
111 protected void onStart() {
113 if (mAccountWasSet
) {
114 onAccountSet(mAccountWasRestored
);
120 * Sets and validates the ownCloud {@link Account} associated to the Activity.
122 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
124 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
126 * @param account New {@link Account} to set.
127 * @param savedAccount When 'true', account was retrieved from a saved instance state.
129 private void setAccount(Account account
, boolean savedAccount
) {
130 Account oldAccount
= mAccount
;
131 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
134 mAccountWasSet
= true
;
135 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
138 swapToDefaultAccount();
144 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
146 * If no valid ownCloud {@link Account} exists, the the user is requested
147 * to create a new ownCloud {@link Account}.
149 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
151 * @return 'True' if the checked {@link Account} was valid.
153 private void swapToDefaultAccount() {
154 // default to the most recently used account
155 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
156 if (newAccount
== null
) {
157 /// no account available: force account creation
158 createFirstAccount();
159 mRedirectingToSetupAccount
= true
;
160 mAccountWasSet
= false
;
161 mAccountWasRestored
= false
;
164 mAccountWasSet
= true
;
165 mAccountWasRestored
= (newAccount
.equals(mAccount
));
166 mAccount
= newAccount
;
172 * Launches the account creation activity. To use when no ownCloud account is available
174 private void createFirstAccount() {
175 AccountManager am
= AccountManager
.get(getApplicationContext());
176 am
.addAccount(AccountAuthenticator
.ACCOUNT_TYPE
,
181 new AccountCreationCallback(),
190 protected void onSaveInstanceState(Bundle outState
) {
191 super.onSaveInstanceState(outState
);
192 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
193 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
198 * Getter for the main {@link OCFile} handled by the activity.
200 * @return Main {@link OCFile} handled by the activity.
202 public OCFile
getFile() {
208 * Setter for the main {@link OCFile} handled by the activity.
210 * @param file Main {@link OCFile} to be handled by the activity.
212 public void setFile(OCFile file
) {
218 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
220 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
222 public Account
getAccount() {
228 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
230 protected boolean isRedirectingToSetupAccount() {
231 return mRedirectingToSetupAccount
;
236 * Helper class handling a callback from the {@link AccountManager} after the creation of
237 * a new ownCloud {@link Account} finished, successfully or not.
239 * At this moment, only called after the creation of the first account.
241 * @author David A. Velasco
243 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
246 public void run(AccountManagerFuture
<Bundle
> future
) {
247 FileActivity
.this.mRedirectingToSetupAccount
= false
;
248 boolean accountWasSet
= false
;
249 if (future
!= null
) {
252 result
= future
.getResult();
253 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
254 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
255 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
256 setAccount(new Account(name
, type
), false
);
257 accountWasSet
= true
;
259 } catch (OperationCanceledException e
) {
260 Log_OC
.d(TAG
, "Account creation canceled");
262 } catch (Exception e
) {
263 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
267 Log_OC
.e(TAG
, "Account creation callback with null bundle");
269 if (!accountWasSet
) {
270 moveTaskToBack(true
);
278 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
280 * Child classes must grant that state depending on the {@link Account} is updated.
282 protected abstract void onAccountSet(boolean stateWasRecovered
);
286 public void openFile(OCFile file
) {
288 String storagePath
= file
.getStoragePath();
289 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
291 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
292 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
293 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
295 Intent intentForGuessedMimeType
= null
;
296 if (storagePath
.lastIndexOf('.') >= 0) {
297 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
298 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
299 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
300 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
301 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
305 Intent chooserIntent
= null
;
306 if (intentForGuessedMimeType
!= null
) {
307 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
309 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
312 startActivity(chooserIntent
);
315 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");