Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hacktues.mentors;
- import android.app.Activity;
- import android.content.Context;
- import android.support.design.widget.Snackbar;
- import android.content.pm.ActivityInfo;
- import android.os.Bundle;
- import android.text.InputType;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.afollestad.materialdialogs.MaterialDialog;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONObject;
- import java.util.HashMap;
- public class LoginActivity extends Activity implements View.OnClickListener {
- private static final String MENTOR_PASSWORD = "_hacktues2674";
- Context context;
- TextView email;
- TextView password;
- Button loginButton;
- TextView loginAsMentor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- email = (TextView) this.findViewById(R.id.input_email);
- password = (TextView) this.findViewById(R.id.input_password);
- loginButton = (Button) this.findViewById(R.id.btn_login);
- loginButton.setOnClickListener(this);
- loginAsMentor = (TextView) this.findViewById(R.id.link_join_mentor);
- loginAsMentor.setOnClickListener(this);
- if (SharedPreferencesManager.getName(this) != null) {
- finish();
- return;
- }
- context = this;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_logout) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_login:
- makeLoginRequest();
- break;
- case R.id.link_join_mentor:
- new MaterialDialog.Builder(context)
- .title("Enter as mentor")
- .content("Please enter the mentor password:")
- .inputType(InputType.TYPE_TEXT_VARIATION_NORMAL)
- .input("Secret password", "", new MaterialDialog.InputCallback() {
- @Override
- public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
- String password = charSequence.toString();
- if (password.equals(MENTOR_PASSWORD)) {
- new MaterialDialog.Builder(context)
- .title("Enter as mentor")
- .content("What's your name?")
- .inputType(InputType.TYPE_TEXT_VARIATION_NORMAL | InputType.TYPE_TEXT_FLAG_CAP_WORDS)
- .input("How should we call you?", "", new MaterialDialog.InputCallback() {
- @Override
- public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
- String name = charSequence.toString();
- SharedPreferencesManager.setName(context, name);
- SharedPreferencesManager.makeMentor(context);
- Snackbar.make(getCurrentFocus(),
- "You have successfully logged in as a mentor!", Snackbar.LENGTH_LONG).show();
- finish();
- return;
- }
- })
- .show();
- } else {
- Snackbar.make(getCurrentFocus(), "Invalid mentor password", Snackbar.LENGTH_LONG).show();
- }
- }
- })
- .show();
- break;
- }
- }
- private void makeLoginRequest() {
- RequestQueue queue = Volley.newRequestQueue(context);
- final String URL = "http://hacktues.com/login/remote";
- HashMap<String, HashMap<String, String>> params = new HashMap<String, HashMap<String, String>>();
- HashMap<String, String> data = new HashMap<String, String>();
- data.put("email", email.getText().toString());
- data.put("password", password.getText().toString());
- data.put("remember_me", "0");
- params.put("session", data);
- JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- JSONObject user = response.getJSONObject("user");
- SharedPreferencesManager.setName(context, user.getString("name").toString());
- if (response.getString("team") != "false") {
- JSONObject team = response.getJSONObject("team");
- SharedPreferencesManager.setTeamName(context, team.getString("name"));
- SharedPreferencesManager.setRoom(context, team.getString("room"));
- } else {
- SharedPreferencesManager.setTeamName(context, "No team");
- SharedPreferencesManager.setRoom(context, "0");
- }
- Snackbar.make(getCurrentFocus(), "Successfully logged in!", Snackbar.LENGTH_LONG).show();
- finish();
- return;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Snackbar.make(getCurrentFocus(), "Something went wrong!", Snackbar.LENGTH_LONG).show();
- error.printStackTrace();
- }
- });
- queue.add(req);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement