new stuff for account authenticator
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / AccountUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package eu.alefzero.owncloud;
20
21 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
22
23 import android.accounts.Account;
24 import android.accounts.AccountManager;
25 import android.content.Context;
26 import android.content.SharedPreferences;
27 import android.preference.PreferenceManager;
28
29 public class AccountUtils {
30 public static final String WEBDAV_PATH_1_2 = "/webdav/owncloud.php";
31 public static final String WEBDAV_PATH_2_0 = "/files/webdav.php";
32 public static final String CARDDAV_PATH_2_0 = "/apps/contacts/carddav.php";
33
34 /**
35 * Can be used to get the currently selected ownCloud account in the preferences
36 *
37 * @param context The current appContext
38 * @return The current account or null, if there is none yet.
39 */
40 public static Account getCurrentOwnCloudAccount(Context context){
41 Account[] ocAccounts = AccountManager.get(context).getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
42 Account defaultAccount = null;
43
44 SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(context);
45 String accountName = appPreferences.getString("select_oc_account", null);
46
47 if(accountName != null){
48 for(Account account : ocAccounts){
49 if(account.name.equals(accountName)){
50 defaultAccount = account;
51 break;
52 }
53 }
54 } else if (ocAccounts.length != 0) {
55 // we at least need to take first account as fallback
56 defaultAccount = ocAccounts[0];
57 }
58
59 return defaultAccount;
60 }
61 }