Announcing – “Announce” for Android

Announce is an application for Android 2.0 and above.  It intercepts incoming calls and and reads the name of the caller aloud.  You can also record custom voice tags to use instead of the text to speech engine.

Project Page: https://peterfranza.com/projects/announce-for-android/

Price: Free (That’s a good value)**
**

Enjoy.

BitArrayInputStream

Sometime you just have to read the bits one by one at least now you don’t need to write your own class to do it. This class works for ‘Little Endian’ or ‘Big Endian’.

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

public class BitArrayInputStream {

	private final BitDirection direction;
	private final InputStream inputStream;
	private int currentByte;
	private int currentPosition = -1;

	public BitArrayInputStream(InputStream inputStream) {
		this(BitDirection.HIGHLOW, inputStream);
	}

	public BitArrayInputStream(BitDirection direction,
			InputStream inputStream) {
		this.inputStream = inputStream;
		this.direction = direction;
	}

	public int readBit() throws IOException {
		if(currentPosition == -1) {
			currentByte = inputStream.read();
			if(currentByte == -1) {
				return -1;
			}
			currentPosition = 0;
		}

		int c = -1;

		if(direction.equals(BitDirection.HIGHLOW)) {
			c = (currentByte << currentPosition & 0x80) >> 7;
		} else {
			c = currentByte >> currentPosition & 0x1;
		}

		currentPosition += 1;
		if(currentPosition >= 8) {
			currentPosition = -1;
		}

		return c;
	}

	public static enum BitDirection {HIGHLOW, LOWHIGH}

}

And here is a unit test for it:

public class BitArrayInputStreamTest extends TestCase {

	public void testInit() throws Exception {
		assertNotNull(new BitArrayInputStream(null));
	}

	public void testReadBits() throws Exception {
		BitArrayInputStream i = new BitArrayInputStream(
				new ByteArrayInputStream(new byte[] {(byte) 0xA3}));

		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());

		assertEquals(0, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());


	}

	public void testReadBitsLowHigh() throws Exception {
		BitArrayInputStream i = new BitArrayInputStream(BitDirection.LOWHIGH,
				new ByteArrayInputStream(new byte[] {(byte) 0xA3}));

		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(0, i.readBit());

		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());


	}

	public void testReadBitsEnd() throws Exception {
		BitArrayInputStream i = new BitArrayInputStream(
				new ByteArrayInputStream(new byte[] {(byte) 0xA3}));

		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());

		assertEquals(0, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());

		assertEquals(-1, i.readBit());


	}

	public void testReadBitsMultiByte() throws Exception {
		BitArrayInputStream i = new BitArrayInputStream(
				new ByteArrayInputStream(
                                  new byte[] {(byte) 0xA3, (byte) 0xB6}));

		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());

		assertEquals(0, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());

		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());

		assertEquals(0, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(1, i.readBit());
		assertEquals(0, i.readBit());

	}

}

Look Mom, No Typing

Always wanted to leave a comment about something you’ve read on my site, but couldn’t be bothered to type a response. Well now you can using my new Google Voice Call widget. Now you don’t have to worry about carpel tunnel or any of that fancy spelling stuff. Really all you need is a mouth and a phone.



Ignore Compiler Warnings

Let me be upfront, I will be the first person to tell you that you should treat a warning as if it were an error. The compiler is trying to tell you something, it wants to help you. The very smart people who are designing our compiler aren’t identifying code patterns and exposing them as warnings for their health, so come on people … FIX YOUR WARNINGS!

Ok, now that I got that out of the way, we come to the special case. Third party source code generation tools, I actually love generated source, don’t ask me why but I find it much easier to deal with than all the mucking around you could do in meta-code etc. Just give me a plain ‘ol class that I can use. The drawback to generated source is some of the generators are pretty rough when it comes to warnings. They might have unneeded imports, referenced untyped classes, who knows. We all can agree that it would be optimal if the generators would just produce warning free code, and if it is in your power join up with some of these open-source projects and actually fix them (you’d be my hero).

When that isn’t an option I’d just ignore them, I’m adding an ant task that can read in a fileset and append warning suppression annotations to the class headings. Just remember you have not fixed anything, but to be perfectly practical you may never fix these things.

Updated 4/13/1013
Download Jar File: suppresswarnings-0.6.jar

Usage:

<taskdef resource=”suppresswarnings.properties” classpath=”${suppresswarnings.jarfile}”/>

<fileset dir=”${src.dir}”> <include name=”*\*/\*.java”/> </fileset>

Integer IP Addresses

I know that every couple of years I need the snippet of code that helps me convert the integer version of an IP address back and forth to a string. You would think that after all this time I’d be able to write it blind.

public static String convertIntegerToIp(long ip) {
     StringBuffer buf = new StringBuffer();
      buf.append(((ip >> 24 ) & 0xFF)).append(".")
          .append(((ip >> 16 ) & 0xFF)).append(".")
          .append(((ip >>  8 ) & 0xFF)).append(".")
          .append(( ip        & 0xFF));		
      return buf.toString();
}

 public static long convertStringToIntegerIp(String ip) {
     String[] parts = ip.split("\\.");
     return (Long.valueOf(parts[0]) << 24) +
              (Long.valueOf(parts[1]) << 16) +
              (Long.valueOf(parts[2]) << 8) +
              (Long.valueOf(parts[3]));
 }

In C/C++ you can use an unsigned int instead of a long but in java there are no unsigned types and while I know that you can still do this calculation using a signed integer just as well in java, when you print out the address for visual inspection you will get a negative number and it won’t match the c++ printout so I’ve chosen to use a long to store the values.

Enjoy.