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
.MainApp
;
33 import com
.owncloud
.android
.R
;
34 import com
.owncloud
.android
.authentication
.AccountUtils
;
35 import com
.owncloud
.android
.datamodel
.OCFile
;
36 import com
.owncloud
.android
.oc_framework
.accounts
.OwnCloudAccount
;
37 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavUtils
;
38 import com
.owncloud
.android
.utils
.Log_OC
;
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";
51 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
53 public static final String TAG
= FileActivity
.class.getSimpleName();
56 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
57 private Account mAccount
;
59 /** Main {@link OCFile} handled by the activity.*/
62 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
63 private boolean mRedirectingToSetupAccount
= false
;
65 /** Flag to signal when the value of mAccount was set */
66 private boolean mAccountWasSet
;
68 /** Flag to signal when the value of mAccount was restored from a saved state */
69 private boolean mAccountWasRestored
;
71 /** Flag to signal if the activity is launched by a notification */
72 private boolean mFromNotification
;
77 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
78 * the {@link FileActivity}.
80 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
81 * is requested to create a new one.
84 protected void onCreate(Bundle savedInstanceState
) {
85 super.onCreate(savedInstanceState
);
87 if(savedInstanceState
!= null
) {
88 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
89 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
90 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
92 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
93 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
94 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
97 setAccount(account
, savedInstanceState
!= null
);
103 * Since ownCloud {@link Account}s can be managed from the system setting menu,
104 * the existence of the {@link Account} associated to the instance must be checked
105 * every time it is restarted.
108 protected void onRestart() {
110 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
112 swapToDefaultAccount();
119 protected void onStart() {
121 if (mAccountWasSet
) {
122 onAccountSet(mAccountWasRestored
);
128 * Sets and validates the ownCloud {@link Account} associated to the Activity.
130 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
132 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
134 * @param account New {@link Account} to set.
135 * @param savedAccount When 'true', account was retrieved from a saved instance state.
137 private void setAccount(Account account
, boolean savedAccount
) {
138 Account oldAccount
= mAccount
;
139 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
142 mAccountWasSet
= true
;
143 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
146 swapToDefaultAccount();
152 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
154 * If no valid ownCloud {@link Account} exists, the the user is requested
155 * to create a new ownCloud {@link Account}.
157 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
159 * @return 'True' if the checked {@link Account} was valid.
161 private void swapToDefaultAccount() {
162 // default to the most recently used account
163 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
164 if (newAccount
== null
) {
165 /// no account available: force account creation
166 createFirstAccount();
167 mRedirectingToSetupAccount
= true
;
168 mAccountWasSet
= false
;
169 mAccountWasRestored
= false
;
172 mAccountWasSet
= true
;
173 mAccountWasRestored
= (newAccount
.equals(mAccount
));
174 mAccount
= newAccount
;
180 * Launches the account creation activity. To use when no ownCloud account is available
182 private void createFirstAccount() {
183 AccountManager am
= AccountManager
.get(getApplicationContext());
184 am
.addAccount(MainApp
.getAccountType(),
189 new AccountCreationCallback(),
198 protected void onSaveInstanceState(Bundle outState
) {
199 super.onSaveInstanceState(outState
);
200 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
201 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
202 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
207 * Getter for the main {@link OCFile} handled by the activity.
209 * @return Main {@link OCFile} handled by the activity.
211 public OCFile
getFile() {
217 * Setter for the main {@link OCFile} handled by the activity.
219 * @param file Main {@link OCFile} to be handled by the activity.
221 public void setFile(OCFile file
) {
227 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
229 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
231 public Account
getAccount() {
236 * @return Value of mFromNotification: True if the Activity is launched by a notification
238 public boolean fromNotification() {
239 return mFromNotification
;
243 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
245 protected boolean isRedirectingToSetupAccount() {
246 return mRedirectingToSetupAccount
;
251 * @return 'True' if the server supports the Share API
253 public boolean isSharedSupported() {
254 if (getAccount() != null
) {
255 AccountManager accountManager
= AccountManager
.get(this);
256 return Boolean
.parseBoolean(accountManager
.getUserData(getAccount(), OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
));
262 * Helper class handling a callback from the {@link AccountManager} after the creation of
263 * a new ownCloud {@link Account} finished, successfully or not.
265 * At this moment, only called after the creation of the first account.
267 * @author David A. Velasco
269 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
272 public void run(AccountManagerFuture
<Bundle
> future
) {
273 FileActivity
.this.mRedirectingToSetupAccount
= false
;
274 boolean accountWasSet
= false
;
275 if (future
!= null
) {
278 result
= future
.getResult();
279 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
280 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
281 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
282 setAccount(new Account(name
, type
), false
);
283 accountWasSet
= true
;
285 } catch (OperationCanceledException e
) {
286 Log_OC
.d(TAG
, "Account creation canceled");
288 } catch (Exception e
) {
289 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
293 Log_OC
.e(TAG
, "Account creation callback with null bundle");
295 if (!accountWasSet
) {
296 moveTaskToBack(true
);
304 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
306 * Child classes must grant that state depending on the {@link Account} is updated.
308 protected abstract void onAccountSet(boolean stateWasRecovered
);
312 public void openFile(OCFile file
) {
314 String storagePath
= file
.getStoragePath();
315 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
317 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
318 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
319 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
321 Intent intentForGuessedMimeType
= null
;
322 if (storagePath
.lastIndexOf('.') >= 0) {
323 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
324 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
325 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
326 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
327 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
331 Intent chooserIntent
= null
;
332 if (intentForGuessedMimeType
!= null
) {
333 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
335 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
338 startActivity(chooserIntent
);
341 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");