Thom Nichols

Thom

Technology is evolution outside the gene pool

Changing Calendar Colors on Android

This one is almost silly, but since I haven't posted in a while...

Android doesn't let you change the account color for exchange calendars.  Instead it picks a color at random when the account is first created.  Awesome.  Well, it's not *too* hard to change without a dedicated app.  Let's get started...

First I need to open the android shell (adb shell) and find the calendar database.  Mine was at /data/data/com.android.providers.calendar/databases/calendar.db

Next, let's look at the database...

$ sqlite3 calendar.db
sqlite> select _id, account_name, calendar_displayName, calendar_color from Calendars;
_id|account_name|calendar_displayName|calendar_color
1|blah@gmail.com|Home|-15574510
2|blah@work.com|Work|-15646905

So that last column is the color, in the format described here.  Which explains why all of the numbers are negative - the first byte is always 0xFF (fully opaque.)  Too bad I can't force it to be interpreted as hex.

I'm not putting too much thought into this, so the easiest thing I can think of to make the number easy to interpret as a hex color code: Use Python's struct module to convert numbers to byte representation and vice versa...

$ python
>>> '%08x' % struct.unpack('>I',struct.pack('>i',-15574510))[0]
'ff125a12'

Good - that's what I expected.  Quick explanation of the above - I'm basically telling python to convert a signed number to a byte array (pack,) then "unpacking" that byte array as an unsigned number.  So I can do the inverse to convert a hex value from a color wheel app back to a signed integer. 

So pick a color... I want 0xffbb0088.  To convert that back to a signed number, I do t the inverse of the above in python...

>>> struct.unpack('>i',struct.pack('>I',0xffbb0088))[0]
-4521848

... and there is my number.  Update the account via sqlite...

sqlite> update Calendars set calendar_color=-4521848 where _id=2;

Bam. Not as easy as it should be, but not terribly difficult either.

Category: Android

Add Comment

0 Comments

Enter Your Comment