Friday, 14 June 2013

Windows 7 - Attempting to Hibernate Results in Locking Instead

Overview

This post details how I fixed a problem with my laptop where attempting to hibernate the computer resulted in it locking instead.

This seems to be a reasonably uncommon problem; there are results on Google but there is no clear one-size-fits-all solution. This is largely due to there being multiple reasons this can occur. Whilst you may be more likely to see this problem if you dual boot Windows with another OS, I started seeing the issue after upgrading my laptop's hard drive.

The Problem

Every day, when finished with the laptop, I click the hibernate button, close the lid and place the laptop away in a drawer. Since upgrading my hard drive however, I've found that the next morning the laptop's battery is completely drained and prompts me like I've let the computer running. Tracking that down today, I discovered that when the hibernate button is pressed now, the screen goes blank for a few seconds before returning me to the lock screen. Still fully powered on, just locked.

Saturday, 4 May 2013

Z-Wave - Fixing the Inverted LED Issue on TKB On/Off Switch

On my TKB On/Off Socket, the blue LED turned on when the switch state was in the off position, and the blue LED turned off when the switch state was in the on position. This turns out to be configurable with the right tools. The following post describes how to configure to your preference.

This is probably the epitome of first world problems. I'm concerned that the socket, which I have the power to control over the internet and from my mobile, emits a blue light and I'd rather it didn't. I know, I know, get a life. However, the purpose of this particular socket for me was to have connected in the bedroom. This means this fairly bright LED would be on all night long, and actually would be on about 99% of the time. A problem worth fixing it seems.


Monday, 29 April 2013

Robolectric Error - Couldn't find value resource directory


Android development using Robolectric provides a powerful testing framework which runs on your desktop JVM instead of an Android device or emulator. This makes tests written using Robolectric run a lot faster than those written to run using Android Instrumentation.

However, when setting up Robolectric in IntelliJ, I recently received the following nastiness:

testExceptionThrownGivenNullHeadersInput(LocationHeaderParsingTest)  Time elapsed: 28.753 sec  <<< ERROR!
java.lang.RuntimeException: java.lang.RuntimeException: Couldn't find value resource directory: C:\Programming\Workspace\ .... \.\res\values
at com.xtremelabs.robolectric.res.ResourceLoader.getValueResourceDir(ResourceLoader.java:296)
at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:133)
at com.xtremelabs.robolectric.res.ResourceLoader.setLayoutQualifierSearchPath(ResourceLoader.java:599)
at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:367)
at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:311)
at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:278)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)

My particular setup is as follows:

  1. IntelliJ
  2. Maven multi-module project:
    1. Parent-pom
      1. Android Application
      2. Android Test Project to test the Android Application
  3. Unit tests using the following frameworks
    1. Mockito for mocking 
    2. Robolectric 
    3. Robotium for auotmated UI testing
The problem here is that when creating my Android Test Project, I didn't see the need to create the res/values directory (as my tests didn't require any resources from here). However, the Robolectric framework does not work properly without that directory being present.

Solution
  1. Create an empty directory under the project root called res (if doesn't already exist)
  2. Create an empty directory in res called values
Your project structure should therefore look something like this:

application-myapp-android-parent
     |
     |-> application-myapp-android
     |-> application-myapp-android-test
               |-> res
                     |->values

That should fix your Couldn't find value resource directory issue.

Thursday, 24 January 2013

Setup Guide / Walkthrough of Tenvis JPT3815W 2013 IP Camera

Tenvis JPT3815W 2013



The Camera

Tenvis are a Chinese manufacturer whose product line includes IP Cameras, such as the model JPT3815W 2013 featured in this setup guide. Available to purchase online, this serves the budget-end of the IP Camera market. In fact, this camera currently has a selling price of less than £40. Tenvis offer a Global User Experience Improvment Program, which offers a free camera in return for writing guides to help other users. This camera was received through such a program.

View on Amazon.

However, any opinions expressed about the camera (good and bad) are entirely my own.

I became a member of the improvement program when I reported a security vulnerability to the company. They responded by thanking me for my report, and informed me they would work to fix the vulnerability immediately.

Read on for the full setup guide and review.

Monday, 14 January 2013

Android Daydream Mode - Prevent Screen Burn

Daydream Mode - An Old Solution to an Old Problem

Daydream Mode not working for you?

After having problems with Android 4.2's new Daydream mode, I discovered the problem which was preventing the mode from ever starting. If that's why you're here, skip to the very bottom section entitled "Developers Beware - You Need More Sleep".

What is it all about anyway?

Got an Android 4.2 device? Have you tried the Daydream mode yet? It's basically a screensaver for your phone. It can display items like a clock, top stories from Currents or Flipboard, show a slideshow of your photo albums or even StumbleUpon for you; the last two seem to be wrought with potential dangers in my opinion, but hey, it's an option.

Is it a novelty? Partly. But there is a genuinely good reason for its existence; screen burn. You might have heard of this phenomenon in the world of TVs and monitors. Also known as ghosting. If an image is held on the screen for too long, without it moving or any other activity happening, that image can actually have lasting damage on the screen. The most common symptom of screen burn is the retention of some pixels or parts of an image long after that image should have disappeared, resulting in a ghostly silhouette of the pixels that were once there. Basically what happens to your pixels when they die and can't enter the afterlife. Seen the movie Ghost? It's great isn't it? Ghost is actually an extended metaphor and a warning about screen burn.[1]

Old CRT monitors, like the one pictured below, were bad for screen burn, and was one of the reasons early operating systems supported screen savers.

Ghosting on a CRT monitor

Whilst more modern LCD monitors were less susceptible to screen burn, plasma TVs and monitors also suffered. But what's all this to do with Android Daydream mode?

Unfortunately, modern screen technologies used on your smartphone might be susceptible to screen burn also. OLED screens, and AMOLED, can also be damaged with an image shown on screen for too long. This means your mobile screen might end up retaining the image made up by some pixels which are static and shown for long periods of time. Menu bars, soft key buttons and the likes, these are the kind of areas on the screen which change infrequently, and therefore may end up burning into the screen their spooky, ghostly presence long after you want them to.

Hence the introduction of Android's version of a screen saver; Daydream mode.

Acessing Daydream Mode

Access it by going to "Settings->Display->Daydream".



Daydream mode offers its services only when your phone is charging or in a dock. These options are accessed in the "WHEN TO DAYDREAM" section.


The Clock option is especially suited for those looking for a night time bed-side clock. It even has a "Night Mode" option allowing for the screen to be dimmed to avoid it beaming out too much light in the night.



Developers Beware - You Need More Sleep

What happens if you are in the position of having selected your Daydream options, plugged your phone in and docked it, but nothing ever happens? I discovered that a Developer setting might override your phone's ability to daydream.

Accessing Developer Options

In Android 4.2 (and presumably above), the developer options have been hidden from view by default. To enable that menu, enter the "Settings" section of your phone, and select "About phone" from the bottom. Tap on "Build number" seven times. No no, I'm not kidding. Tap seven times and you will be treated with a toast message informing you that "You are now a developer" which is nice. The developer menu will now be available under the standard "Settings" section of your phone.

Tap on "Build number" 7 times to enable Developer Mode


Disabling Stay Awake

Check your "Developer Options". "Stay awake" must be unchecked for Daydream to work.


References

[1] - Not really

Tuesday, 30 October 2012

Expandable Storage on Android Nexus Devices (Nexus 7 and Samsung Galaxy Nexus)

In this post, I am going to detail how to expand the storage of the device beyond the 8GB, 16GB or 32GB that comes built in to these devices. This process does not require modifying the ROM and does not require rooting the device; it is as simple as installing a single app.

The Nexus 7 tablet is fantastic; it has great dimensions, a good screen and comes at a fantastic price. The only real downside that I ever see about the device (or indeed, any "Nexus" device) is the lack of expandable storage; it has no micro SD card slot. The fact is though that it is very easy and inexpensive to add storage to your device. This is perfect for the long commute or going away on holiday scenarios where the on-board storage just doesn't cut it. Especially useful if you won't have access to your "media in the cloud" due to lack of wifi.

I figure now is a good time to highlight this for users who are tempted by the Nexus 7 tablet or other Nexus device but are hesitating due to the storage restriction. Also, since the announcement of the iPad Mini, now seems like a great time to highlight a strength of having Android as your OS!

This process allows you to connect a pen drive to your tablet or phone, and have any media stored on the pen become accessible to the mobile device. What does accessible mean? Well you be able to open the media straight from the device (streaming it, in the case of music or videos) or you can copy from the pen drive straight onto your device allowing you disconnect the pen drive whilst consuming your media.

With this process, your Nexus device can be expanded to support any size of expandable storage. I frequently use a 32GB pen drive, but I have also successfully used my 128GB Samsung Solid State Hard Drive too. (Note, external USB drives which require moving parts, like 2.5" external hard drives with platter hard drives don't work. This is because the device can't provide the hard drive with the power it needs over USB).

In order to provide more storage to the device, you will need the following:

  1. a Nexus device. I have personally tested this process on the Asus Nexus 7, as well as the Samsung Galaxy Nexus and presumably works all other Nexus devices. If you discover it to work on other devices, please leave a comment below.
  2. a pen drive - any old pen drive will do (in fact, even my 2.5" SSD drive worked a treat).
  3. an OTG micro-USB cable. Clicking on the link will take you to Amazon.co.uk where you can purchase one I recommend.
  4. the Nexus Media Importer application, available from Google Play. This cost just over £1 but trust me, it is well worth the money.

Setting up Pen Drive

Formatting

In order to have the pen drive readable from the tablet, you must ensure the pen drive is formatted correctly. As of version 3, the list of acceptable formats is:
  1. FAT16
  2. FAT32
  3. NTFS
If your pen drive is already in one of the formats above, you can move on. Otherwise, you will need to format your drive; you will only have to do this once but it is required.

I recommend you try NTFS first. FAT32 has a restriction on the file size meaning the largest size an individual file can be is 4GB. NTFS however, supports files up to a rather more impressive 16TB. Yes, TeraBytes. Realistically though, even 4GB is probably going to be fine for most things. Sure, a full HD movie might be bigger than 4GB; if you use FAT32 and need this kind of file size support, you will need to ensure your movie is chopped into parts, with each part smaller than 4GB.

If you experience any problems with NTFS, you can try FAT32 instead. If you are using Windows 7 (or later) and wish to use FAT32, you will want to make sure that you use a 3rd party add-on which supports formatting to Fat32 (like this app http://www.softpedia.com/get/System/Hard-Disk-Utils/FAT32format.shtml)

Adding Media Files

The files don't need to be named in any specific way, or held within a certain directory, so just copy them onto the drive. I would advise copying one file on first and testing it before spending a while copying your whole video library to find a problem later.

Connecting the Pen Drive

You have the USB OTG cable, so simply plug that into your device. Plug the pen drive into the USB slot on the cable. You really can't fail on this step! :-)

Launch the Nexus Media Importer app

If the app offers itself to launch automatically on a device being connected, it is a good idea to let it do this.



Otherwise, launch the app manually in the usual way you launch apps.




The "two arrows" icon in the upper-right of the app is used to trigger a re-scan of devices and might be required if your media isn't visible. Clicking on any of your media will present you with the ability to stream videos/music or open documents and pictures. The app itself is fairly straightforward but let me know if you have any specific questions/problems when trying this yourself.

Conclusion

There you go. The one single downside of the "Nexus" devices solved! Let me know how you get on if you're attempting this at home.

Friday, 14 September 2012

Read X509 Certificate in Java

Reading an X509 certificate in Java can be done using the following code. I used it specifically for obtaining a reference to the Apple WWDR certificate, but generally speaking, this will work for reading any certificate in X509 format.

import java.security.cert.*;
import java.io.*;
import org.apache.commons.io.IOUtils;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import java.security.KeyStoreException;


public static X509Certificate readWWDRCertificate(String keyFile) throws IOException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, CertificateException
{
FileInputStream fis = null;
ByteArrayInputStream bais = null;
try 
{
// use FileInputStream to read the file
fis = new FileInputStream(keyFile);

// read the bytes
byte value[] = new byte[fis.available()];
fis.read(value);
bais = new ByteArrayInputStream(value);

// get X509 certificate factory
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

// certificate factory can now create the certificate 
return (X509Certificate)certFactory.generateCertificate(bais);
}
finally 
{
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(bais);
}
}

The parameter you pass in should be the absolute location of the certificate file. In my case, my String parameter is something like "C:\\cert\AppleWWDRCA.cer".