Android App for setting up devices

I started this last week sometime before the hackathon and finally got back on to it tonight. Most of my Android programming has been purely native C++, so setting up a UI is causing a bit of pain.

The first couple of screens are fine, actually the third screen is now, I click something and it loads up a new Intent. The first screen is just buttons, the second screens fetches devices that are not setup and displays those.

When a device that hasn’t been setup has been selected then the next screen pops up. I already know how to pass data from one activity to another so that’s not the problem. The issue at the moment is the UI layout. The first attempt threw all the widgets to the top of the display when it was run. Then I realised I needed to setup things up using layouts. So far so good, I’ve got all my inputs, some with buttons next to the text input field.

Most of my UI stuff has been laying stuff out and then resizing everything to the display and accounting for the aspect ratio. This is alien to me but I am getting used to it as I go along.

I know that on Android I can also use portrait and landscape modes, to which I haven’t yet sussed out yet, but I do know they use fragments. I’ll get to that soon enough.

The Android app is simple enough anyway, so there’s little need to fancy it up. It needs to do a few jobs only. And they are all using a basic UI and no graphics (yet).

There’s no time tonight to do the communication between the app and the server, but essentially it will assign the device to an area and a category and give it a name.

The following step from this will be assigning the playlist, which, erm, I do want to use thumbnails for.

EDIT:

Using the putExtra, I’ve sent the stuff I need to the new UI screen. I’ve done that before so it was easy this time.

Lesson learnt at my first Hackathon

At the Sprint24 Hackathon in Liverpool at Ziferblat I learned a lesson fast. My dietary needs and health come first over trying to do what everyone else is doing.

I’d already prepared myself by taking the necessary tablets with me in case of a minor hiccup, ie they had normal milk. But this wasn’t enough.

The team I was with at first had a great web interface designer and had no problems knocking up pages for the challenge we took. Two drinks of coffee and a smidgen of a dash of milk. One tablet to counter.

A few hours in and I was on my own. At that point I’d had two cans of fizzy ‘zero’ cans of pop. During the second can I took another tablet. I wasn’t feeling too good.

Whilst I was on my own and feeling rough on the inside, I starting to feel like I wasn’t getting anywhere, when one of the organisers came over. I explained my team had gone and that I had reservations as to whether they would be back, which they had told me would be tomorrow morning. This was about 4pm’ish. The organiser, I can’t remember his name but remember what he looked like, said he would hook me up with another team.

Very shortly after I was hooked up with another team and paired with the youngest of the team. A very smart lad he was and understood a hell of a lot. And a surprisingly enough, a very quick learner. We got on with investigating the challenge.

And then, pizza turned up. Which was my downfall. It was only one slice of pizza that took everything out of me.

Because of what the effects where to my body, and with everyone around me snacking continuously, I couldn’t speak out about what it was. Although the truth I did say, was I was zapped of energy and getting tired.

I also know that there’s alcoholic drinks I can’t have. One of them being lager. I must’ve had two before I realised for certain that there was no coming back from this. Two light beers would normally have no effect me. This was all adding up

All I did, was keep smiling the best I could. My brain had shut down on me and my normal mode of focus was shot too hell. Try as I did, I couldn’t get my body from feeling so rough and lethargic. I had to eventually tell the team I was calling it a day and going home. I’d rather tell them that rather than say I would be back in the morning and not turn up.

The day before, the missus had bought in some stuff that she would have prepared for me for this event, but I turned it down. These were all on my safe to eat and drink list and would’ve lasted more than two days.

Lesson: It’s not good for your health trying to fit in. Especially when you cannot eat or drink what’s to hand, when there’s a supermarket just down the road.

It’s 2am, I’m at home and I’ve not started to recover yet.

Good luck to all at Ziferblat Sprint24 Hackathon

Perseverance and a nice win

I’ve already got the media manager software completed, although it will get a huge face lift in the near future. I’ve also got a running server that at the moment does quite a lot of things. I’ve also got the player on the devices making solid connections. New devices show up in the file system and internal databases. All the servers responses are currently 100%.

Tonight, I got my android app communicating with the server. After a silly mistake, the app now fetches new devices from the server and displays them ready for the next step. All connections are completely over the internet and via TCP. Eventually, once all test have been completed, all the security will be put into place. I’m not even thinking about security just yet because the later I leave it before the final product, the more secure it will be.

Actually, that very last sentence has just got me thinking about security. I seriously need to start securing this right now. Tomorrow I will implement the initial security layer (which is configurable).

Still no code snippets, sorry.

And still a fair bit of work to get done.

10 PRINT "HELLO WORLD"
20 GO TO 10

Personal project update Wed 20/09/17

The player software I am writing which had a big refactor last night now sends its unique ID to the server. The server also now stores this without a playlist which can be picked up by the mobile application which I started last week, but tomorrow I will work on.

At the moment, the server does actually look through the internal database and responds to the client, which is working so far. At the moment it just responds with a zero playlist and closes the connection.

I was too busy to be adding the encryption which can actually wait for now.

The next step is to get the Android software to be able to create temporary playlists, pick up empty devices, duplicate other devices playlists and set a new device up. Just this bit will take a little while because the Android API is quite huge and I’m used to using Qt, not Android Studio.

What I ought to be doing, which is what I do at work, is to keep a progress diary. I’ll make a start on that now actually. This I can update every time I work on my own projects.

I’m happy with the progress so far because even though I get a maximum of 2 hours after work and maybe a little bit more at the weekends, this particular project is taking shape finally.

There will be one main PC piece of software that will control everything as well as a few Android apps that do their bits. The devices player and the server are the easy bits. Although through time, I’ll have to look at making this scalable.

Project update (no code snippets tonight)

My personal project has taken a huge face lift. The original player code has been stripped and split into two classes, the media client layer and the player itself.

The media client is the heart of the player which handles the communication with the server and supplies the player with its playlist. It’s still not compete by far, but closer to what I need it to do.

The player at the moment does zip all. The intention is to have it playing a media file and then check with the media client of any updates which will adjust it’s playlist. All previous tests of the media player have been running great.

The android app is still waiting on the above media client and its communication with the server so that I can move that one forward.

Basically tonight has been refactoring a lot of code. With a lot of testing in between. So far so good, but I’ve run out of time for tonight. Two hours isn’t enough.

I’ll need some time to do some Blender work soon also.

Devices unique identifier

Now I could have just got the MAC address and used that to communicate with the server, but I wanted to take it to the extremes. Mainly because I wanted to obscure the communication with the previous methods of encryption so the server can identify which device is making a request.

For this I create a string using the devices host name and all the network interfaces connected to it with their MAC addresses. Although I might change this in the near future, at the moment, this gives a good length string for the server to identify the device.

Below is the code used to create this identity string.

    public String host;
    
    private void testMACAddress() {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            
            host = ip.getHostName();

            Enumeration&ltNetworkInterface&gt networks = NetworkInterface.getNetworkInterfaces();
            while (networks.hasMoreElements()) {
                NetworkInterface network = networks.nextElement();
                byte[] mac = network.getHardwareAddress();

                if (mac != null) {
                    StringBuilder sb = new StringBuilder();
                    
                    for (int i = 0; i &lt mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i &lt mac.length - 1) ? "-" : ""));
                    }
                    
                    host += " " + network.getDisplayName() + " " +  sb.toString();
                }
            }
        } catch (UnknownHostException | SocketException e) {
            System.out.println(e.getMessage());
        }
    }

As mentioned above, this identity string will then be encrypted and sent to the server. Further communication will commence if the server accepts it.

Simplex Noise in Java

Well, I finally got round to testing out the noise algorithms again. And this time in Java.

I grabbed OpenSimplexNoise class and had to add my own functions to it so that I could use ‘octaves’ and ‘persistence’ values to add roughness to it. The extra functions which handle 2D and 3D noise are:

    public double OctavePerlin2(double x, double y, int octaves, double persistence) {
        double total = 0;
        double frequency = 1;
        double amplitude = 1;
        double maxValue = 0;  // Used for normalizing result to 0.0 - 1.0
        for(int i=0;i<octaves;i++) {
            total += eval(x * frequency, y * frequency) * amplitude;

            maxValue += amplitude;

            amplitude *= persistence;
            frequency *= 2;
        }

        return total/maxValue;
    }
    
    public double OctavePerlin3(double x, double y, double z, int octaves, double persistence) {
        double total = 0;
        double frequency = 1;
        double amplitude = 1;
        double maxValue = 0;  // Used for normalizing result to 0.0 - 1.0
        for(int i=0;i<octaves;i++) {
            total += eval(x * frequency, y * frequency, z * frequency) * amplitude;

            maxValue += amplitude;

            amplitude *= persistence;
            frequency *= 2;
        }

        return total/maxValue;
    }

Then I added a UI using Swing and got it all working including adding a water level.

[ADDED] And a better image:

All I need now is to break up the calculations into blocks and run each block on its own thread when I come to using this again.

The class which creates the bitmap:

public class BufferedImageNoise {
    
    int w, h;
    long seed;
    double scale;
    
    int off_x, off_y;
    
    int octaves;
    double persistence;
    
