Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. package com.example.dep.androidapp;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.support.annotation.NonNull;
  9. import android.support.design.widget.Snackbar;
  10. import android.support.v7.app.AppCompatActivity;
  11. import android.app.LoaderManager.LoaderCallbacks;
  12. import android.content.CursorLoader;
  13. import android.content.Loader;
  14. import android.database.Cursor;
  15. import android.net.Uri;
  16. import android.os.AsyncTask;
  17. import android.os.Build;
  18. import android.os.Bundle;
  19. import android.provider.ContactsContract;
  20. import android.text.TextUtils;
  21. import android.view.KeyEvent;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.view.inputmethod.EditorInfo;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.AutoCompleteTextView;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.TextView;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.regex.Matcher;
  34. import java.util.regex.Pattern;
  35.  
  36. import static android.Manifest.permission.READ_CONTACTS;
  37.  
  38. // A login screen that offers login via username/password.
  39.  
  40. public class LoginActivity extends AppCompatActivity {
  41.  
  42. public static final Pattern VALID_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
  43. public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
  44.  
  45. // A dummy authentication store containing known user names and passwords.
  46. // TODO: remove after connecting to a real authentication system.
  47.  
  48. private static final String[] DUMMY_CREDENTIALS = new String[]{
  49. "p13053:!11AA!", "p13025:!22BB!"
  50. };
  51.  
  52. // Keep track of the login task to ensure we can cancel it if requested.
  53. private UserLoginTask mAuthTask = null;
  54.  
  55. private AutoCompleteTextView mUsernameView;
  56. private EditText mPasswordView;
  57. private View mProgressView;
  58. private View mLoginFormView;
  59.  
  60. @Override
  61. protected void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.activity_login);
  64. mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
  65. mPasswordView = (EditText) findViewById(R.id.password);
  66.  
  67. Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
  68. mUsernameSignInButton.setOnClickListener(new OnClickListener() {
  69. @Override
  70. public void onClick(View view) {
  71. attemptLogin();
  72. }
  73. });
  74.  
  75. mLoginFormView = findViewById(R.id.login_form);
  76. mProgressView = findViewById(R.id.login_progress);
  77. }
  78.  
  79. /* Attempts to sign in or register the account specified by the login form. If there are form
  80. errors (invalid username, missing fields, etc.), the errors are presented */
  81. private void attemptLogin() {
  82. if (mAuthTask != null) {
  83. return;
  84. }
  85.  
  86. // Reset errors.
  87. mUsernameView.setError(null);
  88. mPasswordView.setError(null);
  89.  
  90. // Store values at the time of the login attempt.
  91. String username = mUsernameView.getText().toString();
  92. String password = mPasswordView.getText().toString();
  93.  
  94. boolean cancel = false;
  95. View focusView = null;
  96.  
  97. // Check for a valid password, if the user entered one.
  98. if (TextUtils.isEmpty(password)) {
  99. mPasswordView.setError(getString(R.string.error_field_required));
  100. focusView = mPasswordView;
  101. cancel = true;
  102. } else if (!isPasswordValid(password)) {
  103. mPasswordView.setError("The password is incorrect.");
  104. focusView = mPasswordView;
  105. cancel = true;
  106. }
  107.  
  108. // Check for a valid username.
  109. if (TextUtils.isEmpty(username)) {
  110. mUsernameView.setError(getString(R.string.error_field_required));
  111. focusView = mUsernameView;
  112. cancel = true;
  113. } else if (!isUsernameValid(username)) {
  114. mUsernameView.setError("The username is incorrect.");
  115. focusView = mUsernameView;
  116. cancel = true;
  117. }
  118.  
  119. if (cancel) { // There was an error; don't attempt login and focus the field with an error.
  120. focusView.requestFocus();
  121. } else { // Show a progress spinner, and kick off a background task to perform the user login attempt.
  122. showProgress(true);
  123. mAuthTask = new UserLoginTask(username, password);
  124. mAuthTask.execute((Void) null);
  125. }
  126. }
  127.  
  128. private boolean isUsernameValid(String username) {
  129. Matcher matcher = VALID_USERNAME_REGEX .matcher(username);
  130. return matcher.find();
  131. }
  132.  
  133. private boolean isPasswordValid(String password) {
  134. Matcher matcher = VALID_PASSWORD_REGEX .matcher(password);
  135. return matcher.find();
  136. }
  137.  
  138. //Shows the progress UI and hides the login form.
  139. @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  140. private void showProgress(final boolean show) {
  141. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  142. // for very easy animations. If available, use these APIs to fade-in
  143. // the progress spinner.
  144. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  145. int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  146.  
  147. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  148. mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  149. show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  150. @Override
  151. public void onAnimationEnd(Animator animation) {
  152. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  153. }
  154. });
  155.  
  156. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  157. mProgressView.animate().setDuration(shortAnimTime).alpha(
  158. show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  159. @Override
  160. public void onAnimationEnd(Animator animation) {
  161. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  162. }
  163. });
  164. } else {
  165. // The ViewPropertyAnimator APIs are not available, so simply show
  166. // and hide the relevant UI components.
  167. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  168. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  169. }
  170. }
  171.  
  172. public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
  173.  
  174. private final String mUsername;
  175. private final String mPassword;
  176.  
  177. UserLoginTask(String username, String password) {
  178. mUsername = username;
  179. mPassword = password;
  180. }
  181.  
  182. @Override
  183. protected Boolean doInBackground(Void... params) {
  184. // TODO: attempt authentication against a network service.
  185.  
  186. try { // Simulate network access.
  187. Thread.sleep(2000);
  188. } catch (InterruptedException e) {
  189. return false;
  190. }
  191.  
  192. for (String credential : DUMMY_CREDENTIALS) {
  193. String[] pieces = credential.split(":");
  194. if (pieces[0].equals(mUsername)) {
  195. // Account exists, return true if the password matches.
  196. return pieces[1].equals(mPassword);
  197. }
  198. }
  199. return true;
  200. }
  201.  
  202. @Override
  203. protected void onPostExecute(final Boolean success) {
  204. mAuthTask = null;
  205. showProgress(false);
  206.  
  207. if (success) {
  208. //finish();
  209. Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
  210. LoginActivity.this.startActivity(mainpageIntent);
  211. } else {
  212. mPasswordView.setError(getString(R.string.error_incorrect_password));
  213. mPasswordView.requestFocus();
  214. }
  215. }
  216.  
  217. @Override
  218. protected void onCancelled() {
  219. mAuthTask = null;
  220. showProgress(false);
  221. }
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement