diff options
Diffstat (limited to 'android/Fix My Street/src')
8 files changed, 1291 insertions, 0 deletions
diff --git a/android/Fix My Street/src/com/android/fixmystreet/About.java b/android/Fix My Street/src/com/android/fixmystreet/About.java new file mode 100644 index 000000000..9ddffadf6 --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/About.java @@ -0,0 +1,61 @@ +package com.android.fixmystreet; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.text.util.Linkify; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.TextView; + +public class About extends Activity { + + private Bundle extras = null; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.about); + extras = getIntent().getExtras(); + + // add links + TextView noteView = (TextView) findViewById(R.id.faq); + Linkify.addLinks(noteView, Linkify.ALL); + } + + // **************************************************** + // Options menu functions + // **************************************************** + + // TODO - add Bundles for these? + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + MenuItem homeItem = menu.add(0, 0, 0, "Home"); + MenuItem aboutItem = menu.add(0, 1, 0, "Help"); + aboutItem.setIcon(android.R.drawable.ic_menu_info_details); + homeItem.setIcon(android.R.drawable.ic_menu_edit); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case 0: + Intent i = new Intent(About.this, Home.class); + if (extras != null) { + i.putExtras(extras); + } + startActivity(i); + return true; + case 1: + Intent j = new Intent(About.this, Help.class); + if (extras != null) { + j.putExtras(extras); + } + startActivity(j); + return true; + } + return false; + } +}
\ No newline at end of file diff --git a/android/Fix My Street/src/com/android/fixmystreet/Details.java b/android/Fix My Street/src/com/android/fixmystreet/Details.java new file mode 100644 index 000000000..83c5cc78c --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Details.java @@ -0,0 +1,249 @@ +// ******************************************************************************** +//details.java +//This file is where most of the work of the application happens. It collects the +//subject of the problem, plus the user's name and email, from the Android form. +//It uploads them to FixMyStreet, and shows a success or failure message. +// +//******************************************************************************** + +package com.android.fixmystreet; + +import java.util.regex.*; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +//import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.EditText; + +public class Details extends Activity { + private EditText nameET; + private EditText emailET; + private EditText subjectET; + String storedName; + String storedEmail; + private String subject; + private String name; + private String email; + private View submitButton; + //private static final String LOG_TAG = "Details"; + public static final String PREFS_NAME = "FMS_Settings"; + final int NAME_WARNING = 999; + final int SUBJECT_WARNING = 998; + final int EMAIL_WARNING = 997; + private Bundle extras; + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // set up the page + setContentView(R.layout.details); + nameET = (EditText) findViewById(R.id.name_text); + emailET = (EditText) findViewById(R.id.email_text); + subjectET = (EditText) findViewById(R.id.subject_text); + submitButton = this.findViewById(R.id.submit_button); + + // set the button listeners + setListeners(); + + // fill in name/email, if already defined + // NB - from settings, rather than from bundle... + SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); + name = settings.getString("myName", ""); + email = settings.getString("myEmail", ""); + nameET.setText(name); + emailET.setText(email); + + extras = getIntent().getExtras(); + if (extras != null) { + // Details extras + subject = extras.getString("subject"); + } + if (subject != null) { + subjectET.setText(subject); + } + } + + private void setListeners() { + // Save info and pass back to Home activity + submitButton.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + subject = subjectET.getText().toString(); + email = emailET.getText().toString(); + name = nameET.getText().toString(); + if (!textFieldsAreValid(subject)) { + showDialog(SUBJECT_WARNING); + } else if (!textFieldsAreValid(name)) { + showDialog(NAME_WARNING); + } else if (!isValidEmailAddress(email)) { + showDialog(EMAIL_WARNING); + } else { + if (true) { + Intent i = new Intent(Details.this, Home.class); + extras.putString("name", name); + extras.putString("email", email); + extras.putString("subject", subject); + i.putExtras(extras); + startActivity(i); + } + } + } + }); + } + + // ********************************************************************** + // textFieldsAreValid: Make sure that fields aren't blank + // ********************************************************************** + public static boolean textFieldsAreValid(String field) { + if (field == null || field.length() == 0 || field.trim().length() == 0) { + return false; + } + return true; + } + + // ********************************************************************** + // isValidEmailAddress: Check the email address is OK + // ********************************************************************** + public static boolean isValidEmailAddress(String emailAddress) { + String emailRegEx; + Pattern pattern; + // Regex for a valid email address + emailRegEx = "^[A-Za-z0-9._%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,4}$"; + // Compare the regex with the email address + pattern = Pattern.compile(emailRegEx); + Matcher matcher = pattern.matcher(emailAddress); + if (!matcher.find()) { + return false; + } + return true; + } + + // ********************************************************************** + // onCreateDialog: Dialog warnings + // ********************************************************************** + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case SUBJECT_WARNING: + return new AlertDialog.Builder(Details.this).setTitle("Subject") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }).setMessage("Please enter a subject!").create(); + case NAME_WARNING: + return new AlertDialog.Builder(Details.this) + .setTitle("Name") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Please enter your name. We'll remember it for next time.") + .create(); + + case EMAIL_WARNING: + return new AlertDialog.Builder(Details.this) + .setTitle("Email") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Please enter a valid email address. We'll remember it for next time.") + .create(); + + } + return null; + } + + // Save user's name and email, if already defined + @Override + protected void onStop() { + super.onStop(); + + name = nameET.getText().toString(); + email = emailET.getText().toString(); + + // Save user preferences + SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putString("myName", name); + editor.putString("myEmail", email); + + // Don't forget to commit your edits!!! + editor.commit(); + } + + // Look at this - is it working ok + // public boolean testProviders() { + // Log.e(LOG_TAG, "testProviders"); + // // StringBuilder sb = new StringBuilder("Enabled Providers"); + // // List<String> providers = locationmanager.getProviders(true); + // // for (String provider : providers) { + // // Log.e(LOG_TAG, "Provider = " + provider); + // // listener = new LocationListener() { + // // public void onLocationChanged(Location location) { + // // } + // // + // // public void onProviderDisabled(String provider) { + // // } + // // + // // public void onProviderEnabled(String provider) { + // // } + // // + // // public void onStatusChanged(String provider, int status, + // // Bundle extras) { + // // } + // // }; + // // + // // locationmanager.requestLocationUpdates(provider, 0, 0, listener); + // // + // // sb.append("\n*").append(provider).append(": "); + // // + // // Location location = locationmanager.getLastKnownLocation(provider); + // // + // // if (location != null) { + // // latitude = location.getLatitude(); + // // longitude = location.getLongitude(); + // // latString = latitude.toString(); + // // longString = longitude.toString(); + // // Log.e(LOG_TAG, "Latitude = " + latString); + // // Log.e(LOG_TAG, "Longitude = " + longString); + // // if (provider == "gps") { + // // // Only bother with GPS if available + // // return true; + // // } + // // } else { + // // Log.e(LOG_TAG, "Location is null"); + // // return false; + // // } + // // } + // // LocationManager lm = (LocationManager) + // // context.getSystemService(Context.LOCATION_SERVICE); + // // + // // Location loc = lm.getLastKnownLocation("gps"); + // // if (loc == null) + // // { + // // locType = "Network"; + // // loc = lm.getLastKnownLocation("network"); + // // } + // // + // // textMsg.setText(sb); + // + // return true; + // } +} diff --git a/android/Fix My Street/src/com/android/fixmystreet/Help.java b/android/Fix My Street/src/com/android/fixmystreet/Help.java new file mode 100644 index 000000000..3153ffe3c --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Help.java @@ -0,0 +1,59 @@ +package com.android.fixmystreet; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.text.util.Linkify; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.TextView; + +public class Help extends Activity { + private Bundle extras = null; + + @Override + protected void onCreate(Bundle icicle) { + super.onCreate(icicle); + extras = getIntent().getExtras(); + setContentView(R.layout.help); + TextView noteView = (TextView) findViewById(R.id.faq); + Linkify.addLinks(noteView, Linkify.ALL); + } + + // **************************************************** + // Options menu functions + // **************************************************** + + // TODO - add Bundles for these? + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + MenuItem homeItem = menu.add(0, 0, 0, "Home"); + MenuItem aboutItem = menu.add(0, 1, 0, "About"); + homeItem.setIcon(android.R.drawable.ic_menu_edit); + aboutItem.setIcon(android.R.drawable.ic_menu_info_details); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case 0: + Intent i = new Intent(Help.this, Home.class); + if (extras != null) { + i.putExtras(extras); + } + startActivity(i); + return true; + case 1: + Intent j = new Intent(Help.this, About.class); + if (extras != null) { + j.putExtras(extras); + } + startActivity(j); + return true; + } + return false; + } +} diff --git a/android/Fix My Street/src/com/android/fixmystreet/Home.java b/android/Fix My Street/src/com/android/fixmystreet/Home.java new file mode 100644 index 000000000..38875fd2b --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Home.java @@ -0,0 +1,726 @@ +// ************************************************************************** +// Home.java +// ************************************************************************** +package com.android.fixmystreet; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; + +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; +import android.net.Uri; +import android.os.Bundle; +import android.os.Environment; +import android.os.Handler; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Button; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; +import android.provider.MediaStore; +import android.view.View; +import android.view.View.OnClickListener; + +public class Home extends Activity { + // **************************************************** + // Local variables + // **************************************************** + //private static final String LOG_TAG = "Home"; + private Button btnReport; + private Button btnDetails; + private Button btnPicture; + // Info that's been passed from other activities + private Boolean haveDetails = false; + private Boolean havePicture = false; + private String name = null; + private String email = null; + private String subject = null; + // Location info + LocationManager locationmanager; + LocationListener listener; + private Double latitude; + private Double longitude; + private String latString = ""; + private String longString = ""; + // hacky way of checking the results + private static int globalStatus = 13; + private static final int SUCCESS = 0; + private static final int LOCATION_NOT_FOUND = 1; + private static final int UPLOAD_ERROR = 2; + private static final int UPLOAD_ERROR_SERVER = 3; + private static final int LOCATION_NOT_ACCURATE = 4; + private static final int PHOTO_NOT_FOUND = 5; + private String serverResponse; + // Thread handling + ProgressDialog myProgressDialog = null; + private ProgressDialog pd; + final Handler mHandler = new Handler(); + final Runnable mUpdateResults = new Runnable() { + public void run() { + pd.dismiss(); + updateResultsInUi(); + } + }; + private Bundle extras; + //private Bitmap bmp = null; + + // Called when the activity is first created + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setContentView(R.layout.home); + + testProviders(); + // showDialog(); + + btnDetails = (Button) findViewById(R.id.details_button); + btnPicture = (Button) findViewById(R.id.camera_button); + btnReport = (Button) findViewById(R.id.report_button); + btnReport.setVisibility(View.GONE); + + if (icicle != null) { + havePicture = icicle.getBoolean("photo"); + } + + extras = getIntent().getExtras(); + checkBundle(); + setListeners(); + } + + @Override + protected void onPause() { + //Log.d("onPause, havePicture = " + havePicture); + removeListeners(); + saveState(); + super.onPause(); + } + + @Override + protected void onStop() { + //Log.d(LOG_TAG, "onStop, havePicture = " + havePicture); + removeListeners(); + super.onStop(); + } + + @Override + public void onRestart() { + //Log.d(LOG_TAG, "onRestart, havePicture = " + havePicture); + testProviders(); + checkBundle(); + super.onRestart(); + } + + // **************************************************** + // checkBundle - check the extras that have been passed + // is the user able to upload things yet, or not? + // **************************************************** + private void checkBundle() { + //Log.d(LOG_TAG, "checkBundle"); + + // Get the status icons... + Resources res = getResources(); + Drawable checked = res.getDrawable(R.drawable.done); + + if (extras != null) { + // Details extras + name = extras.getString("name"); + email = extras.getString("email"); + subject = extras.getString("subject"); + havePicture = extras.getBoolean("photo"); + + // Do we have the details? + if ((name != null) && (email != null) && (subject != null)) { + haveDetails = true; + //Log.d(LOG_TAG, "Have all details"); + checked.setBounds(0, 0, checked.getIntrinsicWidth(), checked + .getIntrinsicHeight()); + // envelope.setBounds(0, 0, envelope.getIntrinsicWidth(), + // envelope + // .getIntrinsicHeight()); + btnDetails.setText("Details added: '" + subject + "'"); + btnDetails.setCompoundDrawables(null, null, checked, null); + } else { + //Log.d(LOG_TAG, "Don't have details"); + } + } else { + extras = new Bundle(); + //Log.d(LOG_TAG, "no Bundle at all"); + } + //Log.d(LOG_TAG, "havePicture = " + havePicture); + + // Do we have the photo? + if (havePicture) { + + checked.setBounds(0, 0, checked.getIntrinsicWidth(), checked + .getIntrinsicHeight()); + // camera.setBounds(0, 0, camera.getIntrinsicWidth(), camera + // .getIntrinsicHeight()); + btnPicture.setCompoundDrawables(null, null, checked, null); + btnPicture.setText("Photo taken"); + // code for if we wanted to show a thumbnail - works but crashes + // ImageView iv = (ImageView) findViewById(R.id.thumbnail); + // try { + // Log.d(LOG_TAG, "Trying to look for FMS photo"); + // FileInputStream fstream = null; + // fstream = new FileInputStream(Environment + // .getExternalStorageDirectory() + // + "/" + "FMS_photo.jpg"); + // Log.d("Looking for file at ", Environment + // .getExternalStorageDirectory() + // + "/" + "FMS_photo.jpg"); + // bmp = BitmapFactory.decodeStream(fstream); + // // bmp = BitmapFactory + // // .decodeStream(openFileInput("FMS_photo.jpg")); + // iv.setImageBitmap(bmp); + // System.gc(); + // } catch (FileNotFoundException e) { + // // TODO Auto-generated catch block + // Log.d(LOG_TAG, "FMS photo not found"); + // e.printStackTrace(); + // } + } + + // We have details and photo - show the Report button + if (haveDetails && havePicture) { + btnReport.setVisibility(View.VISIBLE); + } + } + + // **************************************************** + // setListeners - set the button listeners + // **************************************************** + + private void setListeners() { + btnDetails.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + Intent i = new Intent(Home.this, Details.class); + extras.putString("name", name); + extras.putString("email", email); + extras.putString("subject", subject); + i.putExtras(extras); + startActivity(i); + } + }); + btnPicture.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + File photo = new File( + Environment.getExternalStorageDirectory(), + "FMS_photo.jpg"); + if (photo.exists()) { + photo.delete(); + } + Intent imageCaptureIntent = new Intent( + MediaStore.ACTION_IMAGE_CAPTURE); + imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri + .fromFile(photo)); + startActivityForResult(imageCaptureIntent, 1); + } + }); + btnReport.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + uploadToFMS(); + } + }); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + //Log.d(LOG_TAG, "onActivityResult"); + //Log.d(LOG_TAG, "Activity.RESULT_OK code = " + Activity.RESULT_OK); + //Log.d(LOG_TAG, "resultCode = " + resultCode + "requestCode = " + // + requestCode); + if (resultCode == Activity.RESULT_OK && requestCode == 1) { + havePicture = true; + extras.putBoolean("photo", true); + } + //testProviders(); + //Log.d(LOG_TAG, "havePicture = " + havePicture.toString()); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + //Log.d(LOG_TAG, "onSaveInstanceState"); + if (havePicture != null) { + // Log.d(LOG_TAG, "mRowId = " + mRowId); + outState.putBoolean("photo", havePicture); + } + // if (name != null) { + // // Log.d(LOG_TAG, "mRowId = " + mRowId); + // outState.putString("name", name); + // } + // if (email != null) { + // // Log.d(LOG_TAG, "mRowId = " + mRowId); + // outState.putString("email", email); + // } + // if (subject != null) { + // // Log.d(LOG_TAG, "mRowId = " + mRowId); + // outState.putString("subject", subject); + // } + } + + // TODO - save bits and pieces here + private void saveState() { + // Log.d(LOG_TAG, "saveState"); + // String body = mBodyText.getText().toString(); + // String title = mTitleText.getText().toString(); + // // Log.d(LOG_TAG, "title valid"); + // if (mRowId == null) { + // // Log.d(LOG_TAG, "mRowId = null, creating note"); + // long id = mDbHelper.createNote(body, title); + // if (id > 0) { + // mRowId = id; + // // Log.d(LOG_TAG, "Set mRowId to " + mRowId); + // } + // } else { + // // Log.d(LOG_TAG, "mRowId = " + mRowId + ", updating note"); + // mDbHelper.updateNote(mRowId, body, title); + // } + } + + // ********************************************************************** + // uploadToFMS: uploads details, handled via a background thread + // Also checks the age and accuracy of the GPS data first + // ********************************************************************** + private void uploadToFMS() { + //Log.d(LOG_TAG, "uploadToFMS"); + pd = ProgressDialog + .show( + this, + "Uploading, please wait...", + "Uploading. This can take up to a minute, depending on your connection speed. Please be patient!", + true, false); + Thread t = new Thread() { + public void run() { + doUploadinBackground(); + mHandler.post(mUpdateResults); + } + }; + t.start(); + } + + private void updateResultsInUi() { + if (globalStatus == UPLOAD_ERROR) { + showDialog(UPLOAD_ERROR); + } else if (globalStatus == UPLOAD_ERROR_SERVER) { + showDialog(UPLOAD_ERROR_SERVER); + } else if (globalStatus == LOCATION_NOT_FOUND) { + showDialog(LOCATION_NOT_FOUND); + } else if (globalStatus == PHOTO_NOT_FOUND) { + showDialog(PHOTO_NOT_FOUND); + } else if (globalStatus == LOCATION_NOT_ACCURATE) { + showDialog(LOCATION_NOT_ACCURATE); + } else { + // Success! - Proceed to the success activity! + Intent i = new Intent(Home.this, Success.class); + i.putExtra("latString", latString); + i.putExtra("lonString", longString); + startActivity(i); + } + } + + // ********************************************************************** + // onCreateDialog: Dialog warnings + // ********************************************************************** + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case UPLOAD_ERROR: + return new AlertDialog.Builder(Home.this) + .setTitle("Upload error") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Sorry, there was an error uploading - maybe the network connection is down? Please try again later.") + .create(); + case UPLOAD_ERROR_SERVER: + return new AlertDialog.Builder(Home.this) + .setTitle("Upload error") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Sorry, there was an error uploading. Please try again later. The server response was: " + + serverResponse).create(); + case LOCATION_NOT_FOUND: + return new AlertDialog.Builder(Home.this) + .setTitle("GPS problem") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Could not get location! Can you see the sky? Please try again later.") + .create(); + case PHOTO_NOT_FOUND: + return new AlertDialog.Builder(Home.this).setTitle("No photo") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }).setMessage("Photo not found!").create(); + case LOCATION_NOT_ACCURATE: + return new AlertDialog.Builder(Home.this) + .setTitle("GPS problem") + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, + int whichButton) { + } + }) + .setMessage( + "Sorry, your GPS location is not accurate enough. Can you see the sky?") + .create(); + } + return null; + } + + // ********************************************************************** + // doUploadinBackground: POST request to FixMyStreet + // ********************************************************************** + private boolean doUploadinBackground() { + //Log.d(LOG_TAG, "doUploadinBackground"); + + String responseString = null; + PostMethod method; + + // DefaultHttpClient httpClient; + // HttpPost httpPost; + // HttpResponse response; + // HttpEntity entity; + // UrlEncodedFormEntity urlentity; + // // get the photo data from the URI + // // Uri uri = (Uri) content.getParcelable("URI"); + // Context context = getApplicationContext(); + // ContentResolver cR = context.getContentResolver(); + // + // // Get the type of the file + // MimeTypeMap mime = MimeTypeMap.getSingleton(); + // String type = mime.getExtensionFromMimeType(cR.getType(uri)); + // + // // Get the InputStream + // InputStream in = null; + // + // try { + // in = cR.openInputStream(uri); + // } catch (FileNotFoundException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // + // if (in == null) { + // globalStatus = PHOTO_NOT_FOUND; + // return false; + // } + // + // // Setting the InputStream Body + // InputStreamBody body = new InputStreamBody(in, "image." + type); + + // TODO - check location updates + Location location = locationmanager + .getLastKnownLocation(LocationManager.GPS_PROVIDER); + + if (location != null) { + // TODO - put back in + long currentTime = System.currentTimeMillis(); + long gpsTime = location.getTime(); + long timeDiffSecs = (currentTime - gpsTime) / 1000; + //Log.e(LOG_TAG, "Location accuracy = " + location.getAccuracy()); + //Log.e(LOG_TAG, "Location age = " + timeDiffSecs); + if ((location.getAccuracy() > 150) || (timeDiffSecs > 15)) { + //Log.e(LOG_TAG, "Location not accurate"); + globalStatus = LOCATION_NOT_ACCURATE; + return false; + } + latitude = location.getLatitude(); + longitude = location.getLongitude(); + latString = latitude.toString(); + longString = longitude.toString(); + //Log.e(LOG_TAG, "Latitude = " + latString); + //Log.e(LOG_TAG, "Longitude = " + longString); + } else { + //Log.e(LOG_TAG, "Location is null"); + globalStatus = LOCATION_NOT_FOUND; + return false; + } + + // TEMP - testing + // latString = "51.5"; + // longString = "-0.116667"; + + method = new PostMethod("http://www.fixmystreet.com/import"); + + try { + + // Bitmap bitmap; + // ByteArrayOutputStream imageByteStream; + byte[] imageByteArray = null; + // ByteArrayPartSource fileSource; + + HttpClient client = new HttpClient(); + client.getHttpConnectionManager().getParams().setConnectionTimeout( + 100000); + + // InputStream in = + // this.getResources().openRawResource(R.drawable.tom); + // bitmap = android.provider.MediaStore.Images.Media.getBitmap( + // getContentResolver(), uri); + // imageByteStream = new ByteArrayOutputStream(); + + // if (bitmap == null) { + // Log.d(LOG_TAG, "No bitmap"); + // } + + // Compress bmp to jpg, write to the bytes output stream + // bitmap.compress(Bitmap.CompressFormat.JPEG, 80, imageByteStream); + + // Turn the byte stream into a byte array, write to imageData + // imageByteArray = imageByteStream.toByteArray(); + + File f = new File(Environment.getExternalStorageDirectory(), + "FMS_photo.jpg"); + + // TODO - add a check here + if (!f.exists()) { + } + imageByteArray = getBytesFromFile(f); + +// Log +// .d(LOG_TAG, "len of data is " + imageByteArray.length +// + " bytes"); + + // fileSource = new ByteArrayPartSource("photo", imageData); + FilePart photo = new FilePart("photo", new ByteArrayPartSource( + "photo", imageByteArray)); + + photo.setContentType("image/jpeg"); + photo.setCharSet(null); + + Part[] parts = { new StringPart("service", "your Android phone"), + new StringPart("subject", subject), + new StringPart("name", name), + new StringPart("email", email), + new StringPart("lat", latString), + new StringPart("lon", longString), photo }; + + method.setRequestEntity(new MultipartRequestEntity(parts, method + .getParams())); + + client.executeMethod(method); + responseString = method.getResponseBodyAsString(); + method.releaseConnection(); + + Log.e("httpPost", "Response status: " + responseString); + Log.e("httpPost", "Latitude = " + latString + " and Longitude = " + + longString); + + // textMsg.setText("Bitmap (bitmap) = " + bitmap.toString() + // + " AND imageByteArray (byte[]) = " + // + imageByteArray.toString() + // + " AND imageByteStream (bytearrayoutputstream) = " + // + imageByteStream.toString()); + + } catch (Exception ex) { + //Log.v(LOG_TAG, "Exception", ex); + globalStatus = UPLOAD_ERROR; + serverResponse = ""; + return false; + } finally { + method.releaseConnection(); + } + + if (responseString.equals("SUCCESS")) { + // launch the Success page + globalStatus = SUCCESS; + return true; + } else { + // print the response string? + serverResponse = responseString; + globalStatus = UPLOAD_ERROR; + return false; + } + } + + public void testProviders() { + //Log.e(LOG_TAG, "testProviders"); + // Register for location listener + String location_context = Context.LOCATION_SERVICE; + locationmanager = (LocationManager) getSystemService(location_context); + // StringBuilder sb = new StringBuilder("Enabled Providers"); + // List<String> providers = locationmanager.getProviders(true); + // for (String provider : providers) { + listener = new LocationListener() { + public void onLocationChanged(Location location) { + } + + public void onProviderDisabled(String provider) { + } + + public void onProviderEnabled(String provider) { + } + + public void onStatusChanged(String provider, int status, + Bundle extras) { + } + }; + locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, + 0, listener); + if (!locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + buildAlertMessageNoGps(); + } + } + + private void buildAlertMessageNoGps() { + final AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder + .setMessage( + "Your GPS seems to be disabled. Do you want to turn it on now?") + .setCancelable(false).setPositiveButton("Yes", + new DialogInterface.OnClickListener() { + public void onClick( + @SuppressWarnings("unused") final DialogInterface dialog, + @SuppressWarnings("unused") final int id) { + Intent j = new Intent(); + j + .setAction("android.settings.LOCATION_SOURCE_SETTINGS"); + startActivity(j); + } + }).setNegativeButton("No", + new DialogInterface.OnClickListener() { + public void onClick(final DialogInterface dialog, + @SuppressWarnings("unused") final int id) { + dialog.cancel(); + } + }); + final AlertDialog alert = builder.create(); + alert.show(); + } + + public void removeListeners() { + //Log.e(LOG_TAG, "removeListeners"); + if (locationmanager != null) { + locationmanager.removeUpdates(listener); + } + locationmanager = null; + //Log.d(LOG_TAG, "Removed " + listener.toString()); + } + + // **************************************************** + // Options menu functions + // **************************************************** + + // TODO - add Bundles for these? + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + MenuItem helpItem = menu.add(0, 0, 0, "Help"); + MenuItem aboutItem = menu.add(0, 1, 0, "About"); + aboutItem.setIcon(android.R.drawable.ic_menu_info_details); + helpItem.setIcon(android.R.drawable.ic_menu_help); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case 0: + Intent i = new Intent(Home.this, Help.class); + if (extras != null) { + i.putExtras(extras); + } + startActivity(i); + return true; + case 1: + Intent j = new Intent(Home.this, About.class); + if (extras != null) { + j.putExtras(extras); + } + startActivity(j); + return true; + } + return false; + } + + // read the photo file into a byte array... + public static byte[] getBytesFromFile(File file) throws IOException { + InputStream is = new FileInputStream(file); + + // Get the size of the file + long length = file.length(); + + // You cannot create an array using a long type. + // It needs to be an int type. + // Before converting to an int type, check + // to ensure that file is not larger than Integer.MAX_VALUE. + if (length > Integer.MAX_VALUE) { + // File is too large + } + + // Bitmap bitmap; + // ByteArrayOutputStream imageByteStream; + // byte[] imageByteArray = null; + + // InputStream in = + // this.getResources().openRawResource(R.drawable.tom); + // bitmap = android.provider.MediaStore.Images.Media.getBitmap( + // getContentResolver(), uri); + // imageByteStream = new ByteArrayOutputStream(); + + // Compress bmp to jpg, write to the bytes output stream + // bitmap.compress(Bitmap.CompressFormat.JPEG, 80, imageByteStream); + + // Turn the byte stream into a byte array, write to imageData + // imageByteArray = imageByteStream.toByteArray(); + + // Create the byte array to hold the data + byte[] bytes = new byte[(int) length]; + + // Read in the bytes + int offset = 0; + int numRead = 0; + while (offset < bytes.length + && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { + offset += numRead; + } + + // Ensure all the bytes have been read in + if (offset < bytes.length) { + throw new IOException("Could not completely read file " + + file.getName()); + } + + // Close the input stream and return bytes + is.close(); + return bytes; + } +} diff --git a/android/Fix My Street/src/com/android/fixmystreet/R.java b/android/Fix My Street/src/com/android/fixmystreet/R.java new file mode 100644 index 000000000..54fde9562 --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/R.java @@ -0,0 +1,74 @@ +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package com.android.fixmystreet; + +public final class R { + public static final class attr { + } + public static final class color { + public static final int sky_blue=0x7f040001; + public static final int translucent_blue=0x7f040002; + public static final int transparent=0x7f040000; + } + public static final class drawable { + public static final int add=0x7f020000; + public static final int blue_filler=0x7f020001; + public static final int done=0x7f020002; + public static final int filler=0x7f020003; + public static final int house_icon=0x7f020004; + public static final int spacer=0x7f020005; + public static final int street_background_smaller=0x7f020006; + } + public static final class id { + public static final int background_image=0x7f07000c; + public static final int camera_button=0x7f070009; + public static final int details_button=0x7f07000a; + public static final int email_label=0x7f070006; + public static final int email_text=0x7f070007; + public static final int faq=0x7f070000; + public static final int hello_text=0x7f07000d; + public static final int name_label=0x7f070004; + public static final int name_save=0x7f070001; + public static final int name_text=0x7f070005; + public static final int report_button=0x7f07000b; + public static final int subject_label=0x7f070002; + public static final int subject_text=0x7f070003; + public static final int submit_button=0x7f070008; + } + public static final class layout { + public static final int about=0x7f030000; + public static final int details=0x7f030001; + public static final int help=0x7f030002; + public static final int home=0x7f030003; + public static final int success=0x7f030004; + } + public static final class string { + public static final int about=0x7f050007; + /** Welcome screen + */ + public static final int app_name=0x7f050000; + public static final int close=0x7f050008; + public static final int confirm_location=0x7f050002; + public static final int confirm_location_done=0x7f050003; + public static final int details=0x7f050005; + /** Details screen + */ + public static final int details_screen=0x7f05000a; + public static final int hello=0x7f050001; + public static final int imageUrl=0x7f050009; + /** About screen + */ + public static final int instructions=0x7f05000c; + public static final int report=0x7f050006; + public static final int submit=0x7f05000b; + public static final int take_a_picture=0x7f050004; + } + public static final class style { + public static final int Theme_Filler=0x7f060000; + } +} diff --git a/android/Fix My Street/src/com/android/fixmystreet/Settings.java b/android/Fix My Street/src/com/android/fixmystreet/Settings.java new file mode 100644 index 000000000..1c3e8df90 --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Settings.java @@ -0,0 +1,33 @@ +package com.android.fixmystreet; + +import android.app.Activity; +import android.os.Bundle; +import android.content.SharedPreferences; + +public class Settings extends Activity { + public static final String PREFS_NAME = "Settings"; + + + @Override + protected void onCreate(Bundle state){ + super.onCreate(state); + } + + @Override + protected void onStop(){ + super.onStop(); + + String name = ""; + String email = ""; + + // Save user preferences. We need an Editor object to + // make changes. All objects are from android.context.Context + SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); + SharedPreferences.Editor editor = settings.edit(); + editor.putString("myName", name); + editor.putString("myEmail", email); + + // Don't forget to commit your edits!!! + editor.commit(); + } + } diff --git a/android/Fix My Street/src/com/android/fixmystreet/Stored.java b/android/Fix My Street/src/com/android/fixmystreet/Stored.java new file mode 100644 index 000000000..db83c905f --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Stored.java @@ -0,0 +1,32 @@ +package com.android.fixmystreet; + +import android.content.Context; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.preference.PreferenceManager; + +public class Stored extends PreferenceActivity { + // Option names and default values + private static final String OPT_EMAIL = "email"; + private static final boolean OPT_EMAIL_DEF = true; + private static final String OPT_NAME = "name"; + private static final boolean OPT_NAME_DEF = true; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // addPreferencesFromResource(R.xml.settings); + } + + // Get email, if stored + public static boolean getEmail(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(OPT_EMAIL, OPT_EMAIL_DEF); + } + + // Get name, if stored + public static boolean getName(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(OPT_NAME, OPT_NAME_DEF); + } +}
\ No newline at end of file diff --git a/android/Fix My Street/src/com/android/fixmystreet/Success.java b/android/Fix My Street/src/com/android/fixmystreet/Success.java new file mode 100644 index 000000000..ca3dcee77 --- /dev/null +++ b/android/Fix My Street/src/com/android/fixmystreet/Success.java @@ -0,0 +1,57 @@ +//************************************************************* +// +//************************************************************* + +package com.android.fixmystreet; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +//import android.util.Log; +import android.view.KeyEvent; +import android.view.Menu; +import android.view.MenuItem; + +public class Success extends Activity { + + //private static final String LOG_TAG = "Success"; + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.success); + } + + // **************************************************** + // Options menu functions + // **************************************************** + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + MenuItem helpItem = menu.add(0, 0, 0, "Home"); + helpItem.setIcon(android.R.drawable.ic_menu_edit); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case 0: + Intent i = new Intent(Success.this, Home.class); + startActivity(i); + return true; + } + return false; + } + + // disable the Back key in case things get submitted twice + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK) { + return true; + } + return false; + } + +}
\ No newline at end of file |