Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Correct typo in Hazard.State

  • Loading branch information...
commit 6ef8ddcc8d3ddc4883bbe24020d1e60d8ec99572 1 parent 5ce8185
rpassmore authored March 13, 2011
91  src/rpassmore/app/fillthathole/ViewHazardActivity.java
@@ -70,6 +70,7 @@
70 70
 import android.widget.TextView;
71 71
 import android.widget.Button;
72 72
 import android.widget.CheckBox;
  73
+import android.widget.Toast;
73 74
 
74 75
 public class ViewHazardActivity extends Activity {
75 76
 
@@ -150,8 +151,8 @@ public void onCreate(Bundle savedInstanceState) {
150 151
       }        
151 152
     }
152 153
   }    
153  
-  
154  
-  
  154
+
  155
+
155 156
   /**
156 157
    * loads the hazard and populates the ui from the id supplied in the bundle
157 158
    * @param b
@@ -227,7 +228,7 @@ private void populateFromHazard() {
227 228
     }
228 229
 
229 230
     additionalInfo.setChecked(hazard.isHasAdditionalInfo());
230  
-    
  231
+
231 232
     hazardDepth.setText(Double.toString(hazard.getDepth()));
232 233
     hazardSize.setText(Double.toString(hazard.getSize()));
233 234
     hazardDistFromKerb.setText(Double.toString(hazard.getDistFromKerb()));
@@ -241,38 +242,64 @@ public void submitClickHandler(View view) {
241 242
       case R.id.submit:
242 243
         persistHazard();
243 244
 
244  
-        // TODO
245  
-        // if the hazard has been submitted successfully set the state to
246  
-        // submitted
  245
+        // TODO        
247 246
         // if photo has been submitted successfully set hasPhoto true
248  
-       
249  
-        
250  
-        
251  
-        
  247
+
  248
+        String error = null;
252 249
         try {                            
253 250
           HttpClient client = new DefaultHttpClient();  
254 251
           String postURL = "http://www.fillthathole.org.uk/services/submit_hazard";
255 252
           HttpPost post = new HttpPost(postURL); 
256  
-         //hopefully dont need to set user agent
  253
+          //hopefully dont need to set user agent
257 254
           //post.setHeader("User-Agent", "Fill%20That%20Hole/1.11 CFNetwork/485.12.7 Darwin/10.4.0");      
258 255
           ByteArrayEntity ent = new ByteArrayEntity(hazard.createSubmitStr().getBytes("UTF8"));          
259 256
           ent.setContentType("application/x-www-form-urlencoded");       
260 257
           post.setEntity(ent);          
261 258
           HttpResponse responsePOST = client.execute(post);  
262  
-          HttpEntity resEntity = responsePOST.getEntity();  
  259
+          HttpEntity resEntity = responsePOST.getEntity();            
263 260
           if (resEntity != null) {    
264  
-            String s = EntityUtils.toString(resEntity);
265  
-            Log.i("RESPONSE", EntityUtils.toString(resEntity));
  261
+            String result = EntityUtils.toString(resEntity);
  262
+
  263
+            JSONObject json = new JSONObject(result);
  264
+            if(json != null) {
  265
+              String hazard_id = json.getString("hazard_id");
  266
+              String reporter_key = json.getString("reporter_key");
  267
+              if(hazard_id == null || reporter_key == null) {
  268
+                //somekind of error occured try and get an error string
  269
+                error = json.getString("error");              
  270
+                Log.e(getPackageName(), "Error submitting hazard: " + error);
  271
+              } else {
  272
+                //store the returned hazard key
  273
+                hazard.setHazardId(hazard_id);
  274
+                hazard.setReporterKey(reporter_key);
  275
+                hazard.setState(Hazard.State.SUBMITTED);
  276
+
  277
+                persistHazard();
  278
+              }
  279
+            } else {
  280
+              error = "Unreconised response from web server";              
  281
+              Log.e(getPackageName(), "Error submitting hazard: empty response");
  282
+            }
266 283
           }
267  
-        } catch (Exception e) {
268  
-          e.printStackTrace();
  284
+        } catch (Exception e) {          
  285
+          error = "Web server could not contacted";
269 286
           Log.e(getPackageName(), "Error submitting hazard", e);
270 287
         }            
271 288
 
272  
-        hazard.setState(Hazard.State.SUMITTED);
273  
-        dbAdapter.open();
274  
-        dbAdapter.save(hazard);
275  
-        dbAdapter.close();
  289
+        //display any error to the user
  290
+        if(error != null) {         
  291
+          Toast toast = Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG);
  292
+          toast.show();
  293
+        } else {
  294
+          if(hazard.getPhoto() != null) {
  295
+            //submit the attached photo
  296
+            submitPhoto();
  297
+          } 
  298
+         
  299
+          //inform user submission complete
  300
+          Toast toast = Toast.makeText(getApplicationContext(), "Hazard submitted successfully", Toast.LENGTH_SHORT);
  301
+          toast.show();                   
  302
+        }        
276 303
         break;
277 304
     }
278 305
     finish();
@@ -281,6 +308,16 @@ public void submitClickHandler(View view) {
281 308
   /**
282 309
    * 
283 310
    */
  311
