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
.network
.webdav
.WebdavUtils
;
37 import com
.owncloud
.android
.utils
.Log_OC
;
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";
50 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
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
;
70 /** Flag to signal if the activity is launched by a notification */
71 private boolean mFromNotification
;
75 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
76 * the {@link FileActivity}.
78 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
79 * is requested to create a new one.
82 protected void onCreate(Bundle savedInstanceState
) {
83 super.onCreate(savedInstanceState
);
86 if(savedInstanceState
!= null
) {
87 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
88 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
89 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
91 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
92 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
93 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
96 setAccount(account
, savedInstanceState
!= null
);
101 * Since ownCloud {@link Account}s can be managed from the system setting menu,
102 * the existence of the {@link Account} associated to the instance must be checked
103 * every time it is restarted.
106 protected void onRestart() {
108 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
110 swapToDefaultAccount();
117 protected void onStart() {
119 if (mAccountWasSet
) {
120 onAccountSet(mAccountWasRestored
);
126 * Sets and validates the ownCloud {@link Account} associated to the Activity.
128 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
130 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
132 * @param account New {@link Account} to set.
133 * @param savedAccount When 'true', account was retrieved from a saved instance state.
135 private void setAccount(Account account
, boolean savedAccount
) {
136 Account oldAccount
= mAccount
;
137 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
140 mAccountWasSet
= true
;
141 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
144 swapToDefaultAccount();
150 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
152 * If no valid ownCloud {@link Account} exists, the the user is requested
153 * to create a new ownCloud {@link Account}.
155 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
157 * @return 'True' if the checked {@link Account} was valid.
159 private void swapToDefaultAccount() {
160 // default to the most recently used account
161 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
162 if (newAccount
== null
) {
163 /// no account available: force account creation
164 createFirstAccount();
165 mRedirectingToSetupAccount
= true
;
166 mAccountWasSet
= false
;
167 mAccountWasRestored
= false
;
170 mAccountWasSet
= true
;
171 mAccountWasRestored
= (newAccount
.equals(mAccount
));
172 mAccount
= newAccount
;
178 * Launches the account creation activity. To use when no ownCloud account is available
180 private void createFirstAccount() {
181 AccountManager am
= AccountManager
.get(getApplicationContext());
182 am
.addAccount(MainApp
.getAccountType(),
187 new AccountCreationCallback(),
196 protected void onSaveInstanceState(Bundle outState
) {
197 super.onSaveInstanceState(outState
);
198 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
199 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
200 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
205 * Getter for the main {@link OCFile} handled by the activity.
207 * @return Main {@link OCFile} handled by the activity.
209 public OCFile
getFile() {
215 * Setter for the main {@link OCFile} handled by the activity.
217 * @param file Main {@link OCFile} to be handled by the activity.
219 public void setFile(OCFile file
) {
225 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
227 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
229 public Account
getAccount() {
234 * @return Value of mFromNotification: True if the Activity is launched by a notification
236 public boolean fromNotification() {
237 return mFromNotification
;
241 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
243 protected boolean isRedirectingToSetupAccount() {
244 return mRedirectingToSetupAccount
;
249 * Helper class handling a callback from the {@link AccountManager} after the creation of
250 * a new ownCloud {@link Account} finished, successfully or not.
252 * At this moment, only called after the creation of the first account.
254 * @author David A. Velasco
256 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
259 public void run(AccountManagerFuture
<Bundle
> future
) {
260 FileActivity
.this.mRedirectingToSetupAccount
= false
;
261 boolean accountWasSet
= false
;
262 if (future
!= null
) {
265 result
= future
.getResult();
266 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
267 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
268 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
269 setAccount(new Account(name
, type
), false
);
270 accountWasSet
= true
;
272 } catch (OperationCanceledException e
) {
273 Log_OC
.d(TAG
, "Account creation canceled");
275 } catch (Exception e
) {
276 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
280 Log_OC
.e(TAG
, "Account creation callback with null bundle");
282 if (!accountWasSet
) {
283 moveTaskToBack(true
);
291 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
293 * Child classes must grant that state depending on the {@link Account} is updated.
295 protected abstract void onAccountSet(boolean stateWasRecovered
);
299 public void openFile(OCFile file
) {
301 String storagePath
= file
.getStoragePath();
302 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
304 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
305 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
306 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
308 Intent intentForGuessedMimeType
= null
;
309 if (storagePath
.lastIndexOf('.') >= 0) {
310 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
311 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
312 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
313 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
314 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
318 Intent chooserIntent
= null
;
319 if (intentForGuessedMimeType
!= null
) {
320 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
322 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
325 startActivity(chooserIntent
);
328 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");