Category Archives: Programming

Algorithms for computing Pythagoras Sum

While helping a friend of mine with programming assignments, I stumbled upon a question on two different algorithms of calculating Pythagoras Sum (Pythagoras Addition), through a function called pythag

One algorithm is straight-forward, which is just  c = \sqrt{a^2 + b^2}. \,

The other, however, is very intriguing, it makes use of absolute, min, max and several lines of weird mathematical operations to get the result. Continue reading

Sony Z3 Top Speaker Noise

[Solved]Sony Z3 Top Speaker Noise Problem

I have recently purchased a Sony Xperia Z3. There seems to be a strange problem with the Sony Z3 top speaker. Most of the time it produces a static background noise when I run any applications that has sound. The noise can still be heard even if I have muted the media volume. But there are occasions where the noise does not occur.

I searched online and found out this a common problem for Sony Z3, the solutions they suggested on the forums(tuning the equalizer settings) did not help me. I have also contacted Sony customer service and they responded that “It happens when you use applications that access Internet.” That seems logical enough first but I soon realized that sometimes the noise does not occur even when I am using the applications that access Internet.

When I tracked down the cases where the noise did not occur, I found out the root cause of it: The Alarm Volume. 

sony z3 sound setting

Alarm volume setting in Sony Z3 sound and notification setting

This is totally strange but it is indeed the cause of the noise. Whenever I lower the alarm volume to below about 60% and reopen the applications, the noise disappears. The alarm volume is not supposed to be related to the sound inside the applications but it seems that Sony Z3 has a software bug that caused them to be linked.

Also note that the global alarm volume in the sound settings will be overwritten by individual alarm volumes that you set in the alarm application.

sony z3 alarm setting

Alarm volume setting in Sony Z3 Alarm app

So in order to make sure the noise does not come back, you need to set both the global alarm volume and individual alarm volumes to below about 60%.

go lang gopher

Working with Go lang

During my internship at IBM Singapore, I am using go lang to develop the back-end of the software. It is a nice new language, with a lot of features that I found really useful. Previously I have mostly used Java and JavaScript in programming, and go lang has the best of both.

First-Class Functions

I have always liked the flexibility with functions in JavaScript, the fact that you can treat functions like any other normal objects and pass them around. This advantage made me hate Java where methods are just methods.

In go lang, functions are surprising quite flexible, they can be used as values and passed around, and anonymous functions with closure are supported. All of these are still on top of statically-typed nature of the language.

Multiple Named return values

This is a great feature in my opinion. For Java and JavaScript, functions can only return one value, causing a lot of troubles when you want to do error checking or simply need to return multiple values. In go lang, the ability to return multiple values makes all of these very easy. On top of that, the return values can be named as variables in the function signature and saves the trouble of declaring them in the function’s body.

And more

Go lang also has great packaging system, allowing reusable components to be packaged and easily integrated into other projects. It has OOP-styled types like struct and interface. It also supports encoding and decoding of JSON internally.

I have not had a chance to use the multi-threading and concurrency components of the language, and I shall wait to be delighted.

[Solved] onDataChanged not called, Android Wearable

I was doing a school project on Android Wearable for the past few days. A strange bug happened.

I used  Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)for triggering  onDataChanged in WearableListenerService. However, I noticed that sometimes  onDataChanged is not called even though the putDataItem method has fired.

The strange thing is this bug only happens on the first call, subsequently the calls trigger normally. More interestingly, the bug only happens sometimes, once in a while, the first call will trigger normally.

Aftering debugging and getting nowhere, I decided to get help from stackoverflow. I found an answer that was relevant:

http://stackoverflow.com/a/24697312/1472186

The answer suggests adding a time field into the DataMap  so that the data is really changed every time. I did not think that was the cause of the problem, because I followed the online tutorial and had a count value in my DataMap, which increments every time. This should have caused the data to change every time.

However, after I added the time field into the DataMap, the bug disappeared. So it was true that my old implementation was not causing any changes. The count value did not work. After thinking through the process again, I figured out why.

The count did increase after each call, from 0 to 1, 2, 3, … However, those values are stored in the DataMap within the wearable device, not on the phone. So it was possible that wearable device already has a DataMap in which there is a data with count 0, although my phone app does not have the DataMap yet. When I send a new DataMap with count 0 from phone to the wearable, that DataMap is already present on the wearable, and hence no change will be detected and onDataChanged will not be triggered.

For time, since the current time is unique and always increasing, there would not be a case where the time in DataMap on the phone already exists on the wearable, hence always triggering onDataChanged.