Monday, March 07, 2011

#WP7Dev app feedback #4

Here's the feedback I recently gave to the creator of an app.

This is part of my series of app reviews.

In summary, I said it was "a lovely simple app which is well executed. It has impressive performance in terms of the speed of loading remote data."

Here's the feedback:
  • In the Marketplace description, some names are not capitalized correctly.
  • The highlight effect you have applied to the application icon is similar to the effect automatically applied to most iPhone apps. I would recommend removing this highlighting from the icon image so the app looks like it belongs on the platform.
  • I like the splash screen image but the logo it displays doesn't seem to match the branding of the rest of the app (apart from the color).
  • While I like the color you have used in the app I wonder if it's everyone's favourite. Have you considered using the phone accent color for this? It may help the user gain a greater sense of ownership of, affinity for and connection with the app.
  • Add tombstoning support to remember the selected pivot item and scroll position in that item.
  • There is no need to include the same options in both the application bar and the application menu.
  • I would recommend only including the "reload" option on the application bar. I would expect that this would be a frequently used option and having it always available will be helpful. Having it in the application menu makes it harder to access as it requires an extra tap and therefore takes slightly longer.
  • I would expect that the settings would be rarely accessed and so access to this functionality should be moved to the application menu so it doesn't distract or get pressed accidentally.
  • The convention for pivot item headers is for them to be entirely lower case. You should adjust "About" and "Settings" accordingly.
  • Add tombstoning support to remember which of the "About" or "Settings" pivot items were displayed when returning to the app. Remembering scroll position would also be good too.
  • The Settings screen has application bar items which are duplicated in the application bar menu. There is no need for this duplication.
  • On the setting screen there should be no need to have a cancel button which just closes the current page/navigates backwards. The back button on the phone does this already.
  • On the setting screen there is no need to have a Save button as the selected options are automatically remembered applied.
  • The entire application bar can therefore be entirely removed from the settings screen.
  • The about screen also has an application bar with save and cancel options but these don't relate to anything on screen.
  • On the about screen I would recommend making the email address stand out more to make it more obvious that it can be tapped to trigger sending an email. Colouring the email address with the phone accent color and increasing it's size would be my recommendation.
  • In the email created from the about screen, you may want to include the version number of the application. This will likely be helpful to you when investigating any reported error or problem.

Thursday, March 03, 2011

Still not got the pre-update? Fake it!

<justforfun>
You've sat quietly by as the rest of the world seems to be going on about the first Windows Phone 7 update (the pre-update) but have yet to have your device let you know that the update is available to you.



Well, if you want to get a feel for what it'll be like when that moment does finally come, you just need a simple bit of code:

Guide.BeginShowMessageBox("An update is available.",
                          "Updates can make your phone work better and add new features. They can make your phone more secure too.\r\n\r\nTo learn more and install this update, connect your phone to your computer.",
                          new []{"close"},
                          0,
                          MessageBoxIcon.None,
                          null,
                          null);

</justforfun>

Wednesday, March 02, 2011

WP7Clipboard - A clipboard API for #wp7dev

Background
  • Windows Phone 7 doesn't have an API for a Clipboard. (Actually, in the current version it doesn't even include a clipboard. - But, the ability to cut, copy and paste text will be added in the update coming later this month.)
  • I wrote an app, back in December 2010 (see video and please comment at http://www.wp7comp.com/scratch-pad/trackback/), that enables the cutting, copying and pasting of text and even sharing it between multiple applications. (Some people have the idea that you can't share data between apps on the device but I proved this isn't the case. Check the video or get it from the Marketplace to see/confirm that it doesn't use any unrestricted APIs or use a web service for storing/sharing the data.)
Short version
Head over to http://wp7clipboard.codeplex.com/ and get the code to see how I did this and use the library in your own applications.

Longer version
Constraints are good. They focus the mind and can provide structure where chaos can sometimes reign.
By their nature, mobile devices are very constrained. Many of these constraints are passed to (forced upon?) the applications that run on the devices.

Windows Phone 7 development has some potentially limiting constraints in this area:
  • No cut and paste support (currently)
  • No clipboard
  • No shared file system

There are 2 ways we can react to constraints:

  1. We can complain about them.
or
  1. We can work with them.

I opt for the later.
I knew there had to be a way to do this! Just because it seemed that everyone and their dog was complaining about a "lack of copy and paste" wasn't going to stop me.

"Cut and paste" is really only about copying and manipulating objects (strings in this case) in memory. Just because there isn't a UI for this doesn't mean we can't do it. We just need to build a UI.

Just because the platform doesn't provide a clipboard (functionality to store objects in persistent memory and make them available to all apps) doesn't mean we can't create the functionality ourselves. But this is where the fun begins...

All Silverlight based apps run inside a sandbox and can only create files inside "IsolatedStorage". There's no shared storage facility.

Rather than focus on what we don't have, let's think about what we can do.
Well, even in a Silverlight app we can use some of the XNA libraries.
Through the XNA APIs we can create and read images in the Pictures hub. (We can't delete them but we'll just have to live with that.)
After a little experimentation I discovered that the API will only allow us to save a valid JPEG file in to the pictures hub.
Never mind, there are ways of hiding information within pictures.
I had another idea in mind though. Due to something I worked on a long time ago, I know that the JPEG file format includes an end of file marker. I also know that almost all implementations of code to read JPEG format files ignores everything after the end of file marker. A quick check later and I verified that this is the case on WP7 too.

Yes, that's the secret, I include the data I wish to store in the "clipboard" inside a JPEG image file (the clipboard images littered through this post) after the end of file marker. Then, when I want to read from the "clipboard" I just get the most recent file from the users MediaLibrary and retrieve the extra data from the end of the file.

AFAIK only one person managed to correctly guess what I'd done.



Now you can do this in your apps too. I've encapsulated the functionality from ScratchPad into a separate library and made it open source at http://wp7clipboard.codeplex.com/
Originally this only supported text but I've started to add some functionality for processing (cutting and pasting) images too. It's still a beta but hopefuly some of you will find it useful, helpful or interesting.




A word of warning. Using this method creates files on the users phone which you can't programmtically delete. After sustained use, this will clutter up the users "saved pictures" folder and they will have to manually delete the files there. If you use this library/technique you may want to warn your users about this and give them the option to disable the functionality.

If possible I'd recommend sharing data between apps by synchronising it to a web based system. Connectivity issues aside that will probably make working with large amounts of data easier and provide backup/redundancy options.



I'd love any feedback you have on this.

Tuesday, March 01, 2011

How ScratchPad does copy and paste ... [#wp7dev]

In response to public demand I'm going to explain how I got Scratch Pad (my Windows Phone 7 app which supports cut 'n' paste and a shared clipboard API) to work.

This is the first of 2 posts on the subject. In this post I'm going to explain how I manipulate the text to create cut and paste within the app. In the next I'll show how I created a shared clipboard. And how you can add the functionality to your own apps too!

The cutting, copying and pasting of text within an app is simple really. It basically involves storing a copy of the text in question in a string variable within the code.

private string localClipboard;

To be able to select more than a single word, the code supports a "shift" mode:

private bool shiftSelected;

private int shiftSelectPositionStart;

private int shiftSelectPositionLength;

private void ShiftClick(object sender, EventArgs e)
{
    shiftSelected = !shiftSelected;

    shiftSelectPositionStart = tb.SelectionStart;
    shiftSelectPositionLength = tb.SelectionLength;
}

Which can be combined with a SelectionChanged event on the text to capture the larger string. (Being sure to consider if the new selection point is before or after the original/previous selection.)

private void SelectionChanged(object sender, RoutedEventArgs e)
{
    var currentStart = tb.SelectionStart;
    var currentLength = tb.SelectionLength;

    if (shiftSelected)
    {
        if (shiftSelectPositionStart < currentStart)
        {
            tb.SelectionStart = shiftSelectPositionStart;
            tb.SelectionLength = currentStart - shiftSelectPositionStart + currentLength;
        }
        else
        {
            tb.SelectionStart = currentStart;
            tb.SelectionLength = shiftSelectPositionStart - currentStart + shiftSelectPositionLength;
        }
    }

    shiftSelected = false;
}
Copying text is easy:
private void CopyClick(object sender, EventArgs e)
{
    this.localClipboard = tb.SelectedText;
}
But cutting text is slightly more complicated as we need to replace the original string with the text before and after the selection.
private void CutClick(object sender, EventArgs e)
{
    this.localClipboard = tb.SelectedText;

    var removeAt = tb.SelectionStart;

    var afterCut = tb.Text.Substring(removeAt + tb.SelectionLength);

    var beforeCut = tb.Text.Substring(0, removeAt);

    tb.Text = beforeCut + afterCut;

    tb.SelectionStart = beforeCut.Length;
}
Then that just leaves pasting the text back in at a newly selected location.
private void PasteClick(object sender, EventArgs e)
{
    var insertAt = tb.SelectionStart;

    tb.Text = tb.Text.Substring(0, insertAt) +
              this.localClipboard +
              tb.Text.Substring(insertAt + tb.SelectionLength);

    tb.SelectionStart = insertAt + this.localClipboard.Length;
    tb.SelectionLength = 0;
}
The above also selects the newly pasted string. Simples! Now, while you digest all that, please head on over to http://www.wp7comp.com/scratch-pad/ and leave some lovely comments - Thanks.

What hackers can teach us about what might be coming in future versions of Windows Phone

Ever wondered why so many of the "hacked" Windows Phone 7 apps show loading additional ringtones or accessing the camera directly?

Well I've had a little look at what some of the hackers have been doing and have found a few managed assemblies that aren't publicly available and it seems to be these that most of the people exploring in this area have been looking at.

So, for your amusement, interest and to satisfy your curiosity, here, in no particular order are some of those methods:

public abstract class Camera : IDisposable
{
  // ...
}

public sealed class PhotoCamera : Camera
{
  // ...
}

public sealed class VideoCamera : Camera
{
  // ...
}

public sealed class RingtoneLibrary
{
  // ...
  public void AddRingtone(Stream ringtoneSource, string ringtoneName)
  {
    // ...

    StreamHelper helper = new StreamHelper(ringtoneSource);
    try
    {
        NativeMethods.MediaApi_AddRingtoneFile(helper.GetTempFile(), ringtoneName);
    }
    finally
    {
        helper.Cleanup();
    }

    // ...
  }

  // ...
}

[DllImport("netcfmail3_7.dll")]
internal static extern byte GetMessageData(IntPtr pMessageNode, byte[] dataBuffer, int cbDataBuffer, byte[] idBuffer, int cbIdBuffer);
 
[DllImport("netcfmail3_7.dll")]
internal static extern int SendAsAttachment(string to, string subject, string messageClass, byte[] data, int cbData, string attachmentFileName);

[DllImport("netcfmail3_7.dll")]
internal static extern int SendInBody(string to, string subject, string messageClass, string body, int cchBody);

public CivicAddress ResolveAddress(GeoCoordinate coordinate)
{
  // ...
}

public static extern int HostGetAvailableFreeSpace(IntPtr pRuntimeHost, out long pAvailableSpace);

The things that jump out at me, apart from the camera access are the methods relating to emails!

My theory is that if there is managed functionality in the framework already (and its use doesn't pose a threat to the security of the users data or device) it's not unreasonable to assume that this could be made public in the future.

Just in case it bears repeating, this is all speculation and I have no special insight into what might be coming in future versions.