Beem is Now Open-Source

I am proud to announce today, that one of my largest Windows Phone projects – Beem, is now open-source, in an effort to improve the product and to facilitate community contributions (there were many requests to add features – now devs can easily chime in).

Beem Plus

You can find the project, as well as any additional details on GitHub.

New Week – New FileExplorer Release

To continue the tradition of a weekly FileExplorer build, here is the next update, bringing you the following capabilities and fixes:

Show the path

You can actually see the current folder tree path in the control when navigating through. You can also select and copy the path (but not modify it at this point).

Use file format restrictions

As you already know, when accessing the Windows Phone external storage (if any is present, of course), you are only limited to seeing the files that have been explicitly associated with your application.

However, in a lot of cases you might want to restrict the visible file range even further. For those scenarios, the FileExplorer control now carries two new properties:

  • ExtensionRestrictions – an enum value that can be one of the following: None, InheritManifest, Custom. When None is selected, you will see all files when working with the isolated storage, and only files that have registered file extensions in external storage. InheritManifest, on the other hand, might be handy if you want to select a file from the isolated storage and limit the picker to files that are “pre-approved” in the manifest – obviously, this setting is unnecessary when handling external storage, since the policy is enforced by the OS by default. Last but not least, Custom allows you to define a set of your own filters. And more about that below.

  • Extensions – a generic collection that contains a set of extensions that you want to restrict the picker to. It is only used when the ExtensionRestriction property is set to Custom. You need to preserve all extensions in the .[EXT] format. So if, let’s say, I want to let the user pick only XPS files, I need to add a .xps entry to the collection. This property will not allow you to select files from external storage with extensions that have not been registered in the manifest.

As usual, you can download the latest (79607) checkin and go to Experimental > FileExplorerExperimental to see what this control is about.

I would love to hear your feedback and comments.

Testing the Future

…control that will be integrated in the Coding4Fun ToolkitFileExplorer!

You can download the Coding4Fun Toolkit source code here. Once downloaded, go to Experimental > FileExplorer. The sample project carries an alpha implementation of the control, and I would love to get your feedback on it – let me know what you want to see become a part of it.

NOTE: This custom control currently works only on Windows Phone 8.

wp_ss_20130625_0001

The control will play the role of the standard Windows 8 FilePicker, but on Windows Phone. Its goal is to allow developers to provide an easy-to-use interface to interact with the isolated storage, as well as with the external storage in the context of native phone applications.

What can the control do at this point?

  • 1. Pick a file from the isolated storage (no file format restrictions)
  • 2. Pick a file from the external storage, if such is available (restricted to files that have an extension registered with the app)
  • 3. Navigate through the folder tree in both the isolated storage and external storage (where available)

 

In its current implementation, I am using the control as a way to open files for read. No built-in functionality is introduced to facilitate writing at this point.

What do I have planned for the control?

  • 1. File extension filter for files in the isolated storage (this can be both manifest-based and individual)
  • 2. Multi-file select (return a batch of files from one or different locations)
  • 3. Folder select
  • 4. Windows Phone 7.x support for isolated storage

 

Important disclaimer

The control is in its alpha stage. DO NOT use it in production.

Last.fm API for a Windows Phone App – Auth

As per the request of many Beem users, I am implementing Last.fm track scrobbling. The first part of this task is to implement an API client for the Last.fm web service, and step one is user authentication. Last.fm is not using OAuth, but rather its own implementation of an authentication engine that relies on a composite MD5 secret.

But let’s begin with the basics. As it is a mobile application, I need to perform a request to the core endpoint with the auth.getMobileSession method. The URL is https://ws.audioscrobbler.com/2.0/. Remember to use HTTPS, as it is an inherent requirement for the request to be successful. Two required components of the request are the API key and the API secret – both can be obtained as you register your own application on the Last.fm developer portal.

NOTE: Ignore the is+[space] part and just use the code that comes afterwards.

The API call is complete only when it is accompanied by a signature. The signature is generated by building a composite string, made of each parameter and value concatenated together (with no delimiters) in alphabetical order, followed by the API secret, that are later hashed with an MD5 helper. Since by default the Windows Phone 7.1 SDK comes without the standard .NET MD5CryptoServiceProvider, I have to carry an internal implementation. You could take a look at the specifics of the MD5 algorithm here, or you could download a ready-to-go class created by Reid Borsuk and Jenny Zheng here (which is what I am using in the app). The method I am using to get the signature looks like this:

 public string GetSignature(Dictionary<string, string> parameters)
{
string result = string.Empty;

IOrderedEnumerable<KeyValuePair<string, string>> data = parameters.OrderBy(x=>x.Key);

foreach (var s in data)
{
result += s.Key + s.Value;
}

result += SECRET;
result = MD5Core.GetHashString(Encoding.UTF8.GetBytes(result));

return result;
}

The next step is to perform the authentication request itself, to get the session key. A raw implementation of the necessary method can look like this:

 public void GetMobileSession(string userName, string password, Action<string> onCompletion)
{
var parameters = new Dictionary<string, string>();
parameters.Add("username", userName);
parameters.Add("password", password);
parameters.Add("method", "auth.getMobileSession");
parameters.Add("api_key", API_KEY);

string signature = GetSignature(parameters);

string comboUrl = string.Concat(CORE_URL, "?method=auth.getMobileSession", "&api_key=", API_KEY,
"&username=", userName, "&password=", password, "&api_sig=", signature);

var client = new WebClient();
client.UploadStringAsync(new Uri(comboUrl),string.Empty);
client.UploadStringCompleted += (s, e) =>
{
try
{
onCompletion(e.Result);
}
catch (WebException ex)
{
HttpWebResponse response = (HttpWebResponse)ex.Response;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Debug.WriteLine(reader.ReadToEnd());
}
}
};
}

As the signature is obtained, I still need to include the parameters in the URL, including the method, API key and the proper credentials. The request has to be a POST one, therefore I am using UploadStringAsync instead of DownloadStringAsync, which will execute a GET request.

Simple as that, you have the auth session key.

Last.fm API for a Windows Phone App – Scrobbling a Track

As I discussed the basic of authentication in my previous post, the most important Last.fm feature that is added to Beem in itself is track scrobbling, which will allow you to keep records of what you listened to from your favorite music aggregation service. The implementation of the method used to send the track from the app to Last.fm is extremely similar to GetMobileSession.


public void ScrobbleTrack(string artist, string track, string sessionKey,
Action<string> onCompletion)
{
string currentTimestamp = DateHelper.GetUnixTimestamp();

var parameters = new Dictionary<string, string>();
parameters.Add("artist[0]", artist);
parameters.Add("track[0]", track);
parameters.Add("timestamp[0]", currentTimestamp);
parameters.Add("method", "track.scrobble");
parameters.Add("api_key", API_KEY);
parameters.Add("sk", sessionKey);

string signature = GetSignature(parameters);

string comboUrl = string.Concat(CORE_URL, "?method=track.scrobble", "&api_key=", API_KEY,
"&artist[0]=", artist, "&track[0]=", track, "&sk=", sessionKey,
"&timestamp[0]=", currentTimestamp,
"&api_sig=", signature);

var client = new WebClient();
client.UploadStringAsync(new Uri(comboUrl), string.Empty);
client.UploadStringCompleted += (s, e) =>
{
try
{
onCompletion(e.Result);
}
catch (WebException ex)
{
HttpWebResponse response = (HttpWebResponse)ex.Response;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Debug.WriteLine(reader.ReadToEnd());
}
}
};
}

The new required parameters here are the artist name, the track name, a UNIX-style timestamp and the session key that you obtained from the core authentication method. Although there is no method in C# to give you the UNIX timestamp right away, you can easily do it like this:

 using System;

namespace Beem.Utility
{
public static class DateHelper
{
public static string GetUnixTimestamp()
{
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
return ((int)t.TotalSeconds).ToString();
}
}
}

Also notice that the parameters for the track are sent in array format. Since I am only scrobbling one track at a time, I can use the index zero [0]. Your situation might be different. ScrobbleTrack can be invoked like this:

 LastFmClient client = new LastFmClient();
client.ScrobbleTrack("Armin van Buuren", "In and Out Of Love", "SESSION_KEY",
(s) =>
{
Debug.WriteLine("Success!");
});

You should now see the track registered on Last.fm.