Showing posts with label health. Show all posts
Showing posts with label health. Show all posts

Windows 8 alarm app to make you Get Up and Move like a Pro

This is a lengthy post for something I think is really worthwhile. If you’re into code exploration read on…

Last week I got frustrated while looking for a simple app that could alert me from time to time that I should get up and move/take a break instead of sitting during work hours for more than 1 | 2 | 3 | 4 | ? hours. You know…software people tend to pass maybe 1/4 of their lives stationary in a chair. That’s too much time if we think about it.

Here’s my tweet:

Yes, Mac OS has an option to announce the time on Date & Time preferences (Clock tab). A robot voice can speak the time on the quarter hour, on the half hour and on the hour but it’s very limiting (lack configuration options). It wasn’t what I was looking for…

I then tried Mac OS Calendar app with its built-in alerts but it’s not the right tool for the job.

So I thought, let me go to Windows 8 side. I use Parallels Desktop 9 for Mac and so both operating systems live side by side in perfect harmony. With a Window 8 Parallels Virtual Machine I can switch from Mac OS Mavericks to Windows 8 and vice-versa with a 4 finger swipe gesture in the MacBook magic trackpad. This is real good. If you don’t know Parallels Desktop for Mac I highly recommend you go take a look at it.

On the Windows 8 side I just stumbled on the Alarms metro app and I really liked it.

Nice looking/modern and to the point Windows 8 Alarms metro appFigure 1 - Nice looking/modern and to the point Windows 8 Alarms metro app

It has a pretty beautiful and well thought/modern UI and the Win 8 “toast” alert notification is really cool and guess what: the notifications raised in Windows also show as Mac OS Notifications1 thanks to Parallels for Mac integration. Besides that, the notifications are shown just while you’re working/while the PC is awake. This is really what I was looking for…

Using the Alarms app you can configure the alarm repeat frequency and select a sound (the available sounds really nice too). One downside is that as of now (Windows 8.1) it doesn’t have an option to set an hourly alarm. I suggested it to the Win team. Lacking the hourly option I started creating alarms like crazy let’s say from 30 in 30 minutes just to find another limitation: the Alarms app allows only 20 alarms. I tried to cheat it looking at the Alarms.json file used to persist the alarms data. The file is saved here:

C:\Users\Leniel\AppData\Local\Packages\Microsoft.WindowsAlarms_8wekyb3d8bbwe\
LocalState\Alarms\Alarms.json

but adding a new alarm by hand in this JSON file had no effect when reopening the Alarms app. I think the app code limits the alarms when parsing this file.

Of course I could set the alarms with a minor frequency like from 1 to 1 hour (what I did and that works beautifully). As a coder and explorer I started looking for some code that would allow me to overcome the manual alarms creation/configuration and the 20 alarms limit – read Automate the whole process.

I found Alarm toast notifications sample on Windows Dev Center. With the code in hands I could play with it and understand how Windows 8 does those pretty looking “toast” notifications. This was also a great opportunity to learn a little bit about Win 8 app development since I had never played with it. I’m more a web guy doing lots of ASP.NET webstack of love development these days.

I got the .zip file with C# code and fired our dear Visual Studio 2013. I was prompted to get a developer certificate and then the solution loaded without a glitch and compiled just fine. Went through the code and what I can say is that: win development (WPF/XAML) and desktop dev in general is really not as cool as doing web ASP.NET dev – this is just my humble opinion though. Lots and lots of code just to manage guess what WINDOWS. XAML designer is a heavy weight guy and takes seconds to load even on a powerful machine. Despite this, the code is easy to understand if you’ve done something as Win Forms dev.

The sample app has basically a window with 2 buttons. When clicking the first button it dispatches a notification toast to Win 8. The are plenty of options regarding the toast alert notification but I’m only interested in simulating the click of the Default Snooze button.

Figure 2 - Alarm Notifications C# Sample app main windowFigure 2 - Alarm Notifications C# Sample app main window

I started with this idea: I want to get this app notification fired with Windows Task Scheduler. Task Scheduler is such a powerful tool in Windows and allows for a great range of configuration.

To that end I created a Task that calls the win 8 sample app from time to time, say from 1 in 1 hour (just like my original alarms idea) or from 50 in 50 minutes or any interval desired.

First attempt to see if/how this could work was: right-clicked the AlarmNotifications project in Visual Studio and selected Store => Create App Packages and then Visual Studio built a local package that got saved to

C:\temp\Alarm toast notifications sample\C#\AppPackages

I promptly installed it running the PowerShell script provided - it’s called Add-AppDevPackage.ps1.

Figure 3 - Installing the App package with PowerShell scriptFigure 3 - Installing the App package with PowerShell script

After installation the app called Alarm Notifications C# could be opened in Win 8 Apps. Worked perfectly.

Note: One thing I’ve learnt is that there’s no need to package and install the app manually. You can just deploy it using Visual Studio’s green/play button to Run in Local Machine. This is way better because doing it from inside Visual Studio you can change the app code and redeploy immediately with the click of that button.

Now the question is how to open this app using Task Scheduler? This is a good question!

First blocker: Win 8 metro apps cannot be opened directly. Win 8 apps are installed in this folder:

C:\Program Files\WindowsApps

I then opened the sample app’s folder

Microsoft.SDKSamples.AlarmNotification.CS_1.0.0.1_neutral__8wekyb3d8bbwe

and looked for the AlarmNotifications.exe thinking I could use this simple command

Start AlarmNotifications.exe

as the Start Program action in Task Scheduler task.

If you double-click AlarmNotifications.exe directly you’ll get this error message:

This application can only run in the context of an app container.

Oh oh… this is bad. Sure. So how can the app be opened with Task Scheduler help? Aha… 2nd good question. Googling for that and there’s a way around it called Protocol Activation. Basically you register a protocol in the project’s Package.appxmanifest file in Visual Studio like this:

Figure 4 - Defining the app Protocol that will be used as the Start Program argument in Task SchedulerFigure 4 - Defining the app Protocol that will be used as the Start Program argument in Task Scheduler

The protocol I registered for the sample app was toast. Doing this now I’m able to launch the app using Task Scheduler with this command:

Explorer toast:

This is cool man! We just overcame Win 8 metro apps .exe limitation.

Now that a protocol was defined for the app, a small piece of code is necessary and goes into the App.xaml.cs code behind:

protected override void OnActivated(IActivatedEventArgs args)
{
    if(args.Kind == ActivationKind.Protocol)
    {
        ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;
                
        var rootFrame = new Frame();
        rootFrame.Navigate(typeof(MainPage), args);
                
        Window.Current.Content = rootFrame;
    }

    Window.Current.Activate();
}

This code watches over how the app was activated… in this specific case it checks for the Protocol activation method.

With this in place we can pass the ball to Task Scheduler.

Here’s the Task Scheduler task action:

Figure 5 - Task Scheduler Task’s Action explorer toast:Figure 5 - Task Scheduler Task’s Action explorer toast:

The trigger:

Figure 6 - Task Scheduler Task’s Trigger At Startup and repeat indefinitely every 1 minuteFigure 6 - Task Scheduler Task’s Trigger At Startup and repeat indefinitely every 1 minute

Note that for testing I set the task to repeat every 1 minute. This is the part you should change let’s say from 1 to 1 hour.

I exported this task’s XML and put it in this repo at GitHub. If you wish you can import this task in your Windows Task Scheduler.

You can play with the task’s schedule the way you want. There are plenty of configuration options available.

OK. Now the foundation is laid but there’s yet one blocker to overcome. The sample app has a window called MainPage and this window has 2 buttons as seen in Figure 2. The alarm notification will only be triggered when clicking the Default Snooze button in that window. So no matter if Task Scheduler opens the app, the alarm notification will never appear. What I’m doing is an automated process and so somehow that button must be clicked in the code. What to do in this case? It’s simple: simulate the button click programmatically. Let me Google for it. The answer is here:

Windows 8 Button click programmatically in C# and XAML

The next thing to do is to find in the sample app’s code where that code should go. I put it in the Toast.xaml.cs code behind file in the ToastScenario constructor:

public ToastScenario()
{
    this.InitializeComponent();

    // Let's call the XAML button click programmatically
    ButtonAutomationPeer peer = new ButtonAutomationPeer(DefaultSnooze);
           
    IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
    invokeProv.Invoke();
}

This did the trick!

I also changed the click even handler a little bit:

private void SendToast_Click(object sender, RoutedEventArgs e)
{
    Button b = sender as Button;

    if(b != null)
    {
        // Get some data from the button the user clicked on.
        string toastTemplate = b.Name;
        string alarmName = "";

        if(toastTemplate.Contains("Custom"))
        {
            alarmName = "This will make you fell better. Snooze me if you want...";
        }
        else
        {
            alarmName = "This will make you fell better. Snooze me if you want...";
        }

        // Create the toast content by direct string manipulation.
        // See the Toasts SDK Sample for other ways of generating toasts.
        string toastXmlString =
            "<toast duration=\"long\">\n" +
                "<visual>\n" +
                    "<binding template=\"ToastText02\">\n" +
                        "<text id=\"1\">Get Up and Move coder!</text>\n" +
                        "<text id=\"2\">" + alarmName + "</text>\n" +
                    "</binding>\n" +
                "</visual>\n" +
                "<commands scenario=\"alarm\">\n" +
                    "<command id=\"snooze\"/>\n" +
                    "<command id=\"dismiss\"/>\n" +
                "</commands>\n" +
                "<audio src=\"ms-winsoundevent:Notification.Looping.Alarm2\" loop=\"true\" />\n" +
            "</toast>\n";

        // Display the generated XML for demonstration purposes.
        rootPage.NotifyUser(toastXmlString, NotifyType.StatusMessage);

        // Create an XML document from the XML.
        var toastXML = new XmlDocument();
        toastXML.LoadXml(toastXmlString);

        // Create the toast and attach event listeners
        ToastNotification toast = new ToastNotification(toastXML);
        //toast.Activated += ToastActivated;
        //toast.Dismissed += ToastDismissed;
        //toast.Failed += ToastFailed;

        // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
        ToastNotificationManager.CreateToastNotifier().Show(toast);

        // Exit the app after dispatching the toast
        Application.Current.Exit();
    }
}

It dispatches the notification immediately and closes the Application after that.

Once the toast notification appears one must take action: click the small X icon or click dismiss to make the notification go away. Of course after doing this you should actually Get Up and Move/exercise for some time. Why not go get some water or even coffee? Mug

Figure 7 - Customized Windows 8 Toast NotificationFigure 7 - Customized Windows 8 Toast Notification

Here’s a YouTube video where I demo the app running with the help of Task Scheduler:

You can see the toast notification appear right after I run the Task Scheduler task. See how beautiful it is an how catching is the sound. By the way, the sound can be changed too editing the <audio> tag in the toast XML template in Toast.xaml.cs:

string toastXmlString =
    "<toast duration=\"long\">\n" +
        "<visual>\n" +
            "<binding template=\"ToastText02\">\n" +
                "<text id=\"1\">Get Up and Move coder!</text>\n" +
                "<text id=\"2\">" + alarmName + "</text>\n" +
            "</binding>\n" +
        "</visual>\n" +
        "<commands scenario=\"alarm\">\n" +
            "<command id=\"snooze\"/>\n" +
            "<command id=\"dismiss\"/>\n" +
        "</commands>\n" +
        "<audio src=\"ms-winsoundevent:Notification.Looping.Alarm2\" loop=\"true\" />\n" +
    "</toast>\n";

One interesting thing is that If the toast is not closed/dismissed the sound gets louder and louder…

Let’s get up and move because back pain is back pain and sitting all day long can put you in trouble.

Hope you liked this code exploration.

1 to get the alert notification on the Mac OS side, the modified sample app must be opened and present in the Mac OS dock; otherwise the notification can only be seen in the Windows side. This is a Parallels for Mac limitation and I hope in the future they also make it possible to show notifications from windows metro apps that are running in the background. The nice thing is that the alarm sound still plays while on the Mac OS side.

Get the codez
The modified source code is available at this GitHub repo:

https://github.com/leniel/Windows8Toast

References
Alarm toast notifications sample at Windows Dev Center

Toast notification overview (Windows Runtime apps)

31 DAYS OF WINDOWS 8 | DAY #10: TOAST NOTIFICATIONS

Windows 8.1 Preview and ‘Alarm’/’Call’ Applications

Why is it so important to Move Your Body?

Permit me to go a little bit "off topic" with this post… is it really off topic to a computer professional?

I know this is kinda of cliché and that it may scares you, but physical activity is a human necessity and as such it deserves special attention. So here I am writing about it hoping that someone that reads this takes notice and start acting right away. Do not leave for tomorrow what you can do today! Yet another cliché I know but it is perfect in this phrase.

