I am trying to implement an account manager in my application to avoid having the user log in every time the application is opened.
Thus, basically I already have an authentication function in which the user can put his username and password and where we get the token from the server (at the moment authentication is basic). Now I want to add an AccountManager, but I really don’t understand which part will go there.
I need quite simple:
- add account if i never logged in before
- automatically if my account exists
- If automatic authentication does not work, get a new token on the server
Here is my code:
AuthenticationActivity.java
public class AuthenticationActivity extends Activity { private EditText editTextUsername; private EditText editTextPassword; private Button buttonLogin; private ProgressBar spinner; private TextView error; private TextView register; private boolean accountRegistred; AccountManager accountManager; public static final String AUTHENTICATION = "authentication";
AuthenticatorService.java
public class AuthenticatorService extends Service { private static AccountAuthenticator accountAuthenticator = null; public AuthenticatorService() { super(); } @Override public IBinder onBind(Intent intent) { IBinder ret = null; if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) { ret = getAuthenticator().getIBinder(); } return ret; } private AccountAuthenticator getAuthenticator() { if (AuthenticatorService.accountAuthenticator == null) { AuthenticatorService.accountAuthenticator = new AccountAuthenticator(this); } return AuthenticatorService.accountAuthenticator; } public class AccountAuthenticator extends AbstractAccountAuthenticator { private Context context; public AccountAuthenticator(Context context) { super(context); this.context = context; } @Override public Bundle editProperties(AccountAuthenticatorResponse accountAuthenticatorResponse, String s) { return null; } @Override public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { Bundle reply = new Bundle(); Intent i = new Intent(context, AuthenticationActivity.class); i.setAction("com.readyo.app.authentication.addnewaccount"); i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); i.putExtra("AuthTokenType", authTokenType); reply.putParcelable(AccountManager.KEY_INTENT, i); return reply; } @Override public Bundle confirmCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, Bundle bundle) throws NetworkErrorException { return null; } @Override public Bundle getAuthToken(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; } @Override public String getAuthTokenLabel(String s) { return null; } @Override public Bundle updateCredentials(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String s, Bundle bundle) throws NetworkErrorException { return null; } @Override public Bundle hasFeatures(AccountAuthenticatorResponse accountAuthenticatorResponse, Account account, String[] strings) throws NetworkErrorException { return null; } } }
I also have code to access the server via HTTP, but I'm not sure if this will be important here.
Thank you for your time.
source share