package com.geel9.facepunch.activities;
import org.json.JSONException;
import org.json.JSONObject;
import com.crittercism.app.Crittercism;
import com.geel9.facepunch.APISession;
import com.geel9.facepunch.R;
import com.geel9.facepunch.APISession.LoginCallback;
import com.geel9.facepunch.APISession.LoginResult;
import com.geel9.facepunch.R.id;
import com.geel9.facepunch.R.layout;
import com.geel9.facepunch.R.string;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends FPActivity
{
// Used for restoring form input
private class State
{
public String username;
public String password;
}
@Override
public void onBackPressed() {
super.onBackPressed();
//return;
}
@Override
public void onCreate( Bundle savedInstanceState )
{
// Load layout
super.onCreate( savedInstanceState );
setContentView( R.layout.login );
Intent i = getIntent();
// Views
final Button loginButton = (Button)findViewById( R.id.loginButton );
final Button skipButton = (Button)findViewById(R.id.prefsButton);
final EditText usernameField = (EditText)findViewById( R.id.usernameField );
final EditText passwordField = (EditText)findViewById( R.id.passwordField );
// Get preference access
final SharedPreferences prefs = getSharedPreferences("com.geel9.facepunch_preferences", MODE_PRIVATE );
//Load preferences
skipButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
finish();
}
});
// Log in request handler
loginButton.setOnClickListener( new OnClickListener()
{
public void onClick( View v )
{
final String username = usernameField.getText().toString();
final String password = passwordField.getText().toString();
if ( username.length() == 0 || password.length() == 0 ) return;
final ProgressDialog loginDialog = ProgressDialog.show( LoginActivity.this, "", getString( R.string.loggingIn ), true );
loginDialog.show();
// Attempt logging in
api.login( username, password, new APISession.LoginCallback()
{
public void onResult( APISession.LoginResult success )
{
int userId = prefs.getInt("userid", -1);
int loggedInId = api.bb_userid;
if(loggedInId != -1){
if(userId != loggedInId || userId == -1){
System.out.println("Registering. ID: " + api.bb_userid);
prefs.edit().putInt("userid", loggedInId).commit();
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(LoginActivity.this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "coffeyjosh1@gmail.com");
startService(registrationIntent);
}
}
try{
if(loginDialog != null && loginDialog.isShowing() && loginDialog.getWindow() != getWindow())
loginDialog.dismiss();
}catch(Exception e){
return;
}
if ( success == APISession.LoginResult.SUCCESS)
{
prefs.edit().putString("sessionid", api.sessionID);
prefs.edit().putString("username", api.username);
prefs.edit().putInt("userid", api.userId());
prefs.edit().commit();
Crittercism.setUsername(username);
// Ask the user if the successful login details should be saved
if ( !username.equals( prefs.getString( "username", "" ) )
|| !password.equals( prefs.getString( "password", "" ) ) )
ShowRememberDialog(username, password, prefs);
else
finish();
} else {
String failMessage = "Could not login. Please try again.";
if(success == APISession.LoginResult.FAILED_WRONGPASSWORD)
failMessage = getString(R.string.failedLoginWrongPassword);
else if(success == APISession.LoginResult.FAILED_FAILLIMIT)
failMessage = getString(R.string.failedLoginFailLimit);
else if(success == APISession.LoginResult.FAILED_NOSOURCE)
failMessage = getString(R.string.failedLoginNoSource);
else if(success == APISession.LoginResult.FAILED_BADMD5)
failMessage = "The MD5 somehow failed. Tell geel9.";
new AlertDialog.Builder( LoginActivity.this )
.setMessage( failMessage )
.setCancelable( true )
.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialog, int which ) {} } )
.create()
.show();
}
}
} );
}
} );
// Restore form input
State data = (State)getLastCustomNonConfigurationInstance();
if ( data != null )
{
usernameField.setText( data.username );
passwordField.setText( data.password );
} else {
// Attempt to retrieve stored login details
String username = prefs.getString("username", "");
String password = prefs.getString("password", "");
usernameField.setText( username );
passwordField.setText( password) ;
}
}
public void ShowRememberDialog(final String username, final String password, final SharedPreferences prefs){
new AlertDialog.Builder( LoginActivity.this )
.setMessage( getString( R.string.successfulLoginRememberPassword ) )
.setCancelable( true )
.setPositiveButton( getString( R.string.rememberPassword ), new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int id )
{
SharedPreferences.Editor editor = prefs.edit();
editor.putString( "username", username );
editor.putString( "password", password );
editor.commit();
finish();
}
} )
.setNegativeButton( getString( R.string.forgetPassword ), new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int id )
{
finish();
}
} )
.create()
.show();
}
// Save activity state
@Override
public Object onRetainCustomNonConfigurationInstance()
{
final State data = new State();
data.username = ( (EditText)findViewById( R.id.usernameField ) ).getText().toString().trim();
data.password = ( (EditText)findViewById( R.id.passwordField ) ).getText().toString().trim();
return data;
}
}