您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AddAccountActivity.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.rthoni.camotion.ui;
  2. import android.accounts.Account;
  3. import android.accounts.AccountAuthenticatorResponse;
  4. import android.accounts.AccountManager;
  5. import android.app.Activity;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.view.View;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. import com.luticate.auth.dbo.LuLoginDbo;
  14. import com.luticate.utils.business.LuPromise;
  15. import com.luticate.utils.business.LuRequest;
  16. import com.rthoni.camotion.R;
  17. import com.rthoni.camotion.dbo.LocationDbo;
  18. import com.rthoni.camotion.ui.dialogs.LoginDialog;
  19. import org.json.JSONObject;
  20. public class AddAccountActivity extends AppCompatActivity {
  21. private EditText _nameInput;
  22. private EditText _usernameInput;
  23. private EditText _passwordInput;
  24. private EditText _domainInput;
  25. private CheckBox _checkBoxHttps;
  26. private AccountAuthenticatorResponse _response;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. LuRequest.init(this);
  31. setContentView(R.layout.activity_add_account);
  32. _nameInput = (EditText) findViewById(R.id.name_input);
  33. _usernameInput = (EditText) findViewById(R.id.username_input);
  34. _passwordInput = (EditText) findViewById(R.id.password_input);
  35. _domainInput = (EditText) findViewById(R.id.domain_input);
  36. _checkBoxHttps = (CheckBox) findViewById(R.id.checkBoxHttps);
  37. findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. check();
  41. }
  42. });
  43. findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. setResult(Activity.RESULT_CANCELED);
  47. finish();
  48. }
  49. });
  50. Bundle extras = getIntent().getExtras();
  51. if (extras != null) {
  52. _response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
  53. }
  54. LocationDbo location = new LocationDbo();
  55. location.setHttps(true);
  56. try {
  57. String data = getIntent().getDataString();
  58. Uri uri= Uri.parse(data);;
  59. String config = uri.getQueryParameter("config");
  60. JSONObject json = new JSONObject(config);
  61. location.fromJson(json);
  62. }
  63. catch (Exception e)
  64. {
  65. }
  66. _nameInput.setText(location.getName());
  67. _usernameInput.setText(location.getUsername());
  68. _passwordInput.setText(location.getPassword());
  69. _domainInput.setText(location.getDomain());
  70. _checkBoxHttps.setChecked(location.isHttps());
  71. }
  72. private LocationDbo validate()
  73. {
  74. LocationDbo location = new LocationDbo();
  75. location.setName(_nameInput.getText().toString());
  76. location.setUsername(_usernameInput.getText().toString());
  77. location.setPassword(_passwordInput.getText().toString());
  78. location.setDomain(_domainInput.getText().toString());
  79. location.setHttps(_checkBoxHttps.isChecked());
  80. _nameInput.setError(null);
  81. _usernameInput.setError(null);
  82. _passwordInput.setError(null);
  83. _domainInput.setError(null);
  84. if (location.getName().isEmpty()) {
  85. _nameInput.setError(getResources().getString(R.string.error_name));
  86. _nameInput.requestFocus();
  87. return null;
  88. }
  89. if (location.getUsername().isEmpty()) {
  90. _usernameInput.setError(getResources().getString(R.string.error_username));
  91. _usernameInput.requestFocus();
  92. return null;
  93. }
  94. if (location.getPassword().isEmpty()) {
  95. _passwordInput.setError(getResources().getString(R.string.error_password));
  96. _passwordInput.requestFocus();
  97. return null;
  98. }
  99. if (location.getDomain().isEmpty()) {
  100. _domainInput.setError(getResources().getString(R.string.error_domain));
  101. _domainInput.requestFocus();
  102. return null;
  103. }
  104. return location;
  105. }
  106. private void addAccount(LocationDbo location)
  107. {
  108. String username = location.getPseudoMail();
  109. AccountManager accountManager = AccountManager.get(this);
  110. Account newUserAccount = new Account(username, getResources().getString(R.string.account_type));
  111. boolean accountCreated = accountManager.addAccountExplicitly(newUserAccount, location.getPassword(), null);
  112. if (accountCreated) {
  113. accountManager.setUserData(newUserAccount, "Https", location.isHttps() ? "true" : "false");
  114. accountManager.setUserData(newUserAccount, "Domain", location.getDomain());
  115. Bundle result = new Bundle();
  116. result.putString(AccountManager.KEY_ACCOUNT_NAME, username);
  117. result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.account_type));
  118. if (_response != null) {
  119. _response.onResult(result);
  120. }
  121. finish();
  122. }
  123. }
  124. private void check()
  125. {
  126. final LocationDbo location = validate();
  127. if (location == null) {
  128. return;
  129. }
  130. LoginDialog dlg = new LoginDialog(this);
  131. dlg.login(location).then(new LuPromise.LuConsumer<LuLoginDbo>() {
  132. @Override
  133. public void execute(LuLoginDbo data) {
  134. addAccount(location);
  135. }
  136. }, new LuPromise.LuConsumer<LuPromise.LuPromiseError>() {
  137. @Override
  138. public void execute(LuPromise.LuPromiseError error) {
  139. Toast.makeText(AddAccountActivity.this, error.toString(), Toast.LENGTH_LONG).show();
  140. }
  141. });
  142. }
  143. }