X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/fbd720e8874d5adcfd0dcc5aa8c602ecb54ef83c..5dc43aab3cc1243f9989dd6a0c7dd3350c3f709b:/src/com/owncloud/android/AccountUtils.java diff --git a/src/com/owncloud/android/AccountUtils.java b/src/com/owncloud/android/AccountUtils.java index 49b0a62d..7297ee10 100644 --- a/src/com/owncloud/android/AccountUtils.java +++ b/src/com/owncloud/android/AccountUtils.java @@ -1,10 +1,10 @@ /* ownCloud Android client application * Copyright (C) 2012 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,7 +18,7 @@ package com.owncloud.android; -import com.owncloud.android.authenticator.AccountAuthenticator; +import com.owncloud.android.authentication.AccountAuthenticator; import com.owncloud.android.utils.OwnCloudVersion; import android.accounts.Account; @@ -31,17 +31,19 @@ public class AccountUtils { public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php"; public static final String WEBDAV_PATH_2_0 = "/files/webdav.php"; public static final String WEBDAV_PATH_4_0 = "/remote.php/webdav"; + private static final String ODAV_PATH = "/remote.php/odav"; public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php"; public static final String CARDDAV_PATH_4_0 = "/remote/carddav.php"; public static final String STATUS_PATH = "/status.php"; /** - * Can be used to get the currently selected ownCloud account in the - * preferences + * Can be used to get the currently selected ownCloud {@link Account} in the + * application preferences. * - * @param context The current appContext - * @return The current account or first available, if none is available, - * then null. + * @param context The current application {@link Context} + * @return The ownCloud {@link Account} currently saved in preferences, or the first + * {@link Account} available, if valid (still registered in the system as ownCloud + * account). If none is available and valid, returns null. */ public static Account getCurrentOwnCloudAccount(Context context) { Account[] ocAccounts = AccountManager.get(context).getAccountsByType( @@ -53,6 +55,7 @@ public class AccountUtils { String accountName = appPreferences .getString("select_oc_account", null); + // account validation: the saved account MUST be in the list of ownCloud Accounts known by the AccountManager if (accountName != null) { for (Account account : ocAccounts) { if (account.name.equals(accountName)) { @@ -60,8 +63,10 @@ public class AccountUtils { break; } } - } else if (ocAccounts.length != 0) { - // we at least need to take first account as fallback + } + + if (defaultAccount == null && ocAccounts.length != 0) { + // take first account as fallback defaultAccount = ocAccounts[0]; } @@ -83,11 +88,26 @@ public class AccountUtils { } - public static void setCurrentOwnCloudAccount(Context context, String name) { - SharedPreferences.Editor appPrefs = PreferenceManager - .getDefaultSharedPreferences(context).edit(); - appPrefs.putString("select_oc_account", name); - appPrefs.commit(); + public static boolean setCurrentOwnCloudAccount(Context context, String accountName) { + boolean result = false; + if (accountName != null) { + Account[] ocAccounts = AccountManager.get(context).getAccountsByType( + AccountAuthenticator.ACCOUNT_TYPE); + boolean found = false; + for (Account account : ocAccounts) { + found = (account.name.equals(accountName)); + if (found) { + SharedPreferences.Editor appPrefs = PreferenceManager + .getDefaultSharedPreferences(context).edit(); + appPrefs.putString("select_oc_account", accountName); + + appPrefs.commit(); + result = true; + break; + } + } + } + return result; } /** @@ -95,8 +115,11 @@ public class AccountUtils { * @param version version of owncloud * @return webdav path for given OC version, null if OC version unknown */ - public static String getWebdavPath(OwnCloudVersion version) { + public static String getWebdavPath(OwnCloudVersion version, boolean supportsOAuth) { if (version != null) { + if (supportsOAuth) { + return ODAV_PATH; + } if (version.compareTo(OwnCloudVersion.owncloud_v4) >= 0) return WEBDAV_PATH_4_0; if (version.compareTo(OwnCloudVersion.owncloud_v3) >= 0 @@ -107,5 +130,28 @@ public class AccountUtils { } return null; } + + /** + * Constructs full url to host and webdav resource basing on host version + * @param context + * @param account + * @return url or null on failure + */ + public static String constructFullURLForAccount(Context context, Account account) { + try { + AccountManager ama = AccountManager.get(context); + String baseurl = ama.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); + String strver = ama.getUserData(account, AccountAuthenticator.KEY_OC_VERSION); + boolean supportsOAuth = (ama.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null); + OwnCloudVersion ver = new OwnCloudVersion(strver); + String webdavpath = getWebdavPath(ver, supportsOAuth); + + if (webdavpath == null) return null; + return baseurl + webdavpath; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } }