Friday, January 6, 2012

Viewing application/x-chess-pgn MIME type

Starting with v3.1, Chess for Android registers itself as a viewer for the application/x-chess-pgn MIME type. This implies that when other Android applications request viewing a game in PGN format, Chess for Android, or any other chess program that supports this type, will open the game.

I am new to this mechanism, but the following two ways of viewing a game seem to work well. To request viewing a game in PGN format that resides in a file (with proper permissions, so that other applications can read it), simply use the following code sequence:


Intent intent = new Intent(Intent.ACTION_VIEW);

File file = ... file in pgn format ....;
intent.setDataAndType(Uri.fromFile(file), "application/x-chess-pgn");
startActivity(intent);

Alternatively, to view a game in PGN format that resides in a String, use the following code sequence (in this case, avoid confusing characters such as ':' in the String format):



Intent intent = new Intent(Intent.ACTION_VIEW);
String data = ".... game in pgn format ....";
intent.setDataAndType(Uri.parse(data), "application/x-chess-pgn");

startActivity(intent);

Although of course one could also set up an intent that invokes one particular chess application such as Chess for Android directly, this late-binding approach is more flexible since it eventually gives the user a choice what program capable of viewing PGN should be used.

Any input or suggestions for improvement welcome.