Monday, July 15, 2013

Android apps - Architecture (Activity,Services,BroadcastRecievers and Alarms)

In this post I will describe the architecture of one application - "Do not disturb" (similar to the iOS6 feature with the same name ) . It appears a simple one, but turns out to be fairly complex from Architecture stand-point.


The app is a custom alarm-clock, during the time it runs, incoming calls and notifications are silenced, unless you call twice in 3 minutes. An sms saying that the call was silenced is sent to the caller.


The straightforward part is an activity which provide the GUI to set the hour and a button to click "start". Upon clicking either start or stop, an intent is sent to a service which handle it.

The service handle the start command by registering an alarm in the alarm-manager to the designated time. It also becomes foreground-service to add a status-bar notification, an icon, of alarm-clock is set, and it stays active without any cpu usage, but will (almost) guranteed to not get killed.
The service handle the stop command by canceling the alarm in the alarm-manager.
On the service sits a broadcast receiver for  telephony, which will count the number of calls of each number, send the automatic SMS if needed , and un-silence the phone if needed.

To cope with a case of device-restart (user-restart or just software/hardware bug) we add a broadcast receiver for BOOT_COMPLETED and ACTION_EXTERNAL_APPLICATIONS_AVAILABLE, which will re-start the service and test if an alarm is active.

Friday, October 26, 2012

Eclipse tweaking

My eclipse died today (again)
I did the mistake of installed a bunch of plugins and then a few more, and lo and behold, the eclipse slowed down and a day later failed to start.
Eclipse plugins/distributions hell makes "DLL-Hell" seam a likes a pleasant place.

I just installed a new clean replacement. Few performance tweaks worth doing.

  • Change the ridiculously low memory cap of -Xmx384m to at least -Xmx1024m in eclipse.ini 
  • Use a shared, external plugin folders, so that you will not have to re-install all of them when you decide to upgrade to the next great eclipse version.  Just add this to the eclipse.ini
  • Install this tiny but great plugin: which allows you to open shell/window and to re-size with ctrl+.  Just put it in the plugin directory above.
  • If you have use an SSD drive as the eclipse drive. The change in start-up time is incredible.

C# ain`t too bad

C# ain`t too bad

Nothing is really free in Microsoft`s world , but you can start by using the express editions of Visual C# 2010 and Visual Web Developer 2010 .
It amazing how much better the (java) free eclipse IDE is compared to the free visual C# , but if you are willing to put few hundred $`s , The Ultimate version+Resharper should give eclipse a very good competition.

Few notes:
LINQ is absolutely brilliant.
Delegates/Events are cool too. They are explained here.
WPF DependencyProperty is explained here.

NUnit is a great testing tool , especially with the new attributes and constraints
But it does not integrate to VisualStudio Express (and Microsoft does not allow plugin/extensions to the epress edition)
Don`t worry , just do:
1. Add reference to NUnit in the project. (Add reference -> .Net -> component name=NUnit frameworl
2. Open the nunit-x86.exe (on windows7) , create a project and use "Add assembly" to target the relevant project dll/exe.
3. Don`t run it manually, instead change the Program.cs to run it, instructions here . Note that you will need to add the nunit-gui-runner.dll from the NUnit ../bin/../lib directory

The Agile Dogma 

Dogma :: "prescribed doctrine proclaimed as unquestionably true by aparticular group"
In the recent years I worked both under the waterfall-dogma and under the Agile-dogma.
I have come to three conclusion:

  1. Both Dogmas are wrong as a dogma.
  2. Both have valid use-case for particular projects.
  3. Most developers don`t understand the difference between prototyping methodology and product-development methodology. Agile is the second, not the first.

When you start a new software project, you need to decide which product-prototyping method is right for you and then which software-development dogma is the right for the project. Choosing the wrong one is a clear path to failure, so choose wisely.

Step 1: You think you have a great product-idea. How can you know it`s indeed a great one.

First ask yourself, is my idea is a bright new product which was never commercially tried before (GPS-enabled Sun-glasses) or is it only a small improvement on an existing successful product
( new email client )

  • If it`s a new product - you must create a minimal prototype to test if it will actual sell.  It should be very fast - up to a month`s work.  Agile/Waterfall do not apply for such short time frame. 
  • If it`s an incarnation of an exiting product - great! someone else already "prototyped it" for you. Fill free to use their product to and jump to the next stage.

Step 2:  You verified the idea and shown a prototype which people love.  Should you have one public-release or a lot of small public-releases?

  1. What is the use-pattern of your product?Is it a "use-once"(a movie) , "use-for-few-weeks"(most games) or "continues-yearly-use" (most websites) ?  
  2. Can an existing user get the new version of  the product very easily?
    Is it automatic(a website), a manual-update (mobile-app) or plain-impossible(firmware,hardware)
If the answer is a clear "YES" to both of the questions above, go for multiple-releases.
If the answer is no even to one,  you need one big release (didn`t talk about agile/waterfall yet)