    double water;
    
    OpenSimplexNoise noise;
    
    public BufferedImageNoise(int width, int height, long seed, double scale, int octaves, double persistence, double water) {
        w = width; h = height;
        this.seed = seed;
        this.scale = scale;
        this.octaves = octaves;
        this.persistence = persistence;
        this.water = water;
        
        off_x = off_y = 0;
        
        noise = new OpenSimplexNoise(seed);
    }
    
    public BufferedImage getImage() {
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y &lt h; y++)
        {
            for (int x = 0; x &lt w; x++)
            {
                double value = noise.OctavePerlin2(x / scale, y / scale, octaves, persistence) + 1.3;
                
                int rgb = value &gt water ? 0x010101 * (int)((value) * 127.5) : 0x000088;
                image.setRGB(x, y, rgb);
            }
        }
        return image;
    }
}

TCP Encryption

Tonight I grabbed an old class that uses a pseudo random number generator and implemented it into the TCP client server encryption test. Both the client and server create a new Object of the class and both set the same seed. Both the client and server will grab the next byte and add the next random number to it on a read, on a write, it will subtract the next random byte.

This worked as expected. The following code is an example of a complex random number generator.

public class WLPRNG {
    
    long seed;
    
    public WLPRNG(long seed) { this.seed = seed; }
    
    public int nextInt() {
        long result = seed + 0x123defca;
        result = Long.rotateLeft(result, 19);
        result += 0xbead6789;
        result *= 0x1234567c;
        
        int temp = (int)result;
        
        result ^= 0x5ecdab73;
        result = Long.rotateLeft(result, 48);
        
        if (temp % 4 == 0) result *= 0x87650027;
        
        result += 13;
        seed = result;
        
        return (int)result;
    }

    public byte nextByte() {
        return (byte)nextInt();
    }
}

The next step for me to try out is to use and encrypted message from the client which sends the MAC address of the client using encryption, and then both the client and server will communicate using the MAC address as a key for another pseudo random number generator.

Over time, the server can update the encryption methods used by the clients.

Cryptography Secure TCP Sockets in Java

Well, I finally got round to testing out a basic encryption over TCP sockets.

First off, the main() entry looks very familiar and is easy to understand. I simply create the listening server (doesn’t spawn thread for simplicity), with the ability to safely shut it down.

Then I use the DataInputStream and DataOutputStream to send data back and forth. In this test case I sent a string which both the client and server will print to the console.

Once that was working, I extended the OutputStream and InputStream and override the read() and write() methods (shown in the code examples below). Notice that I manipulate the byte in two different ways, one by adding and subtracting and the other by XOR’ing (commented out but works). If these manipulations match up whilst data is being sent back and forth then there’s no errors.

Main class

The main class and entry point to the test which spawns the server and then runs a couple of tests:

package com.wlgfx.cryptotest;

public class CryptoServer {

    public static void main(String[] args) {
        WLCryptoServer server_run = new WLCryptoServer();
        Thread server = new Thread(server_run);
        server.start();
        
        System.out.println("Server thread started");
        
        new Thread(new WLCryptoClient("Hello world!")).start();
        
        try { Thread.sleep(500); } catch (InterruptedException ex) {}
        
        new Thread(new WLCryptoClient("Jump around and stomp your feet!")).start();
        
        try { Thread.sleep(500); } catch (InterruptedException ex) {}
        
        server_run.quitServer();
    }
}

Server class

The server code which take the input and output streams and creates new subclasses of the encryption layer classes and then creates the usual DataInputStream and DataOutputStream for the rest of the normal IO. This matched in the following code for the client.

package com.wlgfx.cryptotest;

import com.wlgfx.cryptotest.WLC.WLInputStream;
import com.wlgfx.cryptotest.WLC.WLOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class WLCryptoServer implements Runnable {
    
    private final int port = 1999;
    private boolean quit = false;

    @Override
    public void run() {
        try {
            ServerSocket socket =  new ServerSocket(port);
            socket.setSoTimeout(1000);
            
            while (!quit) {
                Socket client = socket.accept();
                
                WLOutputStream wlout = new WLOutputStream(client.getOutputStream());
                DataOutputStream out = new DataOutputStream(wlout);
            
                WLInputStream wlin = new WLInputStream(client.getInputStream());
                DataInputStream in = new DataInputStream(wlin);
                
                String text = in.readUTF();
                System.out.println("SERVER: " + text);
                out.writeUTF(text);
                
                client.close();
            }
        } catch (IOException ex) {
            System.out.println("SERVER: " + ex.getLocalizedMessage());
        }
    }
    
    public void quitServer() {
        quit = true;
    }
}

Client class

The client code is just the same as the server class code.

package com.wlgfx.cryptotest;

import com.wlgfx.cryptotest.WLC.WLInputStream;
import com.wlgfx.cryptotest.WLC.WLOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class WLCryptoClient implements Runnable {
    
    private String text;
    private Socket socket;
    
    private final int port = 1999;
    
    public WLCryptoClient(String test) {
        text = test;
    }
    
    @Override
    public void run() {
        try {
            socket = new Socket("localhost", port);
            
            WLOutputStream wlout = new WLOutputStream(socket.getOutputStream());
            DataOutputStream out = new DataOutputStream(wlout);
            
            WLInputStream wlin = new WLInputStream(socket.getInputStream());
            DataInputStream in = new DataInputStream(wlin);
            
            out.writeUTF(text);
            text = in.readUTF();
            System.out.println("CLIENT: " + text);
            
            socket.close();
        } catch (IOException ex) {
            System.out.println("CLIENT: " + ex.getLocalizedMessage());
        }
    }
    
}

The encryption class

This is where you can see exactly how easy it is to manipulate the data being sent back and forth.  Here I am showing you two methods, XOR and add and subtract. It’s very simple, but for heavier encryption you could look further at CSPRNG, or Crypto Secure Pseudo Random Number Generator. PRNG’s are ten a penny, from simple ones to hefty things. Take your pick. The Crypto and Secure layers are the ones you really need for truly undecipherable data communication.

package com.wlgfx.cryptotest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class WLC {

    // NB: short inner classes are static to avoid errors
    
    public static class WLInputStream extends InputStream {

        private InputStream in;

        public WLInputStream(InputStream input) {
            in = input;
        }

        @Override
        public int read() throws IOException {
            byte a = (byte) in.read();
            a--;
            return (int) a;

            //return in.read() ^ 255; // WORKS TOO
        }
    }

    public static class WLOutputStream extends OutputStream {

        private OutputStream out;

        public WLOutputStream(OutputStream output) {
            out = output;
        }

        @Override
        public void write(int b) throws IOException {
            byte a = (byte) b;
            a++;
            out.write((int) a);

            //out.write(b ^ 255); // WORKS TOO
        }
    }
}
Console output:
run:
Server thread started
SERVER: Hello world!
CLIENT: Hello world!
SERVER: Jump around and stomp your feet!
CLIENT: Jump around and stomp your feet!
SERVER: Accept timed out
BUILD SUCCESSFUL (total time: 1 second)

Until next time

😛

Tonight I’ve sussed out the thread pooling in Java. I wanted a way of being able to queue up threads to be run, but I also wanted to allow the execution of so many threads at a time in this pool. I’ve heard about these thread pools but as of yet, not used them. I need something like this for my own project for both the server and the client.

package threadpooltest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadPoolTest {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
        
        for (int i = 0; i <= 8; i++)
        {
            WorkerTest task = new WorkerTest("Task " + i);
            System.out.println("A new task has been added : " + task.getName());
            executor.execute(task);
        }
        
        System.out.println("Main thread sleeping");
        try { Thread.sleep(8000); } 
        catch (InterruptedException ex) {}
        System.out.println("Finished sleeping");
        
        for (int i = 0; i <= 10; i++)
        {
            WorkerTest task = new WorkerTest("Task " + (i + 5));
            System.out.println("A new task has been added : " + task.getName());
            executor.execute(task);
        }
        
        // THIS DOESN'T WORK
        //System.out.println("WAITING FOR TASKS TO FINISH");
        //try { executor.awaitTermination(1, TimeUnit.DAYS); } 
        //catch (InterruptedException ex) {}
        //System.out.println("FINISHED");
        
        executor.shutdown();
    }
    
}

No proper code snippet plugin yet! Grrr!

Anyway, the above code creates the thread pool which will run a max of 5 threads and queue the rest up. Just what I wanted.

package threadpooltest;

import java.util.concurrent.TimeUnit;

public class WorkerTest implements Runnable {

    private String name;
    
    public WorkerTest(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    @Override
    public void run() {
        try
        {
            Long duration = (long) (Math.random() * 5);
            System.out.println("Start Task: " + name);
            TimeUnit.SECONDS.sleep(duration);
            System.out.println("Finished Task: " + name);
        }
        catch (InterruptedException e) {}
    }
}

The above is the example thread I was testing this with.

All I need now is to do the same with C++, but I’ve already found a header only project on github that does just that.

More to come…

EDIT:

I’ve also noted that the above works on Android too.

At the moment, I’m not holding my breath to see it this works with GWT.