Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lt.ebp.take2ga.authentication;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.graphics.LinearGradient;
- import android.graphics.Shader;
- import android.graphics.Typeface;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableStringBuilder;
- import android.text.style.StyleSpan;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONException;
- import org.json.JSONObject;
- import lt.ebp.take2ga.MainActivity;
- import lt.ebp.take2ga.R;
- import lt.ebp.take2ga.utils.JSONParser;
- import lt.ebp.take2ga.utils.RequestManager;
- /**
- * User login activity
- * Created by emilis on 16-04-02.
- */
- public class LoginActivity extends Activity implements OnClickListener, RequestManager.ResponseListener {
- private EditText user, pass;
- private Button mSubmit, mRegister;
- // Progress Dialog
- private ProgressDialog pDialog;
- //JSON element ids from repsonse of php script:
- private static final String TAG_SUCCESS = "success";
- private static final String TAG_MESSAGE = "error";
- RequestManager requestManager = new RequestManager();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
- //setup input fields
- user = (EditText)findViewById(R.id.email);
- pass = (EditText)findViewById(R.id.password);
- TextView memberLogin = (TextView)findViewById(R.id.memberLogin);
- TextView notMember = (TextView)findViewById(R.id.not_a_member_yet);
- Button forgot = (Button)findViewById(R.id.forgot);
- Button login = (Button)findViewById(R.id.login);
- Button watch = (Button)findViewById(R.id.register);
- Typeface geomanist = Typeface.createFromAsset(getAssets(), "fonts/geomanist-regular-webfont.ttf");
- Typeface roboto_medium = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
- Typeface gadugi = Typeface.createFromAsset(getAssets(), "fonts/gadugi.ttf");
- Typeface gadugi_bold = Typeface.createFromAsset(getAssets(), "fonts/gadugib.ttf");
- final SpannableStringBuilder string_notmember = new SpannableStringBuilder(notMember.getText());
- final SpannableStringBuilder string_watch = new SpannableStringBuilder(watch.getText());
- final StyleSpan geomanist_span = new StyleSpan(geomanist.getStyle());
- final StyleSpan gadugi_span = new StyleSpan(gadugi.getStyle());
- final StyleSpan gadugi_bold_span = new StyleSpan(gadugi_bold.getStyle());
- string_notmember.setSpan(geomanist_span, 0, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- string_notmember.setSpan(gadugi_bold_span, 6, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- string_watch.setSpan(gadugi_span, 0, 11, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- string_watch.setSpan(gadugi_bold_span, 11, 15, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- string_watch.setSpan(gadugi_span, 15, 18, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- string_watch.setSpan(gadugi_bold_span, 18, 26, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- notMember.setText(string_notmember);
- watch.setText(string_watch);
- memberLogin.setTypeface(geomanist);
- Shader memberGradient=new LinearGradient(0, 0, 0, memberLogin.getPaint().getTextSize(),
- new int[]{Color.parseColor("#fa9338"),Color.parseColor("#ff7800")},
- new float[]{0, 1}, Shader.TileMode.CLAMP);
- memberLogin.getPaint().setShader(memberGradient);
- forgot.setTypeface(geomanist);
- notMember.setTypeface(gadugi);
- login.setTypeface(gadugi_bold);
- user.setTypeface(roboto_medium);
- pass.setTypeface(roboto_medium);
- //setup buttons
- mSubmit = (Button) findViewById(R.id.login);
- mRegister = (Button) findViewById(R.id.register);
- //register listeners
- mSubmit.setOnClickListener(this);
- mRegister.setOnClickListener(this);
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.login:
- if (JSONParser.isConnected(LoginActivity.this)) {
- new AttemptLogin().execute();
- } else {
- Toast.makeText(LoginActivity.this, "No internet access", Toast.LENGTH_LONG).show();
- }
- break;
- case R.id.register:
- Intent i = new Intent(this, RegisterActivity.class);
- startActivity(i);
- break;
- default:
- break;
- }
- }
- public void onResponseReceived(final JSONObject response) {
- try {
- int success = response.optInt(TAG_SUCCESS);
- if (success == 1) {
- Log.d("Login Successful!", response.toString());
- String token = response.getString("token");
- SharedPreferences sharedPreferences = getSharedPreferences("Login.loginPrefs", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString("loginToken", token);
- editor.commit();
- Intent i = new Intent(LoginActivity.this, MainActivity.class);
- finish();
- startActivity(i);
- } else {
- Log.d("Login Falure!", response.getString(TAG_MESSAGE));
- this.runOnUiThread(new Runnable() {
- protected final String error = response.getString(TAG_MESSAGE);
- public void run() {
- Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
- }
- });
- }
- } catch (JSONException e) {
- Log.d("Error", e.toString());
- }
- pDialog.dismiss();
- }
- class AttemptLogin extends AsyncTask<String, String, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(LoginActivity.this);
- pDialog.setMessage("Attempting login..");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... args) {
- String username = user.getText().toString();
- String password = pass.getText().toString();
- requestManager.loginUser(username, password, LoginActivity.this);
- return null;
- }
- // After completing background task Dismiss the progress dialog
- protected void onPostExecute(String file_url) {
- //dismiss the dialog once product deleted
- pDialog.dismiss();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement