1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012  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 as published by 
   7  *   the Free Software Foundation, either version 2 of the License, or 
   8  *   (at your option) any later version. 
  10  *   This program is distributed in the hope that it will be useful, 
  11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13  *   GNU General Public License for more details. 
  15  *   You should have received a copy of the GNU General Public License 
  16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  20 package com
.owncloud
.android
; 
  22 import com
.owncloud
.android
.authentication
.AccountAuthenticator
; 
  23 import com
.owncloud
.android
.utils
.OwnCloudVersion
; 
  25 import android
.accounts
.Account
; 
  26 import android
.accounts
.AccountManager
; 
  27 import android
.content
.Context
; 
  28 import android
.content
.SharedPreferences
; 
  29 import android
.preference
.PreferenceManager
; 
  31 public class AccountUtils 
{ 
  32     public static final String WEBDAV_PATH_1_2 
= "/webdav/owncloud.php"; 
  33     public static final String WEBDAV_PATH_2_0 
= "/files/webdav.php"; 
  34     public static final String WEBDAV_PATH_4_0 
= "/remote.php/webdav"; 
  35     private static final String ODAV_PATH 
= "/remote.php/odav"; 
  36     public static final String CARDDAV_PATH_2_0 
= "/apps/contacts/carddav.php"; 
  37     public static final String CARDDAV_PATH_4_0 
= "/remote/carddav.php"; 
  38     public static final String STATUS_PATH 
= "/status.php"; 
  41      * Can be used to get the currently selected ownCloud account in the 
  44      * @param context The current appContext 
  45      * @return The current account or first available, if none is available, 
  48     public static Account 
getCurrentOwnCloudAccount(Context context
) { 
  49         Account
[] ocAccounts 
= AccountManager
.get(context
).getAccountsByType( 
  50                 AccountAuthenticator
.ACCOUNT_TYPE
); 
  51         Account defaultAccount 
= null
; 
  53         SharedPreferences appPreferences 
= PreferenceManager
 
  54                 .getDefaultSharedPreferences(context
); 
  55         String accountName 
= appPreferences
 
  56                 .getString("select_oc_account", null
); 
  58         if (accountName 
!= null
) { 
  59             for (Account account 
: ocAccounts
) { 
  60                 if (account
.name
.equals(accountName
)) { 
  61                     defaultAccount 
= account
; 
  67         if (defaultAccount 
== null 
&& ocAccounts
.length 
!= 0) { 
  68             // we at least need to take first account as fallback 
  69             defaultAccount 
= ocAccounts
[0]; 
  72         return defaultAccount
; 
  78      * Checks, whether or not there are any ownCloud accounts setup. 
  80      * @return true, if there is at least one account. 
  82     public static boolean accountsAreSetup(Context context
) { 
  83         AccountManager accMan 
= AccountManager
.get(context
); 
  84         Account
[] accounts 
= accMan
 
  85                 .getAccountsByType(AccountAuthenticator
.ACCOUNT_TYPE
); 
  86         return accounts
.length 
> 0; 
  90     public static boolean setCurrentOwnCloudAccount(Context context
, String accountName
) { 
  91         boolean result 
= false
; 
  92         if (accountName 
!= null
) { 
  93             Account
[] ocAccounts 
= AccountManager
.get(context
).getAccountsByType( 
  94                     AccountAuthenticator
.ACCOUNT_TYPE
); 
  95             boolean found 
= false
; 
  96             for (Account account 
: ocAccounts
) { 
  97                 found 
= (account
.name
.equals(accountName
)); 
  99                     SharedPreferences
.Editor appPrefs 
= PreferenceManager
 
 100                             .getDefaultSharedPreferences(context
).edit(); 
 101                     appPrefs
.putString("select_oc_account", accountName
); 
 114      * @param version version of owncloud 
 115      * @return webdav path for given OC version, null if OC version unknown 
 117     public static String 
getWebdavPath(OwnCloudVersion version
, boolean supportsOAuth
) { 
 118         if (version 
!= null
) { 
 122             if (version
.compareTo(OwnCloudVersion
.owncloud_v4
) >= 0) 
 123                 return WEBDAV_PATH_4_0
; 
 124             if (version
.compareTo(OwnCloudVersion
.owncloud_v3
) >= 0 
 125                     || version
.compareTo(OwnCloudVersion
.owncloud_v2
) >= 0) 
 126                 return WEBDAV_PATH_2_0
; 
 127             if (version
.compareTo(OwnCloudVersion
.owncloud_v1
) >= 0) 
 128                 return WEBDAV_PATH_1_2
; 
 134      * Constructs full url to host and webdav resource basing on host version 
 137      * @return url or null on failure 
 139     public static String 
constructFullURLForAccount(Context context
, Account account
) { 
 141             AccountManager ama 
= AccountManager
.get(context
); 
 142             String baseurl 
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
); 
 143             String strver  
= ama
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
); 
 144             boolean supportsOAuth 
= (ama
.getUserData(account
, AccountAuthenticator
.KEY_SUPPORTS_OAUTH2
) != null
); 
 145             OwnCloudVersion ver 
= new OwnCloudVersion(strver
); 
 146             String webdavpath 
= getWebdavPath(ver
, supportsOAuth
); 
 148             if (webdavpath 
== null
) return null
; 
 149             return baseurl 
+ webdavpath
; 
 150         } catch (Exception e
) {