Iterating over the lines of a file in Java

If you’re trying to write a standard command-line tool in Java you are going to experience pain.

In Python, if we want to do something with each line on input to your program, we can do something like:

import sys
for ln in sys.stdin:
    print(ln)  # (We have an extra newline but who's counting?)

In Java, after some fighting, the two closest alternatives I can find are:

import java.util.Scanner;
class IterateInputScanner {
    public static void main(String[] args) {
        for(String ln : (Iterable)(() -> new Scanner(System.in).useDelimiter("\n"))) {
            System.out.println(ln);
        }
    }
}

or, less fashionably, and even more horribly:

import java.io.BufferedReader;
import java.io.InputStreamReader;
class IterateInputReader {
    public static void main(String[] args) throws java.io.IOException {
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(System.in));
        String ln;
        while((ln = reader.readLine()) != null) {
            System.out.println(ln);
        }
    }
}

Note that if you are reading from a stream other than stdin you will probably want to wrap this in a try-with-resources to close the stream when you’ve finished.

Take your pick, and I hope it saves you some fighting.

Raspberry Pi Jam “Chaos Car!”

Raspberry Pi 1
+ battery pack
+ Bluetooth USB dongle
+ i-racer bluetooth car
+ Raspberry Pi camera
+ some Python code
+ loads of sellotape
=

Chaos car!

Here’s the code:

#!/usr/bin/env python2

import os
import random
import bluetooth
import sys
import time

car_name = "DaguCar"

def find_car_mac( car_name ):
    ret = None

    print "Scanning for a device called %s..." % car_name

    devices = bluetooth.discover_devices()

    for addr in devices:
        dev_name = bluetooth.lookup_name( addr )
        if dev_name == car_name:
            print "Car found!  (MAC=%s)" % addr
            time.sleep( 1 )
            return addr
        else:
            print "Skipping device named '%s'." % dev_name

    sys.stderr.write( "Couldn't find a device called %s!\n" % car_name )
    sys.exit( 1 )


#car_mac = find_car_mac( car_name )

print "Using hard-coded MAC address"
car_mac = "20:13:04:23:05:71"

print "Connecting to the car at %s..." % car_mac

sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect( ( car_mac, 1 ) )

class Car:
    def turn_right(self):
        print("looking right")
        sock.send( '\x6A' )
    def turn_left(self):
        print("looking left")
        sock.send( '\x5A' )
    def roll_forward(self):
        print("watch out in front coming forward")
        sock.send( '\x1A' )
    def roll_backward(self):
        print("beep beep going backward")
        sock.send( '\x2A' )
    def wait(self):
        print("waiting")
        sock.send( '\x00' )

car = Car()

instruction = ["turn right", "turn left", "roll forward", "roll backward", "wait"]

while True:
    ci = random.choice(instruction)
    if ci == "turn right":
        car.turn_right()
    elif ci == "turn left":
        car.turn_left()
    elif ci == "roll forward":
        car.roll_forward()
    elif ci == "roll backward":
        car.roll_backward()
    elif ci == "wait":
        car.wait()
    time.sleep(0.5)

Improving the code to avoid bumping into walls is left as an exercise for the reader.

Automated UI tests on Android

I recently fought the Android emulator a lot to get my UI tests to run automatically during the build of Rabbit Escape, so I thought I’d better write down what I did before I forget.

I already have tests that drive the Android UI (see e.g. SmokeTest.java – it’s not pretty, but it seems reliable, and it catches real problems) – this post is about how to run them automatically during our build.

Note that to run the Android emulator I’m fairly sure you need a windowing environment, so I don’t think this could be moved to a headless build server. If course, you could always fight some kind of framebuffer thing.

Here is the part of our Makefile that launches the tests:

android-smoke-tests:
	@echo ". Running Android smoke tests"
	./build-scripts/android-start-emulator "android-8" "3.2in QVGA (ADP2)"
	./build-scripts/android-test "free" "app-free-debug"
	./build-scripts/android-test "" "app-paid-debug"
	./build-scripts/android-stop-emulator

and here is ./build-scripts/android-start-emulator – it starts up and emulator, waits for it to be ready, and unlocks its screen.:

#!/bin/bash

set -x
set -u
set -e

# Args

TARGET="$1"   # E.g. "android-8"
DEVICE="$2"   # E.g. "3.2in QVGA (ADP2)"

# Setup

ADB="${HOME}/Android/Sdk/platform-tools/adb"
EMULATOR="${HOME}/Android/Sdk/tools/emulator"
ANDROID="${HOME}/Android/Sdk/tools/android"
NAME="rabbitescape-${TARGET}"
TMP="/data/local/tmp"

${ANDROID} create avd \
    --force \
    --name "${NAME}" \
    --target "${TARGET}" \
    --abi "armeabi" \
    --device "${DEVICE}"

# Start the emulator
${EMULATOR} -avd "${NAME}" &

# Wait for the device to boot and unlock it
${ADB} wait-for-device shell < ${TMP}/zero
getprop dev.bootcomplete > ${TMP}/bootcomplete
while cmp ${TMP}/zero ${TMP}/bootcomplete; do
{
    echo -n "."
    sleep 1
    getprop dev.bootcomplete > ${TMP}/bootcomplete
}; done
echo "Booted."
exit
ENDSCRIPT

echo "Waiting 30 secs for us to be really booted"
sleep 30

echo "Unlocking screen"
${ADB} shell "input keyevent 82"

Now here is android-test – it launches the JUnit test code on the running emulator::

#!/bin/bash

set -x
set -u
set -e

PKGSUFFIX="$1"   # E.g. "free"
APKNAME="$2"     # E.g. "app-free-debug"

APPID="net.artificialworlds.rabbitescape${PKGSUFFIX}"
TESTAPPID="net.artificialworlds.rabbitescape${PKGSUFFIX}.test"
APK="rabbit-escape-ui-android/app/build/outputs/apk/${APKNAME}.apk"
TESTAPK="rabbit-escape-ui-android/app/build/outputs/apk/${APKNAME}-androidTest.apk"
ADB="${HOME}/Android/Sdk/platform-tools/adb"
DIR="/data/local/tmp/${APPID}"
TESTDIR="/data/local/tmp/${TESTAPPID}"

function run_test()
{
    TMPFILE=$(mktemp)

    ${ADB} shell am instrument \
        -w \
        -r \
        -e class "$1" \
        "${TESTAPPID}/android.test.InstrumentationTestRunner" \
    | tee ${TMPFILE}

    egrep "OK (.* tests?)" ${TMPFILE}
}

${ADB} push "${APK}" "${DIR}"
${ADB} push "${TESTAPK}" "${TESTDIR}"

${ADB} shell pm install -r "${DIR}"
${ADB} shell pm install -r "${TESTDIR}"

run_test rabbitescape.ui.android.DialogsTest
run_test rabbitescape.ui.android.SmokeTest
run_test rabbitescape.ui.android.TestAndroidConfigUpgradeTo1

And here is android-stop-emulator – it shuts down the emulator:

#!/bin/bash

set -x
set -u

echo -e "auth $(cat ~/.emulator_console_auth_token)\nkill" \
    | telnet localhost 5554

echo "Emulator stopped."

Submitting a package to F-Droid

Here’s what I needed to get a dev environment for F-Droid up and running on Ubuntu 16.10, using F-Droid server version 0.7.0 (commit id 8147f9235), so that I could submit a package for inclusion in the F-Droid repository.

Doing this is apparently the best way to get your own package into the repository, since you can provide a direct merge request for the metadata about your package, making it easy for the maintainers.

References:

Setup

Before you start, manually install the Android SDK at ~/Android/Sdk/ – see Download Android Studio. I installed version 23.0.2, but you will probably have a later one and may need to adjust the version number below.

Note: If you’re only planning to contribute a package I’m fairly certain you don’t need to install the Android SDK at all – you can just use the build server by running ./makebuildserver as I outline below.

Also before you start, if you want to contribute to the server project you should fork the F-Droid server project by going to gitlab.com/fdroid/fdroidserver and clicking Fork. When you’ve done that, the git clone command below will need to change to clone your own fork via SSH, instead of the HTTPS one cloning the main repo that is shown below. Do the same for the F-Droid data project, which holds the information about the packages in F-Droid. It’s the data project where you will want to make changes if you are submitting a package.

