Saturday 24 November 2018

TravelTimeCalculator: Setting Up dHelper on Android Unit Test for TDD

Now, I know how the database should be organised. For the implementation, I'll use Test-Driven Development that I described in an earlier blog post for TrafficControl.

The first step is to be able to initiate the database helper at all.

When trying to do that in a local unit test, I wasn't able to initiate the dbHelper class since I couldn't get the context for the app. The reason is probably that I need more resources such as a database that will require an Android environment.

Instead, I was able to use the context in an instrumented unit test. This will be much slower than running locally, since the test needs to be loaded into a real or simulated phone. After some dependency issues for the test APK package that I didn't see for the app, I was finally able to test a simple instrumented test.


In the test, I added a dbHelper that I didn't use. For the local unit test, that step caused a crash.

More information about Android testing can be found here.

The first Test-Driven Development test will be to add columns to the database and check if those columns exist.

Saturday 17 November 2018

TravelTimeCalculator: Defining a SQLite Database for Directions

In order to reduce the number of requests to Google Directions API for directions, my app will need to save and reuse old queries to an Android SQLite database.

A SQLite query for directions will handle the following information:
  • Origin Latitude 
  • Origin Longitude 
  • Destination Latitude
  • Destination Longitude
  • The mode of transportation (DRIVING, TRANSIT, WALKING, BICYCLE)
  • Travel Time in seconds
  • Travel Duration in meters
The app will search for coordinates to find existing queries. In order to reduce the possible coordinates to a feasible number of coordinates, I'll cast the coordinates to integers, after multiplying by 1000. 

This means that the coordinate (13.00213121, 55.432923) will be converted to (13002, 55433). The highest rounded error will be: 

After querying, the database will be populated with data for recently used directions.

Whenether the user makes a new query, the database will start by finding all results that has origin and destination coordinates that are matching the query. The swapped coordinates will also count as hits. The query (13012,55605, 13002, 55605) shall find the directions above.

I'll use the Android SQLite tutorial as inspiration for the database.


One entry to the database might be


13002, 55605, 13012, 55605, "DRIVING", 240, 2000.

Those values that aren't defined will have the default value "NULL".

Saturday 10 November 2018

TravelTimeCalculator: Sending Directions Request and Analysing the response

First, I need to request the INTERNET service in Android:
android.permission.INTERNET

I'll use a HTTP library called Volley. and also RequestQueue.

The request is based on JsonObjectRequest and when it gets the response, it will invoke a method that will interpret the response JSON object.



After the response is fetched, readDirections method will find the duration and distance in the JSON object.




The response from the method will be sent as an array of two integers.

Next step will be to design the database and to add and search for entries in the database.

Saturday 3 November 2018

TravelTimeCalculator: Hiding My API Key from Git

In the last blog post, I used a Google Directions API to get directions. In order to use more than one requests per day, an API key is needed.

API keys are used by applications to get access to several different online services.

Generally, it kind of cheap for Google Directions API: it will cost half a USD cent to query one api. Still, I'll need to reduce the requests for Google Directions and store the existing results to a local database. I'll elaborate more on that in the future.

When pushing my code to GIT, I need to make sure that no one will copy my key and freeload on my billing account. Luckily, this is easy to do.

As described by Varun Barad in his article at Medium.com, simply put the API keys in a file outside the GIT repo and point to that file using the Android gradle system.

For Python, it seems to be easier. Black Tech Diva has an instruction for that language.