Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. package lt.ebp.take2ga.authentication;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.graphics.Color;
  9. import android.graphics.LinearGradient;
  10. import android.graphics.Shader;
  11. import android.graphics.Typeface;
  12. import android.os.AsyncTask;
  13. import android.os.Bundle;
  14. import android.text.Spannable;
  15. import android.text.SpannableStringBuilder;
  16. import android.text.style.StyleSpan;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.view.View.OnClickListener;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27.  
  28. import lt.ebp.take2ga.MainActivity;
  29. import lt.ebp.take2ga.R;
  30. import lt.ebp.take2ga.utils.JSONParser;
  31. import lt.ebp.take2ga.utils.RequestManager;
  32.  
  33. /**
  34. * User login activity
  35. * Created by emilis on 16-04-02.
  36. */
  37. public class LoginActivity extends Activity implements OnClickListener, RequestManager.ResponseListener {
  38.  
  39. private EditText user, pass;
  40. private Button mSubmit, mRegister;
  41.  
  42. // Progress Dialog
  43. private ProgressDialog pDialog;
  44.  
  45. //JSON element ids from repsonse of php script:
  46. private static final String TAG_SUCCESS = "success";
  47. private static final String TAG_MESSAGE = "error";
  48.  
  49. RequestManager requestManager = new RequestManager();
  50.  
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.login);
  55.  
  56. //setup input fields
  57. user = (EditText)findViewById(R.id.email);
  58. pass = (EditText)findViewById(R.id.password);
  59.  
  60. TextView memberLogin = (TextView)findViewById(R.id.memberLogin);
  61. TextView notMember = (TextView)findViewById(R.id.not_a_member_yet);
  62. Button forgot = (Button)findViewById(R.id.forgot);
  63. Button login = (Button)findViewById(R.id.login);
  64. Button watch = (Button)findViewById(R.id.register);
  65.  
  66. Typeface geomanist = Typeface.createFromAsset(getAssets(), "fonts/geomanist-regular-webfont.ttf");
  67. Typeface roboto_medium = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
  68. Typeface gadugi = Typeface.createFromAsset(getAssets(), "fonts/gadugi.ttf");
  69. Typeface gadugi_bold = Typeface.createFromAsset(getAssets(), "fonts/gadugib.ttf");
  70.  
  71. final SpannableStringBuilder string_notmember = new SpannableStringBuilder(notMember.getText());
  72. final SpannableStringBuilder string_watch = new SpannableStringBuilder(watch.getText());
  73.  
  74. final StyleSpan geomanist_span = new StyleSpan(geomanist.getStyle());
  75. final StyleSpan gadugi_span = new StyleSpan(gadugi.getStyle());
  76. final StyleSpan gadugi_bold_span = new StyleSpan(gadugi_bold.getStyle());
  77.  
  78. string_notmember.setSpan(geomanist_span, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  79. string_notmember.setSpan(gadugi_bold_span, 6, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  80.  
  81. string_watch.setSpan(gadugi_span, 0, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  82. string_watch.setSpan(gadugi_bold_span, 11, 15, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  83. string_watch.setSpan(gadugi_span, 15, 18, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  84. string_watch.setSpan(gadugi_bold_span, 18, 26, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  85.  
  86. notMember.setText(string_notmember);
  87. watch.setText(string_watch);
  88.  
  89. memberLogin.setTypeface(geomanist);
  90.  
  91. Shader memberGradient=new LinearGradient(0, 0, 0, memberLogin.getPaint().getTextSize(),
  92. new int[]{Color.parseColor("#fa9338"),Color.parseColor("#ff7800")},
  93. new float[]{0, 1}, Shader.TileMode.CLAMP);
  94. memberLogin.getPaint().setShader(memberGradient);
  95.  
  96. forgot.setTypeface(geomanist);
  97. notMember.setTypeface(gadugi);
  98. login.setTypeface(gadugi_bold);
  99. user.setTypeface(roboto_medium);
  100. pass.setTypeface(roboto_medium);
  101.  
  102. //setup buttons
  103. mSubmit = (Button) findViewById(R.id.login);
  104. mRegister = (Button) findViewById(R.id.register);
  105.  
  106. //register listeners
  107. mSubmit.setOnClickListener(this);
  108. mRegister.setOnClickListener(this);
  109.  
  110. }
  111.  
  112. public void onClick(View v) {
  113. switch (v.getId()) {
  114. case R.id.login:
  115. if (JSONParser.isConnected(LoginActivity.this)) {
  116. new AttemptLogin().execute();
  117. } else {
  118. Toast.makeText(LoginActivity.this, "No internet access", Toast.LENGTH_LONG).show();
  119. }
  120. break;
  121. case R.id.register:
  122. Intent i = new Intent(this, RegisterActivity.class);
  123. startActivity(i);
  124. break;
  125.  
  126. default:
  127. break;
  128. }
  129. }
  130.  
  131. public void onResponseReceived(final JSONObject response) {
  132. try {
  133. int success = response.optInt(TAG_SUCCESS);
  134. if (success == 1) {
  135. Log.d("Login Successful!", response.toString());
  136. String token = response.getString("token");
  137. SharedPreferences sharedPreferences = getSharedPreferences("Login.loginPrefs", Context.MODE_PRIVATE);
  138. SharedPreferences.Editor editor = sharedPreferences.edit();
  139. editor.putString("loginToken", token);
  140. editor.commit();
  141. Intent i = new Intent(LoginActivity.this, MainActivity.class);
  142. finish();
  143. startActivity(i);
  144. } else {
  145. Log.d("Login Falure!", response.getString(TAG_MESSAGE));
  146. this.runOnUiThread(new Runnable() {
  147. protected final String error = response.getString(TAG_MESSAGE);
  148.  
  149. public void run() {
  150. Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
  151. }
  152. });
  153. }
  154. } catch (JSONException e) {
  155. Log.d("Error", e.toString());
  156. }
  157.  
  158. pDialog.dismiss();
  159. }
  160.  
  161. class AttemptLogin extends AsyncTask<String, String, String> {
  162.  
  163. @Override
  164. protected void onPreExecute() {
  165. super.onPreExecute();
  166. pDialog = new ProgressDialog(LoginActivity.this);
  167. pDialog.setMessage("Attempting login..");
  168. pDialog.setIndeterminate(false);
  169. pDialog.setCancelable(true);
  170. pDialog.show();
  171. }
  172.  
  173. @Override
  174. protected String doInBackground(String... args) {
  175. String username = user.getText().toString();
  176. String password = pass.getText().toString();
  177. requestManager.loginUser(username, password, LoginActivity.this);
  178.  
  179. return null;
  180. }
  181.  
  182. // After completing background task Dismiss the progress dialog
  183.  
  184. protected void onPostExecute(String file_url) {
  185. //dismiss the dialog once product deleted
  186. pDialog.dismiss();
  187. }
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement