Last active
December 17, 2015 17:48
-
-
Save kodamirmo/5648260 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.irving.cares; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import com.irving.cares.webservices.WebServiceLogin; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.content.pm.Signature; | |
import android.util.Base64; | |
import android.util.Log; | |
import android.view.Menu; | |
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 com.facebook.*; | |
import com.facebook.model.*; | |
public class Login extends Activity implements OnClickListener{ | |
private EditText etUser; | |
private EditText etPassowrd; | |
private Button buttonLogin; | |
private Button buttonLoginFacebook; | |
private TextView tvUser; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
initViews(); | |
} | |
private void initViews(){ | |
etUser=(EditText)findViewById(R.id.etLoginUser); | |
etPassowrd=(EditText)findViewById(R.id.etLoginPassword); | |
tvUser=(TextView)findViewById(R.id.tvUserFacebook); | |
buttonLogin=(Button)findViewById(R.id.btnLogin); | |
buttonLogin.setOnClickListener(this); | |
buttonLoginFacebook=(Button)findViewById(R.id.btnLoginFacebook); | |
buttonLoginFacebook.setOnClickListener(this); | |
etUser.setText("Sonae"); | |
etPassowrd.setText("dwojeD"); | |
printHashKey(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.login, menu); | |
return true; | |
} | |
@Override | |
public void onClick(View v) { | |
if(v==buttonLogin){ | |
AsyncTaskWSLogin servicio=new AsyncTaskWSLogin(this, etUser.getText().toString(), | |
etPassowrd.getText().toString()); | |
servicio.execute(); | |
} | |
else if(v==buttonLoginFacebook){ | |
initSesionOnFacebook(); | |
} | |
} | |
private void initSesionOnFacebook(){ | |
Session.openActiveSession(this, true, new Session.StatusCallback() { | |
// callback when session changes state | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
Log.i("TAG","abreidno session"); | |
if (session.isOpened()) { | |
Log.i("TAG", "Sesion abierta"); | |
// make request to the /me API | |
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { | |
// callback after Graph API response with user object | |
@Override | |
public void onCompleted(GraphUser user, Response response) { | |
if (user != null) { | |
Log.i("TAG", "Usuario de face:" + user.getName()); | |
try{ | |
tvUser.setText("" + user.getName()); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
else | |
Log.i("TAG", "Usuario de face: null"); | |
} | |
}); | |
} | |
else | |
Log.i("TAG", "Sesion no abierta =("); | |
} | |
}); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
Log.i("TAG", "En on activity result"); | |
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); | |
} | |
public void printHashKey() { | |
Log.i("TAG", "Hash"); | |
try { | |
PackageInfo info = getPackageManager().getPackageInfo("com.irving.cares", | |
PackageManager.GET_SIGNATURES); | |
for (Signature signature : info.signatures) { | |
MessageDigest md = MessageDigest.getInstance("SHA"); | |
md.update(signature.toByteArray()); | |
Log.d("TAG", | |
Base64.encodeToString(md.digest(), Base64.DEFAULT)); | |
} | |
} catch (NameNotFoundException e) { | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
} | |
/////////////////////////////////////////////////////// | |
private class AsyncTaskWSLogin extends AsyncTask<Void, Void, Boolean> { | |
protected ProgressDialog dialogo; | |
private Activity act; | |
private String user; | |
private String password; | |
public AsyncTaskWSLogin(Activity actividad,String user,String pass) { | |
act=actividad; | |
this.user=user; | |
password=pass; | |
Log.i("TAG", "En constructor de consultando"); | |
} | |
protected void onPreExecute() { | |
Log.i("TAG", "En preExectute"); | |
dialogo = new ProgressDialog(act); | |
dialogo.setIndeterminate(true); | |
dialogo.setMessage("Log in"); | |
dialogo.setTitle("Target Cares"); | |
dialogo.setCancelable(false); | |
dialogo.show(); | |
} | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
Log.i("TAG", "En backgroud Login"); | |
WebServiceLogin login=new WebServiceLogin(); | |
return login.iniciarSesion(user, password); | |
} | |
protected void onPostExecute(Boolean result) { | |
Log.i("TAG", "En PostExecute"); | |
Log.i("TAG","PostExecute:" + result); | |
dialogo.cancel(); | |
if(result){ | |
Intent homeScreen=new Intent(act,HomePage.class); | |
homeScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
act.startActivity(homeScreen); | |
} | |
else | |
Toast.makeText(act, "Password wonkie", Toast.LENGTH_LONG).show(); | |
} | |
}// Termina AsyncTask | |
//////////////////////////////////////////////////////// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment