Advertisement
Guest User

Untitled

a guest
Sep 21st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. package com.example.dep.unipiinformaticsapp;
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.annotation.TargetApi;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.AsyncTask;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.text.TextUtils;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.AutoCompleteTextView;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.BufferedWriter;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.io.OutputStream;
  26. import java.io.OutputStreamWriter;
  27. import java.net.HttpURLConnection;
  28. import java.net.MalformedURLException;
  29. import java.net.URL;
  30. import java.net.URLEncoder;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33.  
  34. public class LoginActivity extends AppCompatActivity {
  35.  
  36. public static final Pattern VALID_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
  37. public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
  38.  
  39.  
  40. private static final String[] DUMMY_CREDENTIALS = new String[]{
  41. "p13053:!11AA!", "p13025:!22BB!"
  42. };
  43.  
  44.  
  45. private UserLoginTask mAuthTask = null;
  46. private AutoCompleteTextView mUsernameView;
  47. private EditText mPasswordView;
  48. private View mProgressView;
  49. private View mLoginFormView;
  50.  
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_login);
  55. mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
  56. mPasswordView = (EditText) findViewById(R.id.password);
  57.  
  58. Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
  59. mUsernameSignInButton.setOnClickListener(new OnClickListener() {
  60. @Override
  61. public void onClick(View view) {
  62. attemptLogin();
  63. }
  64. });
  65.  
  66. mLoginFormView = findViewById(R.id.login_form);
  67. mProgressView = findViewById(R.id.login_progress);
  68. }
  69.  
  70. // Attempts to sign in and presents any form errors (invalid username, missing fields, etc.)
  71. private void attemptLogin() {
  72. if (mAuthTask != null)
  73. return;
  74.  
  75. // Reset errors.
  76. mUsernameView.setError(null);
  77. mPasswordView.setError(null);
  78.  
  79. // Store values at the time of the login attempt.
  80. String username = mUsernameView.getText().toString();
  81. String password = mPasswordView.getText().toString();
  82.  
  83. boolean cancel = false;
  84. View focusView = null;
  85.  
  86. // Check for a valid password, if the user entered one.
  87. if (TextUtils.isEmpty(password)) {
  88. mPasswordView.setError(getString(R.string.error_field_required));
  89. focusView = mPasswordView;
  90. cancel = true;
  91. } else if (!isPasswordValid(password)) {
  92. mPasswordView.setError("The password is incorrect.");
  93. focusView = mPasswordView;
  94. cancel = true;
  95. }
  96.  
  97. // Check for a valid username.
  98. if (TextUtils.isEmpty(username)) {
  99. mUsernameView.setError(getString(R.string.error_field_required));
  100. focusView = mUsernameView;
  101. cancel = true;
  102. } else if (!isUsernameValid(username)) {
  103. mUsernameView.setError("The username is incorrect.");
  104. focusView = mUsernameView;
  105. cancel = true;
  106. }
  107.  
  108. if (cancel) { // There was an error; don't attempt login and focus the field with an error.
  109. focusView.requestFocus();
  110. } else { // Show a progress spinner, and kick off a background task to perform the user login attempt.
  111. showProgress(true);
  112. mAuthTask = new UserLoginTask(username, password, LoginActivity.this.getApplicationContext());
  113. mAuthTask.execute((String) null);
  114. }
  115. }
  116.  
  117. private boolean isUsernameValid(String username) {
  118. Matcher matcher = VALID_USERNAME_REGEX .matcher(username);
  119. return matcher.find();
  120. }
  121.  
  122. private boolean isPasswordValid(String password) {
  123. Matcher matcher = VALID_PASSWORD_REGEX .matcher(password);
  124. return matcher.find();
  125. }
  126.  
  127. //Shows the progress UI and hides the login form.
  128. @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
  129. private void showProgress(final boolean show) {
  130. // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
  131. // for very easy animations. If available, use these APIs to fade-in
  132. // the progress spinner.
  133. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
  134. int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
  135.  
  136. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  137. mLoginFormView.animate().setDuration(shortAnimTime).alpha(
  138. show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
  139. @Override
  140. public void onAnimationEnd(Animator animation) {
  141. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  142. }
  143. });
  144.  
  145. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  146. mProgressView.animate().setDuration(shortAnimTime).alpha(
  147. show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
  148. @Override
  149. public void onAnimationEnd(Animator animation) {
  150. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  151. }
  152. });
  153. } else {
  154. // The ViewPropertyAnimator APIs are not available, so simply show
  155. // and hide the relevant UI components.
  156. mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
  157. mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
  158. }
  159. }
  160.  
  161. public class UserLoginTask extends AsyncTask<String, Void, String> {
  162.  
  163. Context context;
  164. private final String mUsername;
  165. private final String mPassword;
  166.  
  167. UserLoginTask(String username, String password, Context context) {
  168. mUsername = username;
  169. mPassword = password;
  170. this.context = context;
  171. }
  172.  
  173. @Override
  174. protected String doInBackground(String... params) {
  175.  
  176. try {
  177. URL url = new URL("http://10.0.2.2/login.php");
  178. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  179. httpURLConnection.setRequestMethod("POST");
  180. httpURLConnection.setDoOutput(true);
  181. httpURLConnection.setDoInput(true);
  182. OutputStream outputStream = httpURLConnection.getOutputStream();
  183. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  184. String data = URLEncoder.encode("Username", "UTF-8")+" = "+URLEncoder.encode(mUsername,"UTF-8")+" & " + URLEncoder.encode("Password", "UTF-8")+" = "+URLEncoder.encode(mPassword,"UTF-8");
  185. bufferedWriter.write(data);
  186. bufferedWriter.flush();
  187. bufferedWriter.close();
  188. outputStream.close();
  189.  
  190. InputStream inputStream = httpURLConnection.getInputStream();
  191. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  192. String response = "", line = "";
  193. while ((line = bufferedReader.readLine()) != null){
  194. response += line;
  195. }
  196. bufferedReader.close();
  197. inputStream.close();
  198. httpURLConnection.disconnect();
  199. return response;
  200.  
  201. } catch (MalformedURLException e) {
  202. e.printStackTrace();
  203. } catch(IOException e){
  204. e.printStackTrace();
  205. }
  206. return null;
  207. }
  208.  
  209. @Override
  210. protected void onPostExecute(String result) {
  211.  
  212. Toast.makeText(context,result,Toast.LENGTH_LONG).show();
  213.  
  214. //mAuthTask = null;
  215. //showProgress(false);
  216.  
  217. //if (success) {
  218. // //finish();
  219. // Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
  220. // LoginActivity.this.startActivity(mainpageIntent);
  221. //} else {
  222. // mPasswordView.setError(getString(R.string.error_incorrect_password));
  223. // mPasswordView.requestFocus();
  224. //}
  225. }
  226.  
  227. @Override
  228. protected void onCancelled() {
  229. mAuthTask = null;
  230. showProgress(false);
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement