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
;
37 import com
.owncloud
.android
.lib
.accounts
.OwnCloudAccount
;
38 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
40 import com
.owncloud
.android
.utils
.Log_OC
;
44 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
46 * @author David A. Velasco
48 public abstract class FileActivity
extends SherlockFragmentActivity
{
50 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
51 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
52 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
53 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
55 public static final String TAG
= FileActivity
.class.getSimpleName();
58 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
59 private Account mAccount
;
61 /** Main {@link OCFile} handled by the activity.*/
64 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
65 private boolean mRedirectingToSetupAccount
= false
;
67 /** Flag to signal when the value of mAccount was set */
68 private boolean mAccountWasSet
;
70 /** Flag to signal when the value of mAccount was restored from a saved state */
71 private boolean mAccountWasRestored
;
73 /** Flag to signal if the activity is launched by a notification */
74 private boolean mFromNotification
;
79 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
80 * the {@link FileActivity}.
82 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
83 * is requested to create a new one.
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
89 if(savedInstanceState
!= null
) {
90 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
91 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
92 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
94 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
95 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
96 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
99 setAccount(account
, savedInstanceState
!= null
);
105 * Since ownCloud {@link Account}s can be managed from the system setting menu,
106 * the existence of the {@link Account} associated to the instance must be checked
107 * every time it is restarted.
110 protected void onRestart() {
112 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
114 swapToDefaultAccount();
121 protected void onStart() {
123 if (mAccountWasSet
) {
124 onAccountSet(mAccountWasRestored
);
130 * Sets and validates the ownCloud {@link Account} associated to the Activity.
132 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
134 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
136 * @param account New {@link Account} to set.
137 * @param savedAccount When 'true', account was retrieved from a saved instance state.
139 private void setAccount(Account account
, boolean savedAccount
) {
140 Account oldAccount
= mAccount
;
141 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
144 mAccountWasSet
= true
;
145 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
148 swapToDefaultAccount();
154 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
156 * If no valid ownCloud {@link Account} exists, the the user is requested
157 * to create a new ownCloud {@link Account}.
159 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
161 * @return 'True' if the checked {@link Account} was valid.
163 private void swapToDefaultAccount() {
164 // default to the most recently used account
165 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
166 if (newAccount
== null
) {
167 /// no account available: force account creation
168 createFirstAccount();
169 mRedirectingToSetupAccount
= true
;
170 mAccountWasSet
= false
;
171 mAccountWasRestored
= false
;
174 mAccountWasSet
= true
;
175 mAccountWasRestored
= (newAccount
.equals(mAccount
));
176 mAccount
= newAccount
;
182 * Launches the account creation activity. To use when no ownCloud account is available
184 private void createFirstAccount() {
185 AccountManager am
= AccountManager
.get(getApplicationContext());
186 am
.addAccount(MainApp
.getAccountType(),
191 new AccountCreationCallback(),
200 protected void onSaveInstanceState(Bundle outState
) {
201 super.onSaveInstanceState(outState
);
202 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
203 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
204 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
209 * Getter for the main {@link OCFile} handled by the activity.
211 * @return Main {@link OCFile} handled by the activity.
213 public OCFile
getFile() {
219 * Setter for the main {@link OCFile} handled by the activity.
221 * @param file Main {@link OCFile} to be handled by the activity.
223 public void setFile(OCFile file
) {
229 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
231 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
233 public Account
getAccount() {
238 * @return Value of mFromNotification: True if the Activity is launched by a notification
240 public boolean fromNotification() {
241 return mFromNotification
;
245 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
247 protected boolean isRedirectingToSetupAccount() {
248 return mRedirectingToSetupAccount
;
253 * @return 'True' if the server supports the Share API
255 public boolean isSharedSupported() {
256 if (getAccount() != null
) {
257 AccountManager accountManager
= AccountManager
.get(this);
258 return Boolean
.parseBoolean(accountManager
.getUserData(getAccount(), OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
));
264 * Helper class handling a callback from the {@link AccountManager} after the creation of
265 * a new ownCloud {@link Account} finished, successfully or not.
267 * At this moment, only called after the creation of the first account.
269 * @author David A. Velasco
271 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
274 public void run(AccountManagerFuture
<Bundle
> future
) {
275 FileActivity
.this.mRedirectingToSetupAccount
= false
;
276 boolean accountWasSet
= false
;
277 if (future
!= null
) {
280 result
= future
.getResult();
281 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
282 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
283 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
284 setAccount(new Account(name
, type
), false
);
285 accountWasSet
= true
;
287 } catch (OperationCanceledException e
) {
288 Log_OC
.d(TAG
, "Account creation canceled");
290 } catch (Exception e
) {
291 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
295 Log_OC
.e(TAG
, "Account creation callback with null bundle");
297 if (!accountWasSet
) {
298 moveTaskToBack(true
);
306 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
308 * Child classes must grant that state depending on the {@link Account} is updated.
310 protected abstract void onAccountSet(boolean stateWasRecovered
);
314 public void openFile(OCFile file
) {
316 String storagePath
= file
.getStoragePath();
317 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
319 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
320 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
321 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
323 Intent intentForGuessedMimeType
= null
;
324 if (storagePath
.lastIndexOf('.') >= 0) {
325 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
326 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
327 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
328 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
329 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
333 Intent chooserIntent
= null
;
334 if (intentForGuessedMimeType
!= null
) {
335 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
337 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
340 startActivity(chooserIntent
);
343 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");