Friday, December 30, 2011

Missing pthread_mutex_timedlock on Android

I stumbled upon a problem today that the pthread_mutex_timedlock function definition is missing on Android, though it's declared in the pthread.h.
The first thought was to implement it as a loop with a call to pthread_mutex_trylock and some sleep until we are able to get the lock or until the timeout happens, felt a bit awkward.

I took a look a the Mono for Android source code and that's exactly what they do there (./mono/io-layer/mono-mutex.c), so go figure, looks like this solution might not be that bad after all.


Here is how it's implemented in Mono for example:
int
pthread_mutex_timedlock (pthread_mutex_t *mutex, CONST_NEEDED struct timespec *timeout)
{
 struct timeval timenow;
 struct timespec sleepytime;
 int retcode;
 
 /* This is just to avoid a completely busy wait */
 sleepytime.tv_sec = 0;
 sleepytime.tv_nsec = 10000000; /* 10ms */
 
 while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY) {
  gettimeofday (&timenow, NULL);
  
  if (timenow.tv_sec >= timeout->tv_sec &&
      (timenow.tv_usec * 1000) >= timeout->tv_nsec) {
   return ETIMEDOUT;
  }
  
  nanosleep (&sleepytime, NULL);
 }
 
 return retcode;
}

Wednesday, December 28, 2011

... из жизни


Сегодня на обеде видел одного папашу с маленьким ребёнком (меньше года). Дядька это болтал с двумя тётками которые сидели с ним за столом, пил большую банку Monster Energy Drink и тряс коленкой на которой сидел ребёнок ну наверное с частотой не меньше 300 ударов в минуту, так что казалось что у ребёнка либо голова отвалится, либо будет обширное сотрясение мозга.
"Энергетики" это вам не игрушка....



Nouns vs Verbs

Stumbled upon this post today,  it's almost 6 years old, but I haven't seen it before and enjoyed reading it quite a bit:

Execution in the Kingdom of Nouns

Hope you like it too.

Monday, December 26, 2011

Говорят дети


Саша просит поиграться на таблете:
- Хочу игру где убивают злых поросят ...

(Angry Birds)

Friday, December 23, 2011

How to setup Eclipse for debugging with Android NDK


Just wanted to bookmark couple of useful links on how to setup Eclipse IDE for debugging with NDK. Sometimes it might be more convenient than doing ndk-gdb from terminal, that I still didn't quite get used to yet. It took a bit of time and few tries to get things going. I had some problems with symbols loading and matching, and finally succeeded. Anyways if stuck check comments to these articles. 
  1. Setup your project to be a mixed Java, C, and C++ project:
  2. Setup your project to enable debugging:

Thursday, December 08, 2011

Хочу как у мусорщика


По дороге в детский садик, Саша позади в детском сиденье возится с кепкой.
Я: -Саша ты что там с кепкой делаешь.
Саша: -Хочу как у мусорщика.....

... и одевает кепку набок ...

... а ведь было же время когда завораживали мусорные машины с автоматическими подъёмниками, строительная техника и т.д. ... поменялись с возрастом интересы и приоритеты как-то ....

Friday, December 02, 2011

Android NDK logging

Just wanted to share the snipped that often used in the different Android native projects.
The following are some useful macros for logging with NDK.

#include<android/log.h>

#define LOGD(LOG_TAG, ...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGV(LOG_TAG, ...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGE(LOG_TAG, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

You can define more as needed just take a look at the android/log.h header file for all available priority values.
Then I would usually define log TAG:
#define LOG_TAG "MyModule"

and use the macros like this:
LOGD(LOG_TAG, "my val: %s", val);


fopen with native library on Android

If you develop an Android application with some native C/C++ layer (NDK) and 
try to open the file from it by doing FILE *pFile = fopen ("myfile.txt" ,"w");
the fopen call will fail.

The reason is that the current working directory for you application is the root of file system '/' and naturally your application doesn't have access to it.

In order to fix this problem you have to pass the full path for the file to fopen call.
One way to do this is to pass the base path to your application data folder where your application has read/write permissions. For example you can use the Context API getFilesDir().getAbsolutePath()
and pass the result to the native function call.
This particular API resolves currently into something like: "/data/data/[app package]/files/"

In your native code then the fopen call should succeed with 
FILE *pFile = fopen ("/data/data/[app package]/files/myfile.txt" ,"w"); 

Hope this was helpful.

Friday, November 18, 2011

Improved Git development model

If you are just starting to utilize Git in your team I would highly recommend to take a look at the git-flow https://github.com/nvie/gitflow and read the following article:
http://nvie.com/posts/a-successful-git-branching-model/

Enjoy!

Thursday, November 17, 2011

More fun with Android NDK7

I tried to compile cURL library Android NDK7 for x86 architecture and it looks like though the bug was reported in NDK6b: http://code.google.com/p/android/issues/detail?id=19851, the libc is still missing few functions sigsetjmp and siglongjmp.
The easiest solution for me was to get the libc from the latest ICS source tree (built for x86 arch).
Looking forward to get this fixed in NDK8, hopefully before Christmas.


if you are using sem_timedwait on iOS or Mac OS X ....

While porting some C/C++ code to iOS found out that sem_timedwait is not implemented on iOS or Mac OS X and seems like on many BSD systems. Here is the discussion on the topic:
http://stackoverflow.com/questions/641126/posix-semaphores-on-mac-os-x-sem-timedwait-alternative
The discussion a bit old but it seems like it is still a valid one.

For now I conditionally compiled the code with the call sem_wait, will look into a better cross-platform solution later.

Wednesday, November 16, 2011

Native libraries porting to iOS

Recently I spent a bit of time working on porting (getting built) some native libraries with Android NDK and iOS. The port to Android NDK deserves it's own post, that I might write down later.

As for iOS, I primarily needed openssl, curl, zlib and found nice little script, that helps you with the build:
http://code.google.com/p/ios-static-libraries. Just comment out what you don't need and you should be good to go. It's quite easy to repurpose the script to pull the source from your local repos if needed.

One of the problem I had was that I have only iOS SDK 5.0 on the machine and the libcurl build for iPhone device was breaking because "cpp" compiler (export CXXCPP=${DEVROOT}/usr/bin/cpp
) was not found . Apple provided "cpp" compiler for the emulator, but conveniently left it out from the iPhone device toolchain. The easy fix for that is to create the symbolic link in the /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin directory:
ln -s llvm-cpp-4.2 cpp

Hope this saves you a bit of time.

Sunday, October 23, 2011

Android NDK no love for wchar_t

I had to port some existing C/C++ code to Android (NDK) recently.
The code was using wchar_t and wstring quite often.
After a bit of research found out that it might not be as easy as I initially thought.

Basically, the wchar_t support is broken and most of the w* functions are not implemented as far as I can tell.
The wchar_t was essentially defined as one byte char before Android Froyo (2.2) and only starting from 2.3 it is 4 bytes long.

Here is the quite entertaining comment from their wchar.h file:

/* IMPORTANT: Any code that relies on wide character support is essentially
 *            non-portable and/or broken. the only reason this header exist
 *            is because I'm really a nice guy. However, I'm not nice enough
 *            to provide you with a real implementation. instead wchar_t == char
 *            and all wc functions are stubs to their "normal" equivalent...
 */

Is seems like there are at least couple of possible ways to solve this problem:
1. Fix the existing code, and remove the dependency on wchar_t (wstring etc).
2. Try to use the CrystaX android development toolkit instead, that seems to have a better support for wchar_t and other w* APIs: http://www.crystax.net/en/android/ndk/6

Will continue digging into this tomorrow.

Wednesday, October 12, 2011

MIRcon 2011

I'm attending the MIRcon 2011 conference - the second annual conference on information security that my company organizes:

http://www.mandiant.com/news_events/article/mircon_event_page

The conference is open to everybody and it is free.

