5a7c57e6f6ee781328a295efa4b95b6e37b430fb
[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 version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18
19 package com.owncloud.android.workaround.accounts;
20
21 import android.accounts.AbstractAccountAuthenticator;
22 import android.accounts.Account;
23 import android.accounts.AccountAuthenticatorResponse;
24 import android.accounts.AccountManager;
25 import android.accounts.NetworkErrorException;
26 import android.app.Service;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.os.Bundle;
30 import android.os.IBinder;
31 //import android.util.Log;
32
33 public class AccountAuthenticatorService extends Service {
34
35 private AccountAuthenticator mAuthenticator;
36 //static final public String ACCOUNT_TYPE = "owncloud";
37
38 @Override
39 public void onCreate() {
40 super.onCreate();
41 mAuthenticator = new AccountAuthenticator(this);
42 }
43
44 @Override
45 public IBinder onBind(Intent intent) {
46 return mAuthenticator.getIBinder();
47 }
48
49
50 public static class AccountAuthenticator extends AbstractAccountAuthenticator {
51
52 public static final String KEY_AUTH_TOKEN_TYPE = "authTokenType";
53 public static final String KEY_REQUIRED_FEATURES = "requiredFeatures";
54 public static final String KEY_LOGIN_OPTIONS = "loginOptions";
55
56 public AccountAuthenticator(Context context) {
57 super(context);
58 }
59
60 @Override
61 public Bundle addAccount(AccountAuthenticatorResponse response,
62 String accountType, String authTokenType,
63 String[] requiredFeatures, Bundle options)
64 throws NetworkErrorException {
65 //Log.e("WORKAROUND", "Yes, WORKAROUND takes the control here");
66 final Intent intent = new Intent("com.owncloud.android.workaround.accounts.CREATE");
67 intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
68 response);
69 intent.putExtra(KEY_AUTH_TOKEN_TYPE, authTokenType);
70 intent.putExtra(KEY_REQUIRED_FEATURES, requiredFeatures);
71 intent.putExtra(KEY_LOGIN_OPTIONS, options);
72
73 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
75 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
76 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
77 intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
78
79 final Bundle bundle = new Bundle();
80 bundle.putParcelable(AccountManager.KEY_INTENT, intent);
81 return bundle;
82 //return getCommonResultBundle();
83 }
84
85
86 @Override
87 public Bundle confirmCredentials(AccountAuthenticatorResponse response,
88 Account account, Bundle options) throws NetworkErrorException {
89 return getCommonResultBundle();
90 }
91
92 @Override
93 public Bundle editProperties(AccountAuthenticatorResponse response,
94 String accountType) {
95 return getCommonResultBundle();
96 }
97
98 @Override
99 public Bundle getAuthToken(AccountAuthenticatorResponse response,
100 Account account, String authTokenType, Bundle options)
101 throws NetworkErrorException {
102 return getCommonResultBundle();
103 }
104
105 @Override
106 public String getAuthTokenLabel(String authTokenType) {
107 return "";
108 }
109
110 @Override
111 public Bundle hasFeatures(AccountAuthenticatorResponse response,
112 Account account, String[] features) throws NetworkErrorException {
113 return getCommonResultBundle();
114 }
115
116 @Override
117 public Bundle updateCredentials(AccountAuthenticatorResponse response,
118 Account account, String authTokenType, Bundle options)
119 throws NetworkErrorException {
120 return getCommonResultBundle();
121 }
122
123 @Override
124 public Bundle getAccountRemovalAllowed(
125 AccountAuthenticatorResponse response, Account account)
126 throws NetworkErrorException {
127 return super.getAccountRemovalAllowed(response, account);
128 }
129
130 private Bundle getCommonResultBundle() {
131 Bundle resultBundle = new Bundle();
132 resultBundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION);
133 resultBundle.putString(AccountManager.KEY_ERROR_MESSAGE, "This is just a workaround, not a real account authenticator");
134 return resultBundle;
135 }
136
137 }
138 }