Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.98 KB | None | 0 0
  1. package com.hacktues.mentors;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.support.design.widget.Snackbar;
  6. import android.content.pm.ActivityInfo;
  7. import android.os.Bundle;
  8. import android.text.InputType;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14.  
  15. import com.afollestad.materialdialogs.MaterialDialog;
  16. import com.android.volley.RequestQueue;
  17. import com.android.volley.Response;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.android.volley.toolbox.Volley;
  21.  
  22. import org.json.JSONObject;
  23.  
  24. import java.util.HashMap;
  25.  
  26. public class LoginActivity extends Activity implements View.OnClickListener {
  27.  
  28.     private static final String MENTOR_PASSWORD = "_hacktues2674";
  29.  
  30.     Context context;
  31.  
  32.     TextView email;
  33.     TextView password;
  34.     Button loginButton;
  35.     TextView loginAsMentor;
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_login);
  41.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  42.  
  43.         email = (TextView) this.findViewById(R.id.input_email);
  44.         password = (TextView) this.findViewById(R.id.input_password);
  45.  
  46.         loginButton = (Button) this.findViewById(R.id.btn_login);
  47.         loginButton.setOnClickListener(this);
  48.  
  49.         loginAsMentor = (TextView) this.findViewById(R.id.link_join_mentor);
  50.         loginAsMentor.setOnClickListener(this);
  51.  
  52.         if (SharedPreferencesManager.getName(this) != null) {
  53.             finish();
  54.             return;
  55.         }
  56.  
  57.         context = this;
  58.     }
  59.  
  60.     @Override
  61.     public boolean onCreateOptionsMenu(Menu menu) {
  62.         getMenuInflater().inflate(R.menu.menu_main, menu);
  63.         return true;
  64.     }
  65.  
  66.     @Override
  67.     public boolean onOptionsItemSelected(MenuItem item) {
  68.         int id = item.getItemId();
  69.  
  70.         if (id == R.id.action_logout) {
  71.             return true;
  72.         }
  73.  
  74.         return super.onOptionsItemSelected(item);
  75.     }
  76.  
  77.     @Override
  78.     public void onClick(View v) {
  79.         switch (v.getId()) {
  80.             case R.id.btn_login:
  81.                 makeLoginRequest();
  82.                 break;
  83.             case R.id.link_join_mentor:
  84.                 new MaterialDialog.Builder(context)
  85.                         .title("Enter as mentor")
  86.                         .content("Please enter the mentor password:")
  87.                         .inputType(InputType.TYPE_TEXT_VARIATION_NORMAL)
  88.                         .input("Secret password", "", new MaterialDialog.InputCallback() {
  89.                             @Override
  90.                             public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
  91.                                 String password = charSequence.toString();
  92.  
  93.                                 if (password.equals(MENTOR_PASSWORD)) {
  94.                                     new MaterialDialog.Builder(context)
  95.                                             .title("Enter as mentor")
  96.                                             .content("What's your name?")
  97.                                             .inputType(InputType.TYPE_TEXT_VARIATION_NORMAL | InputType.TYPE_TEXT_FLAG_CAP_WORDS)
  98.                                             .input("How should we call you?", "", new MaterialDialog.InputCallback() {
  99.                                                 @Override
  100.                                                 public void onInput(MaterialDialog materialDialog, CharSequence charSequence) {
  101.                                                     String name = charSequence.toString();
  102.  
  103.                                                     SharedPreferencesManager.setName(context, name);
  104.                                                     SharedPreferencesManager.makeMentor(context);
  105.                                                     Snackbar.make(getCurrentFocus(),
  106.                                                             "You have successfully logged in as a mentor!", Snackbar.LENGTH_LONG).show();
  107.                                                     finish();
  108.                                                     return;
  109.                                                 }
  110.                                             })
  111.                                             .show();
  112.                                 } else {
  113.                                     Snackbar.make(getCurrentFocus(), "Invalid mentor password", Snackbar.LENGTH_LONG).show();
  114.                                 }
  115.                             }
  116.                         })
  117.                         .show();
  118.                 break;
  119.         }
  120.     }
  121.  
  122.     private void makeLoginRequest() {
  123.         RequestQueue queue = Volley.newRequestQueue(context);
  124.         final String URL = "http://hacktues.com/login/remote";
  125.  
  126.         HashMap<String, HashMap<String, String>> params = new HashMap<String, HashMap<String, String>>();
  127.         HashMap<String, String> data = new HashMap<String, String>();
  128.  
  129.         data.put("email", email.getText().toString());
  130.         data.put("password", password.getText().toString());
  131.         data.put("remember_me", "0");
  132.  
  133.         params.put("session", data);
  134.  
  135.         JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
  136.                 new Response.Listener<JSONObject>() {
  137.                     @Override
  138.                     public void onResponse(JSONObject response) {
  139.                         try {
  140.                             JSONObject user = response.getJSONObject("user");
  141.                             SharedPreferencesManager.setName(context, user.getString("name").toString());
  142.                             if (response.getString("team") != "false") {
  143.                                 JSONObject team = response.getJSONObject("team");
  144.  
  145.                                 SharedPreferencesManager.setTeamName(context, team.getString("name"));
  146.                                 SharedPreferencesManager.setRoom(context, team.getString("room"));
  147.                             } else {
  148.                                 SharedPreferencesManager.setTeamName(context, "No team");
  149.                                 SharedPreferencesManager.setRoom(context, "0");
  150.                             }
  151.  
  152.  
  153.                             Snackbar.make(getCurrentFocus(), "Successfully logged in!", Snackbar.LENGTH_LONG).show();
  154.                             finish();
  155.                             return;
  156.                         } catch (Exception e) {
  157.                             e.printStackTrace();
  158.                         }
  159.                     }
  160.                 }, new Response.ErrorListener() {
  161.             @Override
  162.             public void onErrorResponse(VolleyError error) {
  163.                 Snackbar.make(getCurrentFocus(), "Something went wrong!", Snackbar.LENGTH_LONG).show();
  164.                 error.printStackTrace();
  165.             }
  166.         });
  167.  
  168.         queue.add(req);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement