Understanding Launcher and Choosers in Windows Phone 7

One of the curious decision made by the WP7 team was the decision to lock every application into its own sandbox and not permit unauthorized access to various information stores, such as the contact list.  To get at this necessary information or to use it to help the user send a text or call someone WP7 provides Launchers and Choosers.

While both Launchers and Choosers share many similarities, they are inherently different in their purpose as well as their effect on your application.  Launchers are “fire and forget” actions that take you out of your application and into a new application, such as the web browser.  The most important aspect of Launchers is understanding that when they are launched they will cause your application to be “tombstoned”.  Choosers, on the other hand, are designed to be encapsulated bits of functionality that work within your application to accomplish the task, picking a picture for example.

In terms of usage, there is very little difference, from a progamatic sense, between setting up a launcher and chooser.  I have some code below that shows how one might invoke the WebBrowser task:

image 

Both Launchers and Choosers are very similar to Dialogs from client programming land.  For this code, I am hard coding http://www.google.com as the Url that shows up in the resulting Web Browser.  Remember that using this sort of task will cause the user to “leave” your application and thus causing your application to be “tombstoned”.  The user can still reach your application either by hitting the “Start” key on the device and then reselecting your application from the app list, or with enough presses of the “Back” button on the device.

Choosers are, as their name suggests, for choosing something; however they are still identified as a task.  Here is some code for selecting a contacts phone number from the users contact list:

image

The first thing to notice is that I am displaying two functions, both the event handler for the button this is tied to as well as an event handler for the choosers “Completed” event.  With the chooser you will still be leaving your application, but your application will not, in most cases, be “tombstoned”.

As of the current RTM release 16 total launchers and choosers and, for now, no way to create custom launchers and choosers, something I hope that will change in the future.  Below is a list of these launchers and choosers:

Launchers

  • EmailComposeTask
  • MarketplaceDetailTask
  • MarketplaceHubTask
  • MarketplaceReviewTask
  • MarketplaceSearchTask
  • MediaPlayerLauncher
  • PhoneCallTask
  • SaveEmailAddressTask
  • SavePhoneNumberTask
  • SearchTask
  • SmsComposeTask
  • WebBrowserTask

Choosers

  • EmailAddressChooserTask
  • CaptureCameraTask
  • PhoneNumberChooserTask
  • PhotoChooserTask

Launchers and Choosers appear to be Microsoft’s way to prevent people from harvesting information, secretly, from their mobile applications.  I do wonder how people will receive this, because as a developer I am worried about asking the user to select many of their contacts for an operation; they would have to go back and forth, rather then say selecting which contacts as a mass select.

Have fun using Launchers and Choosers.

Explaining the “with Google” on the back of your phone

Linux is the core of the Google Android mobile operating system.  This is a fact that most people know and understand and, myself, having worked with Linux for a long time I know all too well the vast amount of fragmentation that that various OS have caused.  Thus we know that at some point the same thing will happen with Android, it really is only a matter of time and it is not something that can be helped.

It was these facts that led me to presume that having “with Google” on the back of my Droid meant that my phone ran a “sanctioned” version of Android, or rather a version that is directly backed and approved by Google.  This is partially true.  Google does in fact define a set of compatibility standards that device OEMs must follow in order to run the Android OS as well as how they must implement the public APIs the framework exposes.

So what exactly does the “with Google” mean on the back of your phone. Well basically, that Google has approved the device and initial installation and that certify that it produces the intended “Android Experience”.  This is not to say a phone is does not have the label cannot produce the “Android Experience”, the label simply means that the OEM has taken the time to go through Google to verify that that hardware is acceptable.

Windows Phone 7 Developer Launch

This week I was at the Windows Phone 7 Developer Launch event in Detroit, MI.  As someone really enjoys developing in the mobile space Windows Phone 7 has me really excited, more so every time I see it.  The developer experience is just light years ahead of what Apple and Google provide, yes even iOS dev with MonoTouch fails to even touch WP7 dev with Silverlight and .NET.  Microsoft has pulled out all the stops to make the development experience between Silverlight for the Web the same as Silverlight on the phone.

You can say that I am just drinking their Kool-Aid but I seriously invite you to take a look at the phone if you have not done so already.  It really is incredible.  In my opinion, it will be very interesting to see if Microsoft can take a large chunk from Apple and Google.  Much of that will hinge on the acceptance of the Metro design that Microsoft is pushing.  Their idea is to make using the phone like navigating through an airport and/or a mass transit system.  You may never have been there before but because of the signage you can instantly find your way around.  Windows Phone 7 is designed to truly give you information you want at a glance, and the hub design lends itself well to this concept.

The Hub design allows you to group related applications together to provide a tightly knit experience and rather then go screen by screen, the hub gives the impression of grouping through text and pictures scrolling off the visible screen at the same time indicating to the user that there is more content.

I honestly have not been this excited about a Microsoft product in a long time.  I do get excited about their products often, but I am really psyched about the phone.  As I code for Android and iPhone, the developer experience alone is enough to make me want to salivate.  Their ad for the phone is also pretty funny:

I love the part where the kid beans his Dad in the head while they are trying to play catch.

Overall, I really enjoyed the event and learned quite a bit.  I encourage you to take a look at the new Microsoft phones.

West Michigan Day of .NET Sessions Announced

Being on the planning committee does have its advantages.  Not only have I had the opportunity to work with some really great people to help plan for Day of .NET here in West Michigan, I have also gotten to see what people in the community are looking at; I gotta say, I like where our community is head from a technical perspective.

Anyway, just moments ago Chris Woodruff gave his final approval of the sessions for the 2010 event.  Here is a list of the people selected to speak

  • Dennis Burton
  • Michael Eaton
  • Sam Nasr
  • Jay Harris
  • Jeff McWherter
  • David Giard
  • Jennifer Marsman (MSFT)
  • Dan Woolston
  • John Hauck
  • Mark Miller
  • Mike Amundsen
  • Steve Mushkat
  • Steve Schofield
  • Dave Leininger
  • Matthew Groves
  • Jason Farrell (Me)
  • Stephen Cleary
  • Jonathon Tower

I am honored to have the chance to speak at the event this year with so many fine speakers, many of whom I have looked up to as I have come up through the ranks here in West Michigan.  All Sessions and their abstracts are available here (http://www.dayofdotnet.org/GrandRapids/Fall2010/Sessions.aspx)

Come check it out October 23 (Saturday), 2010 @ Calvin College in Grand Rapids, MI from 830am – 5pm

the social network

This evening I went and saw “the social network”, a movie that I have been excited about since it premiered.  I have always harbored a deal of respect for Mark Zuckerberg and this movie did little to diminish that, though some of the decisions that were made seemed a bit harsh to other parties, some times radical situations require radical action and I think that is what Facebook experienced.

Told through the inner dealings of the many lawsuits that faced the company stemming from its initial creation you get a real good look at how the idea was propagated. It was really neat to see how certain ideas sprouted from the natural interactions Mark had with other people and fellow classmates and how the idea consumed him until it dominated his every waking moment.

This is a must see for any programmer, cause we can all identify with a lot of the concepts in the movie, from Mark hacking networks in the early part to the terminology thrown around the facebook office that programmers are all to familiar with. Just a great movie in all respects and one that I am definitely going to get the blu for on day 1.

Sorry for the lack of technical content as of late.  Expect an announcement regarding West Michigan Day of .NET soon.

Cheers

Introduction to Android Presentation

I recently gave a presentation for the Northern Michigan .NET User Group (http://nmichigan.net/) on the topic of the Android Mobile OS. A weird topic, I admit, for a .NET user group but, these days branching out is essential to survival as we all know.  The presentation covered what Android is and how fast it is growing and talked about the various released and unreleased versions of Android.  I then talked about how developers develop applications on Android, including an explanation of the tools and conventions.  I then presented code for a simple HelloWorld application using Toast notifications.  After this we talked about many of the development aspects of the code: the separation of the UI from the code, how Java handles events, and what is an Activity.

As a closing exercise for the presentation I presented the source for the the Twitter Profiler application which uses the Twitter API to look up public information for a user.  I then showed how quickly this app can be deployed by deploying it to the Android Market and demonstrating how quickly it became available (10s).  As promised I am making the slide deck and sample code available. Download it below.

http://cid-630ed6f198ebc3a4.office.live.com/embedicon.aspx/Public/Android%20Presentation.zip

New Speaking Engagement

Apologies for the lack of new content recently, still get re-acclimated to life back in Michigan.  Recently, my good friend Chris Woodruff (http://chriswoodruff.com/) offered me the chance to substitute for him speaking at the .NET User Group in Traverse City Michigan.  I accepted and plan to give my Introduction to Android talk which covers the understanding of Android, the different versions, where its headed, setup of the environment using Eclipse and ADT, and a brief introduction to programming against the OS.

This is the meeting information:

NMC Campus – Beckett Building – Room 215
Start – 6:00pm

Really looking forward to meeting more people from the community.  If you can make it, please attend.

Refactoring a Mobile Client Logon Process

Recently we encountered a problem with the client application I had been developing.  The application features a force logoff, whereby when the device is connected to an external power source the application will log the current user off.  This was designated a requirement by the client to prevent confusion amongst factory workers.  However, the flaw was that the force logoff caused dispose problems with the main form.  I wanted to detail the process of solving the problem.

When I initially designed the system I designated the Login form act as a “host” for the application, that is used the ShowDialog method to effectively place the main form over the login form.  While this seemed to work, after some time the QA team began complaining about a crash when the application was removed from a charging cradle.  We examined this problem over the period of a day and determined that the form was seeing part of it garbage collected.  You see, this form is rather large so my attempt was to keep a reference to it and simply hide/show the main form as needed per the login process.

Our first attempt was to completely decouple this process: thus a new instance would be created each time within a using statement.  We knew this would hamper performance as the application transitioned; but we would rather not have the application crash.  On the whole, this approach seemed to work and so QA was made happy….for a day.  It started when one of the QA members had the application crash with a similar error, but this time coming from a different place.  Furthermore, the change seemed to permit the existence of ”zombie” forms under certain situations.  Data became corrupted due to double execution of certain operations.  It was clear we had to return to the drawing board.

A two hour brainstorm session yielded some ideas, but nothing we felt would actually work.  That night at my hotel I was watching Futurama and playing with idea on a notepad when suddenly it hit me.  The application uses a strategy pattern to carry out workflows which map to business operations.  My thought was, what if I consider the entire application to be one giant workflow.  This would allow me to lose the multi-form interactions that were giving us grief and make the application simpler.  A firm belief of mine is that a refactor should  be more about removing code then about adding code.

The next morning I presented the idea to the client and my team.  At this stage I am on my way out with this client as I return to Michigan, so there is a need for me, as the lead, to begin writing concept documentation.  However, it was decided that I was the only one who  had the vision and could carry this change out quickly, thus I took the task.

At its root the problem is very easy.  1) Transform the login form into a login user control 2) Split the existing MainForm apart so that a user control is used for this process that fits into the “workflow” 3) update type loading so that we can operate on a higher abstraction then those simply for control involved in workflows.

This provides an example of why it is important to decouple components of responsibilities in an application and why the use of a consistent abstraction is recommended for application.  UserControls in this system all inherent from one of two classes: WorkflowUserControlBase and UserControlBase.  As  you might guess from the naming, WorkflowUserControlBase inherits from UserControlBase.  This is so when working with the components within the plumbing I dont need to worry about what control I am working with, I can operate on its abstraction; however these were all coded for WorkflowUserControlBase because only workflow control were involved in this process.  This is why the definition of workflow within the application had to be changed.

The workflows operate via .NET events which allow for highly decoupled pieces.  It made this substantially easier as my changes never went beyond the entry point tier, a good sign of an orthagonal design.  The only concession I had to make was while it would be ideal to fully implement the concept of a ProcessWorkflow which contains subworkflows upon subworkflows managed through the Strategy pattern, the time given to me simply did not allow for it, thus I improvised.

I coupled (loosely) the LoginUserControl and MainMenuUserControl in a symbiotic relationship.  The login will take the user to the main menu on a successful login and the main menu will load the login user control if a logout is requested.  I was even able to move our splash screen from the login form into the main form.  Remember the main menu acted (and still does) as a launch pad for the rest of the application.  In a properly developed application Point C should be launched from Point B. But Point C should NOT care about Point B was reached.  This is the very essence of decoupling and orthagonality.

The end result is a much cleaner, straightforward, and simpler application; which is what a refactor should always yield.  With a single form managing the entire application, the chances of the bug rearing  its head again are nearly non-existent (never say never, especially with computers).  There are still some pending issues with the application which I will not be able to get to ahead of my departure back to Michigan on Friday, however, I can rest easy knowing that I went from knowing nothing about developing with Compact Framework to developing a solid stable application that will help to improve the clients productivity; and lets not forget the lessons that I have learned that will me with future development projects.

Hierarchical Data in SilverLight

I had an idea recently to start a new side project that would help me learn Silverlight as well as provide a valuable service to RCM, the company that I am currently employed with.  My goal was to use to provide a clean interface to allow managers to get at certain bits of information needed to make decisions.

One of the requirements was the ability to display employees by office, thus I decided I would see how the scenario of binding Hierarchial data would be done using XAML; also I wanted to start building my experience with MVVm.

Note: As of SilverLight 4 the TreeView is now a standard component

Here is the basic XAML for the Treeview on my XAML page:

image

Side Note: Great resource for understanding many of the components in SilverLight 4:
Silverlight Toolkit Samples

Notice that we can easily create a static structure using TreeViewItem tags:

image

image

Of course, honestly, this isnt very useful and you can figure this much out about the control just by looking.  Lets talk about data binding, which is one of the forte’s for XAML.

The first thing to understand is that you will not be using the typical ItemTemplate for this because it does not handle hierarchial data, instead we use the HierarchicalDataTemplate:

image

Now, my first thought was the HierarchialDataTemplate would take, as its ItemsSource, something which inherits IDictionary, not true, it actually requires a ObservableCollection.  Before we get to addressing that lets flush out the data we are going to use for this exercise: a collection of sports teams from various leagues.