Here is the couple of thoughts after the first day:
1. It's amazing how much is going on in the area of cyber security, cyber attacks prevention and instant response. Some of the stories can really make you paranoid about security of your personal data and especially (oh so popular)  the cloud security.
2. Ditch Windows. I haven't been using Windows OS actively recently. I'm doing fine with Mac OS X as my primary development platform and different distros of Linux (Ubuntu, CentOS, etc.). Windows is not doing good against the cyber attacks in part because of the properties of OS itself and in part of how popular it is. The more I get familiar with different flavors of *nix (Linux, BSD, etc), the more I get impression that it's kind of more difficult to make some consistent low level type of malware or find some common security vulnerability that can be scaled well, because the *nix distros are different, with possibly different vulnerabilities and differently broken APIs.

Looking forward to the second day of the conference.

Saturday, October 08, 2011

printf ("Goodbye Dennis Ritchie");

Rest in peace Dennis Ritchie


Wednesday, October 05, 2011

Rest in Peace Steve Jobs

Steve Jobs, Apple co-founder and former CEO, has died at the age of 56. So sad :(

Rest in peace Steve ....
http://www.apple.com/stevejobs




Thursday, September 29, 2011

Android HttpURLConnection improvements

This is awesome news , the Android team is finally fixing and improving the HttpURLConnection.


"Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.
For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward."
http://android-developers.blogspot.com/2011/09/androids-http-clients.html?m=1

Wednesday, September 28, 2011

MacBook, the battery status check

Pick up this useful bit today. If you need to check the battery status on the MacBook you can use the command:
ioreg -w0 -l | grep -i capacity

On my MacBook I got:
    | |           "MaxCapacity" = 5936
    | |           "CurrentCapacity" = 1106
    | |           "LegacyBatteryInfo" = {"Amperage"=18446744073709549771,"Flags"=4,"Capacity"=5936,"Current"=1106,"Voltage"=10837,"Cycle Count"=356}
    | |           "DesignCapacity" = 6900

The 6900 is the designed capacity of the battery and 5936 is the max I can get at this point. This kind of gives some info how much the battery degraded over time.




Wednesday, September 14, 2011

Chrome on Mac OS X with Android User Agent

open /Applications/Google\ Chrome.app --args -user-agent="Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17"

Wednesday, September 07, 2011

Michael Arrington interview with Arianna Huffington

I found this interview quite entertaining.


Saturday, July 02, 2011

Thursday, June 30, 2011

Ref: Android UI: Tips, Tricks, and Techniques

Just saving the pointer to this post here:
Android UI: Tips, Tricks, and Techniques

This presentation video is worth watching if you are looking for the few tips on the Android code optimization and UI optimization in particular from the couple of the "founding fathers" so to speak :)
The guys do not go deeper on the subject but give a pretty good overview on things to be aware of. The more detailed articles are available on android developers web site.

Android - libs-for-android

Couple of days ago I started to list few quite useful Android open source projects to learn from.

Here is another one I forgot to mention:

libs-for-android

Slightly different approach to ImageLoader implementation, with the public API that binds image view the the url. Also it uses http API from java.net instead of org.apache, that I would probably change, cause I remember having few issues with java.net connections before.

Enjoy!

Wednesday, June 29, 2011

Odd Day

Today one colleague of mine and just a very good guy, that I really enjoyed working with, had his last day at the company after 12.5 years of service.
It's kind of sad to go back to work tomorrow and not to see him again, but I'm glad to see him happy about the new opportunities for him.
12 years at the same company is a long time.....

....And the things get "better" when you find out that there is another great guy that I learned a lot from is most likely to leave in 3 months from now.

Certainly today was not the most productive day.
So, the plan for tomorrow: regroup and try to get my mojo back.

Monday, June 27, 2011

Chromebook - first impression

Just got Chromebook today that was sent to every Google IO attendee this year and wanted to share my first impression.

Pros:

1. The start up and shutdown time is pretty fast.

2. The battery time is very good so far

3. It doesn't get as hot as MacBook Pro


Cons:

1. The Chromebook device feels flimsy. It's all plastic and it gets kind of annoying when the screen vibrates when I type on the keyboard. Might need to get used to the keyboard, missing some keys when I type fast and navigation keys layout is a bit strange.

2. The Wifi is the "corner stone" of this device is very mediocre. In the house where I have full strengh Wifi reception on my MacBook laptop the Chromebook shows just a half (two bars). It shows 3 bars out of 4 when I sit in just 4 meters from the wifi router. It said on the box that the device comes with 100MB/month 3G data allowance, and this is very limiting for the device where network presence is everything.

3. Touchpad does not feel like too responsive, probably I just might need to get used to it.

4. I do't know if it's a quality of the screen or the fonts, but the text doesn't look that pleasant for reading , especially the small fonts. Some fonts seem to be off from the fonts I got used to see in the browser on Mac or Windows.

5. Gtalk feels more limited since I can not resize the chat window or I can't pop it out, cause there is nowhere to pop it out to.

6. I still kind doubt that the browser-only OS will be sufficient for me and that I would like to keep everything I do in the cloud.

It feels like Android OS at this point has some advantages, cause one can still run web applications on it as well as the native apps.

Finally, I understand that I got the Chromebook for free and really should thank Google for this instead, but I kind of hope that my feedback would help to make the next devices and Crome OS better.

I will keep playing with Chromebook some more and maybe, who knows, will change my opinion or even find some positive aspects of this device.

So far I'm not impressed.

Update: One thing the full screen browser experience kind of lack is ability to see both browser windows side by side, it pretty much invaluable in situation when our small kid wants to watch some cartoons on youtube and I would like to read emails or news at the same time.

Android - some best practices with source code

Android applications development although seems to be quite simple, has few things that can be described as a best application design practices. I personally learned many things from my own trials and mistakes, but nowadays you can make few shortcuts and learn from experts.

There are some good code samples that show some good practices shipped with Android SDK. You can always get the Android source code (expect the latest Honeycomb 3.0) and explore how everything is done there.

Also I'm going to list few projects that one can use as a reference for some good practices or inspiration:

1. The Google IO schedule application: http://code.google.com/p/iosched/
2. The Romain Guy's application that he created for Google IO 2011, that has some tips on animation and images handling (LruCache): http://code.google.com/p/xlarge-demos/
3. The application created by Reto Meier that shows some best practices for location aware applications: http://code.google.com/p/android-protips-location/. The post about this can be found here: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
4. The Droid-fu library that contains some useful ideas and patterns: https://github.com/kaeppler/droid-fu . I found ImageLoader to be pretty useful.

These are just few recent ones (except droid-fu) .... more is coming.

Thursday, June 23, 2011

Android devices stats

One of the projects I'm working here on is the framework of collecting applications metrics for Android, IOS and RIM applications. There is a bit of client code involved as well as the "big data" processing (Hadoop, Vertica) on the server side.

Recently I was asked if it's possible to get the applications usage report on Phone vs Tablet devices for the different platforms.
Don't ask me why, I don't know the answer to it, it's just needs to be done, period :)

The situation with IOS devices is pretty simple, cause you are pretty much limited to 3 devices:
iPod touch
iPhone
iPad


For android the situation is more complicated, especially since many early tablets are still running Android 2.1 and 2.2 and virtually indistinguishable from the phone with slightly bigger screen.

Currently we use Build.PRODUCT as the main device identifier, that, for example, has "htc_supersonic" value for HTC EVO phones. Now the problem is that we have more than 2000 different android "products" values reported, and every day the new devices are getting announced. So somebody would have to maintain manually the mapping between the device product name and some commercial product name and the type in order to get the kind of report we are being asked for, something like htc_supersonic <=> HTC EVO, Phone.

I wonder if there is such mapping is readily available and also it might be convenient if Google could open the API for that, since the kind of do the final certification for Android devices as far as understand.

Anyways, if you stumbled upon similar problem and have a solution to it, please share.

How to leave the running process on Linux after SSH session is disconnected

.... I had to use this pretty often recently, to kick off the script on the remote machine and exit SSH session while leaving the process running.

Here is how to start the process:
nohup ./myscript.sh > /dev/null 2>&1 &


