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