People of my "professional class" and also from my generation also known as generation Y are the ones most impacted by the lack of physical exercises in this modern age of ours. In our age we are completely immersed in technology with products that make us want to avoid the outsides. Just to name a few: computers, internet, video games, delivery services, etc.

One thing that I as a computer engineer "have done" in the past 7 to 8 years was not moving my body to the extent needed.

When I was a kid ( 6 to 11 years old ) I used to play soccer almost 7 days a week. I lived near a soccer field. Then I moved to Volta Redonda where I live now and it ceased to happen.

In Volta Redonda I used to go to school every day cycling, that was about 6 KM (this route in Google Maps) each day. I used to play soccer with my street friends in front of my house (this green field seen in Google Street View) at least once a week, but as we all grew up we just did not keep contact and then the soccer games disappeared. I also used to go to Águas Quentes Country Club Resort "near" my house that has a lot of pools to practice swimming and that is now just past as well. I am not a member of the club anymore.

People that work with computers pass a lot of time fixed in a single place (programmers do it more frequently) as is my case and this tends to cause bad consequences to the body. Lots and lots of articles and TV programs debate this theme and lots and lots of people just do not give the necessary attention to this subject.

I am writing about this because I think it is crucial to our lives, that is, we were made to be in constant movement and it makes sense to expose my point of view since I am an avid user of computers and everything in between. Maybe this post will even serve as a start point to someone studying the impact of modern life in human health. What does a generation Y guy think about the subject? So here it is…

I definitely started to pay attention to this only after almost 2 years working at Chemtech. The work rhythm is intense and for someone that is starting in the job market, exercising can be a point of no interest and if you do not wake up to reality things can get worrisome. We had gymnastics at the workplace but that was not really something that I would call exercising, you know. While at Chemtech, on the weekends I was always inclined to rest since I was tired of working the whole week.

Move Your BodyOK. What have I found? We need a way of moving our body, working out for at least 30 minutes a day. You can do the kind of exercise that is a best fit to you, but the thing is: do something to move your body. Do not tie yourself to the same routine each and every day - car/bus to workplace, seat, a little walking to have lunch, in a sitting position again and then car/bus to home.

Since I started college in 2003 my exercising level downgraded to unacceptable levels. To correct this, one thing that I started doing lately was going to the gym to stretch my muscles – 5 days a week – 1/1.5 hours a day.

I am in the 3rd month mark now and I am really satisfied with the results because my overall mood got better and now I know I am contributing to my body and health. Body, Mind and Soul must be in synchrony.

The benefits of physical exercising are numerous and if you want to know more about them, refer to this article.

If you can not go to the gym: go walking/cycling to your workplace, do stair climbing (avoid elevators), but definitely do something that puts your body in movement. Start slowly and then increase the pace. You will see that as times goes by it starts to be a part of your day and the day you do not exercise you feel something is wrong/missing. Your body will ask for it. Amazing, isn't it? This is only a part of what we need to keep a healthy life. Of course, what you eat will have great impact in your health. Exercising alone will not be sufficient. Watch out for your way of life.

I think the point is: we all know that these are difficult days and that arranging all this stuff seems daunting enough. We already have a bunch of things to do but what if we do not keep close attention to our body health? We will not have anything else to be preoccupied with because maybe we will not be able to do that bunch of things again without a capable body. Did you get the point? I hope so.

An advice from an intensive computer user to everyone out there that thinks that exercises are not important: exercises do make a difference in life even more if you are a computer professional that depends on computers to get things done. It is better to start early and to keep it a constant in your life. You will save yourself and your money from doctors, medicines, hospitals, etc., that is, while it is not a guarantee it is most probably that you will save yourself from suffering. You will get a better quality of life having fun along the way.

Shake Yourself! I am linking to a Yes song titled Owner of a Lonely Heart that passes the message.

Updated on 12/30/2013

I’ve quit the gym because after 2 to 3 years there things started to get boring. I prefer the outdoors and cycling is my favorite exercise. The feeling I get while pedaling is inexplicable.

StravaIf you happen to own a mobile device, there’s a great app to keep track of your activity be it running or bike riding. It’s called Strava.

Here’s my latest activity:

Complementary reading

Sitting is Making You Fat and Killing You by Phil Haack