Let`s see some samples:
  • For a web-site like an online-store/news-site the answer for both is a clearest yes. The users use your site on a continues-yearly-use and each time a user enters the site he will get the new version automatically.
  • For a media (movie/presentation) the answer is a clear no, as most of the users will see the movie once in the first month and will not bother to re-view it once you release a new version.
  • For downloadable game, the answer is a clear no. Although most game have an auto-update feature for bug-fixes, most users will play the game only in the first month. 
  • For firmware for hardware-devices the answer is usually no. Although the user use in continually  99% of the users won`t upgrade the firmware version.
  • For a software library (like logging framework) the answer is again a clear no.  Other developers will not update your library automatically, and will usually do it once  a year, when they need an extra feature the new version has.

If you answered no to one of the above, you need to have one ultimate public-release
But, on some products , you can still have multiple "closed-beta releases".
  1. First find a large pool of  "beta" users which you can ship your product too. They should be the typical audience , but their size should be up to 1% of the intended users. (You can pay them)
  2. Divide them to multiple distinct groups.  If you plan around 5 iterations, create 5-8 groups.
  3. In the end of each iteration, use one "fresh" group and get it`s feedback. You will not reuse that group again
  4. In the last iteration , release to the general public.
The large pool of users is a must. Using the same person all the time, usually your product-manager or in small-startup`s your spouse, gives very bad results.

Sometimes you can`t find any users which will test your product. In this case you can still create internal-releases or internal milestones in which each feature is finished and ready to be internally tested. This is true to both agile and waterfall approaches.

The obvious Bottom line is "NEVER DO THE FIRST-TEST JUST BEFORE PUBLIC RELEASE".  You will be surprised how many companies do just that.

Step 3:  You know how many releases you can do.  What software principle should you use?

All software products can be separated into multiple small independent features. This is true if you are building a algorithmic-focus search-engine , or a design-focus web-site.
So first divide it to these features and work on them.
Waterfall is great if it is done on one small feature. Meaning you will spend a 1 day on design, 3 days on development&unit-testing ,and then 1 days on system-testing.

But what of the features that take few months to develop?
Well, first try again to break them into sub-features, most features can be broken with enough thought.
If you really can`t , and it`s rare and usually limited to algorithm-intensive or framework-intensive work, then go waterfall on this particular feature.  Let me emphasize that these kind of features are very-rare in web/media/mobile products and most developers today should do it very rarely.

So bottom line here, design+implement+test in the waterfall way is good as long as it is for small features. (some will say this actual == most-of-agile)

Heroku and Amazon ElasticBeanstalk

Amazon Elastic Beanstalk is a platform-as-a-service solution for web-application development.
What it means is that you upload a java WAR ("jar for the web) file and Amazon takes care of hosting it on a pre-installed Tomcat web server.

Heroku was created by a different company to sugar-coat the Amazon platform.  For some extra money per month, you will get:
  • much much better documentation
  • better, simpler, eclipse-plugins
  • Easy configuration of external services like non-sql DB and Caching.
If you are working on a small scale project , and your time is precious , go Heroku.

Wednesday, May 11, 2011


On requirements
1. You ain`t gonna need it - Always implement things when you actually need them, never when you just foresee that you need them.
2. MoSCoW method (Must Should Could Won`t)All requirements are important, but they are prioritized to deliver the greatest and most immediate business benefits early. Developers will initially try to deliver all the M, S and C requirements but the S and C requirements will be the first to go if the delivery timescale looks threatened.

On project scheduling
1. Brooks`s law - adding manpower to a late software project makes it later. "Nine women can't make a baby in one month"
2. Parkinson`s Law - always time each task. Work expands so as to fill the time available for its completion

On Design
1. Analysis paralysis - over-analyzing (or over-thinking) a situation, so that a decision or action is never taken, in effect paralyzing the outcome. A decision can be treated as over-complicated, with too many detailed options, so that a choice is never made, rather than try something and change if a major problem arises.

2. KISS principle - Keep it simple stupid. The principle is best exemplified by the story of Johnson handing a team of design engineers a handful of tools, with the challenge that the jet aircraft they were designing must be repairable by an average mechanic in the field under combat conditions with only these tools. Hence, the 'stupid' refers to the relationship between the way things break and the sophistication available to fix them.
.... It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away
3. Rule of three ( not two as many think , three!) - Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that you are allowed to copy and paste the code once, but that when the same code is replicated three times, it should be extracted into a new procedure.