package com.mcgillece.parkdroid;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.mcgillece.parkdroid.GlobalsApp.ConnectionType;
import com.mcgillece.parkdroid.imagecapture.ImageSetGround;
public class Login extends Activity
{
private static final int ACTIVITY_SET_GROUND = 0;
private TextView _status;
private GlobalsApp globalsApp;
private String _username;
private EditText _usernameEditTxt;
private RadioGroup _connTypeGroup;
private RadioButton _connTypeOption1;
private RadioButton _connTypeOption2;
private int _groundX;
private int _groundY;
private int _groundX1;
private int _groundY1;
private TextView _groundTxt;
private int _groundPointsOrientation;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle state)
{
super.onCreate(state);
setContentView(R.layout.login);
// Handle Globals in here
globalsApp = (GlobalsApp)getApplicationContext();
_status = (TextView) findViewById(R.id.login_status);
_usernameEditTxt = (EditText) findViewById(R.id.login_txt_username);
_connTypeGroup = (RadioGroup) findViewById(R.id.connectionTypeGroup);
_connTypeOption1 = (RadioButton) findViewById(R.id.connectionTypeGroup_option1);
_connTypeOption2 = (RadioButton) findViewById(R.id.connectionTypeGroup_option2);
_groundTxt = (TextView) findViewById(R.id.login_groundtext);
//----------------------------
// Restore preferences
//----------------------------
SharedPreferences settings = this.getPreferences(MODE_PRIVATE);
// Username
_username = settings.getString("username", "");
_usernameEditTxt.setText(_username);
// Connection Types
_connTypeOption1.setChecked(settings.getBoolean("connTypeOption1", false));
_connTypeOption2.setChecked(settings.getBoolean("connTypeOption2", false));
// Ground Truth
_groundX = settings.getInt("ground_x", 0);
_groundY = settings.getInt("ground_y", 0);
_groundX1 = settings.getInt("ground_x1", 0);
_groundY1 = settings.getInt("ground_y1", 0);
_groundPointsOrientation = settings.getInt("ground_orientation", 0);
if (_groundX == 0 && _groundY == 0 && _groundX1 == 0 && _groundY1 == 0)
{
_groundTxt.setText("None");
}
else
{
if (_groundPointsOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
_groundTxt.setText("Portrait\n");
else
_groundTxt.setText("Landscape\n");
_groundTxt.append(
"X: " + _groundX + "% " +
"Y: " + _groundY + "%\n" +
"X1: " + _groundX1 + "% " +
"Y1: " + _groundY1 + "%");
}
// Set Globals Apps, so Draw receives initial settings
int[] tempArray = { _groundX, _groundY, _groundX1, _groundY1 };
globalsApp.set_groundPointsArray(tempArray);
//----------------------------
// Button Set Ground
//----------------------------
Button btnSetGround = (Button)findViewById(R.id.login_setGround);
btnSetGround.setOnClickListener(
new OnClickListener()
{
public void onClick(View viewParam)
{
// Display the username and the password in string format
_status.setText("Initializing Set Ground...");
Display getOrient = getWindowManager().getDefaultDisplay();
// Set the initial orientation to help with setting points
// if (getOrient.getWidth() < getOrient.getHeight())
// globalsApp.set_initialOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// else
globalsApp.set_initialOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Start ImageSetGround Activity
Intent myIntent = new Intent(viewParam.getContext(), ImageSetGround.class);
startActivityForResult(myIntent, ACTIVITY_SET_GROUND);
}
}
); // end of ClickListener
//----------------------------
// Button Login
//----------------------------
Button btnLogin = (Button)findViewById(R.id.login_button);
btnLogin.setOnClickListener
(
new OnClickListener()
{
public void onClick(View viewParam)
{
// Set username
_username = _usernameEditTxt.getText().toString();
// Connection Type
switch (_connTypeGroup.getCheckedRadioButtonId())
{
case R.id.connectionTypeGroup_option1:
globalsApp.set_connectionType(ConnectionType.LOCAL);
break;
case R.id.connectionTypeGroup_option2:
globalsApp.set_connectionType(ConnectionType.SSH);
break;
default:
break;
}
if(_username.length() <= 0)
{
_status.setText("Please enter a Username.");
}
else
{
globalsApp.set_username(_username);
// Display the username and the password in string format
_status.setText("Logging in...");
// Start Parkdroid Activity
Intent myIntent = new Intent(viewParam.getContext(), Parkdroid.class);
startActivity(myIntent);
finish();
}
}
}
); // end of ClickListener
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Upon return of the Set Ground activity, update point coordinates
switch(requestCode)
{
case ACTIVITY_SET_GROUND:
_status.setText("");
int[] tempArray = globalsApp.get_groundPointsArray();
if (globalsApp.get_initialOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
_groundTxt.setText("Portrait\n");
else
_groundTxt.setText("Landscape\n");
_groundTxt.append(
"X: " + tempArray[0] + "% " +
"Y: " + tempArray[1] + "%\n" +
"X1: " + tempArray[2] + "% " +
"Y1: " + tempArray[3] + "%");
break;
}
}
@Override
protected void onStop(){
super.onStop();
CheckBox checkboxNameSave = (CheckBox) findViewById(R.id.login_cb_saveUsername);
if (checkboxNameSave.isChecked())
{
// Save user preferences. We need an Editor object to
// make changes. All objects are from android.context.Context
SharedPreferences settings = this.getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", _username);
editor.putBoolean("connTypeOption1", _connTypeOption1.isChecked());
editor.putBoolean("connTypeOption2", _connTypeOption2.isChecked());
// Store latest ground point values
int[] tempArray = globalsApp.get_groundPointsArray();
editor.putInt("ground_x", tempArray[0]);
editor.putInt("ground_y", tempArray[1]);
editor.putInt("ground_x1", tempArray[2]);
editor.putInt("ground_y1", tempArray[3]);
editor.putInt("ground_orientation", globalsApp.get_initialOrientation());
// Commit changes
editor.commit();
}
}
}
|