diff --git a/app/src/main/java/org/deke/risk/riskahead/ReportActivity.java b/app/src/main/java/org/deke/risk/riskahead/ReportActivity.java index 9bb3a75..48e2cdd 100644 --- a/app/src/main/java/org/deke/risk/riskahead/ReportActivity.java +++ b/app/src/main/java/org/deke/risk/riskahead/ReportActivity.java @@ -9,7 +9,6 @@ import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.util.Log; -import android.util.Patterns; import android.view.View; import android.widget.ArrayAdapter; import android.widget.DatePicker; @@ -67,7 +66,6 @@ public class ReportActivity extends BaseActivity { private int year, month, day; private int hour, minute; - public HashMap categories; public ArrayList incidentCategoryList; @@ -76,6 +74,8 @@ public class ReportActivity extends BaseActivity { super.onCreate(savedInstanceState); userHasToBeLoggedIn(); + Intent intent = getIntent(); + String pos = intent.getStringExtra(EXTRA_MESSAGE); getCategories(); final Calendar c = Calendar.getInstance(); @@ -93,8 +93,6 @@ public class ReportActivity extends BaseActivity { txtPosition = (EditText) findViewById(R.id.input_report_position); txtPositionDetail = (TextView) findViewById(R.id.lbl_report_position_detail); - restoreSharedPreferences(); - if(txtDay.getText().toString().equals("")) { txtDay.setText(new StringBuilder() .append(year).append("-").append(month + 1).append("-") @@ -113,8 +111,6 @@ public class ReportActivity extends BaseActivity { } }); - Intent intent = getIntent(); - String pos = intent.getStringExtra(EXTRA_MESSAGE); if(pos != null) { txtPosition.setText(pos); @@ -122,9 +118,26 @@ public class ReportActivity extends BaseActivity { Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); try { - List
addresses = geocoder.getFromLocation(Double.parseDouble(position[0]), Double.parseDouble(position[1]), 1); - txtPosition.setText(addresses.get(0).getLocality()); - txtPositionDetail.setText(addresses.get(0).getAddressLine(0)); + List
addressList = geocoder.getFromLocation(Double.parseDouble(position[0]), Double.parseDouble(position[1]), 1); + + if (addressList != null && addressList.size() > 0) { + Address address = addressList.get(0); + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { + if(txtPosition.getText().equals("") && address.getAddressLine(i) != null) txtPosition.setText(address.getAddressLine(i)); + if(address.getAddressLine(i) != null) sb.append(address.getAddressLine(i)).append("\n"); + + } + + //if(address.getLocality() != null) sb.append(address.getLocality()).append("\n"); + //if(address.getPostalCode() != null) sb.append(address.getPostalCode()).append("\n"); + if(address.getCountryName() != null) sb.append(address.getCountryName()); + + + txtPositionDetail.setText(sb.toString()); + } + latitude = position[0]; longitude = position[1]; } catch (IOException e) { @@ -147,6 +160,7 @@ public class ReportActivity extends BaseActivity { showDialog(DATE_DIALOG_ID); } }); + txtPosition.setEnabled(false); txtTime.setOnClickListener(new View.OnClickListener() { @Override @@ -155,11 +169,13 @@ public class ReportActivity extends BaseActivity { } }); + restoreSharedPreferences(); + txtTitle.addTextChangedListener(new TextValidator(txtTitle) { @Override public void validate(TextView textView, String text) { if (text.length() < 5) { - textView.setError("Your password must be at least\n" + + textView.setError("Title must be at least\n" + "5 characters in length."); } } @@ -169,37 +185,13 @@ public class ReportActivity extends BaseActivity { @Override public void validate(TextView textView, String text) { if (text.length() < 5) { - textView.setError("Your password must be at least\n" + + textView.setError("Description must be at least\n" + "5 characters in length."); } } }); } - private void restoreSharedPreferences() { - SharedPreferences prefs = getPreferences(MODE_PRIVATE); - String title = prefs.getString(SAVED_TITLE, null); - if (title != null) txtTitle.setText(title); - - String descr = prefs.getString(SAVED_DESCRIPTION_LONG, null); - if (descr != null) txtDescription.setText(descr); - - int crimec = prefs.getInt(SAVED_CRIME_CATEGORY, 0); - txtCrimeCategory.setSelection(crimec); - - String day = prefs.getString(SAVED_DATE_INCIDENT, null); - if (day != null) txtDay.setText(day); - - String time = prefs.getString(SAVED_DATE_TIME, null); - if (time != null) txtTime.setText(time); - - String posi = prefs.getString(SAVED_POSITION, null); - if (posi != null) txtPosition.setText(posi); - - latitude = prefs.getString("latitude",null); - longitude = prefs.getString("longitude",null); - } - private StringRequest getStringRequestAddIncidentWithPosition(final String textshort, final String textlong, final int crimecategory, final String latitude, final String longitude, final int status, final int radius, final String day, final String time) { return new StringRequest(Request.Method.POST, AppConfig.URL_REGISTER, new Response.Listener() { @@ -214,8 +206,19 @@ public class ReportActivity extends BaseActivity { if (!error) { showMessage("Report added!"); SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); - editor.clear(); - editor.apply(); + editor.clear().commit(); + txtTitle.setText(""); + txtDescription.setText(""); + txtCrimeCategory.setSelection(0); + txtDay.setText(""); + txtTime.setText(""); + txtPosition.setText(""); + findViewById(R.id.btn_report_position).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + gotoMapActivity(); + } + }); gotoMapActivity(latitude+":"+longitude); } else { String errorMsg = jObj.getString("error_msg"); @@ -253,7 +256,6 @@ public class ReportActivity extends BaseActivity { params.put("radius",Integer.toString(radius)); params.put("happened_at",year+"-"+month+"-"+day+" "+hour+":"+minute+":00"); - return params; } @@ -264,11 +266,9 @@ public class ReportActivity extends BaseActivity { protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: - // set date picker as current date return new DatePickerDialog(this, datePickerListener, year, month,day); case TIME_DIALOG_ID: - // set date picker as current date return new TimePickerDialog(this, timePickerListener, hour,minute,true); } @@ -305,15 +305,7 @@ public class ReportActivity extends BaseActivity { } }; - @Override - protected int getLayoutResourceId() { - return R.layout.activity_report; - } - @Override - protected String getActivityName() { - return mActivityTitle; - } @Override protected void onStop() { @@ -331,6 +323,32 @@ public class ReportActivity extends BaseActivity { editor.apply(); } + private void restoreSharedPreferences() { + SharedPreferences prefs = getPreferences(MODE_PRIVATE); + + txtTitle.setText(prefs.getString(SAVED_TITLE, "")); + txtDescription.setText(prefs.getString(SAVED_DESCRIPTION_LONG, "")); + txtCrimeCategory.setSelection(prefs.getInt(SAVED_CRIME_CATEGORY, 0)); + txtDay.setText(prefs.getString(SAVED_DATE_INCIDENT, new StringBuilder() + .append(year).append("-").append(month + 1).append("-") + .append(day).append(" ").toString())); + + txtTime.setText(prefs.getString(SAVED_DATE_TIME, new StringBuilder().append(hour) + .append(":").append(minute).append(" ").toString())); + + txtPosition.setText(prefs.getString(SAVED_POSITION, "0:0")); + + latitude = prefs.getString("latitude","0"); + longitude = prefs.getString("longitude","0"); + + findViewById(R.id.btn_report_position).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + gotoMapActivity(latitude + ":" + longitude); + } + }); + } + public void getCategories(){ String tag_string_req = "getincidentcategories"; StringRequest strReq = getStringRequestGetIncidentCategories(); @@ -397,4 +415,14 @@ public class ReportActivity extends BaseActivity { } }; } + + @Override + protected int getLayoutResourceId() { + return R.layout.activity_report; + } + + @Override + protected String getActivityName() { + return mActivityTitle; + } } diff --git a/app/src/main/java/org/deke/risk/riskahead/helper/TextValidator.java b/app/src/main/java/org/deke/risk/riskahead/helper/TextValidator.java index ee1c164..17a2978 100644 --- a/app/src/main/java/org/deke/risk/riskahead/helper/TextValidator.java +++ b/app/src/main/java/org/deke/risk/riskahead/helper/TextValidator.java @@ -12,6 +12,7 @@ public abstract class TextValidator implements TextWatcher { public TextValidator(TextView textView) { this.textView = textView; + if(this.textView.getText().equals("")) this.textView.setError("Please enter a value"); } public abstract void validate(TextView textView, String text); diff --git a/app/src/main/res/layout/activity_report.xml b/app/src/main/res/layout/activity_report.xml index 7bff981..4174eab 100644 --- a/app/src/main/res/layout/activity_report.xml +++ b/app/src/main/res/layout/activity_report.xml @@ -141,35 +141,36 @@ + android:layout_toRightOf="@+id/lbl_report_category" + android:layout_toEndOf="@+id/lbl_report_category"/> + bootstrap:bootstrapSize="lg" + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" /> + android:layout_alignLeft="@+id/input_report_position" + android:layout_alignStart="@+id/input_report_position" />