Programming in C#, Java, and god knows what not

Straw That Broke the Camel's Back

I loved Windows Mobile 6.5 so at first I was looking forward to Windows Phone 7. Than rumors started that it will not support legacy applications. Fine I thought, I will just rewrite them as I need and at least I will get to do some refactoring.

Then multitasking was killed. Yes, platform does support it, Microsoft’s own applications can use it but mere third-party developers were not worthy of it. That also unfortunately meant that I would need to rethink some applications. I didn’t felt good about it but I still wanted to develop for it.

I thought that half of this things I hear around are just misunderstanding and everything will become clear once official development tools go out. This happened today. And it is clear…

Phone registration tool was last straw for me.

You see, once you buy phone, you cannot just do development on it - you MUST register it with Microsoft. They will than link your development phone to your MarketPlace account. That account costs $100 per year. This is little bit high but I was prepared to do it.

Only one small problem remains - I am from Croatia. That means that I CAN NOT register an account and I CAN NOT make any application for it. Even local debugging without any thoughts about selling is impossible for me.

I heard semi-official position on this: I should just find somebody that lives in “correct” country and he can apply instead of me. Never mind that I would break Windows Phone Marketplace Application Provider Agreement. This will just “result in possible revocation of your Marketplace Account, removal of your Applications from Marketplace, loss of Application ratings and reviews, and forfeiture of any associated Account Fee.”

I did my share of Windows Mobile development of both paid and freeware applications but love that started with Embedded Visual Basic and had it’s peek with C# and .NET Compact Framework is no longer there. And it is not because newest platform is bad. Technical aspects of Windows Phone 7 development allow for better things that any version before.

It is politics surrounding it that make me sad. Intentional crippling of platform, complicating development without any good reason and treating me as second class citizen are things I am not used to.

I cannot say anything for sure - life is too unpredictable for this - but I doubt that Windows Phone 7 will be visiting my pocket any time soon. So long, and thanks for all the fish.

P.S. No matter what tone of this post is, I will always value Microsoft’s programming tools. I can say, without any doubt, I’ve never had better working environment than Visual Studio and I will definitely continue to use .NET Framework. Microsoft did phenomenal job with them.

O Tempora

Illustration

Some time ago I had need for NTP server of my own. It was just quick and dirty solution for problem that probably affects only me.

I selected this small project as my venture into open source world. Project is hosted at GitHub. If you need it, you got it.

[2015-04-25: Project is moved to GitHub]

Error Validating the Default for Column...

Illustration

For one project of mine I needed to use GUID for column identification inside SQL Server 2008 R2. As usually, I expected everything to go fine. I just add new column (Id in my case) and set it’s data type to “uniqueidentifier”. After that just set that field’s default to whichever function generates new GUID. Since I am running SQL Server 2008 R2 I opted to use NEWSEQUENTIALID (I will explain why in some other post).

As I am programmer first and database administrator only when needed, my natural environment when dealing with database is right-clicking Design and doing changes in more visual way.

As soon as I changed “Default Value or Binding” property to “(NEWSEQUENTIALID())” I was greeted with “Error validating the default for column ‘id’. Do you want to cancel your changes?” After checking all documentation and rechecking it again, I just answered to this question “NO”.

After other fields were adjusted and I tried to save changes, I was greeted with another message: “Warnings were encountered during pre-save validation process, and might result in failure during save. Do you want to continue attempting to save?”. Correct answer here is “YES”.

Save went without hitch. After additional Internet searches I found that this is simple error in validation procedure. And it is in SQL Server since version 2005. Two versions after, it is still alive and kicking.

Until Microsoft fixes this in SQL Server 2020, just remember to hit first “NO” and second “YES”.

Keeping Screen on

When Android developer wants to keep screen on while application is running, it often ends up with him managing wake locks. Quite often this is not needed.

If only thing you want to do is to keep screen on while your application is running (e.g. video player) and you do not need more advanced power management control, you should just add another flag to existing window.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    setContentView(...); 
    ...2
}

System itself will now ensure that screen is kept turned on while your application is visible. As soon as your application is in background, everything returns back to normal. Added benefit of this approach is that no additional permissions are required.

Icons

Illustration

As a programmer I do need a lot of icons. And since I am cheap bastard, I usually try to get free ones.

Company from which I got most freebie offers is definitely IconShock. Guys just can’t stop producing free icon sets. Latest is their Social icon set. It has image for any social site there is (both raster and vector). And best of all - these icons are FREE for COMMERCIAL use also.

Yes, they may not earn much money that way, but they will always have my eternal love for this. Who knows, maybe I will even stop being cheap and buy something from them finally.

In any case I do love these guys.

P.S. They do not sponsor me. This is spontaneous outburst of pure love.

Froyo and Calendar

I had application on market for quite a while and suddenly I started receiving “SQLiteException”. Quick look at stack trace showed little bit more details:

android.database.sqlite.SQLiteException: no such column: Calendars._id: , while compiling: SELECT _id, title, dtstart FROM view_events WHERE (1) AND (Calendars._id=1)

This clearly pointed toward code that fills calendar events. Generally it looked like this:

Uri calendarsUri;
Uri eventsUri;
if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1 
    calendarsUri = Uri.parse("content://calendar/calendars");
    eventsUri = Uri.parse("content://calendar/events");
} else { //Android 2.2 (Froyo) and later
    calendarsUri = Uri.parse("content://com.android.calendar/calendars");
    eventsUri = Uri.parse("content://com.android.calendar/events");
}
...
Cursor cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "Calendars._id=" + calendarId, null, null);
...

Exception pointed right to that last line with query. Froyo had no idea what it should do with Calendars._id.

To make long ranting short, correct code should be:

Cursor cursor;
if (android.os.Build.VERSION.SDK_INT <= 7) { //up-to Android 2.1 
    cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "Calendars._id=" + calendarId, null, null);
} else {
    cursor = resolver.query(eventsUri, new String[]{ "_id", "title", "dtstart" }, "calendar_id=" + calendarId, null, null);
}

With this everything started working again and, since calendar code got first-class citizen treatment and it is finally documented in SDK, I doubt that there will be further breaking changes down the road.

Progress Indicator in Windows 7 Taskbar

Illustration

One of little things that mean a lot in Windows 7 is (at least for me) ability to show progress in taskbar. There is no longer need to Alt-Tab every minute or so into long running task (most often file copy). Progress of any long-running operation can (and should) be visible in taskbar button itself.

It is surprisingly easy to use this feature in our programs. Everything that we need is contained withing two functions: SetProgressState and SetProgressValue. Setting percentage is as easy as

var res = _taskbarList.SetProgressValue(owner.Handle, (ulong)newProgressPercentage, 100);

Although that minimal code will do it’s work, there is need for some additional preparation around it. We need to initialize feature itself, we need to check whether function is even available (XP is pretty much alive) and some error checking would be nice touch.

Because of this I made sample class in C# that should enable your program to have all those goodies. And, as always, sample is available for download.

64-Bit Registry View

If you are creating application that works with registry (e.g. your own registry editor) you are probably aware that 64-bit and 32-bit applications do not necessarily share registry keys. If you have 32-bit application your view of registry will not match one that 64-bit application gets.

.NET Framework 4.0 helps here a little. There is RegistryView enumeration available so code can specify which “bit-ness” of registry is desired. It works both ways: 32-bit application can specify 64-bit view and 64-bit application can specify 32-bit view.

As always, you cannot get what is not there. If you have 32-bit Windows, specifying 64-bit view will still give you 32-bit one.

ClientLogin

Google offers quite rich API for users of their services. Most of it needs some form of authentication. Without much ado here is my C# implementation. Only limitation is lack of captcha support, everything else should be there.

P.S. If you are interested in Google Data API, you might want to check .NET client library. It is open source project and it covers pretty much whole API suite.

NTP Server

Few days ago I stumbled upon problem. I had three Windows 7 computers without Internet access and I needed time synchronization between them. It wasn’t really important that time is correct - it was important that time is same. Solution was simple - just make one of computers NTP server and let others synchronize to it (using standard Windows time synchronization). While Windows 7 synchronizes time perfectly over Internet and/or when you are member of domain, it does not include NTP server.

As I tried to install some other software it became clear that I need to stop and DISABLE “Windows Time” service. NTP servers can only work if they can use UDP port 123. As long as “Windows Time” service is running that port is taken by it. For my scenario this service does nothing so disabling it caused no problems.

As I tried few NTP server solutions I got more and more desperate - either they weren’t free (yes, I am cheap bastard) or they would not work as service under Windows 7. After some time I just decided to make my own program. How hard can it be?

While I consider most of RFCs technical descriptions pure beauty, somebody made a mess of this one. All information was there, how to synchronize, which algorithms to use, probably even genealogy of authors - only thing not there was pure and simple ASCII format of UDP packet. Fortunately authors of SNTP (which is mostly compatible with NTP) did much better work with it. That combined with old pal WireShark made this program possible.

As any program that is written in one afternoon, this one has fair share of things not done. First of all, this program was only tested with Windows XP and Windows 7 as clients. While other versions should work, I spent no time in actually testing it.

This server is also not fully NTP compliant. All fields are there and all clients will consider it valid time source but stratum, precision, root delay and root dispersion numbers are just hard-coded instead of calculated. This should present no trouble in local network and if precision in seconds is satisfactory, but it is definitely not time server you would use if every microsecond is important.

Program requires .NET 2.0 and there is no setup. When you extract it to desired folder first order of day is performing installation from administrator prompt (cmd with Run as Administrator is fine). There we just run program with /install as parameter (“tempora.exe /install”) and service will be installed and started (if you remembered to DISABLE “Windows Time” service first). As long as you don’t uninstall it (“tempora.exe /uninstall”) it will start at each startup and it will return current time to all it’s clients.

Download is here.

P.S. Do not forget to DISABLE “Windows Time” service. ;)

[2015-04-25: Program is available on GitHub]