102f2be6d77795c7f49b30159aa6d589333411cd
[pub/Android/ownCloud.git] / oc_jb_workaround / src / com / owncloud / android / workaround / accounts / AccountAuthenticatorService.java
1 /* ownCloud Jelly Bean Workaround for lost credentials
2 * Copyright (C) 2013 ownCloud Inc.
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 2 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
20 package com.owncloud.android.workaround.accounts;
21
22 import android.accounts.AbstractAccountAuthenticator;
23 import android.accounts.Account;
24 import android.accounts.AccountAuthenticatorResponse;
25 import android.accounts.AccountManager;
26 import android.accounts.NetworkErrorException;
27 import android.app.Service;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.os.Bundle;
31 import android.os.IBinder;
32 //import android.util.Log;
33
34 public class AccountAuthenticatorService extends Service {
35
36 private AccountAuthenticator mAuthenticator;
37 static final public String ACCOUNT_TYPE = "owncloud";
38
39 @Override
40 public void onCreate() {
41 super.onCreate();
42 mAuthenticator = new AccountAuthenticator(this);
43 }
44
45 @Override
46 public IBinder onBind(Intent intent) {
47 return mAuthenticator.getIBinder();
48 }
49
50
51 public static class AccountAuthenticator extends AbstractAccountAuthenticator {
52
53 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
54 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
55 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
56
57 public AccountAuthenticator(Context context) {
58 super(context);
59 }
60
61 @Override
62 public Bundle addAccount(AccountAuthenticatorResponse response,
63 String accountType, String authTokenType,
64 String[] requiredFeatures, Bundle options)
65 throws NetworkErrorException {
66 //Log.e("WORKAROUND", "Yes, WORKAROUND takes the control here");
67 final Intent intent = new Intent("com.owncloud.android.workaround.accounts.CREATE");
68 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
69 response);
70 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
71 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
72 intent.putExtra(KEY_LOGIN_OPTIONS, options);
73
74 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
75 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
76 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
77 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
78 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
79
80 final Bundle bundle = new Bundle();
81 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
82 return bundle;
83 //return getCommonResultBundle();
84 }
85
86
87 @Override
88 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
89 Account account, Bundle options) throws NetworkErrorException {
90 return getCommonResultBundle();
91 }
92
93 @Override
94 public Bundle editProperties(AccountAuthenticatorResponse response,
95 String accountType) {
96 return getCommonResultBundle();
97 }
98
99 @Override
100 public Bundle getAuthToken(AccountAuthenticatorResponse response,
101 Account account, String authTokenType, Bundle options)
102 throws NetworkErrorException {
103 return getCommonResultBundle();
104 }
105
106 @Override
107 public String getAuthTokenLabel(String authTokenType) {
108 return "";
109 }
110
111 @Override
112 public Bundle hasFeatures(AccountAuthenticatorResponse response,
113 Account account, String[] features) throws NetworkErrorException {
114 return getCommonResultBundle();
115 }
116
117 @Override
118 public Bundle updateCredentials(AccountAuthenticatorResponse response,
119 Account account, String authTokenType, Bundle options)
120 throws NetworkErrorException {
121 return getCommonResultBundle();
122 }
123
124 @Override
125 public Bundle getAccountRemovalAllowed(
126 AccountAuthenticatorResponse response, Account account)
127 throws NetworkErrorException {
128 return super.getAccountRemovalAllowed(response, account);
129 }
130
131 private Bundle getCommonResultBundle() {
132 Bundle resultBundle = new Bundle();
133 resultBundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
134 resultBundle.putString(AccountManager.KEY_ERROR_MESSAGE, "This is just a workaround, not a real account authenticator");
135 return resultBundle;
136 }
137
138 }
139 }