image

Finally, I developed the following method to do the grouping in anticipation that this would be the construct that would be used to display the data:

image

However, as I mentioned before this needed to be converted into a listing that could be used, but I couldnt just use GetAllTeams, it had to have a particular structure. A common property, for example, to act as the display. Thus I created the HierarchyGrouping class to act as a proxy for groupings. The structure is general enough:

image

This is the necessary structure for the HierarchialDataTemplate to operate properly. Because of this conversion the MVVm pattern makes excellent sense as a means to facilitate the conversion, here is the TestPageViewModel class:

image

What we are doing here is defining a common property with a private setter so that the ViewModel class sets it.  Ideally, we would like this to be an ObservableCollection, but in this case we are not going to be changing this list.

With our list set, it is time to  update our XAML to bind the source:

image

I have added three things to this code that are of note:

  • ItemsSource on sdk:TreeView – this indicates the property off the ViewModel that will server as the source of items for the control as a whole.  Note that this corresponds to our GroupListing property that we defined on the TestPageViewModel – this is not a coincidence.
  • ItemsSource on the HierarchicalDataTemplate – This is where things start to get a bit curious, notice that I am specifying the property on the HierarchyGrouping which contains the sub-elements relating to the parent.  This is actually very flexible, because the possibility of n levels is easily supported.
  • TextBlock – this really confused me at first. This is how  you want to display each item in the list, because every thing is considered to be of the same; hence the same type being used to hold the values.  Here I am simply binding to our Display property, which is ubiquitously available.

Now, I can already guess the next question on your mind, “but I want to style the parent nodes differently then the leaf nodes!”  The answer to this question is using a Nested HierarchicalDataTemplate.  You can get information for that here: http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html

Now lets finish up, we just need to add a bit of code and witness how MVVm ensure we write less code and leverage the power of the XAML interpreter.  This small bit of code will take care of all the binding to be done:

   1: var viewModel = new TestPageViewModel();

   2: viewModel.SetGroupByLeague(Team.GetTeamsGroupedByLeague());

   3:  

   4: treeView.DataContext = viewModel;

This is the essence of MVVm; as every control in XAML has a DataContext property we can leverage this fact and set the ViewModel as the DataContext of the page and all of our binding is handled by the XAML. Combine this with Observable properties and the ease of writing interactive code skyrockets.  So if your application is working you should see something like this:

image

Understanding Web References with Compact Framework

One of the core requirements of the client application for the client I am working with is it must communication with a web service. To do this, we created an interface that was implemented in two different ways: a local mocked data source and a remote data source which housed our web service calls.  The mock was little more then a way to mock out local scenarios locally as this application could not be properly debugged when it ran with the remote data source.

This application is built on the Windows Mobile 6 framework and its original concept was conceived by a consultant the client brought in. We decided to utilize WCF for the web service, this would come at a price, however, compact framework only supports basicHttpBinding; which is the slowest and least efficient of all the bindings available in WCF.

To create the proxy classes, we used a tool called NetCfSvcUtil which is proved by the Power Toys for .NET Compact Framework 3.5, this is also what our consultant, with many years of experience in mobile development said we had to use after doing his research.  Thus we did it; it was a very manual process requiring a command line tool to generate the classes and then a copy/paste into a file that at its peak was 14k+ lines of code.  The consultant was removed from the team after his performance proved less then satisfactory.

I took over the project and had to first had to create a solid architecture to prevent the application from crashing randomly and without warning.  Once this was complete we evolved and flushed the application out into what it is now.  A special thanks to Microsoft for their help and guidance in getting the concept into my head of why the problem was occurring.

A second problem cropped up with respect to performance.  Despite what we did we could not get calls to take less then 4s.  After some timing research we determine that our server side methods were running in under .5s in the extreme high case (yes our machines on the back are insane).  Thus we dove into why this might be.  Spent 5 days in and out of the proxy code and scouring the Internet; I will say that after developing this application it is not surprising why Microsoft is getting killed by RIM, Apple, and Google in the mobile device market, really hoping 7 proves to be better.  But there are so few solid examples online of using WinMo 6.

Anyway, thankfully, our client has Premier support with Microsoft, so we decided to throw this issue to them.  Instantly we were told that our previous consultant had made a gross error; I also fault myself for not also doing my own investigation.  Microsoft had us use Web References and instantly we saw a 50% speed improvement.  Apparently doing this, despite the fact that you are effectively doing ASMX over WCF, is that it allows the proxy code to take certain shortcuts that allow it to operate faster.  Still cant use anything besides basicHttpBinding, but Ill take whatever I can get.

So bottom line: do NOT use the Net CF power toys, they arent supported by Microsoft and does not generate efficient code.  When speed is an necessity, use Web References (though 7 does support Service References).