Archis's Blog

October 30, 2008

The "My Pictures" folder on Windows Mobile

Filed under: Uncategorized — Tags: , , , — archisgore @ 4:22 am

Ever wanted to write a killer pictures app for WM? Ever wanted to do something cool like anotate pictures? Chances are, you’ll want to include a good old conventional folder-browser dialog to ensure your users can locate their folders manually.

SHGetSpecialFolder, while a great API in theory and documentation, is something you should treat with great skepticism – especially if you’re going to write to the folder returned by this function without confirming it with your users first.

SHGetSpecialFolder returns a hardcoded path to \My Documents\My Pictures. Don’t see anything wrong with that? Wait till you add a storage card to your phone (which you’re bound to do, unless you have one of the expensive models that come with a few gigs of inbuilt storage (and yet this will fail on that kind of storage too – HTC Diamond is a known example)). If your camera is storing pictures on a storage card, then it turns out, there is either no spec for where this path will be stored in a uniform manner, or the OEMs didn’t stick to it. Either way, there’s very little we can do about it. It’s out there, and that’s how it is, and you need to live with it.

There isn’t really a reliable way to get the folder to which your camera is saving pictures by default, and while a best-effort attempt at locating it can be done by some registry hacks and SHGetSpecialFolder, it would be a good precaution to confirm the path with your users first, and give them the option of locating the path if you get it wrong.

October 7, 2008

Avoid GetUniqueDeviceId on Windows Mobiles

Filed under: Uncategorized — Tags: , , , , — archisgore @ 12:47 pm

Again, a piece of information learnt the hard way. GetUniqueDeviceId, while the MSDN documentation guarantees a unique device ID in each device, doesn’t quite work the way it’s supposed to in actual practice. I don’t know exactly why that is, and there’s not much documentation, but the symptom is that multiple devices will keep throwing the same Device ID and thus its uniqueness is not guaranteed.

There are various workarounds you can use. In case your app just needs a different identifier for each device, the simplest and most elegant method is to generate a GUID and store it. Naturally, the side-effect of this is that if your app is ever uninstalled, the GUID is lost and the device gets a new identifier.

In such a case, where you need the device to be identified as the same device without storing any state locally, you may use a fixed seed and CryptProtectData it to generate the ID. CryptProtectData uses a provisioned private key on the device to encrypt a given string. This way, regardless of the state of your app, so long as you give it an identical string as input multiple times, you will get the exact same device identifier on the device, and yet a different identifier on different devices.

You can always go one step further to guarantee uniqueness and consistency even more. That would be to get the device’s IMEI number (if the device has a phone in it), and use CryptProtectData on that.

Using the webbrowser control in Windows Mobile

Filed under: Uncategorized — Tags: , , , , , , — archisgore @ 10:10 am

After eighteen months, I got a weekend free to write my own code, and hence continues my series on avoiding some major gottcha’s in WM coding.

One of the major frustrations you would hit is the webbrowser control. There are many scenarios when you want a formatted text based UX, or a flow-style UX where the WebBrowser control comes in pretty handy. However, here are a few things you should know before you start coding – lest you end up investing months of effort and finding out you won’t get the “exact” same effect as you hoped for.

All in all, it’s an amazing component that at times you will be very very thankful they included.

1. Most of the COM Interfaces that allow extensibility on the desktop aren’t supported on the phone (duh!). Most specifically the ones you’ll miss are the IDocHostUIHandler (preventing you from overloading the context menu), and IInternetSecurityManager (preventing you from implementing your own security zone/settings).

2. The DOM elements are specially made-for-mobile COM classes which are named IPIEHTML* instead of the IHTML*. Your Anchor would be IPIEHTMLAnchorElement (or somethign like that). There isn’t any documentation on these so don’t waste time hunting. Instead open up webvw.idl found in one of the sdk include paths and you’ll be able to figure out what does what.

3. DHTML is *NOT* supported. Many people repeatedly try to modify formatting/style of elements in a generated page. The precise and unambigious answer is “it cannot be done”. Don’t waste time trying. Having said that, the IPIEHTMLControlElement derivatives support some on-the-fly modification such as setting/modifing text in text boxes, or editing of lists, and disabling/enabling controls.

4. Some features not supported on desktop are actually supported on the mobile control. Some esoteric CSS pseudoclasses such as :focus which don’t work on desktop IE, work perfectly on pocket IE. You can make some really cool effects by changing formatting when a control is focussed – especially on smartphones where the dotted-line focus highlight can be too thin to notice and you might want to add a little more jazz to your links. In this case, there’s an undocumented CSS extension called x-focus-border which when set to none will get rid of the focus rectangle (found this in the comments section of the IE Mobile Blog).

5. If you’re using Document->Write to insert HTML into the control from your app, then note that the DocumentComplete event is not guaranteed to be fired. I found no documentation on this yet, but I’ve seen the symptom a few times. I admit that I may be doing something wrong here, but it’s something worth keeping in mind if you’re going to rely on that. Ensure you test this out with your pages first. To be more specific, when I didn’t put a background picture for the page, I didn’t get this event. When I did put a background picture, the event fires consistently as expected. The workaround, of course, is to put a dummy one-pixel white image as background.

That’s all I can think of for now. Will post more regularly on avoiding various gottchas that can end up saving a lot of time when developing for WM, if you know what to look out for.

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 122 other followers