JAJAH Development Blog

Blogs by JAJAH Developers

Archive for the ‘Windows Mobile’ Category

Sunday, June 1st, 2008

I’m sure all of us developers has stories where they submitted code, after carefully debugging and testing it, only to get 5 minutes later rejects from QA and you’re clueless for the reason. This is one of those stories.

We’re developing a VoIP client. One of the features is letting the user know if she missed calls while the client was off-line. In order to do this, we ask for the mobile’s time. Problem is, mobile devices has a tendency for not having an accurate time. It might be because mobile users adjusted the time but keep using manufacturer default time zone. It can also happen that you forgot to adjust the time properly while traveling abroad. In some cases, your fancy device might just decide to reset itself for no apparent reason. At any case, chances are that the device’s time is not the same as your server. This is obviously true when dealing with global service provider like JAJAH that supplies services to users around the world.

Back to the the client. We decided to send the local time of the device over a Web-Service and have the back-end server send back missed calls and adjust time of call with the client’s local time. All went nice and dandy on our development environment. Results came back as they should have, edge cases were also tested and worked flawlessly. Then, we gave the client to our QA team.  After a while, they came back telling that there’re inconsistencies with the missed call features. So frustrating!

I won’t bother you will all the details. Eventually, it turned out that DateTime is a wise-guy object. It has a kind property which affects the result of WS de-serialization. The local device-time we sent from the client was de-serialized to UTC time on the server side. So, everything worked fine as long as the device and the development environment were both on the same time zone (GMT +2). Once we moved to an environment in New-York (GMT -8), the server thoughts we’re looking for missed call that are due in 10 hours. Indeed, quite a paradox.

In order to fix the issue, and make sure that the DateTime objects is not converted to a UTC format, simply make sure use the unspecified kind.  For example:

DateTime myDateTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
This post is dedicated to Boaz for his great help in solving this problem.

Thursday, April 17th, 2008

In a previous post, I wrote about how to run a method when the device wakes up, or comes back from sleep mode. In that post I wrote about calling the CeRunAppAtEvent native function, which signals a given  event (hEvent) when the device wakes up.  I used the WaitForSingleObject(hEvent, INFINITE) function to wait until the hEvent is signaled.

The problem occurs when wanting to close the application, and a thread, which called the WaitForSingleObject()  is still waiting for the event. In that post, I wrote that in case wanting to close the application the below function should be called in order to terminate the waiting thread. 

public static void Close()
{
    _close = true;
    if(_wakeupServiceThread != null)
       _wakeupServiceThread.Abort();
}

Unfortunately, The wakeupServiceThread could not be aborted, since it was waiting due to the call to WaitForSingleObject function.

The solution was to stop waiting on the WaitForSingleObject function,  and for that the _nativeWakupEvent event needed to be signaled.

Here is the code:

[DllImport("coredll.dll", SetLastError = true)]
        public static extern bool EventModify(IntPtr hEvent,
            [In, MarshalAs(UnmanagedType.U4)] int dEvent);
private const int NATIVE_EVENT_SET = 3;

public static void Close()
{
     _close = true;
     if (_hEvent != IntPtr.Zero)
             SetEvent(_hEvent);
}
private static bool SetEvent(IntPtr hEvent)
{
     return EventModify(hEvent, NATIVE_EVENT_SET);
}

Wednesday, April 9th, 2008

In a windows mobile device you can only view the running applications, and this is only if the application has a form. This means that you cannot view various processes that are running on a WM device, like you would be able to do on you PC, or Laptop using the "Task Manager’. fortunately, Visual Studio, has a solution for this problem:

Go to Visual Studio ->Remote Tools and select Remote Process Viewer

visual studio remote process viewer 

Select the device which you would like to see its processes.

image

you will get a window, with all process that are running on your Windows Mobile device. In this window you will be able to view the processes and kill them, just like you would be able to do with the regular Windows "Task Manager"  )

In addition to a list of processes, you can also see other info as threads, and from what module is the process.

image

RIA on the mobile phones and small devices

Sunday, March 30th, 2008

It is said that by 2013, 31 percent of all mobile phones will be smart phones, and by then, a smart phone might be almost like a full blown PC. In the meanwhile it seems like everyone is trying to push their feet into the blooming mobile phone market. We’ve seen a lot of ups and downs in this area, lots of promises that hasn’t been fulfilled. In fact only since the arrival of the iPhone, surfing the web become reasonable on a mobile phone.

I refer to RIA, not just as rich applications that runs inside the mobile Internet browser, but, also as reach applications that are easier and consistent to develop, that runs across platforms and more important across devices. This has been the main hurdle, beside the low performance issues, to develop for mobile phone and devices. The resources needed to port a mobile application to run on multiple devices is enormous. There got to be a better way to develop applications and games for the mobile phones, something as approachable as web development. I’ve heard lately that the "Developer is King" these days it’s more like the Web Developer is king.

I will summarize in this text the current leading technologies in the area.

 

Adobe Flash and Flash Lite

Flash Lite is the first RIA technology to run on mobile phones (since 2003), since then it has shipped pre-installed on more then . It is safe to say that, Flash runs on mobile phones, but, it is always a subset and always with some limitations. The  best thing about Flash Lite and Flash in general is that it’s easy to develop highly interactive applications, and the promise of - develop once deploy everywhere, is currently the closest to reality with it.

The Flash Lite player isn’t exactly the same as the full Flash player we use inside our desktop’s browser. The current version of the full Flash Player is v9, Flash Lite is similar in capabilities to older versions. The Flash Player is backward compatible.

Flash Lite 1.1 - Similar to Flash 4 - Simple games, screen-savers and animations.

Flash Lite 2.1 - Similar to Flash 7 - Small games and applications, much more advanced Object Oriented programming.

Flash Lite 3 - Similar to Flash 8 - Richer games, Youtube, live video and audio e.g., justin.tv, pandora.

Take a look at Flash Lite 3 in action

Pocket PC and Windows Mobile were supported by Flash and Flash Lite for a long time already. It’s been used also to enrich WM applications UI. Flash can be integrated inside a .NET Compact Framework 2.0 application to rapidly create rich UI. Microsoft recently announced they’ll support

Flash can be found, to some extent, on other type of devices, e.g., the cool Chumby runs Flash Lite 3, sony PSP runs Flash 7.

There are already some mobile devices that run the full version of Flash 9, e.g., Nokia N810

that are pre-installed with Flash-Lite 1 - 3

 

Microsoft SilverLight

Microsoft has finally realized that a development platform isn’t necessary an OS, it saw how Flash is becoming just that, and want to join (take over) the party with its SilveLight. SL is a rich environment targeted to work inside the browser and across desktop platforms as well as on mobile phones and devices.

The first SilverLight release 1.0, was mainly intended for PR. It lacks any impressive capabilities beside good support for video. With the upcoming SilverLight 2.0, which is currently released as beta 1, we’ll have the chance to really estimate it’s powers and if it can compete with Flash. SL 2.0  looks very powerful, as it supports a respectable subset of the CLR (Dot.Net runtime), rich UI framework and all kind of other goodies.

As for SilverLight on the mobile, there was a great buzz recently when Nokia announced  it’s going to support SilverLIght on it’s S60 and some S40 mobile phones. it is not clear yet what will be the limitation of the runtime on these devices only that it’ll "initially" support only SL 1.0, which means logic written in JavaScript and no CLR.

Windows Mobile will support SilverLight 1.0 as well from the middle of 2008.

SilverLight looks very promising, but it’s still a premature technology, especially for mobile devices. I wouldn’t fire my Visual Studio to develop a mobile SilverLight app, just yet.

Visitmix.com has some cool

 

Apple iPhone:

Although the iPhone isn’t exactly an ubiquitous platform and holds only 0.14 percent of the mobile phone share. It is still the most buzzfull mobile device ever. Creating your software to run on the iPhone and leveraging it’s dreamy features like the Multi-Touch and the accelerometer can lead to tons of PR and even some paying users.

The iPhone has redefined the way we interact with a mobile phone and lifted it to a all new level. Apple recently released the iPhone SDK but still keep it very restrictive. Hopefully Apple won’t repeat her history of dismissing 3rd parties, an act that we all know hurt her greatly in the past.

I’m a little tired of the iPhone and Flash affair already, this is the story in short: To the amazement of many, the iPhone was released without any kind of Flash support. The two most common assumptions were that Flash is too slow for the iPhone or that Apple is pissed over Adobe for some obscured reason. Lately Steve Jobs bashed on the Flash Player performance on the iPhone, Adobe replied and there were others. After the release of the iPhone SDK, Adobe’s CEO announced they will develop a Flash Player version for the iPhone. Hours later, Adobe clarified that it won’t be that easy to develop a decent version of the Flash player, only with the iPhone SDK and without Apple co-op. Now it seems that we won’t get Flash on the iPhone , at least until the next buzz alert.

It was a similar scenario for Sun’s Java, first they said they’re going to put Java on the iPhone only to realize later it’s not feasible under current restrictions. Don’t wait for SilverLIght to appear on the iPhone anytime soon, either.

Apple probably wants to push her own iPhone RIA platform, and insist to remain a sealed garden with a lot of restrictions to third party applications developed by their new SDK.

 

Google Android

Google has realized the need for a standardization on the mobile jungle, and came out with it’s , a platform designed to give the power to the developer.

