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
;
74 /** Flag to signal if the server supports the Share API */
75 private boolean mIsSharedSupported
;
81 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
82 * the {@link FileActivity}.
84 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
85 * is requested to create a new one.
88 protected void onCreate(Bundle savedInstanceState
) {
89 super.onCreate(savedInstanceState
);
92 if(savedInstanceState
!= null
) {
93 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
94 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
95 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
97 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
98 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
99 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
102 setAccount(account
, savedInstanceState
!= null
);
107 * Since ownCloud {@link Account}s can be managed from the system setting menu,
108 * the existence of the {@link Account} associated to the instance must be checked
109 * every time it is restarted.
112 protected void onRestart() {
114 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
116 swapToDefaultAccount();
123 protected void onStart() {
125 if (mAccountWasSet
) {
126 onAccountSet(mAccountWasRestored
);
132 * Sets and validates the ownCloud {@link Account} associated to the Activity.
134 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
136 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
138 * @param account New {@link Account} to set.
139 * @param savedAccount When 'true', account was retrieved from a saved instance state.
141 private void setAccount(Account account
, boolean savedAccount
) {
142 Account oldAccount
= mAccount
;
143 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
146 mAccountWasSet
= true
;
147 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
150 swapToDefaultAccount();
156 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
158 * If no valid ownCloud {@link Account} exists, the the user is requested
159 * to create a new ownCloud {@link Account}.
161 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
163 * @return 'True' if the checked {@link Account} was valid.
165 private void swapToDefaultAccount() {
166 // default to the most recently used account
167 AccountManager accountManager
= AccountManager
.get(this);
168 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
169 if (newAccount
== null
) {
170 /// no account available: force account creation
171 createFirstAccount();
172 mRedirectingToSetupAccount
= true
;
173 mAccountWasSet
= false
;
174 mAccountWasRestored
= false
;
177 mAccountWasSet
= true
;
178 mAccountWasRestored
= (newAccount
.equals(mAccount
));
179 mAccount
= newAccount
;
181 setIsSharedSupported( Boolean
.getBoolean(accountManager
.getUserData(mAccount
, OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
)));
186 * Launches the account creation activity. To use when no ownCloud account is available
188 private void createFirstAccount() {
189 AccountManager am
= AccountManager
.get(getApplicationContext());
190 am
.addAccount(MainApp
.getAccountType(),
195 new AccountCreationCallback(),
204 protected void onSaveInstanceState(Bundle outState
) {
205 super.onSaveInstanceState(outState
);
206 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
207 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
208 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
213 * Getter for the main {@link OCFile} handled by the activity.
215 * @return Main {@link OCFile} handled by the activity.
217 public OCFile
getFile() {
223 * Setter for the main {@link OCFile} handled by the activity.
225 * @param file Main {@link OCFile} to be handled by the activity.
227 public void setFile(OCFile file
) {
233 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
235 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
237 public Account
getAccount() {
242 * @return Value of mFromNotification: True if the Activity is launched by a notification
244 public boolean fromNotification() {
245 return mFromNotification
;
249 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
251 protected boolean isRedirectingToSetupAccount() {
252 return mRedirectingToSetupAccount
;
257 * @return 'True' if the server supports the Share API
259 public boolean isSharedSupported() {
260 return mIsSharedSupported
;
264 public void setIsSharedSupported(boolean mIsSharedSupported
) {
265 this.mIsSharedSupported
= mIsSharedSupported
;
269 * Helper class handling a callback from the {@link AccountManager} after the creation of
270 * a new ownCloud {@link Account} finished, successfully or not.
272 * At this moment, only called after the creation of the first account.
274 * @author David A. Velasco
276 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
279 public void run(AccountManagerFuture
<Bundle
> future
) {
280 FileActivity
.this.mRedirectingToSetupAccount
= false
;
281 boolean accountWasSet
= false
;
282 if (future
!= null
) {
285 result
= future
.getResult();
286 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
287 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
288 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
289 setAccount(new Account(name
, type
), false
);
290 accountWasSet
= true
;
292 } catch (OperationCanceledException e
) {
293 Log_OC
.d(TAG
, "Account creation canceled");
295 } catch (Exception e
) {
296 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
300 Log_OC
.e(TAG
, "Account creation callback with null bundle");
302 if (!accountWasSet
) {
303 moveTaskToBack(true
);
311 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
313 * Child classes must grant that state depending on the {@link Account} is updated.
315 protected abstract void onAccountSet(boolean stateWasRecovered
);
319 public void openFile(OCFile file
) {
321 String storagePath
= file
.getStoragePath();
322 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
324 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
325 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
326 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
328 Intent intentForGuessedMimeType
= null
;
329 if (storagePath
.lastIndexOf('.') >= 0) {
330 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
331 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
332 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
333 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
334 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
338 Intent chooserIntent
= null
;
339 if (intentForGuessedMimeType
!= null
) {
340 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
342 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
345 startActivity(chooserIntent
);
348 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");