This post is mostly for myself so I don't forget ....

Sunday, June 05, 2011

Richard Feynman: "Fun to Imagine"

Stumbled upon quite interesting and entertaining videos by Richard Feynman:
http://www.youtube.com/playlist?p=PLC351CC566C21C884

Friday, May 06, 2011

Google IO 2011, here I come!

Having early flight tomorrow morning to San Francisco, will stay in Palo Alto for the weekend, meet friends, drive around, visit few places (Napa Valley? ;) ).
Then off to the city for Google IO conference, can't wait. Should be very interesting :)

Tuesday, April 12, 2011

50 years of space exploration

It's been 50 years since the first human journey to outer space.

I can't say that I'm completely happy with the pace of the space exploration.
There is no doubt that a lot was done there, but it feels like there is more that could have been accomplished during these years.

It's amazing to remember how much enthusiasm towards the space we had when we were young. I pictured future to be a bit different back then after all the sci-fi books I've read after all the sci-fi movies I watched.

It's amazing that after 1972 we never visited the moon anymore or any other planet.

Nowadays it looks like the next generation of young people is more interested in all kind of gadgets/gizmos (iPods/iPads). They either want to be some kind of celebrity and/or own bunch of "stuff" or/and be rich.

I hope that I'm all wrong about this, we will see.

One more day till vacation ...

... looking forward to enjoy this weekend and the next week in Playa del Carmen, Mexico.
Just went through all my scuba gear, packed it all up and ready to go.
...one more day, yeah! :)

Friday, February 11, 2011

Running Man

About 5 month ago I started running.

Our company built one mile trail around the campus at that time and I had to try it. Also my daughter signed up for "Girls on the Run" 5K (km) marathon for December, so I really needed to get in shape in about three months.

I'm quite fit person, but never was a good runner. The most I even run before was when I was at school and we did 3km (a bit less than 2 miles) timed runs in the physical education classes. My work days are full of sitting behind the desk, working on the computer type of "activities", and this doesn't really promote healthy well-being.

My first run I barely made 1 mile, the pace was very moderate, but my heart was racing fast (165bpm+) and I felt really out of breath at the end of the run. This was quite motivating experience. After this, I was even more determined to run 5k marathon by December and I knew that I would have to push harder to reach that goal.

So I kept running about 3-4 times a week and slowly increase the distance. I started to notice that it becomes easier and easier to run once I find the rhythm. By December I reached 5 km (3.2 miles) distance. The first time I run 5K I was like, WOW(!!!), I did it, I never run more than 3K in my whole life and now I did it. There were few other positive changes that I noticed: my running heart rate dropped from (160bpm+) to 130bpm range, the stairs do not cause such a spike in the blood pressure anymore, I forgot when I had a headache last time and I started to feel better overall.

I've read many discussions online where people are complaining on all kinds of running injures they start getting once they start running more. Initially my knees started to bother me sometimes as well. Don't make it stop you from running. You have to listen to your body of course, and if your body starts to "complain" you are maybe pushing it a bit too hard, especially in the beginning, give it more time to heal and recover, but do not stop running, just push a bit less. Instead of increasing distance or time by a mile a week/month, try half a mile or quarter of a mile or maybe even less than that. Try to change the technique how you run to avoid unnecessary injures, try to land softer on the balls of your feet, right under your body center, open up your posture, relax more.

I've heard many people who run actually say how much they enjoy running. For me running is continuous struggle with myself and my laziness. But I found one interesting thing for myself: I really like the feeling after the good run (has something to do with natural biochemistry and metabolism), and the feeling of "victory" over myself (no matter how much I want to stop and rest on the last mile I just have to keep going).

This week I run four days so far, two of them were 4 miles (6.4km) run, the best time was around 32 minutes, maybe it's not that fast, but I'm pretty happy about this accomplishments. If I keep up the pace I hope to be able to run 10K marathon (10km ~ 6.2 miles) in around 50 minutes.

Hope this short post will spread some motivation around.

Keep running, keep pushing! :)