PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/techspace/phoneapp/ActivityDoor.java

https://bitbucket.org/gctechspace/techspace_phoneapp
Java | 359 lines | 284 code | 28 blank | 47 comment | 14 complexity | 45d6352183cbe17fc6639369db52cdf3 MD5 | raw file
  1. package com.techspace.phoneapp;
  2. /**
  3. * Author: dtbaker@gmail.com
  4. * Date: 11th Dec 2011
  5. */
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.ProgressDialog;
  9. import android.content.*;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.Message;
  13. import android.text.Editable;
  14. import android.util.Log;
  15. import android.view.Menu;
  16. import android.view.MenuInflater;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.TextView;
  22. import com.google.android.c2dm.C2DMessaging;
  23. import org.apache.http.HttpResponse;
  24. import org.apache.http.NameValuePair;
  25. import org.apache.http.client.ResponseHandler;
  26. import org.apache.http.client.entity.UrlEncodedFormEntity;
  27. import org.apache.http.client.methods.HttpGet;
  28. import org.apache.http.client.methods.HttpPost;
  29. import org.apache.http.impl.client.BasicResponseHandler;
  30. import org.apache.http.impl.client.DefaultHttpClient;
  31. import org.apache.http.message.BasicNameValuePair;
  32. import java.net.URI;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. public class ActivityDoor extends Activity
  36. {
  37. private ProgressDialog pd; // shown when trying to auth code
  38. private static final String TAG = "ActivityDoor";
  39. public static final String UPDATE_UI_ACTION = "com.techspace.auth.UPDATE_UI";
  40. public static final String UPDATE_UI_DOOR_STATUS = "com.techspace.door.UPDATE_UI";
  41. private static final String DOOR_CODE_AUTH_STATUS = "DoorAuthStat";
  42. private static final String DOOR_STATUS_CHECK = "DoorStatus";
  43. private static final String PIN_ACTION_DOOR = "1"; // strings for http post. meh
  44. private static final String PIN_ACTION_CHECKIN = "2";
  45. private static final int DOOR_CODE_AUTH_STATUS_SUCCESS = 1;
  46. private static final int DOOR_CODE_AUTH_STATUS_FAIL = 2;
  47. private int door_auth_status = 0;
  48. private String current_pin_action = "";
  49. /** Called when the activity is first created. */
  50. @Override
  51. public void onCreate(Bundle savedInstanceState)
  52. {
  53. super.onCreate(savedInstanceState);
  54. setScreenContent(R.layout.door);
  55. // the intent that handles updates from our http thread.
  56. registerReceiver(authCheckReceiver, new IntentFilter(UPDATE_UI_ACTION));
  57. registerReceiver(doorStatusReceiver, new IntentFilter(UPDATE_UI_DOOR_STATUS));
  58. }
  59. @Override
  60. public void onDestroy() {
  61. unregisterReceiver(authCheckReceiver);
  62. unregisterReceiver(doorStatusReceiver);
  63. super.onDestroy();
  64. }
  65. /** this does the menu settings area **/
  66. /*@Override
  67. public boolean onCreateOptionsMenu(Menu menu) {
  68. MenuInflater inflater = getMenuInflater();
  69. inflater.inflate(R.menu.door_menu, menu);
  70. return true;
  71. }
  72. @Override
  73. public boolean onOptionsItemSelected(MenuItem item) {
  74. switch (item.getItemId()) {
  75. case R.id.door_menu_settings:
  76. // swap to the settings layout
  77. setScreenContent(R.layout.door_settings);
  78. break;
  79. }
  80. return true;
  81. }*/
  82. private void setScreenContent(int screenId) {
  83. setContentView(screenId);
  84. switch (screenId) {
  85. case R.layout.door_status: {
  86. Button exitButton = (Button) findViewById(R.id.button_exit);
  87. Button retryButton = (Button) findViewById(R.id.button_retry);
  88. TextView textView = (TextView) findViewById(R.id.status_text);
  89. // set status text:
  90. String status_text = "";
  91. switch(door_auth_status){
  92. case DOOR_CODE_AUTH_STATUS_SUCCESS: {
  93. status_text += "Pin code success.";
  94. if(current_pin_action == PIN_ACTION_DOOR){
  95. status_text += " Door should be triggered.";
  96. }else if(current_pin_action == PIN_ACTION_CHECKIN){
  97. status_text += " Checkin recorded, thanks!";
  98. }
  99. break;
  100. }
  101. case DOOR_CODE_AUTH_STATUS_FAIL: {
  102. status_text += "Failed to authorise pin code. Action logged. Try again?";
  103. break;
  104. }
  105. default:{
  106. status_text += "Failed with an unknown status "+door_auth_status;
  107. break;
  108. }
  109. }
  110. textView.setText(status_text);
  111. // button actions:
  112. exitButton.setOnClickListener(new View.OnClickListener() {
  113. public void onClick(View v) {
  114. finish();
  115. }
  116. });
  117. retryButton.setOnClickListener(new View.OnClickListener() {
  118. public void onClick(View v) {
  119. setScreenContent(R.layout.door);
  120. }
  121. });
  122. break;
  123. }
  124. case R.layout.door: {
  125. // final because we use it in our anonymous setOnClickListener function below.
  126. final EditText numberText = (EditText) findViewById(R.id.pin_code);
  127. final Context c = this;
  128. // grab our screen elements:
  129. TextView door_status = (TextView) findViewById(R.id.door_status_text);
  130. Button exitButton = (Button) findViewById(R.id.button_door_cancel);
  131. Button button0 = (Button)findViewById(R.id.n0);
  132. Button button1 = (Button)findViewById(R.id.n1);
  133. Button button2 = (Button)findViewById(R.id.n2);
  134. Button button3 = (Button)findViewById(R.id.n3);
  135. Button button4 = (Button)findViewById(R.id.n4);
  136. Button button5 = (Button)findViewById(R.id.n5);
  137. Button button6 = (Button)findViewById(R.id.n6);
  138. Button button7 = (Button)findViewById(R.id.n7);
  139. Button button8 = (Button)findViewById(R.id.n8);
  140. Button button9 = (Button)findViewById(R.id.n9);
  141. Button buttonBack = (Button)findViewById(R.id.delete_button);
  142. Button buttonDoorAction = (Button)findViewById(R.id.button_open);
  143. Button buttonCheckin = (Button)findViewById(R.id.button_checkin);
  144. button0.setOnClickListener(new CharacterButtonListener(numberText, "0"));
  145. button1.setOnClickListener(new CharacterButtonListener(numberText, "1"));
  146. button2.setOnClickListener(new CharacterButtonListener(numberText, "2"));
  147. button3.setOnClickListener(new CharacterButtonListener(numberText, "3"));
  148. button4.setOnClickListener(new CharacterButtonListener(numberText, "4"));
  149. button5.setOnClickListener(new CharacterButtonListener(numberText, "5"));
  150. button6.setOnClickListener(new CharacterButtonListener(numberText, "6"));
  151. button7.setOnClickListener(new CharacterButtonListener(numberText, "7"));
  152. button8.setOnClickListener(new CharacterButtonListener(numberText, "8"));
  153. button9.setOnClickListener(new CharacterButtonListener(numberText, "9"));
  154. buttonBack.setOnClickListener(new View.OnClickListener() {
  155. @Override
  156. public void onClick(View v) {
  157. //TextView number = numberText;
  158. CharSequence text = numberText.getText();
  159. // This is annoyingly ugly, but is from the Android source
  160. if (!(text instanceof Editable)) {
  161. numberText.setText(text, TextView.BufferType.EDITABLE);
  162. }
  163. Editable editable = (Editable)numberText.getText();
  164. // Now that we have the editable, edit it.
  165. // This line is not from the Android source.
  166. if (text.length() > 0) {
  167. editable.delete(text.length() - 1, text.length());
  168. }
  169. }
  170. });
  171. exitButton.setOnClickListener(new View.OnClickListener() {
  172. public void onClick(View v) {
  173. finish();
  174. }
  175. });
  176. buttonDoorAction.setOnClickListener(new View.OnClickListener() {
  177. public void onClick(View v) {
  178. current_pin_action = PIN_ACTION_DOOR;
  179. actionPinCode();
  180. }
  181. });
  182. buttonCheckin.setOnClickListener(new View.OnClickListener() {
  183. public void onClick(View v) {
  184. current_pin_action = PIN_ACTION_CHECKIN;
  185. actionPinCode();
  186. }
  187. });
  188. // we need to check the door status with a http request
  189. // and update the header_text portion with the status..
  190. door_status.setText((String)getString(R.string.door_status).replace("{STATUS}","Checking..."));
  191. // start a thread to handle our dppr status http request
  192. new Thread(
  193. new Runnable() {
  194. public void run(){
  195. // we need an intent so that when our HTTP request is finished
  196. // we can kill the popup and display the new status screen.
  197. Intent updateUIIntent = new Intent(ActivityDoor.UPDATE_UI_DOOR_STATUS);
  198. Log.w(TAG, "HTTP door status Thread running... ");
  199. try {
  200. ResponseHandler<String> responseHandler = new BasicResponseHandler();
  201. DefaultHttpClient client = new DefaultHttpClient();
  202. HttpGet get = new HttpGet(ActivityTechSpace.BASE_URL + "/status.php");
  203. Log.v(TAG,"postback built, sending...");
  204. String server_response = client.execute(get,responseHandler);
  205. updateUIIntent.putExtra(DOOR_STATUS_CHECK, server_response);
  206. } catch (Exception e) {
  207. Log.w(TAG, "check status error "+Log.getStackTraceString(e));
  208. updateUIIntent.putExtra(DOOR_STATUS_CHECK, "Failed. Are you on the net?");
  209. }
  210. c.sendBroadcast(updateUIIntent);
  211. }
  212. }
  213. ).start();
  214. break;
  215. }
  216. }
  217. }
  218. private void actionPinCode(){
  219. final EditText numberText = (EditText) findViewById(R.id.pin_code);
  220. final Context c = this;
  221. String pin_number = numberText.getText().toString();
  222. if (pin_number.length() <= 3) {
  223. new AlertDialog.Builder(c)
  224. .setTitle("Error")
  225. .setMessage("Sorry, pin code must be 4 or more digits")
  226. .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  227. public void onClick(DialogInterface dialog, int which) {
  228. // return to screen...
  229. }
  230. })
  231. .show();
  232. } else {
  233. // show progress bar dialog
  234. pd = ProgressDialog.show(
  235. c,
  236. "",
  237. "Please wait while we authorise your code...",
  238. true
  239. );
  240. // start a thread to handle our http request
  241. new Thread(
  242. new Runnable() {
  243. public void run() {
  244. // we need an intent so that when our HTTP request is finished
  245. // we can kill the popup and display the new status screen.
  246. Intent updateUIIntent = new Intent(ActivityDoor.UPDATE_UI_ACTION);
  247. Log.w(TAG, "HTTP Thread running... ");
  248. try {
  249. Log.w(TAG, "pin code = " + numberText.getText().toString());
  250. List<NameValuePair> params = new ArrayList<NameValuePair>();
  251. params.add(new BasicNameValuePair("c", numberText.getText().toString()));
  252. params.add(new BasicNameValuePair("pin_action", current_pin_action ));
  253. DefaultHttpClient client = new DefaultHttpClient();
  254. // Make POST request
  255. URI uri = new URI(ActivityTechSpace.BASE_URL + "/c.php");
  256. Log.w(TAG, "POST CHECK URI = " + uri);
  257. HttpPost post = new HttpPost(uri);
  258. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
  259. post.setEntity(entity);
  260. Log.w(TAG, "auth string built, sending...");
  261. HttpResponse res = client.execute(post);
  262. Log.w(TAG, "...executed");
  263. if (res.getStatusLine().getStatusCode() == 200) {
  264. // auth worked! set message.
  265. Log.w(TAG, "Success in auth");
  266. updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_SUCCESS);
  267. } else {
  268. Log.w(TAG, "Error in auth " + String.valueOf(res.getStatusLine().getStatusCode()));
  269. updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_FAIL);
  270. }
  271. } catch (Exception e) {
  272. Log.w(TAG, "check auth error " + Log.getStackTraceString(e));
  273. updateUIIntent.putExtra(DOOR_CODE_AUTH_STATUS, DOOR_CODE_AUTH_STATUS_FAIL);
  274. }
  275. c.sendBroadcast(updateUIIntent);
  276. }
  277. }
  278. ).start();
  279. }
  280. }
  281. /** we need an intent broadcast receiver so we can update the UI from the http thread above **/
  282. private final BroadcastReceiver authCheckReceiver = new BroadcastReceiver() {
  283. @Override
  284. public void onReceive(Context context, Intent intent) {
  285. // check auth response, default to error:
  286. door_auth_status = intent.getIntExtra(DOOR_CODE_AUTH_STATUS, 4);
  287. if(door_auth_status == 4){
  288. door_auth_status = DOOR_CODE_AUTH_STATUS_FAIL;
  289. return; // no intent set. ignoring..
  290. }
  291. intent.putExtra(DOOR_CODE_AUTH_STATUS, 4); //finished .. not sure if neccessary
  292. try{
  293. Log.w(TAG,"Starting onReceive in intent receiver...");
  294. pd.dismiss(); // kill the progress bar popup.
  295. Log.w(TAG,"onReceive got the status of: "+door_auth_status);
  296. setScreenContent(R.layout.door_status);
  297. }catch(Exception e){
  298. Log.w(TAG,"OnReceive Error "+e.getMessage());
  299. Log.w(TAG, " stack trace: "+Log.getStackTraceString(e));
  300. }
  301. }
  302. };
  303. private final BroadcastReceiver doorStatusReceiver = new BroadcastReceiver() {
  304. @Override
  305. public void onReceive(Context context, Intent intent) {
  306. // check auth response, default to error:
  307. String current_door_status = intent.getStringExtra(DOOR_STATUS_CHECK);
  308. try{
  309. Log.w(TAG, "Door status onReceive got the status of: " + current_door_status);
  310. // update the status
  311. TextView door_status = (TextView) findViewById(R.id.door_status_text);
  312. door_status.setText((String) getString(R.string.door_status).replace("{STATUS}", current_door_status));
  313. }catch(Exception e){
  314. Log.w(TAG,"OnReceive Error in door status "+e.getMessage());
  315. Log.w(TAG, " stack trace: "+Log.getStackTraceString(e));
  316. }
  317. }
  318. };
  319. private class CharacterButtonListener implements View.OnClickListener {
  320. private TextView field;
  321. private String character;
  322. public CharacterButtonListener(TextView field, String character) {
  323. this.field = field;
  324. this.character = character;
  325. }
  326. @Override
  327. public void onClick(View v) {
  328. this.field.append(this.character);
  329. }
  330. }
  331. }