Guest User

Untitled

a guest
Dec 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. package com.elyot.activities.login;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.content.pm.PackageManager;
  7. import android.os.AsyncTask;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.support.annotation.Nullable;
  12. import android.support.design.widget.Snackbar;
  13. import android.support.v4.app.LoaderManager;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.text.TextUtils;
  16. import android.view.KeyEvent;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.view.inputmethod.EditorInfo;
  20. import android.widget.AutoCompleteTextView;
  21. import android.widget.Button;
  22. import android.widget.EditText;
  23. import android.widget.TextView;
  24.  
  25. import com.elyot.R;
  26. import com.elyot.api.APIClientToken;
  27. import com.elyot.api.AccessToken;
  28. import com.elyot.api.ElyotApiProperties;
  29. import com.elyot.api.ServiceGenerator;
  30. import com.elyot.utils.PropertiesUtil;
  31.  
  32. import retrofit2.Call;
  33.  
  34. import static android.Manifest.permission.READ_CONTACTS;
  35.  
  36. /**
  37. * A login screen that offers login via email/password.
  38. */
  39. public class LoginActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks {
  40.  
  41. /**
  42. * Id to identity READ_CONTACTS permission request.
  43. */
  44. private static final int REQUEST_READ_CONTACTS = 0;
  45.  
  46. /**
  47. * A dummy authentication store containing known user names and passwords.
  48. * TODO: remove after connecting to a real authentication system.
  49. */
  50. private static final String[] DUMMY_CREDENTIALS = new String[]{
  51. "foo@example.com:hello", "bar@example.com:world"
  52. };
  53. /**
  54. * Keep track of the login task to ensure we can cancel it if requested.
  55. */
  56. private UserLoginTask mAuthTask = null;
  57.  
  58. // UI references.
  59. private AutoCompleteTextView mEmailView;
  60. private EditText mPasswordView;
  61. private View mProgressView;
  62. private View mLoginFormView;
  63.  
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate( savedInstanceState );
  67. setContentView( R.layout.activity_login );
  68. // Set up the login form.
  69. mEmailView = (AutoCompleteTextView) findViewById( R.id.email );
  70. populateAutoComplete();
  71.  
  72. mPasswordView = (EditText) findViewById( R.id.password );
  73. mPasswordView.setOnEditorActionListener( new TextView.OnEditorActionListener() {
  74. @Override
  75. public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
  76. if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
  77. attemptLogin();
  78. return true;
  79. }
  80. return false;
  81. }
  82. });
  83.  
  84. Button mEmailSignInButton = (Button) findViewById( R.id.email_sign_in_button );
  85. mEmailSignInButton.setOnClickListener( new OnClickListener() {
  86. @Override
  87. public void onClick(View view) {
  88. attemptLogin();
  89. }
  90. } );
  91.  
  92. mLoginFormView = findViewById( R.id.login_form );
  93. mProgressView = findViewById( R.id.login_progress );
  94. }
  95.  
  96. private void populateAutoComplete() {
  97. if (!mayRequestContacts()) {
  98. return;
  99. }
  100. }
  101.  
  102. private boolean mayRequestContacts() {
  103. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  104. return true;
  105. }
  106. if (checkSelfPermission( READ_CONTACTS ) == PackageManager.PERMISSION_GRANTED) {
  107. return true;
  108. }
  109. if (shouldShowRequestPermissionRationale( READ_CONTACTS )) {
  110. Snackbar.make( mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE )
  111. .setAction( android.R.string.ok, new View.OnClickListener() {
  112. @Override
  113. @TargetApi(Build.VERSION_CODES.M)
  114. public void onClick(View v) {
  115. requestPermissions( new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS );
  116. }
  117. } );
  118. } else {
  119. requestPermissions( new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS );
  120. }
  121. return false;
  122. }
  123.  
  124. /**
  125. * Callback received when a permissions request has been completed.
  126. */
  127. @Override
  128. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  129. @NonNull int[] grantResults) {
  130. if (requestCode == REQUEST_READ_CONTACTS) {
  131. if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  132. populateAutoComplete();
  133. }
  134. }
  135. }
  136.  
  137.  
  138. /**
  139. * Attempts to sign in or register the account specified by the login form.
  140. * If there are form errors (invalid email, missing fields, etc.), the
  141. * errors are presented and no actual login attempt is made.
  142. */
  143. private void attemptLogin() {
  144. if (mAuthTask != null) {
  145. return;
  146. }
  147.  
  148. // Reset errors.
  149. mEmailView.setError( null );
  150. mPasswordView.setError( null );
  151.  
  152. // Store values at the time of the login attempt.
  153. String email = mEmailView.getText().toString();
  154. String password = mPasswordView.getText().toString();
  155.  
  156. boolean cancel = false;
  157. View focusView = null;
  158.  
  159. // Check for a valid password, if the user entered one.
  160. if (!TextUtils.isEmpty( password ) && !isPasswordValid( password )) {
  161. mPasswordView.setError( getString( R.string.error_invalid_password ) );
  162. focusView = mPasswordView;
  163. cancel = true;
  164. }
  165.  
  166. // Check for a valid email address.
  167. if (TextUtils.isEmpty( email )) {
  168. mEmailView.setError( getString( R.string.error_field_required ) );
  169. focusView = mEmailView;
  170. cancel = true;
  171. } else if (!isEmailValid( email )) {
  172. mEmailView.setError( getString( R.string.error_invalid_email ) );
  173. focusView = mEmailView;
  174. cancel = true;
  175. }
  176.  
  177. if (cancel) {
  178. // There was an error; don't attempt login and focus the first
  179. // form field with an error.
  180. focusView.requestFocus();
  181. } else {
  182. // Show a progress spinner, and kick off a background task to
  183. // perform the user login attempt.
  184. showProgress( true );
  185. mAuthTask = new UserLoginTask( email, password );
  186. mAuthTask.execute( (Void) null );
  187. }
  188. }
  189.  
  190. private boolean isEmailValid(String email) {
  191. //TODO: Replace this with your own logic
  192. return email.contains( "@" );
  193. }
  194.  
  195. private boolean isPasswordValid(String password) {
  196. //TODO: Replace this with your own logic
  197. return password.length() > 4;
  198. }
  199.  
  200. /**
  201. * Shows the progress UI and hides the login form.
  202. */
  203. @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  204. private void showProgress(final boolean show) {
  205. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  206. // for very easy animations. If available, use these APIs to fade-in
  207. // the progress spinner.
  208. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  209. int shortAnimTime = getResources().getInteger( android.R.integer.config_shortAnimTime );
  210.  
  211. mLoginFormView.setVisibility( show ? View.GONE : View.VISIBLE );
  212. mLoginFormView.animate().setDuration( shortAnimTime ).alpha(
  213. show ? 0 : 1 ).setListener( new AnimatorListenerAdapter() {
  214. @Override
  215. public void onAnimationEnd(Animator animation) {
  216. mLoginFormView.setVisibility( show ? View.GONE : View.VISIBLE );
  217. }
  218. } );
  219.  
  220. mProgressView.setVisibility( show ? View.VISIBLE : View.GONE );
  221. mProgressView.animate().setDuration( shortAnimTime ).alpha(
  222. show ? 1 : 0 ).setListener( new AnimatorListenerAdapter() {
  223. @Override
  224. public void onAnimationEnd(Animator animation) {
  225. mProgressView.setVisibility( show ? View.VISIBLE : View.GONE );
  226. }
  227. } );
  228. } else {
  229. // The ViewPropertyAnimator APIs are not available, so simply show
  230. // and hide the relevant UI components.
  231. mProgressView.setVisibility( show ? View.VISIBLE : View.GONE );
  232. mLoginFormView.setVisibility( show ? View.GONE : View.VISIBLE );
  233. }
  234.  
  235. }
  236.  
  237.  
  238. /**
  239. * Represents an asynchronous login/registration task used to authenticate
  240. * the user.
  241. */
  242. public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
  243.  
  244. private final String mEmail;
  245. private final String mPassword;
  246.  
  247. UserLoginTask(String email, String password) {
  248. mEmail = email;
  249. mPassword = password;
  250. }
  251.  
  252. @Override
  253. protected Boolean doInBackground(Void... params) {
  254. // TODO: attempt authentication against a network service.
  255.  
  256. try {
  257. // Simulate network access.
  258. Thread.sleep( 2000 );
  259. } catch (InterruptedException e) {
  260. return false;
  261. }
  262.  
  263. for (String credential : DUMMY_CREDENTIALS) {
  264. String[] pieces = credential.split( ":" );
  265. if (pieces[0].equals( mEmail )) {
  266. // Account exists, return true if the password matches.
  267. return pieces[1].equals( mPassword );
  268. }
  269. }
  270.  
  271. // TODO: register the new account here.
  272. return true;
  273. }
  274.  
  275. @Override
  276. protected void onPostExecute(final Boolean success) {
  277. mAuthTask = null;
  278. showProgress( false );
  279.  
  280. if (success) {
  281. finish();
  282. } else {
  283. mPasswordView.setError( getString( R.string.error_incorrect_password ) );
  284. mPasswordView.requestFocus();
  285. }
  286. }
  287.  
  288. @Override
  289. protected void onCancelled() {
  290. mAuthTask = null;
  291. showProgress( false );
  292. }
  293. }
  294.  
  295. @NonNull
  296. @Override
  297. public android.support.v4.content.Loader onCreateLoader(int i, @Nullable Bundle bundle) {
  298. return null;
  299. }
  300.  
  301. @Override
  302. public void onLoadFinished(@NonNull android.support.v4.content.Loader loader, Object o) {
  303.  
  304. }
  305.  
  306. @Override
  307. public void onLoaderReset(@NonNull android.support.v4.content.Loader loader) {
  308.  
  309. }
  310.  
  311. /*
  312.  
  313. private void addEmailsToAutoComplete(List<String> emailAddressCollection) {
  314. //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list.
  315. ArrayAdapter<String> adapter =
  316. new ArrayAdapter<>( LoginActivity.this,
  317. android.R.layout.simple_dropdown_item_1line, emailAddressCollection );
  318.  
  319. mEmailView.setAdapter( adapter );
  320. }
  321.  
  322.  
  323. private interface ProfileQuery {
  324. String[] PROJECTION = {
  325. ContactsContract.CommonDataKinds.Email.ADDRESS,
  326. ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
  327. };
  328.  
  329. int ADDRESS = 0;
  330. int IS_PRIMARY = 1;
  331. }
  332. */
  333.  
  334. }
Add Comment
Please, Sign In to add comment