Skip to content

Commit

Permalink
Merge branch 'main' into fix-OneBusAway#851
Browse files Browse the repository at this point in the history
# Conflicts:
#	onebusaway-android/src/main/res/values/strings.xml
  • Loading branch information
viditpawar0 committed Dec 26, 2024
2 parents 7e182b8 + a0e081c commit fe8f430
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 292 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Did you just set up your own [OneBusAway](https://github.com/OneBusAway/onebusaw

## Permissions

In order to support certain features in OneBusAway, we need to request various permissions to access information on your device. See an explanation of why each permission is needed [here](PERMISSIONS.md).
In order to support certain features in OneBusAway, we need to request various permissions to access information on your device. See an explanation of why each permission is needed [here](PRIVACY.md).

## Troubleshooting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
package org.onebusaway.android.io.test;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onebusaway.android.io.elements.ObaRegion;
import org.onebusaway.android.mock.MockRegion;
import org.onebusaway.android.report.connection.ServiceListTask;
import org.onebusaway.android.report.constants.ReportConstants;
import org.onebusaway.android.report.ui.util.ServiceUtils;
import org.onebusaway.android.util.LocationUtils;

import android.location.Location;
import android.text.TextUtils;
Expand All @@ -45,97 +41,102 @@
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

/**
* Tests to evaluate interactions with Open311 system for regions that use Open311.
*
* NOTE: These tests make actual HTTP requests to live Open311 servers (SeeClickFix), because we
* make some assumptions required for text heuristic matching to identify transit-related services.
* If there are changes in the live server (e.g., new services added than aren't transit-related
* but include some of the text we're using to identify transit services), these could break our
* assumptions, and we want to know about that.
/*
TODO: Fix me, this test is disabled as of December 25, 2024, due to undefined behavior. Please refer to the associated ticket for more information.
https://github.com/OneBusAway/onebusaway-android/issues/1289.
*/
@RunWith(AndroidJUnit4.class)
public class ReportProblemOpen311Test {

// Mock region to use in tests
ObaRegion mTampaRegion;

@Before
public void before() {
mTampaRegion = MockRegion.getTampa(getTargetContext());

// Clear all open311 endpoints
Open311Manager.clearOpen311();

assertNotNull(mTampaRegion.getOpen311Servers());

// Read the open311 preferences from the region and set
if (mTampaRegion.getOpen311Servers() != null) {
for (ObaRegion.Open311Server open311Server : mTampaRegion.getOpen311Servers()) {
String jurisdictionId = open311Server.getJuridisctionId();

Open311Option option = new Open311Option(open311Server.getBaseUrl(),
open311Server.getApiKey(),
TextUtils.isEmpty(jurisdictionId) ? null : jurisdictionId);
Open311Manager.initOpen311WithOption(option);
}
}
}

/**
* Tests locations in Hillsborough County for services at that location. There should be at
* least
* ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD services marked as transit services, as HART
* is the primary SeeClickFix account holder and therefore all services are transit-related.
* As of Dec. 2, 2016 services are heuristically matched based on text in the service name
* (SeeClickFix does not support the group or keyword Open311 elements for explicit matching).
*/
@Test
public void testHillsboroughCounty() {
List<Location> locations = new ArrayList<>();

// USF area
locations.add(LocationUtils.makeLocation(28.0612088, -82.415747));
// Downtown
locations.add(LocationUtils.makeLocation(27.9577463, -82.4559472));
// Brandon
locations.add(LocationUtils.makeLocation(27.9520925, -82.3039963));

List<Service> serviceList = _testOpen311AtLocation(locations);

// Make sure we get a valid response here - a failure could be intermittent network issues
assertNotNull(serviceList);

// Mark the services that are transit-related
boolean mIsAllTransitHeuristicMatch = ServiceUtils
.markTransitServices(getTargetContext(), serviceList);
assertTrue(mIsAllTransitHeuristicMatch);

int countGroupTransit = 0;
int countDynamicStop = 0;
int countDynamicTrip = 0;

for (Service s : serviceList) {
if (s.getGroup().equals(ReportConstants.ISSUE_GROUP_TRANSIT)) {
countGroupTransit++;
}
if (s.getType().equals(ReportConstants.DYNAMIC_TRANSIT_SERVICE_STOP)) {
countDynamicStop++;
}
if (s.getType().equals(ReportConstants.DYNAMIC_TRANSIT_SERVICE_TRIP)) {
countDynamicTrip++;
}
}

// We should be over the threshold of assuming that all services (request types) are transit-related
assertTrue(countGroupTransit >= ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD);
///**
// * Tests to evaluate interactions with Open311 system for regions that use Open311.
// *
// * NOTE: These tests make actual HTTP requests to live Open311 servers (SeeClickFix), because we
// * make some assumptions required for text heuristic matching to identify transit-related services.
// * If there are changes in the live server (e.g., new services added than aren't transit-related
// * but include some of the text we're using to identify transit services), these could break our
// * assumptions, and we want to know about that.
// */
//@RunWith(AndroidJUnit4.class)
//public class ReportProblemOpen311Test {

// Everything not arrival times related should be marked as dynamic stop
assertTrue(countDynamicStop >= ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD);
// Mock region to use in tests
// ObaRegion mTampaRegion;
//
// @Before
// public void before() {
// mTampaRegion = MockRegion.getTampa(getTargetContext());
//
// // Clear all open311 endpoints
// Open311Manager.clearOpen311();
//
// assertNotNull(mTampaRegion.getOpen311Servers());
//
// // Read the open311 preferences from the region and set
// if (mTampaRegion.getOpen311Servers() != null) {
// for (ObaRegion.Open311Server open311Server : mTampaRegion.getOpen311Servers()) {
// String jurisdictionId = open311Server.getJuridisctionId();
//
// Open311Option option = new Open311Option(open311Server.getBaseUrl(),
// open311Server.getApiKey(),
// TextUtils.isEmpty(jurisdictionId) ? null : jurisdictionId);
// Open311Manager.initOpen311WithOption(option);
// }
// }
// }

// There should only be one arrival times (trip) request type, because we treat that differently (show arrivals to pick from)
assertTrue(countDynamicTrip == 1);
}
// /**
// * Tests locations in Hillsborough County for services at that location. There should be at
// * least
// * ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD services marked as transit services, as HART
// * is the primary SeeClickFix account holder and therefore all services are transit-related.
// * As of Dec. 2, 2016 services are heuristically matched based on text in the service name
// * (SeeClickFix does not support the group or keyword Open311 elements for explicit matching).
// */
// @Test
// public void testHillsboroughCounty() {
// List<Location> locations = new ArrayList<>();
//
// // USF area
// locations.add(LocationUtils.makeLocation(28.0612088, -82.415747));
// // Downtown
// locations.add(LocationUtils.makeLocation(27.9577463, -82.4559472));
// // Brandon
// locations.add(LocationUtils.makeLocation(27.9520925, -82.3039963));
//
// List<Service> serviceList = _testOpen311AtLocation(locations);
//
// // Make sure we get a valid response here - a failure could be intermittent network issues
// assertNotNull(serviceList);
//
// // Mark the services that are transit-related
// boolean mIsAllTransitHeuristicMatch = ServiceUtils
// .markTransitServices(getTargetContext(), serviceList);
// assertTrue(mIsAllTransitHeuristicMatch);
//
// int countGroupTransit = 0;
// int countDynamicStop = 0;
// int countDynamicTrip = 0;
//
// for (Service s : serviceList) {
// if (s.getGroup().equals(ReportConstants.ISSUE_GROUP_TRANSIT)) {
// countGroupTransit++;
// }
// if (s.getType().equals(ReportConstants.DYNAMIC_TRANSIT_SERVICE_STOP)) {
// countDynamicStop++;
// }
// if (s.getType().equals(ReportConstants.DYNAMIC_TRANSIT_SERVICE_TRIP)) {
// countDynamicTrip++;
// }
// }
//
// // We should be over the threshold of assuming that all services (request types) are transit-related
// assertTrue(countGroupTransit >= ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD);
//
// // Everything not arrival times related should be marked as dynamic stop
// assertTrue(countDynamicStop >= ReportConstants.NUM_TRANSIT_SERVICES_THRESHOLD);
//
// // There should only be one arrival times (trip) request type, because we treat that differently (show arrivals to pick from)
// assertTrue(countDynamicTrip == 1);
// }

// TODO: Re-enable this test. I have disabled this as of December 16, 2023 because the behavior
// that worked in December 2016 is no longer functioning as expected. Figure out if this can
Expand Down Expand Up @@ -198,38 +199,38 @@ public void testHillsboroughCounty() {
// assertTrue(countDynamicTrip == 1);
// }

private List<Service> _testOpen311AtLocation(List<Location> locations) {
for (Location l : locations) {
ServiceListRequest slr = new ServiceListRequest(l.getLatitude(), l.getLongitude());
List<Open311> open311List = Open311Manager.getAllOpen311();
ServiceListTask.Callback callback = new ServiceListTask.Callback() {
@Override
public void onServicesTaskCompleted(ServiceListResponse services, Open311 open311) {
}
};

ServiceListTask serviceListTask = new ServiceListTask(slr, open311List, callback);

try {
// Execute the AsyncTask synchronously
ServiceListResponse services = serviceListTask.execute().get();
List<Service> serviceList = new ArrayList<>();

// Add services to list if service response is successful
if (services != null && services.isSuccess() &&
Open311Manager.isAreaManagedByOpen311(services.getServiceList())) {
for (Service s : services.getServiceList()) {
if (s.getService_name() != null && s.getService_code() != null) {
serviceList.add(s);
}
}
}
return serviceList;
} catch (InterruptedException | ExecutionException e) {
// Print any network errors
e.printStackTrace();
}
}
return null;
}
}
//// private List<Service> _testOpen311AtLocation(List<Location> locations) {
//// for (Location l : locations) {
//// ServiceListRequest slr = new ServiceListRequest(l.getLatitude(), l.getLongitude());
//// List<Open311> open311List = Open311Manager.getAllOpen311();
//// ServiceListTask.Callback callback = new ServiceListTask.Callback() {
//// @Override
//// public void onServicesTaskCompleted(ServiceListResponse services, Open311 open311) {
//// }
//// };
////
//// ServiceListTask serviceListTask = new ServiceListTask(slr, open311List, callback);
////
//// try {
//// // Execute the AsyncTask synchronously
//// ServiceListResponse services = serviceListTask.execute().get();
//// List<Service> serviceList = new ArrayList<>();
////
//// // Add services to list if service response is successful
//// if (services != null && services.isSuccess() &&
//// Open311Manager.isAreaManagedByOpen311(services.getServiceList())) {
//// for (Service s : services.getServiceList()) {
//// if (s.getService_name() != null && s.getService_code() != null) {
//// serviceList.add(s);
//// }
//// }
//// }
//// return serviceList;
//// } catch (InterruptedException | ExecutionException e) {
//// // Print any network errors
//// e.printStackTrace();
//// }
//// }
//// return null;
//// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@

public class TripPlanFragment extends Fragment {

private String fromAddress;
private String toAddress;

/**
* Allows calling activity to register to know when to send request.
*/
Expand Down Expand Up @@ -164,18 +167,55 @@ public Intent parseResult(int i, @Nullable Intent addressIntent) {
if (addressIntent == null) {
return;
}

Uri addressUri = addressIntent.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};
if (addressUri == null) {
return;
}

String[] projection = {ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};

try (Cursor cursor = getContext().getContentResolver().query(addressUri, projection, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int addressIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
String address = cursor.getString(addressIndex).replace("\n", ", ");
TextView addressInput = getActivity().findViewById(addressIntent.getIntExtra(ADDRESS_INPUT_ID_KEY, -1));
addressInput.post(() -> addressInput.setText(address));
if (cursor == null || !cursor.moveToFirst()) {
return;
}

String address = extractAddress(cursor);
int addressInputId = addressIntent.getIntExtra(ADDRESS_INPUT_ID_KEY, -1);
if (addressInputId == -1) {
return;
}

updateAddressInput(address, addressInputId);
updateAddressData(address, addressInputId);
}
};

private String extractAddress(Cursor cursor) {
int addressIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
return cursor.getString(addressIndex).replace("\n", ", ");
}

private void updateAddressInput(String address, int addressInputId) {
TextView addressInput = getActivity().findViewById(addressInputId);
addressInput.post(() -> addressInput.setText(address));
addressInput.requestFocus();
}

private void updateAddressData(String address, int addressInputId) {
CustomAddress customAddress = CustomAddress.getEmptyAddress();
customAddress.setAddressLine(0, address);

if (addressInputId == mFromAddressTextArea.getId()) {
mFromAddress = customAddress;
mBuilder.setFrom(mFromAddress);
} else if (addressInputId == mToAddressTextArea.getId()) {
mToAddress = customAddress;
mBuilder.setTo(mToAddress);
}
}


private final ActivityResultLauncher<TextView> mSelectAddressFromContactLauncher = registerForActivityResult(
selectAddressFromContactContract,
addressIntentActivityResultCallback
Expand Down Expand Up @@ -360,6 +400,8 @@ private void checkRequestAndSubmit() {
if (mBuilder.ready() && mListener != null) {
mFromAddressTextArea.dismissDropDown();
mToAddressTextArea.dismissDropDown();
mFromAddressTextArea.clearFocus();
mToAddressTextArea.clearFocus();
UIUtils.closeKeyboard(getContext(), mFromAddressTextArea);
mListener.onTripRequestReady();
}
Expand Down Expand Up @@ -436,6 +478,22 @@ public void onNothingSelected(AdapterView<?> parent) {
if (BuildConfig.USE_PELIAS_GEOCODING) {
showTutorial(ShowcaseViewUtils.TUTORIAL_TRIP_PLAN_GEOCODER, (AppCompatActivity) getActivity(), null, true);
}

if (fromAddress != null) {
mFromAddressTextArea.setText(fromAddress);
}
if (toAddress != null) {
mToAddressTextArea.setText(toAddress);
}
}



@Override
public void onPause() {
super.onPause();
fromAddress = mFromAddressTextArea.getText().toString();
toAddress = mToAddressTextArea.getText().toString();
}

@Override
Expand Down

This file was deleted.

Loading

0 comments on commit fe8f430

Please sign in to comment.