Run these commands:

# Prerequisites
sudo apt-get install openjdk-8-jdk subversion git git-svn mercurial bzr virtualbox ruby ruby-dev vagrant python3 python3-paramiko python3-pil python3-pyasn1-modules python3-clint
vagrant plugin install vagrant-cachier
ln -s ~/Android/Sdk/build-tools/23.0.2/aapt ~/Android/Sdk/platform-tools/

# Get the code
cd ~/code
git clone https://gitlab.com/fdroid/fdroidserver.git
git clone https://gitlab.com/fdroid/fdroiddata.git
echo 'export PATH="~/code/fdroidserver:$PATH"' >> ~/.profile
source ~/.profile

# Config
cd fdroiddata
cp ../fdroidserver/examples/config.py ./
chmod 0600 config.py
echo 'sdk_path = "$HOME/Android/Sdk"' >> config.py

# Set up Vagrant build box
cd ../fdroidserver
cp ./examples/makebuildserver.config.py ./
./makebuildserver
# Now wait several hours for this to finish

# Build a package (the F-Droid client) just to check it works
cd ../fdroiddata
mkdir repo
fdroid update --create-key
fdroid readmeta  # Should give no output if it worked
fdroid build --server org.fdroid.fdroid

Make your own package

Below I’m using my own package, Rabbit Escape, as an example. Its Android code is inside rabbit-escape-ui-android/app, whereas many programs will just have it directly in a directory called “app”.

Rabbit Escape also builds non-Android-specific Java and other things during its build, so your package may be simpler.

cd ../fdroiddata
fdroid import --url https://github.com/andybalaam/rabbit-escape --subdir rabbit-escape-ui-android/app

Now edit the new file that was created – in my case it was called metadata/net.artificialworlds.rabbitescape.txt.

I set the following info:

Provides:net.artificialworlds.rabbitescape
Categories:Games
License:GPLv2+
Author Name:Andy Balaam and the Rabbit Escape developers
Author Email:rabbitescape@artificialworlds.net
Web Site:http://artificialworlds.net/rabbit-escape
Source Code:https://github.com/andybalaam/rabbit-escape
Issue Tracker:https://github.com/andybalaam/rabbit-escape/issues

Name:Rabbit Escape
Summary:Lemmings-like puzzle/action game
Description:
140 levels of puzzling action!

blah blah blah
.

Repo Type:git
Repo:https://github.com/andybalaam/rabbit-escape
Binaries:https://github.com/andybalaam/rabbit-escape/releases/download/v%v/rabbit-escape-%v.apk

Build:0.10.1,101
    commit=v0.10.1
    subdir=rabbit-escape-ui-android/app
    gradle=paid
    build=cd ../.. && make android-pre

Auto Update Mode:Version v%v
Update Check Mode:Tags v\d+\.\d+(\.\d+)?
Current Version:0.10.1
Current Version Code:101

For more info, see the F-Droid manual.

And then checked it all worked with:

cd ../fdroiddata
fdroid lint net.artificialworlds.rabbitescape
fdroid readmeta
fdroid checkupdates net.artificialworlds.rabbitescape
fdroid rewritemeta net.artificialworlds.rabbitescape

When I got the version stuff right the checkupdates command printed:

INFO: Processing net.artificialworlds.rabbitescape...
INFO: ...updating to version 0.10.1 (101)
INFO: Finished.

Then I made sure it built OK:

fdroid build --server -v -l net.artificialworlds.rabbitescape

Actually, it didn’t work, and I decided I had to request a new package (sox) be installed in the build machine environment (in the fdroidserver project). The relevant commit is here: 19e372026. Actually though, after discussion with the F-Droid devs we agreed I’d be better off not using sox during the build, so I didn’t need this.

Side note: if you do end up needing to modify the build environment for F-Droid, make sure you delete the fdroiddata/buildserver directory when you re-try your build. That one had me stuck for a few days, with the old environment being used no matter what caches I cleared and vagrant commands I ran.

And now I was ready to request my package be included in F-Droid by committing and pushing the changes I had made to the fdroiddata project to my forked repo, and clicking the Merge Request button in the gitlab UI. My merge request is here: gitlab.com/fdroid/fdroiddata/merge_requests/1965