package net.plil.clubinfo.etunicorn.app; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.annotation.TargetApi; import android.app.DownloadManager; import android.content.Intent; import android.content.pm.PackageManager; import android.support.annotation.NonNull; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.app.LoaderManager.LoaderCallbacks; import android.content.CursorLoader; import android.content.Loader; import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.text.TextUtils; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.google.gson.Gson; import net.plil.clubinfo.etunicorn.R; import net.plil.clubinfo.etunicorn.data.Personne; import net.plil.clubinfo.etunicorn.data.User; import net.plil.clubinfo.etunicorn.utils.VolleyUtils; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import static android.Manifest.permission.READ_CONTACTS; /** * A login screen that offers login via email/password. */ public class LoginActivity extends AppCompatActivity { /** * Id to identity READ_CONTACTS permission request. */ private static final int REQUEST_READ_CONTACTS = 0; /** * Keep track of the login task to ensure we can cancel it if requested. */ // UI references. private EditText mUsernameView; private EditText mPasswordView; private View mProgressView; private View mLoginFormView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); // Set up the login form. mUsernameView = (EditText) findViewById(R.id.username); mPasswordView = (EditText) findViewById(R.id.password); mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == R.id.login || id == EditorInfo.IME_NULL) { attemptLogin(); return true; } return false; } }); Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); mEmailSignInButton.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 or register the account specified by the login form. * If there are form errors (invalid email, missing fields, etc.), the * errors are presented and no actual login attempt is made. */ private void attemptLogin() { // Reset errors. mUsernameView.setError(null); mPasswordView.setError(null); // Store values at the time of the login attempt. String email = mUsernameView.getText().toString(); String username = mPasswordView.getText().toString(); showProgress(true); tryLogin(email, username); } /** * 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); } } /** * You need a polytech Lille Account * * @param username The username of the person * @param password The password of the person */ private void tryLogin (String username, String password){ User user = new User(); user.setPassword(password); user.setUsername(username); JsonObjectRequest jsonObjectRequest = null; try { jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, VolleyUtils.baseUri , new JSONObject(new Gson().toJson(user)), new Response.Listener() { @Override public void onResponse(JSONObject response) { showProgress(false); Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { showProgress(false); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); //TODO remove this line Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); // END } } ); } catch (JSONException e) { e.printStackTrace(); } VolleyUtils.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest); } }