/src/com/techspace/phoneapp/ActivityDoor.java
Java | 359 lines | 284 code | 28 blank | 47 comment | 14 complexity | 45d6352183cbe17fc6639369db52cdf3 MD5 | raw file
- package com.techspace.phoneapp;
- /**
- * Author: dtbaker@gmail.com
- * Date: 11th Dec 2011
- */
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.*;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.text.Editable;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.google.android.c2dm.C2DMessaging;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ResponseHandler;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.BasicResponseHandler;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.List;
- public class ActivityDoor extends Activity
- {
- private ProgressDialog pd; // shown when trying to auth code
- private static final String TAG = "ActivityDoor";
- public static final String UPDATE_UI_ACTION = "com.techspace.auth.UPDATE_UI";
- public static final String UPDATE_UI_DOOR_STATUS = "com.techspace.door.UPDATE_UI";
- private static final String DOOR_CODE_AUTH_STATUS = "DoorAuthStat";
- private static final String DOOR_STATUS_CHECK = "DoorStatus";
- private static final String PIN_ACTION_DOOR = "1"; // strings for http post. meh
- private static final String PIN_ACTION_CHECKIN = "2";
- private static final int DOOR_CODE_AUTH_STATUS_SUCCESS = 1;
- private static final int DOOR_CODE_AUTH_STATUS_FAIL = 2;
- private int door_auth_status = 0;
- private String current_pin_action = "";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setScreenContent(R.layout.door);
- // the intent that handles updates from our http thread.
- registerReceiver(authCheckReceiver, new IntentFilter(UPDATE_UI_ACTION));
- registerReceiver(doorStatusReceiver, new IntentFilter(UPDATE_UI_DOOR_STATUS));
- }
- @Override
- public void onDestroy() {
- unregisterReceiver(authCheckReceiver);
- unregisterReceiver(doorStatusReceiver);
- super.onDestroy();
- }
- /** this does the menu settings area **/
- /*@Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.door_menu, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.door_menu_settings:
- // swap to the settings layout
- setScreenContent(R.layout.door_settings);
- break;
- }
- return true;
- }*/
- private void setScreenContent(int screenId) {
- setContentView(screenId);
- switch (screenId) {
- case R.layout.door_status: {
- Button exitButton = (Button) findViewById(R.id.button_exit);
- Button retryButton = (Button) findViewById(R.id.button_retry);
- TextView textView = (TextView) findViewById(R.id.status_text);
- // set status text:
- String status_text = "";
- switch(door_auth_status){
- case DOOR_CODE_AUTH_STATUS_SUCCESS: {
- status_text += "Pin code success.";
- if(current_pin_action == PIN_ACTION_DOOR){
- status_text += " Door should be triggered.";
- }else if(current_pin_action == PIN_ACTION_CHECKIN){
- status_text += " Checkin recorded, thanks!";
- }
- break;
- }
- case DOOR_CODE_AUTH_STATUS_FAIL: {
- status_text += "Failed to authorise pin code. Action logged. Try again?";
- break;
- }
- default:{
- status_text += "Failed with an unknown status "+door_auth_status;
- break;
- }
- }
- textView.setText(status_text);
- // button actions:
- exitButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- retryButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- setScreenContent(R.layout.door);
- }
- });
- break;
- }
- case R.layout.door: {
- // final because we use it in our anonymous setOnClickListener function below.
- final EditText numberText = (EditText) findViewById(R.id.pin_code);
- final Context c = this;
- // grab our screen elements:
- TextView door_status = (TextView) findViewById(R.id.door_status_text);
- Button exitButton = (Button) findViewById(R.id.button_door_cancel);
- Button button0 = (Button)findViewById(R.id.n0);
- Button button1 = (Button)findViewById(R.id.n1);
- Button button2 = (Button)findViewById(R.id.n2);
- Button button3 = (Button)findViewById(R.id.n3);
- Button button4 = (Button)findViewById(R.id.n4);
- Button button5 = (Button)findViewById(R.id.n5);
- Button button6 = (Button)findViewById(R.id.n6);
- Button button7 = (Button)findViewById(R.id.n7);
- Button button8 = (Button)findViewById(R.id.n8);
- Button button9 = (Button)findViewById(R.id.n9);
- Button buttonBack = (Button)findViewById(R.id.delete_button);
- Button buttonDoorAction = (Button)findViewById(R.id.button_open);
- Button buttonCheckin = (Button)findViewById(R.id.button_checkin);
- button0.setOnClickListener(new CharacterButtonListener(numberText, "0"));
- button1.setOnClickListener(new CharacterButtonListener(numberText, "1"));
- button2.setOnClickListener(new CharacterButtonListener(numberText, "2"));
- button3.setOnClickListener(new CharacterButtonListener(numberText, "3"));
- button4.setOnClickListener(new CharacterButtonListener(numberText, "4"));
- button5.setOnClickListener(new CharacterButtonListener(numberText, "5"));
- button6.setOnClickListener(new CharacterButtonListener(numberText, "6"));
- button7.setOnClickListener(new CharacterButtonListener(numberText, "7"));
- button8.setOnClickListener(new CharacterButtonListener(numberText, "8"));
- button9.setOnClickListener(new CharacterButtonListener(numberText, "9"));
- buttonBack.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //TextView number = numberText;
- CharSequence text = numberText.getText();
- // This is annoyingly ugly, but is from the Android source
- if (!(text instanceof Editable)) {
- numberText.setText(text, TextView.BufferType.EDITABLE);
- }
- Editable editable = (Editable)numberText.getText();
- // Now that we have the editable, edit it.
- // This line is not from the Android source.
- if (text.length() > 0) {
- editable.delete(text.length() - 1, text.length());
- }
- }
- });
- exitButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- buttonDoorAction.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- current_pin_action = PIN_ACTION_DOOR;
- actionPinCode();
- }
- });
- buttonCheckin.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- current_pin_action = PIN_ACTION_CHECKIN;
- actionPinCode();
- }
- });
- // we need to check the door status with a http request
- // and update the header_text portion with the status..
- door_status.setText((String)getString(R.string.door_status).replace("{STATUS}","Checking..."));
- // start a thread to handle our dppr status http request
- new Thread(
- new Runnable() {
- public void run(){
- // we need an intent so that when our HTTP request is finished
- // we can kill the popup and display the new status screen.
- Intent updateUIIntent = new Intent(ActivityDoor.UPDATE_UI_DOOR_STATUS);
- Log.w(TAG, "HTTP door status Thread running... ");
- try {
- ResponseHandler<String> responseHandler = new BasicResponseHandler();
- DefaultHttpClient client = new DefaultHttpClient();
- HttpGet get = new HttpGet(ActivityTechSpace.BASE_URL + "/status.php");
- Log.v(TAG,"postback built, sending...");
- String server_response = client.execute(get,responseHandler);
- updateUIIntent.putExtra(DOOR_STATUS_CHECK, server_response);
- } catch (Exception e) {
- Log.w(TAG, "check status error "+Log.getStackTraceString(e));
- updateUIIntent.putExtra(DOOR_STATUS_CHECK, "Failed. Are you on the net?");
- }
- c.sendBroadcast(updateUIIntent);
- }
- }
- ).start();
- break;
- }
- }
- }
- private void actionPinCode(){
- final EditText numberText = (EditText) findViewById(R.id.pin_code);
- final Context c = this;
- String pin_number = numberText.getText().toString();
- if (pin_number.length() <= 3) {
- new AlertDialog.Builder(c)
- .setTitle("Error")
- .setMessage("Sorry, pin code must be 4 or more digits")
- .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // return to screen...
- }
- })
- .show();
- } else {
- // show progress bar dialog
- pd = ProgressDialog.show(
- c,
- "",
- "Please wait while we authorise your code...",
- true
- );
- // start a thread to handle our http request
- new Thread(
- new Runnable() {
- public void run() {
- // we need an intent so that when our HTTP request is finished
- // we can kill the popup and display the new status screen.
- Intent updateUIIntent = new Intent(ActivityDoor.UPDATE_UI_ACTION);
- Log.w(TAG, "HTTP Thread running... ");
- try {
- Log.w(TAG, "pin code = " + numberText.getText().toString());
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("c", numberText.getText().toString()));
- params.add(new BasicNameValuePair("pin_action", current_pin_action ));
- DefaultHttpClient client = new DefaultHttpClient();
- // Make POST request
- URI uri = new URI(ActivityTechSpace.BASE_URL + "/c.php");
- Log.w(TAG, "POST CHECK URI = " + uri);
- HttpPost post = new HttpPost(uri);
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
- post.setEntity(entity);
- Log.w(TAG, "auth string built, sending...");
- HttpResponse res = client.execute(post);
- Log.w(TAG, "...executed");
- if (res.getStatusLine().getStatusCode() == 200) {
- // auth worked! set message.
- Log.w(TAG, "Success in auth");
- updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_SUCCESS);
- } else {
- Log.w(TAG, "Error in auth " + String.valueOf(res.getStatusLine().getStatusCode()));
- updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_FAIL);
- }
- } catch (Exception e) {
- Log.w(TAG, "check auth error " + Log.getStackTraceString(e));
- updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_FAIL);
- }
- c.sendBroadcast(updateUIIntent);
- }
- }
- ).start();
- }
- }
- /** we need an intent broadcast receiver so we can update the UI from the http thread above **/
- private final BroadcastReceiver authCheckReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- // check auth response, default to error:
- door_auth_status = intent.getIntExtra(DOOR_CODE_AUTH_STATUS, 4);
- if(door_auth_status == 4){
- door_auth_status = DOOR_CODE_AUTH_STATUS_FAIL;
- return; // no intent set. ignoring..
- }
- intent.putExtra(DOOR_CODE_AUTH_STATUS, 4); //finished .. not sure if neccessary
- try{
- Log.w(TAG,"Starting onReceive in intent receiver...");
- pd.dismiss(); // kill the progress bar popup.
- Log.w(TAG,"onReceive got the status of: "+door_auth_status);
- setScreenContent(R.layout.door_status);
- }catch(Exception e){
- Log.w(TAG,"OnReceive Error "+e.getMessage());
- Log.w(TAG, " stack trace: "+Log.getStackTraceString(e));
- }
- }
- };
- private final BroadcastReceiver doorStatusReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- // check auth response, default to error:
- String current_door_status = intent.getStringExtra(DOOR_STATUS_CHECK);
- try{
- Log.w(TAG, "Door status onReceive got the status of: " + current_door_status);
- // update the status
- TextView door_status = (TextView) findViewById(R.id.door_status_text);
- door_status.setText((String) getString(R.string.door_status).replace("{STATUS}", current_door_status));
- }catch(Exception e){
- Log.w(TAG,"OnReceive Error in door status "+e.getMessage());
- Log.w(TAG, " stack trace: "+Log.getStackTraceString(e));
- }
- }
- };
- private class CharacterButtonListener implements View.OnClickListener {
- private TextView field;
- private String character;
- public CharacterButtonListener(TextView field, String character) {
- this.field = field;
- this.character = character;
- }
- @Override
- public void onClick(View v) {
- this.field.append(this.character);
- }
- }
- }