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 java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import org
.apache
.http
.protocol
.HTTP
;
26 import android
.accounts
.Account
;
27 import android
.accounts
.AccountManager
;
28 import android
.accounts
.AccountManagerCallback
;
29 import android
.accounts
.AccountManagerFuture
;
30 import android
.accounts
.OperationCanceledException
;
31 import android
.content
.Intent
;
32 import android
.content
.pm
.PackageManager
;
33 import android
.content
.pm
.ResolveInfo
;
34 import android
.net
.Uri
;
35 import android
.os
.Bundle
;
36 import android
.os
.Parcelable
;
37 import android
.webkit
.MimeTypeMap
;
38 import android
.widget
.Toast
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
41 import com
.owncloud
.android
.MainApp
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.lib
.accounts
.OwnCloudAccount
;
47 import com
.owncloud
.android
.lib
.network
.webdav
.WebdavUtils
;
49 import com
.owncloud
.android
.utils
.Log_OC
;
53 * Activity with common behaviour for activities handling {@link OCFile}s in ownCloud {@link Account}s .
55 * @author David A. Velasco
57 public abstract class FileActivity
extends SherlockFragmentActivity
{
59 public static final String EXTRA_FILE
= "com.owncloud.android.ui.activity.FILE";
60 public static final String EXTRA_ACCOUNT
= "com.owncloud.android.ui.activity.ACCOUNT";
61 public static final String EXTRA_WAITING_TO_PREVIEW
= "com.owncloud.android.ui.activity.WAITING_TO_PREVIEW";
62 public static final String EXTRA_FROM_NOTIFICATION
= "com.owncloud.android.ui.activity.FROM_NOTIFICATION";
64 public static final String TAG
= FileActivity
.class.getSimpleName();
67 /** OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located. */
68 private Account mAccount
;
70 /** Main {@link OCFile} handled by the activity.*/
73 /** Flag to signal that the activity will is finishing to enforce the creation of an ownCloud {@link Account} */
74 private boolean mRedirectingToSetupAccount
= false
;
76 /** Flag to signal when the value of mAccount was set */
77 private boolean mAccountWasSet
;
79 /** Flag to signal when the value of mAccount was restored from a saved state */
80 private boolean mAccountWasRestored
;
82 /** Flag to signal if the activity is launched by a notification */
83 private boolean mFromNotification
;
88 * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of
89 * the {@link FileActivity}.
91 * Grants that a valid ownCloud {@link Account} is associated to the instance, or that the user
92 * is requested to create a new one.
95 protected void onCreate(Bundle savedInstanceState
) {
96 super.onCreate(savedInstanceState
);
98 if(savedInstanceState
!= null
) {
99 account
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
100 mFile
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
);
101 mFromNotification
= savedInstanceState
.getBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
);
103 account
= getIntent().getParcelableExtra(FileActivity
.EXTRA_ACCOUNT
);
104 mFile
= getIntent().getParcelableExtra(FileActivity
.EXTRA_FILE
);
105 mFromNotification
= getIntent().getBooleanExtra(FileActivity
.EXTRA_FROM_NOTIFICATION
, false
);
108 setAccount(account
, savedInstanceState
!= null
);
114 * Since ownCloud {@link Account}s can be managed from the system setting menu,
115 * the existence of the {@link Account} associated to the instance must be checked
116 * every time it is restarted.
119 protected void onRestart() {
121 boolean validAccount
= (mAccount
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), mAccount
.name
));
123 swapToDefaultAccount();
130 protected void onStart() {
132 if (mAccountWasSet
) {
133 onAccountSet(mAccountWasRestored
);
139 * Sets and validates the ownCloud {@link Account} associated to the Activity.
141 * If not valid, tries to swap it for other valid and existing ownCloud {@link Account}.
143 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
145 * @param account New {@link Account} to set.
146 * @param savedAccount When 'true', account was retrieved from a saved instance state.
148 private void setAccount(Account account
, boolean savedAccount
) {
149 Account oldAccount
= mAccount
;
150 boolean validAccount
= (account
!= null
&& AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), account
.name
));
153 mAccountWasSet
= true
;
154 mAccountWasRestored
= (savedAccount
|| mAccount
.equals(oldAccount
));
157 swapToDefaultAccount();
163 * Tries to swap the current ownCloud {@link Account} for other valid and existing.
165 * If no valid ownCloud {@link Account} exists, the the user is requested
166 * to create a new ownCloud {@link Account}.
168 * POSTCONDITION: updates {@link #mAccountWasSet} and {@link #mAccountWasRestored}.
170 * @return 'True' if the checked {@link Account} was valid.
172 private void swapToDefaultAccount() {
173 // default to the most recently used account
174 Account newAccount
= AccountUtils
.getCurrentOwnCloudAccount(getApplicationContext());
175 if (newAccount
== null
) {
176 /// no account available: force account creation
177 createFirstAccount();
178 mRedirectingToSetupAccount
= true
;
179 mAccountWasSet
= false
;
180 mAccountWasRestored
= false
;
183 mAccountWasSet
= true
;
184 mAccountWasRestored
= (newAccount
.equals(mAccount
));
185 mAccount
= newAccount
;
191 * Launches the account creation activity. To use when no ownCloud account is available
193 private void createFirstAccount() {
194 AccountManager am
= AccountManager
.get(getApplicationContext());
195 am
.addAccount(MainApp
.getAccountType(),
200 new AccountCreationCallback(),
209 protected void onSaveInstanceState(Bundle outState
) {
210 super.onSaveInstanceState(outState
);
211 outState
.putParcelable(FileActivity
.EXTRA_FILE
, mFile
);
212 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
213 outState
.putBoolean(FileActivity
.EXTRA_FROM_NOTIFICATION
, mFromNotification
);
218 * Getter for the main {@link OCFile} handled by the activity.
220 * @return Main {@link OCFile} handled by the activity.
222 public OCFile
getFile() {
228 * Setter for the main {@link OCFile} handled by the activity.
230 * @param file Main {@link OCFile} to be handled by the activity.
232 public void setFile(OCFile file
) {
238 * Getter for the ownCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
240 * @return OwnCloud {@link Account} where the main {@link OCFile} handled by the activity is located.
242 public Account
getAccount() {
247 * @return Value of mFromNotification: True if the Activity is launched by a notification
249 public boolean fromNotification() {
250 return mFromNotification
;
254 * @return 'True' when the Activity is finishing to enforce the setup of a new account.
256 protected boolean isRedirectingToSetupAccount() {
257 return mRedirectingToSetupAccount
;
262 * @return 'True' if the server supports the Share API
264 public boolean isSharedSupported() {
265 if (getAccount() != null
) {
266 AccountManager accountManager
= AccountManager
.get(this);
267 return Boolean
.parseBoolean(accountManager
.getUserData(getAccount(), OwnCloudAccount
.Constants
.KEY_SUPPORTS_SHARE_API
));
273 * Helper class handling a callback from the {@link AccountManager} after the creation of
274 * a new ownCloud {@link Account} finished, successfully or not.
276 * At this moment, only called after the creation of the first account.
278 * @author David A. Velasco
280 public class AccountCreationCallback
implements AccountManagerCallback
<Bundle
> {
283 public void run(AccountManagerFuture
<Bundle
> future
) {
284 FileActivity
.this.mRedirectingToSetupAccount
= false
;
285 boolean accountWasSet
= false
;
286 if (future
!= null
) {
289 result
= future
.getResult();
290 String name
= result
.getString(AccountManager
.KEY_ACCOUNT_NAME
);
291 String type
= result
.getString(AccountManager
.KEY_ACCOUNT_TYPE
);
292 if (AccountUtils
.setCurrentOwnCloudAccount(getApplicationContext(), name
)) {
293 setAccount(new Account(name
, type
), false
);
294 accountWasSet
= true
;
296 } catch (OperationCanceledException e
) {
297 Log_OC
.d(TAG
, "Account creation canceled");
299 } catch (Exception e
) {
300 Log_OC
.e(TAG
, "Account creation finished in exception: ", e
);
304 Log_OC
.e(TAG
, "Account creation callback with null bundle");
306 if (!accountWasSet
) {
307 moveTaskToBack(true
);
315 * Called when the ownCloud {@link Account} associated to the Activity was just updated.
317 * Child classes must grant that state depending on the {@link Account} is updated.
319 protected abstract void onAccountSet(boolean stateWasRecovered
);
323 public void openFile(OCFile file
) {
325 String storagePath
= file
.getStoragePath();
326 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
328 Intent intentForSavedMimeType
= new Intent(Intent
.ACTION_VIEW
);
329 intentForSavedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), file
.getMimetype());
330 intentForSavedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
332 Intent intentForGuessedMimeType
= null
;
333 if (storagePath
.lastIndexOf('.') >= 0) {
334 String guessedMimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
335 if (guessedMimeType
!= null
&& !guessedMimeType
.equals(file
.getMimetype())) {
336 intentForGuessedMimeType
= new Intent(Intent
.ACTION_VIEW
);
337 intentForGuessedMimeType
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), guessedMimeType
);
338 intentForGuessedMimeType
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
342 Intent chooserIntent
= null
;
343 if (intentForGuessedMimeType
!= null
) {
344 chooserIntent
= Intent
.createChooser(intentForGuessedMimeType
, getString(R
.string
.actionbar_open_with
));
346 chooserIntent
= Intent
.createChooser(intentForSavedMimeType
, getString(R
.string
.actionbar_open_with
));
349 startActivity(chooserIntent
);
352 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
357 public void shareFileWithLink(OCFile file) {
360 Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
361 intentToShareLink.putExtra(Intent.EXTRA_TEXT, "https://fake.url.lolo");
362 intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
364 Intent chooserIntent = Intent.createChooser(intentToShareLink, getString(R.string.action_share_file));
365 startActivity(chooserIntent);
368 Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
373 public void shareFileWithLink(OCFile file
) {
374 if (isSharedSupported()) {
378 //CreateShareOperation createShare = new CreateShareOperation(file.getRemotePath(), ShareType.PUBLIC_LINK, "", false, "", 1);
379 //createShare.execute(getStorageManager(), this, this, mHandler, this);
382 // Get the link --> when the operation is finished
384 String link
= "https://fake.url.lolo";
385 Intent chooserIntent
= null
;
386 List
<Intent
> targetedShareIntents
= new ArrayList
<Intent
>();
387 List
<ResolveInfo
> resInfo
= getPackageManager().queryIntentActivities(createShareWithLinkIntent(link
), PackageManager
.MATCH_DEFAULT_ONLY
);
388 String myPackageName
= getPackageName();
389 if (!resInfo
.isEmpty()) {
390 for (ResolveInfo info
: resInfo
) {
391 if (!info
.activityInfo
.packageName
.equalsIgnoreCase(myPackageName
)) {
392 Intent targetedShare
= createTargetedShare(link
, info
.activityInfo
.applicationInfo
.packageName
, info
.activityInfo
.name
);
393 targetedShareIntents
.add(targetedShare
);
397 if (targetedShareIntents
.size() > 0) {
398 Intent firstTargeted
= targetedShareIntents
.remove(0);
399 chooserIntent
= Intent
.createChooser(firstTargeted
, getString(R
.string
.action_share_file
));
400 chooserIntent
.putExtra(Intent
.EXTRA_INITIAL_INTENTS
, targetedShareIntents
.toArray(new Parcelable
[] {}));
402 // to show standard message
403 chooserIntent
= Intent
.createChooser(null
, getString(R
.string
.action_share_file
));
405 startActivity(chooserIntent
);
408 Log_OC
.wtf(TAG
, "Trying to open a NULL OCFile");
413 Toast t
= Toast
.makeText(this, getString(R
.string
.share_link_no_support_share_api
), Toast
.LENGTH_LONG
);
418 private Intent
createTargetedShare(String link
, String packageName
, String className
) {
419 //Intent targetedShare = createShareWithLinkIntent(link);
420 Intent targetedShare
=new Intent(Intent
.ACTION_MAIN
);
422 targetedShare
.addCategory(Intent
.CATEGORY_LAUNCHER
);
423 targetedShare
.setFlags(Intent
.FLAG_ACTIVITY_NEW_TASK
|
424 Intent
.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
);
425 Log_OC
.e("LOLO", "className: " + className
+ "\npackageName: " + packageName
+ "\n");
426 targetedShare
.setClassName(packageName
, className
);
427 return targetedShare
;
431 private Intent
createShareWithLinkIntent(String link
) {
432 Intent intentToShareLink
= new Intent(Intent
.ACTION_SEND
);
433 intentToShareLink
.putExtra(Intent
.EXTRA_TEXT
, link
);
434 intentToShareLink
.setType(HTTP
.PLAIN_TEXT_TYPE
);
435 return intentToShareLink
;