Here is a good explanation of the Android platform and what it can do for you.

Demonstration of Android power.

Currently there’s not many Android smart-phones, but, with the dedication of google we’ll surly see some more soon.

 

Sun JavaFX Mobile

Sun, the inventor of Java and "Develop once deploy Everywhere" concept, don’t want to lose its mobile presence with j2ME and is porting Java’s richer sister, JavaFX,  to the mobile phone as well.

JavaFX Mobile

 

Trolltech QT

Trolltech the creator of QT - Cross Platform rich development platform, has been recently bought by Nokia and can be seriously considered as a player in the RIA mobile world.

 

Conclusion

For now, we can still use excuses like incompatibility and lake of an appropriate platform to continue and create dull mobile application but it’s going to change very soon.

Tuesday, March 25th, 2008

Using a Native DLL (c++) in a managed (c#) project is  straightforward. you just write the following in your managed code:

[DllImport("coredll.dll")]
public static extern bool CeRunAppAtEvent(string AppName, int Event);

and use the native function like it was managed.

The problem happens when you want to write your own native dll. It seems that the function in the native code must look like this

extern "C" __declspec(dllexport) void function()

confusing a bit, since when not programming for  Windows Mobile you may also write your native function without extern "C"  and add the EntryPoint attribute, and it will work… (for Windows Mobile this is not the case, you have to make sure you write extern "C") 

Wednesday, March 19th, 2008

Its very surprising that there is no wakeup event or member in the managed SystemState class that indicates that the device has returned from sleep mode.

Fortunately, we can use the CeRunAppAtEvent function from coredll.

public static extern bool CeRunAppAtEvent(string AppName,
            int WhichEvent);

Starting an application when the device wakes up, is very straight forward,  and all you need is to pass the path of the application to run (and also remember to remove it…)

Running a method is a bit more complicated; instead of the application path you would write 

"\\\\.\\Notifications\\NamedEvents\\eventName"

Where the eventName is a name of a native event. here is the code you will need to start a wakeup event, catch it and run your method

 private const int NOTIFICATION_EVENT_WAKEUP = 11;
 private const UInt32 INFINITE = 0xFFFFFFFF;

 [DllImport("coredll.dll", SetLastError = true)]
 public static extern bool CeRunAppAtEvent(string AppName,
     int WhichEvent);

 [DllImport("coredll.dll",SetLastError = true)]
 private static extern int WaitForSingleObject(IntPtr hHandle,
     UInt32 dwMilliseconds);

 [DllImport("coredll.dll", SetLastError = true)]
 private static extern IntPtr CreateEvent(IntPtr lpEventAttributes,
         bool bManualReset, bool bInitialState, string spName);

private static General.NoParamsFunctionHandler _eventToRunOnWakeup;

 public static General.NoParamsFunctionHandler EventToRunOnWakeup
 {
     set
     {
         if (_eventToRunOnWakeup == null)// so we only run one thread
             StartOnWakeUpService();
         _eventToRunOnWakeup = value;
     }
 }

 private static void StartOnWakeUpService()
 {
     try
     {
        CeRunAppAtEvent(
        "\\\\.\\Notifications\\NamedEvents\\NativeDeviceWakeUpEvent",
             NOTIFICATION_EVENT_WAKEUP);

      _wakeupServiceThread = new Thread(new ThreadStart(WakeupProc));
      _wakeupServiceThread.Start();
     }
     catch { }
 }

 private static void WakeupProc()
 {
     IntPtr hEvent = CreateEvent(IntPtr.Zero, false, false,
          "NativeDeviceWakeUpEvent");
     while (!_close)
     {
         WaitForSingleObject(hEvent, INFINITE);
         if (_eventToRunOnWakeup != null)
             _eventToRunOnWakeup();
     }
 }

since this thread will be running in the background, we want to make sure it will stop when we are done. hence the below method

public static void Close()
{
    _close = true;
    if(_wakeupServiceThread != null)
       _wakeupServiceThread.Abort();
}

Wednesday, March 12th, 2008

I wanted to test a function’s duration (for various  cases).

I thought it to be simple and be as in . Net C#.

I wrote something like this:

DateTime start = DateTime.Now;
//function code……

DateTime end = DateTime.Now;
TimeSpan duration = end - start;

I got that the duration is 0!! I also tried using

duration.TotalMilliseconds; and DateTime.Now.Ticks;

but the ticks for the start and end were the same. this seemed very strange.

To solve the problem, instead of using  DateTime.Now I used

System.Environment.TickCount;

It seems that the time resolution for DateTime.Now depends on the operation system, and for .Net compact framework its  one Second!!!.

 

Thursday, February 28th, 2008

In this post I describe the problem when changing the screen orientation, well 2 problems and at the end the final solution for changing the screen orientation. if you just want to read about the solution, scroll down…

In order to learn how to rotate the screen orientation I watched a MSDN How To Video, which helped but didn’t give me the right solution (hence this post ) )

By the video, in order to change the screen orientation you would write this line:

SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;

This line does change the screen orientation, the problem occurs if you also want to  monitor the screen orientation change. (see How to Monitor screen orientation change) In my case I wanted my application to always be in landscape mode, meaning that in case of a screen rotation to portrait mode, I will rotate the screen back to landscape mode.

I used the SystemState change event to monitor the change and used

SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;

To change the orientation back to landscape.  The screen orientation indeed changed, but after this change the SystemState change event didn’t always fire, hence in some cases my application was displayed in portrait mode. the reason for this was that the SystemState.DisplayRotation didn’t change. problem…

I thought about using the resize event, and changing the SystemSettings.ScreenOrientation.. but I got an exception.  the reason for the exception was that when being in the Resize function, I changed the SystemSettings.ScreenOrientation, making another call to the Resize function…

I found that other developers had the same problem, and saw 2 solutions, the first is to catch the exception and display to the user a message indication that the application dose not supports this mode, and the second was to use a timer (of 200 ms) inside the Resize(), that will change the orientation. I didn’t like both solutions… so I found a third one… )

my solution:

I wanted to use the SystemState change event, since its much more efficient than the Resize event. I understood that I need to change the SystemState.DisplayRotation, but its read only…

Since the SystemState.DisplayRotation is read from the registry… I was to write the value to the registry causing it to change and hence the SystemState change event will always fire )

problem solved!

note that SystemSettings.ScreenOrientation still needs to be changed, since its the parameter that decides the display orientation.

the code:

void OrientationWatcher_Changed(object sender, ChangeEventArgs args)
{
      int newOrientation = (int)args.NewValue;
      if (condition on newOrientation)
           ChangeToLandscape();

}
public static void ChangeToLandscape()
{
      SystemSettings.ScreenOrientation = ScreenOrientation.Angle270;
      Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\GDI\ROTATION",
               "Angle", 270, RegistryValueKind.DWord);
}

Thursday, February 28th, 2008
getting screen orientation:

There are two members that indicate the screen orientation mode (Landscape or portrait)

SystemState.DisplayRotation

SystemSettings.ScreenOrientation

Note: these 2 members may not always be the same. Ill will discuss this issue in my next post

Monitoring screen orientation

When the screen orientation changes we have 2 ways to know about it:

System state change event

When the SystemState.DisplayRotation is changed, an event is fired. We can catch it and use it:

Creating the event:

private SystemState _orientationWatcher;
_orientationWatcher = new
               SystemState(SystemProperty.DisplayRotation);
_orientationWatcher.Changed += new
               ChangeEventHandler(OrientationWatcher_Changed);

Below is the function that “does something” when there is an orientation change. you should make sure to use the new orientation value from the args param and not get it from the System state since its read from the registry.

void OrientationWatcher_Changed(object sender, ChangeEventArgs args)
{
     int newOrientation = (int)args.NewValue; // new orientation
     //do what you want to do when there is a orientation change
}

resize event

You can use the control’s  resize event and check if the screens orientation was changed. you can use the SystemState or SystemSettings parameters.

The problem with this is that the resize event is called allot of times: its called around 4 times each time you display a screen, create a screen,  and resize the screen - i.e. change the screen’s orientation.

Hence I don’t recommend using it for monitoring the rotation change.

 

 

 

 

How to: Control the keyboard(SIP - Soft Input Panel) on Windows Mobile

Friday, February 1st, 2008

As you might have noticed, what I do here in this blog is write about the things I look for, than, when I can’t find them or I find too complicated and evasive answers I write simple ones here.

So, let’s get to the reason we’re all here.

I will give here an example on how you can change the visibility of the keyboard and you will probably be able to figure out the rest.

the steps are as follows:

  1. Add a reference to Microsoft.WindowsCE.Forms.dll
  2. Create our new class:
    using Microsoft.WindowsCE.Forms;
    
    namespace Environment.Keyboard
    {
        public class Keyboard
        {
  3. Create our private InputPanel member:

    private static InputPanel m_inputPanel = new InputPanel();
  4. Create the property to check if the Keyboard is open:

    public static bool KeyboardOpen
    {
        get { return m_inputPanel.Enabled; }
        set { m_inputPanel.Enabled = value; }
    }
  5. Were done!

It’s that simple. the Enabled property in the InputPanel signifies if the keyboard is currently open or not.

Check out this link for a more comprehensive article.

See you next time,

Nadav

Jajah is the VoIP player that brought you web-activated telephony.