+  private void submitPhoto() {
  312
+    // TODO Auto-generated method stub
  313
+    hazard.setHasPhoto(true);
  314
+    persistHazard();
  315
+  }
  316
+
  317
+
  318
+  /**
  319
+   * 
  320
+   */
284 321
   private void persistHazard() {
285 322
     // populate the hazard and persist it
286 323
     hazard.setLocationDesc(locationDesc.getText().toString());
@@ -301,7 +338,7 @@ private void persistHazard() {
301 338
     hazard.setOnRedRoute(onRedRoute.isChecked());
302 339
     hazard.setOnLevelCrossing(onLevelCrossing.isChecked());
303 340
     hazard.setOnTowPath(onTowPath.isChecked());
304  
-    
  341
+
305 342
     dbAdapter.open();
306 343
     long id = dbAdapter.save(hazard);
307 344
     hazard.setId(id); 
@@ -365,15 +402,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
365 402
       }
366 403
       case PICTURE_ACTIVITY: {
367 404
         if (resultCode == RESULT_OK) {
368  
-                              
  405
+
369 406
           String[] projection = {MediaStore.Images.Media.DATA}; 
370 407
           Cursor cursor = managedQuery(capturedImageURI, projection, null, null, null); 
371 408
           int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
372 409
           cursor.moveToFirst(); 
373 410
           String capturedImageFilePath = cursor.getString(column_index_data);
374  
-                   
  411
+
375 412
           Bitmap bitmap = BitmapFactory.decodeFile(capturedImageFilePath);    
376  
-          
  413
+
377 414
           storeNewPhoto(bitmap, capturedImageFilePath);        
378 415
         }
379 416
         break;
@@ -401,8 +438,8 @@ protected Dialog onCreateDialog(int id) {
401 438
         // Create our AlertDialog
402 439
         Builder builder = new AlertDialog.Builder(this);
403 440
         builder
404  
-            .setMessage(
405  
-                "You can attach a photo of the hazard to your report. Would you like to take a new photo or use an existing one?")
  441
+        .setMessage(
  442
+            "You can attach a photo of the hazard to your report. Would you like to take a new photo or use an existing one?")
406 443
             .setCancelable(true).setPositiveButton("New photo", new DialogInterface.OnClickListener() {             
407 444
               @Override
408 445
               public void onClick(DialogInterface dialog, int which) {              
@@ -413,7 +450,7 @@ public void onClick(DialogInterface dialog, int which) {
413 450
                 values.put(Media.DESCRIPTION, "FillThatHole Image");
414 451
                 values.put(Media.DATA, imageDirectory.getPath() );
415 452
                 values.put(MediaStore.Images.Media.TITLE, filename);  
416  
-                
  453
+
417 454
                 // start the camera and take a new photo
418 455
                 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                                                                             
419 456
                 capturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

0 notes on commit 6ef8ddc

Please sign in to comment.
Something went wrong with that request. Please try again.