Guest User

Untitled

a guest
May 8th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. public class AccountAuthenticatorService extends Service {
  2.  
  3. private static final String LOG_TAG = AccountAuthenticatorService.class.getSimpleName();
  4. private static AccountAuthenticatorImpl sAccountAuthenticator = null;
  5.  
  6. public AccountAuthenticatorService() {
  7. super();
  8. }
  9.  
  10. @Override
  11. public IBinder onBind(Intent intent) {
  12. IBinder ret = null;
  13. if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
  14. ret = getAuthenticator().getIBinder();
  15. }
  16. return ret;
  17. }
  18.  
  19. public AbstractAccountAuthenticator getAuthenticator() {
  20. if (AccountAuthenticatorService.sAccountAuthenticator == null) {
  21. AccountAuthenticatorService.sAccountAuthenticator = new AccountAuthenticatorImpl(this);
  22. }
  23. return AccountAuthenticatorService.sAccountAuthenticator;
  24. }
  25.  
  26. private static class AccountAuthenticatorImpl extends AbstractAccountAuthenticator {
  27. private Context mContext;
  28.  
  29. public AccountAuthenticatorImpl(Context context) {
  30. super(context);
  31. mContext = context;
  32. }
  33.  
  34. @Override
  35. public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
  36. return null;
  37. }
  38.  
  39. // When adding a new account from the cell phone, this window opens
  40. @Override
  41. public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
  42. Intent add = new Intent(mContext, AccountManagerSimpleActivity.class);
  43. add.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
  44. Bundle reply = new Bundle();
  45. reply.putParcelable(AccountManager.KEY_INTENT, add);
  46. return reply;
  47. }
  48.  
  49. @Override
  50. public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
  51. return null;
  52. }
  53.  
  54. @Override
  55. public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
  56. AccountManager am = AccountManager.get(mContext);
  57. String user = account.name;
  58. String pass = am.getPassword(account);
  59. String token = "sample-no-token";
  60.  
  61. Bundle result = new Bundle();
  62. result.putString(AccountManager.KEY_ACCOUNT_NAME, user);
  63. result.putString(AccountManager.KEY_ACCOUNT_TYPE, "com.google");
  64. result.putString(AccountManager.KEY_AUTHTOKEN, token);
  65. return result;
  66. }
  67.  
  68. @Override
  69. public String getAuthTokenLabel(String authTokenType) {
  70. return null;
  71. }
  72.  
  73. @Override
  74. public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
  75. return null;
  76. }
  77.  
  78. @Override
  79. public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
  80. return null;
  81. }
  82.  
  83. }
  84. }
  85.  
  86. // Button to execute the login from gmail account
  87. registration.setOnClickListener(new View.OnClickListener() {
  88. @Override
  89. public void onClick(View v) {
  90. String email = "com.google";
  91. try{
  92. Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
  93. new String[]{email}, true, null, null, null, null);
  94. startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
  95. } catch (ActivityNotFoundException ex) {
  96. Toast.makeText(AccountManagerSimpleActivity.this, "Error de sincronización en las cuentas: " + ex.getMessage(), Toast.LENGTH_LONG);
  97. }
  98. }
  99. });
  100.  
  101. cancel.setOnClickListener(new View.OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. finish();
  105. }
  106. });
  107.  
  108. }
  109.  
  110. // Function that complements the registry by gmail
  111. @SuppressLint("WrongConstant")
  112. @Override
  113. protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  114. if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
  115. String useremail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
  116. Toast.makeText(this,"Cuenta sincronizada con éxito",3000).show();
  117. }else{
  118. Toast.makeText(this,"No hay cuentas sincronizadas",3000).show();
  119.  
  120. }
  121. }
Add Comment
Please, Sign In to add comment