Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.dep.unipiinformaticsapp;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class LoginActivity extends AppCompatActivity {
- public static final Pattern VALID_USERNAME_REGEX = Pattern.compile("^p[0-9]{5}$", Pattern.CASE_INSENSITIVE);
- public static final Pattern VALID_PASSWORD_REGEX = Pattern.compile("^![0-9]{2}[A-Z]{2}!$", Pattern.CASE_INSENSITIVE);
- private static final String[] DUMMY_CREDENTIALS = new String[]{
- "p13053:!11AA!", "p13025:!22BB!"
- };
- private UserLoginTask mAuthTask = null;
- private AutoCompleteTextView mUsernameView;
- private EditText mPasswordView;
- private View mProgressView;
- private View mLoginFormView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- mUsernameView = (AutoCompleteTextView) findViewById(R.id.username);
- mPasswordView = (EditText) findViewById(R.id.password);
- Button mUsernameSignInButton = (Button) findViewById(R.id.btn_login);
- mUsernameSignInButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- attemptLogin();
- }
- });
- mLoginFormView = findViewById(R.id.login_form);
- mProgressView = findViewById(R.id.login_progress);
- }
- // Attempts to sign in and presents any form errors (invalid username, missing fields, etc.)
- private void attemptLogin() {
- if (mAuthTask != null)
- return;
- // Reset errors.
- mUsernameView.setError(null);
- mPasswordView.setError(null);
- // Store values at the time of the login attempt.
- String username = mUsernameView.getText().toString();
- String password = mPasswordView.getText().toString();
- boolean cancel = false;
- View focusView = null;
- // Check for a valid password, if the user entered one.
- if (TextUtils.isEmpty(password)) {
- mPasswordView.setError(getString(R.string.error_field_required));
- focusView = mPasswordView;
- cancel = true;
- } else if (!isPasswordValid(password)) {
- mPasswordView.setError("The password is incorrect.");
- focusView = mPasswordView;
- cancel = true;
- }
- // Check for a valid username.
- if (TextUtils.isEmpty(username)) {
- mUsernameView.setError(getString(R.string.error_field_required));
- focusView = mUsernameView;
- cancel = true;
- } else if (!isUsernameValid(username)) {
- mUsernameView.setError("The username is incorrect.");
- focusView = mUsernameView;
- cancel = true;
- }
- if (cancel) { // There was an error; don't attempt login and focus the field with an error.
- focusView.requestFocus();
- } else { // Show a progress spinner, and kick off a background task to perform the user login attempt.
- showProgress(true);
- mAuthTask = new UserLoginTask(username, password, LoginActivity.this.getApplicationContext());
- mAuthTask.execute((String) null);
- }
- }
- private boolean isUsernameValid(String username) {
- Matcher matcher = VALID_USERNAME_REGEX .matcher(username);
- return matcher.find();
- }
- private boolean isPasswordValid(String password) {
- Matcher matcher = VALID_PASSWORD_REGEX .matcher(password);
- return matcher.find();
- }
- //Shows the progress UI and hides the login form.
- @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
- private void showProgress(final boolean show) {
- // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
- // for very easy animations. If available, use these APIs to fade-in
- // the progress spinner.
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
- int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
- mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
- mLoginFormView.animate().setDuration(shortAnimTime).alpha(
- show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
- }
- });
- mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
- mProgressView.animate().setDuration(shortAnimTime).alpha(
- show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
- }
- });
- } else {
- // The ViewPropertyAnimator APIs are not available, so simply show
- // and hide the relevant UI components.
- mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
- mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
- }
- }
- public class UserLoginTask extends AsyncTask<String, Void, String> {
- Context context;
- private final String mUsername;
- private final String mPassword;
- UserLoginTask(String username, String password, Context context) {
- mUsername = username;
- mPassword = password;
- this.context = context;
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- URL url = new URL("http://10.0.2.2/login.php");
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String data = URLEncoder.encode("Username", "UTF-8")+" = "+URLEncoder.encode(mUsername,"UTF-8")+" & " + URLEncoder.encode("Password", "UTF-8")+" = "+URLEncoder.encode(mPassword,"UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String response = "", line = "";
- while ((line = bufferedReader.readLine()) != null){
- response += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return response;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- Toast.makeText(context,result,Toast.LENGTH_LONG).show();
- //mAuthTask = null;
- //showProgress(false);
- //if (success) {
- // //finish();
- // Intent mainpageIntent = new Intent(LoginActivity.this, NavigationActivity.class);
- // LoginActivity.this.startActivity(mainpageIntent);
- //} else {
- // mPasswordView.setError(getString(R.string.error_incorrect_password));
- // mPasswordView.requestFocus();
- //}
- }
- @Override
- protected void onCancelled() {
- mAuthTask = null;
- showProgress(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement