Tuesday, 3 September 2013

Windows 8, The Frankenstein

A little background about me, I have been using MacOS as my primary OS for the last 5 years, had been using windows since 3.1 as the primary OS prior to that and have been using Linux for the last 10 years time to time. After sticking to mac for so long, now I am exposed to Windows 8 through an ultrabook with touch screen. While using Windows 8, I came across the news that Steve Ballmer is going to retire from his job as the CEO of Microsoft.

Frankenstein is popular novel where Victor Frankenstein creates a creature using body parts from corpses as a scientific experiment. However, Frankenstein, the creature turns out to be evil.

Windows 8 gave me similar feelings with the touch experience. I have used a touch enabled laptop before, thanks to chromebook pixel. The experience was not smooth either but pixel can easily be used via touching. However it was a more frankensteinish experience with Windows 8. First of all the OS and the hardware are not really beware of each other, as you touch the screen the OS starts thing itself as a tablet and fires the touch keyboard on every input enabled field which may become weird. As an example if you swipe the screen to unlock and get ready to type your password you might be disturbed by the touch keyboard which would disappear as you type on the physical one. The experience is not blocking but not smooth either. Since the OS is aware there is physical keyboard attached, it should be pretty easy to not show up the touch keyboard on ultrabooks.

As you get used to touching and trying to use apps, you realize the apps and OS is strickly tight to its roots and not really designed to be used via touch. Its something like Frankenstein who took the old os, with a touch screen from ipad and trying to mimic the mouse with the finger. The experience does not get better if you avoid touching, just like your palm touching the touchpad as you type, you may accidently touch the screen as you are reading, typing or carrying your laptop and unlike smart software disabling your touchpads as you type you do not have any support from OS for touch screen.

I believe resisting to change and holding to your comfort zone is a sign of getting old and windows 8 is not only holding to its own roots its also holding to apple's roots instead of creating something new. I am really looking for Ballmer's departure and someone new to get on board.

Friday, 2 August 2013

I'll be speaking... at JavaOne, jDays and GWT.create

So here is my conference talks schedule for the following months...

JavaOne, San Francisco September 22-26
- The Zodiac of OSGi: Meet Eclipse Libra, Virgo and Gemini 
- Eclipse Orion: The IDE in the Clouds
- Mobile Java with mGWT: Still "Write Once Run Everywhere"
- Eclipse Orion: Your IDE in the Clouds

jDays, Gothenburg November 26-27
- Paranoid Android
Mobile Java with mGWT: Still "Write Once Run Everywhere"
The Zodiac of OSGi: Meet Eclipse Libra, Virgo and Gemini 

GWT.create, San Francisco December 12-13

I'll be updating (updated!) this post as the exact dates and schedules are announced. Stay tuned if you will be around at given dates.

Leap Motion: A small step or a giant leap?

I've been playing with Leap Motion for the last few days and here are my first impressions with this tiny device.

First, this is a great piece of hardware and there are tons of things to be done with it. However it doesn't really have compelling software accompanying it and currently not really going beyond a geek device but still it is a great geek device!! Here are my feelings in a more visual way :)

As you might see, after the shipment my package moved to Paris from Czech Republic instead of coming to Istanbul. The unpacking and installiation was quite 'appleish'. The intro and the included demos were very impressive. The first thing I expected was an out of box replacement for the mouse which this device is definitely not. After a while playing with all the apps, I realized currently most apps are just mimicking a mouse experience instead of creating a unique one for Leap. Leap motion is great device offering a new way of controls and it must be treated that way, just like the iphone first released.

The accuracy of the controller is great! You may realize it loses some finger when you do sharp moves but the accuracy is great for any idea and any app, well may be not for flying a real plane. The gem of this device is the sdk. It is designed in a beautiful way and the more you dig into api, the more you want to write code on it. The APIs are implemented and designed in a beautiful way.

After downloading the sdk from leap's website all you need to do is to follow the guidelines to set up your favourite development environment.

I built some sample apps using Java so I'll continue with the provided Java SDK. Looking at the sample app provided with the sdk, the first thing you will notice is you need to create a class which extends the com.leapmotion.leap.Listener class. Next create another class with the main method and create a  com.leapmotion.leap.controller.Controller and add your listener to the controller you just created.

// your custom listener class
SampleListener listener = new SampleListener();
Controller controller = new Controller();
// add your listener to the controller

The custom listener class which extends Listener needs to implement several methods.

class SampleListener extends Listener {
    public void onInit(Controller controller) {
        //can check if controller.isConnected() and decide how to proceed
    public void onConnect(Controller controller) {
        //this method is invoked after your code is connected to the device
        //it is time to decide what to listen. You can register for swipe, tap, circle... etc
    public void onFrame(Controller controller) {
        // Get the most recent frame
        Frame frame = controller.frame();
        //this is where the fun begines, you can how many hands or fingers in the frame
        //and what they are doing such as moves, gestures...
        //lets see how many fingers are there in the frame
        if (!frame.hands().empty()) {
            Hand hand = frame.hands().get(0);
            FingerList fingers = hand.fingers();
            System.out.println("Fingers " + fingers.count());
        //checking gestures is not much different
        GestureList gestures = frame.gestures();
        for (int i = 0; i < gestures.count(); i++) {
            Gesture gesture = gestures.get(i);
            if (gesture.type()==Gesture.Type.TYPE_SWIPE){
                SwipeGesture swipe = new SwipeGesture(gesture);
                if (swipe.state()==swipe.state().STATE_START){
                        float x=swipe.direction().getX();
                        float y=swipe.direction().getY();
                        float z=swipe.direction().getZ();
                        System.out.println("Swipe started at: "+ x +", "+ y +", "+ z);
                if (swipe.state()==swipe.state().STATE_STOP){
                        float x=swipe.direction().getX();
                        float y=swipe.direction().getY();
                        float z=swipe.direction().getZ();
                        System.out.println("Swipe finished at: "+ x +", "+ y +", "+ z);

As you might see its pretty easy to detect hands, fingers and gestures. Combining those Java's Robot class you may end up with a simple mouse replacement app for Leap Motion or integrate some gestures to your favourite IDEs debugger to debug your code with simple gestures.

Leap Motion is a giant leap as a hardware unit which currently comes with a small step in software. However the opportunities are endless and what can be done with this little device is all up to your imagination!

Friday, 26 July 2013

Android API Fun

Android team have a good sense of humor and adding funny method or variable names to the API since from the first day. Below I compiled a list I have came across don't hesitate to post the ones I've been missing.

public static final float GRAVITY_DEATH_STAR_I

This constant is a member of  SensorManager class since api level 1 and gives the estimated gravity of the first Death Star (in Empire units!). Constant Value: 3.5303614E-7

public static final float GRAVITY_THE_ISLAND

This constant is also a member of SensorManager class since api level 1 and gives the gravity on the island. Which island you ask? The lost island (from the TV series Lost). Constant Value: 4.815162

public static int wtf (String tag, Throwable tr)

wtf method is a member of Log class since api level 8 and luckily doesn't stand for the f word. According to the Java Doc and developer.android.com wtf stands for "what a terrible failure".

public static boolean isUserAMonkey ()

isUserAMonkey method is a member of ActivityManager class since api level 8. This method can be used to determine if the user is a real user or an automated test (well, unless you are developing apps for real monkeys).

public void fyiWillBeAdvancedByHostKThx ()

fyiWillBeAdvancedByHostKThx method is a member of AdapterViewFlipper since api level 11. This method lets AdapterViewFlipper to prepare by no longer advancing its children. Instead it indicates it will be automatically advancing the the views of the AdapterViewFlipper to AppWidgetHost.

public boolean isUserAGoat ()

isUserAGoat method is a member of UserManager since api level 17 and it simply returns true if the user is a goat :) and according to the api documentation "Used to determine whether the user making this call is subject to teleportations". Basically the method is an easter egg and unless you are messing up the interface it will return false.

 Thats my collection so far, feel free to add yours or stay tuned for the upcoming version of the Android to bring some new ones.  

Friday, 19 April 2013

Infamous Java bugs and pitfalls

In year 2000, I was in university and was on the verge of picking a language to build my career on. Java was not yet the mainstream but highly popular and on the spots. Applets were (not yet broken) fancy, shiny when compared to static html pages. Swing was not a bad choice to build desktop apps. J2EE was rising and getting attention.

It has been 13 years since then and Java went mainstream although applets failed miserably, not really considered for desktop apps and J2EE was too complicated to even build simple stuff but still nothing stopped Java from being the most popular programming language.

It was no surprise, Java is beatiful, type safe and easy to learn language. There were many very good implemantation details in Java such the garbage collector, strings (finalized class), collections which offer great implemantations of merge and quicksort, built in hashcode methods and many more. However, still Java is far from being perfect and may introduce some unexpected behaviour.

The abs bug:
Well this is a very minor flaw but there is a probability that Math.abs() function may return negative value. Weird? actually simple, Java integers can get a value between -2,147,483,648 to 2,147,483,647 which clearly shows -2,147,483,648 can not be represented in positive.
So is this a bug? Well the expected value is positive so yes definitely but in the end this is actually an overflow. So how to fix it? One way would be checking Integer.MIN_VALUE before using abs function or using bit operators to manipulate negative sign instead.

Autoboxing the primitives pitfalls:
Autoboxing makes it easy to work with primitive types and their object counterpart. However moving between them may introduce some unexpected behavior. For example Integer i1=6 can not be compared to Integer i2=6 with == operator where int i3=6 can be compared to those with ==. However using equals may not work as expected too. For example Long x=0L; returns true when x.equals(0L) but returns false when x.equals(0). Weird? Not really since x is long where 0 (without L) is int. So those are not even same object types. Also using primitive types with collections may result unexpected behavior. Finally Autoboxing may result problems in overloading. Lets say we have Integer i=6 and call method sum(i); and we have two sum methods like; sum(long val) and sum(Long val). Which one do you think will be called? Again reasonable but not expected to see at first look and may lead problems in your app.

BigDecimal constructor bug:
If you haven't already check Java Puzzlers from Joshua Bloch. If you create two Big decimals using double constructor (x1=new BigDecimal(2.00) and x2=new BigDecimal (1.10)) and use subtract (x1.subtract(x2)) you will end up with 0.8999999999. The double constructor of BigDecimal doesn't work as expected and string constructor needs to be used instead (new BigDecimal("2.00")). This might be a serious problem since BigDecimal is widely used for money calculations!

System.out.println pitfall:
println() is one of the first functions that is tought to cs students. It is easy and used often. Usually quite ok to use when you are trying some logic or debugging some values. However System.out is synchronized so acquires a lock when accessed. So using println can cause your app to run in synchronized context which actually means threads will be blocked when accessing println. Imagine a webserver and an app logging with println and you will end up with thread locks and each request waiting for other. So println is ok and useful but not for real apps and logging!

Map bug:
Again take a look at Java Puzzlers from Joshua Bloch, the fifth puzzle(size matters) introduces a strange behavior between an HashMap and an EnumMap where with same values, one map has a size of 2 where the other is 1. Several Map implementations such as IdentityHashMap, EnumMap may introduce this behavior.
Is this a bug? Well certainly we expect same principles from map implementations but Bloch describes that as the spec was not clear at the time.

Cpu Number bug:
This may not be a huge problem unless you really rely on hardware. To get available processor count Java offers Runtime.getRuntime().availableProcessors() method which returns an int number as the number of the processors available. However you may end up getting unexpected numbers if you give a try. For example on my quad-core i7, I get 8. So this method does not return the number of hardware cpus nor the number of cores but the number of execution engines (virtual cores). In my case because quad-core i7s support hyper treading it actually acts like it has eight cores.
So is this a bug? Definitely not since the hardware and the OS acts as if they have that number of physical cpus but still be careful counting if you rely on solid hardware.

Generic arrays
In Java arrays are created as follows, int[] arr=new int[5]; so if you have a generic type of T, you would expect to create a generic array this way: T[]=new T[5]; but simply you can't. Java does not allow generic array creation and this is actually because generics are implemented in Java using Erasure. Generics are implemented purely in compiler level and actually only one class file is generated for each class. So to create the array we need an ugly cast as follows, T[]=(T[]) new Object[5]; and when you try to compile, the compiler will issue a warning that you are doing an unsafe cast!
Of course this is not bug, it is just an implementation problem given for the sake of simplicity and compability when generics were implemented. But raising a compiler warning on a design issue may confuse someone who faced it for the first time.

So this is definitely not the end of the list but still Java offers a beatiful syntax, type safety and a realiable easy to learn language. And finally no language or implementation is perfect!