Skip to content

Commit

Permalink
Merge pull request #625 from camsys/fix-reverse-npe
Browse files Browse the repository at this point in the history
Fix #623 - NPE when reverse and no origin/destination
  • Loading branch information
barbeau authored Jul 21, 2016
2 parents a7038a9 + 681f0b8 commit 1c43d04
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,26 @@ public CustomAddress[] newArray(int size) {
return new CustomAddress[size];
}
};

/**
* Is this custom address set.
*
* @return true if this address has a valid latitude and longitude
*/
public boolean isSet() {
return getLatitude() != Double.MAX_VALUE && getLongitude() != Double.MAX_VALUE;
}

/**
* Create a blank CustomAddress.
*
* @return CustomAddress with default locale and unset latitude and longitude.
*/
public static CustomAddress getEmptyAddress() {
Locale locale = Locale.getDefault();
CustomAddress addr = new CustomAddress(locale);
addr.setLatitude(Double.MAX_VALUE);
addr.setLongitude(Double.MAX_VALUE);
return addr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public void copyIntoBundle(Bundle target) {
* @return true if ready to submit, false otherwise
*/
public boolean ready() {
return getFrom() != null && getTo() != null && getDateTime() != null;
return getFrom() != null && getFrom().isSet() && getTo() != null
&& getTo().isSet() && getDateTime() != null;
}

private static String getFormattedDate(String format, Date date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@


public class TripPlanActivity extends AppCompatActivity implements TripRequest.Callback,
TripResultsFragment.Listener {
TripResultsFragment.Listener, TripPlanFragment.Listener {

private static final String TAG = "TripPlanActivity";

Expand Down Expand Up @@ -136,6 +136,7 @@ protected void onResume() {
if (fragment == null) {
fragment = new TripPlanFragment();
fragment.setArguments(bundle);
fragment.setListener(this);
getSupportFragmentManager().beginTransaction()
.add(R.id.trip_plan_fragment_container, fragment).commit();
}
Expand Down Expand Up @@ -245,7 +246,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
return false;
}

public void route() {
@Override
public void onTripRequestReady() {

// Remove results fragment if it exists
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.trip_results_fragment_container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@

public class TripPlanFragment extends Fragment {

/**
* Allows calling activity to register to know when to send request.
*/
interface Listener {

/**
* Called when the fields have been populated and a trip plan can occur.
*/
void onTripRequestReady();
}

private static final int USE_FROM_ADDRESS = 1;
private static final int USE_TO_ADDRESS = 2;

Expand All @@ -91,6 +102,8 @@ public class TripPlanFragment extends Fragment {

private TripRequestBuilder mBuilder;

private Listener mListener;

private void resetDateTimeLabels() {
String dateText = new SimpleDateFormat(OTPConstants.TRIP_PLAN_DATE_STRING_FORMAT, Locale.getDefault())
.format(mMyCalendar.getTime());
Expand Down Expand Up @@ -211,15 +224,14 @@ public void onClick(View v) {
}
});


// Start: default from address is Current Location, to address is unset

return view;
}

private void checkRequestAndSubmit() {
if (mBuilder.ready()) {
((TripPlanActivity) getActivity()).route();
mListener.onTripRequestReady();
}
}

Expand All @@ -234,20 +246,20 @@ public void onResume() {
mFromAddress = makeAddressFromLocation();
mBuilder.setFrom(mFromAddress);
}
else {
mFromAddressTextArea.setText(mFromAddress.toString());
}

setAddressText(mFromAddressTextArea, mFromAddress);

mToAddress = mBuilder.getTo();
if (mToAddress != null) {
mToAddressTextArea.setText(mToAddress.toString());

if (mToAddress == null) {
mToAddress = CustomAddress.getEmptyAddress();
mBuilder.setTo(mToAddress);
}

setAddressText(mToAddressTextArea, mToAddress);

boolean arriving = mBuilder.getArriveBy();


if (mMyCalendar == null) {
Date date = mBuilder.getDateTime();
if (date == null) {
Expand Down Expand Up @@ -312,6 +324,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

/**
* Set Listener of this fragment.
*
* @param listener
*/
public void setListener(Listener listener) {
mListener = listener;
}

private void advancedSettings() {

Expand Down Expand Up @@ -413,30 +433,44 @@ private void reverseTrip() {
.setFrom(mFromAddress)
.setTo(mToAddress);

mFromAddressTextArea.setText(mFromAddress.toString());
mToAddressTextArea.setText(mToAddress.toString());
setAddressText(mFromAddressTextArea, mFromAddress);
setAddressText(mToAddressTextArea, mToAddress);

((TripPlanActivity) getActivity()).route();
if (mBuilder.ready()) {
mListener.onTripRequestReady();
}
}

private void setAddressText(TextView tv, CustomAddress address) {
if (address != null && address.getAddressLine(0) != null) {
tv.setText(address.toString());
}
else {
tv.setText(null);
}
}

private void makeNoLocationToast() {
Toast.makeText(getContext(), getString(R.string.tripplanner_error_no_location), Toast.LENGTH_SHORT).show();
}

private CustomAddress makeAddressFromLocation() {
Locale locale = Locale.getDefault();

CustomAddress address = CustomAddress.getEmptyAddress();

Location loc = Application.getLastKnownLocation(getContext(), mGoogleApiClient);
if (loc == null) {
if (getContext() != null) {
Toast.makeText(getContext(), getString(R.string.main_location_unavailable), Toast.LENGTH_SHORT).show();
}
return null;
}
CustomAddress address = new CustomAddress(locale);
else {
address.setLatitude(loc.getLatitude());
address.setLongitude(loc.getLongitude());
}

address.setAddressLine(0, getString(R.string.tripplanner_current_location));
address.setLatitude(loc.getLatitude());
address.setLongitude(loc.getLongitude());

return address;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
android:layout_weight="1"
android:text="@string/tripplanner_current_location"
android:id="@+id/fromAddressTextArea"
android:hint="@string/trip_plan_location_hint"
android:textColor="@color/body_text_1"
android:linksClickable="true"
android:singleLine="true"
Expand Down

0 comments on commit 1c43d04

Please sign in to comment.