Port Android Application to Windows Store app

Author: Han Xia

 Public video in Channel 9 https://channel9.msdn.com/Blogs/OneCode/How-to-Port-Android-apps-to-Windows-Store-apps

About this Development Guide

If you have been developing Android applications and are interested in building your applications for Windows 8, this guide is for you.

The guide covers what you need to know to add Windows 8 development to your skill set, while leveraging what you have already learned building Android applications. After learning this guide, you will know the same and the difference between these two platforms and the best replacement when you port certain features to Windows Store applications.

Target Audience

This document is for Android application developers who are looking to develop their applications for Windows 8.

Conventions Used in this Document

Term

Abbreviation

Windows 8

Win8

Application Programming Interface

API

Integrated Development Environment 

IDE

Visual Studio 2012

VS2012

Windows Runtime

WinRT

 

1. Introducing Windows 8 Platform to Android Application Developers

Windows 8 introduces a new type of application: the Windows Store app. Windows Store apps have a brand new look and feel, run on a variety of devices, and you sell them on the Windows Store.

Microsoft provides powerful development tools, detailed documents and comprehensive samples to help designers and developers to develop Windows Store applications more quickly and easily. In this guide, we focus on how to leverage the skillset of the Android application development to build the Windows Store application. 

To learn more about the business opportunity of Windows 8, see Selling apps. For more info about the features used to build Windows Store apps, see the Windows 8 Product Guide for Developers.

 

The Developer Tools

Microsoft has released a great set of tools for Windows Store application development. The toolset includes:

  • ·         An IDE (for developers): Visual Studio 2012 (VS2012)
  • ·         A user interface design tool (for designers): Blend for Visual Studio 2012
  • ·         Libraries: Windows Runtime (WinRT), DirectX
  • ·         Simulator for tablet to test and debug applications

The tools are designed to let you develop Windows Store consumer applications and games.

Programming architecture 

Windows Store programing model utilizes a layered architecture as shown below.

 

The bottom layer is the windows core that provides the driver level support. The upper layer is the Windows Runtime core that is an extensive library that supersedes much of the Win32 APIs. It provides new object-oriented API sets and new features that Win32 APIs do not provide. We can divide the WinRT APIs into multiple namespaces, for example, UI, Storage, Network, Media and etc.

A WinRT component provides a full set of metadata to expose the namespaces of the WinRT. The metadata format is pretty much the same as a .NET reference assembly (Ecma-335). The metadata describes the code written for the WinRT platform. It defines a programming model that makes it possible to write object-oriented code that can be shared across programming languages.

Language projection is a way that exposes native APIs to three different types of language: Native language (C/C++), managed language ( C#/VB.NET), as well as Html5 and JavaScript.

Besides WinRT APIs, Windows Store app also supports a subset of Win32 APIs. You can check which Win32 APIs are supported by Windows Store app from Win32 and COM for Windows Store apps. If the application is written by managed code, a subset of .Net Framework is also supported. .NET for Windows Store apps provides the supported .Net APIs from Windows Store application.

Comparing the Programming Stack of Windows 8 with Android

This table gives an overview of the Windows Store UI frameworks that provide features comparable to the Android programming layers.

 

Android Frameworks

Functionality

Windows Store UI Frameworks

Android Application Framework

 

Expose required APIs for applications

Windows Runtime (WinRT) projection

.Net for Windows Store App

Supported Win32 APIs

Android Libraries

Provide reusable system level functionalities and resources.

Windows Runtime Library

DirectX(2D, 3D) engine

Android runtime (Dalvik)

Language execution engine

Common Language Runtime (Managed code only)

Linux Kernel

Operating System

Windows NT Kernel

Android is built on Linux kernel while Win8 is built on Windows NT kernel. They are different but totally transparent to the developer.

Android integrates Dalvik virtual machine to execute Java byte code. The corresponding virtual machine for the managed languages is Common language runtime which is a Just-In-time compilation engine. Unlike Dalvik virtual machine, the managed code should be compiled into intermediate language (IL) at the compilation phase. Then the CLR engine compiles the IL code into the machine code at the runtime. This mechanism can get better performance than Java virtual machine.

Android libraries are native libraries which provide reusable system level functionalities and resources. In windows 8 platform, Windows runtime library plays the same role. It’s also native and reusable. If you want to develop games, DirectX engine is the replacement of OpenGL ES library.

Android also provides a framework to expose required APIs for Java applications. However, for Windows Store applications, APIs are exposed by Windows Runtime library. We just use WinRT projection to bind them into the different languages. If you are writing code in managed language, you can also use a subset of .Net libraries for Windows Store app.

Comparing the Programming Language of Windows 8 with Android

This table compares the programming languages of Android and Windows 8.

These two platforms support different programing languages. In Windows 8, we supported multiple languages. However, the nearest replacement for Java is C# language. C# language syntax is similar to Java language, which makes it easier to convert Java code to C# code.

In Win8 platform, C++ becomes the first class language. We can write UI and business logic in C++. But in practice, we just use it to write custom WinRT components instead of applications.

Summary

In this chapter, we looked at the Windows Store UI and the Android programming stacks. Now that you have a high-level idea of how the Windows 8 programming stack maps to the Android stack, we are now going to go one level deeper. In the next section, we will look at the user interface guidelines of Windows Store applications.

 

2. User Interface

Windows 8 uses a novel user interface called Windows Store. It sets itself apart with its clean and simple design and emphasizes color and typography.

In contrast to the application-focused design of Android, Windows 8 uses an information-centric design. Instead of an array of application icons, the start screen of a Windows 8 consists of dynamic tiles that display critical information at a glance to the user. The tiles are dynamic and they continuously display the up-to-date status of the applications. For example, they show you the next appointment on your calendar or the number of new emails waiting for your attention. Users can personalize their start screen by pinning the tiles that they care most about.

Design Principles for Windows Store Apps

Windows Store design has a set of five guiding principles to help you make the best choices when designing your app. These principles are the foundation for building great Windows store apps. Consider these principles as youplan your app , and always ensure your design and development choices live up to these principles.

·         Show pride in craftsmanship
·         Be fast and fluid
·         Be authentically digital
·         Do more with less
·         Win as one

These five Windows Store design principles will help you make the best choices when designing your app.

 

Application User Interface Design

While there are similarities in the design principles of the applications on both platforms, pay close attention to the Windows Store UI. It is best to take advantage of the unique features and strengths of the Windows 8 platform.

Windows 8 encourages showing the content first without any chrome. It uses bold, vibrant and crisp colors and images as well as abstract symbols to represent the real objects. Android allows you to apply multiple art styles to your applications if only it can make your applications more beautiful. Unlike Android, Windows 8 uses very consistent UI controls, design guidelines and manipulations. You can have your special design for your application. But it must be approved by Microsoft for adherence to the design guidelines of Windows Store UI.

XML and XAML

Android UI elements such as widgets and layouts may be specified using XML or created programmatically at runtime or both.

Windows 8 uses a similar design paradigm. Typically, you use XAML, a declarative language, to define the application UI. Extensible Application Markup Language (XAML) is an XML-based markup language used for visual representation of applications in Windows 8.

While both Android and Windows 8 use XML based languages to specify the UI elements, Windows Store UI design is much easier. Visual Studio and Blend, the Windows Store UI design tool, produce XAML code based on WYSIWYG design. Additionally, XAML language is much more flexible than xml. The control’s content property in XAML can accept any type of objects. The containment-friendly feature allows the control to contain any type of objects.

Another key feature is that XAML owns the template. The template can be used to replace the view completely. That’s why we said that the controls are look-less. We can define a template with totally different view, then use this template to replace the controls’ default view.

Controls and the Application Interface

Windows Runtime includes a rich collection of controls that are designed specifically for usability and aesthetics. While you can create your own controls, it is best to use the standard controls wherever possible. These standard controls respond to theme changes and provide a consistent user interface. 

The following table shows the mapping between Android controls and corresponding Windows Store controls.

Android control

Windows Store control

Border

Border

Button

Button

 

HyperlinkButton

AbsoluteLayout

Canvas

CheckBox

CheckBox

GridView

GridView

ImageView

Image

ListView

ListView

MediaController

MediaElement

 

SemanticZoom

ViewGroup

Panel

EditText

PasswordBox

EditText

TextBlock (No edit function, with simple text)

RichTextBlock (No edit function, with simple text)

EditText

TextBox (with edit function)

ProgressBar

ProgressBar

RadioButton, RadioGroup

RadioButton

ScrollView

ScrollViewer

SeekBar

Slider       

LinearLayout

StackPanel

MapView

Bing Map

WebView

WebView

TimePicker

Timepicker(WinJS only)

DatePicker

Datepicker(WinJS control)

 

ExpandableListView

 

Gallery

 

ImageSwitcher

 

Spinner

 

TableLayout

Grid can be the equivalent.

TextSwitcher

 

ViewFlipper

FlipView

ZoomControl

Can be simulated by one of following controls:

ScrollViewer

Semantic zoom

 

TabHost

FlipView is the equivalent.

SlidingDrawer

 

RatingBar

 

Toggle button

ToggleSwitch

Options menu

AppBar

Border

Border

As you can see, Windows 8 offers controls that correspond to most of Android controls. While the look and feel is different, they provide similar functionality. 

Notifications

Both Android and Windows 8 have notification services, but notifications play a key role in Windows Store application. Windows 8 provides number of different means to show notifications to users – via status bar update, a dialog as a toast or live tile notifications.

Windows 8 sets itself apart with the live tiles that show critical information at a glance. Live tiles are used to display non-critical information without disrupting what the user is doing. If you are using status bar notifications in Android, you can use tile notification as a replacement in Windows 8 to show critical information.

The notification service also displays toast notifications that provide time sensitive information. Toast notifications are shown for about 7 seconds for standard toast or 25 seconds for long-duration toast. The user may choose to ignore them. These are the same as Android toast notification.

The following table shows the Windows Store notifications that are closest to different Android notification mechanisms. While you may replace status bar notifications with Tile notifications, tile notifications provide much richer functionality. Additionally, tile notifications do not require user response.

 

Android

Functionality

Windows 8

Status bar notification

Persistent information that require user response.

Tile notifications

Toast notification

Time sensitive non-persistent data that user may not respond to.

Toast Notifications

Dialog notification

Modal alerts that the user must respond to.

Application notifications

Summary

In this chapter, we looked at the similarities between the application design goals of the Android and Windows Store UI. When you plan your Windows Store application, you will be able to leverage your existing work on Android applications.

Revisit the application interface design to make sure you are taking advantage of the Windows 8 design guidelines. You will find that the Windows 8 offers a large library of controls and gestures that have close counterparts on the Android.

3. Developer and Designer Tools

Microsoft released a new version Visual Studio Developer tools which brings the user-friendly, high productivity development environment to Windows Store. Developers who have used Visual Studio will find themselves in a familiar environment. An Android application developer who is familiar with Eclipse will find it easy to quickly migrate to Visual Studio developer tools and become productive.

A Comparison of Android and Windows 8 developer Tools

Visual Studio 2012 is a full-featured Integrated Development Environment (IDE). Visual Studio 2012 facilitates designing, developing, and debugging of Windows Store applications.

Compared to Android developer tools, the Windows 8 developer tools offer richer functionality. The following table gives an overview of the functionality of each of these tools. The table also indicates the equivalent tools that you would use for Android application development.

 

Functionality

Audience

Android tools

Windows 8 tools

Software development kit

Programmers

Android SDK and SDK Tools for Windows

Windows 8 SDK

Primary UI design: Colors, gradients, and animation

UI designers

Defined in XML(No WYSIWYG)

(or tools like ADT UI plug-in

or DroidDraw)

Pixel accurate WYSIWYG layout using Blend for Visual Studio 2012

UI design

UI designers and programmers

Visual Studio 2012

Application development (coding)

Programmers

Eclipse with ADT plug-in

Visual Studio 2012

Testing / Emulation

Testers

Android Emulator

Simulator for tablet

 

 

 

 

 

 

 

 

 

 

 

 

As you plan to develop Windows Store applications, you can continue to leverage the skillsets in your team. You can use the Android team structure and overall development process to build applications for Windows Store. The Windows 8 toolset ensures that the entire team of designers, developers, and testers familiar with Android development tools will find it easy to migrate to the Windows 8 toolset.

To develop applications, software development kit is the first one to be installed. To develop Android apps in the Windows platform, you should install Android SDK and related SDK tools for Windows. To develop Windows Store applications, Windows 8 SDK is required.

The developer generally use integrated development environment (IDE) to write and compile the code. Eclipse with ADT plugin is the IDE for Android. For Windows Store apps, Microsoft provides VS2012 which has included the necessary components of Windows 8 SDK. Therefore, it’s not necessary to install Windows 8 SDK separately if VS2012 is installed.

To design Android app UI, you should install ADT UI plugin in Eclipse or DroidDraw. However, VS2012 has embedded the UI design tool. It also includes a more power design tool Blend for VS2012 which can help to realize pixel accurate WYSIWYG layout.

Android provides an emulator Android Virtual Device which helps to test the application without the real Android device. Since Windows Store application runs in Win8 platform, emulator is not required. But for the convenience of testing applications on tablet, we still provide a simulator for tablet which is also included in VS2012. It can help the developer to test the layout with common touch and rotate events.

 

Summary

In this chapter we looked at the Windows Store Developer Toolset. The tool set includes rich tools that are designed to support every step in the entire application development lifecycle. The design, development, and testing tools are equivalent to the existing Android team roles and processes. The tight integration inside VS2012 helps you to streamline the design, develop the workflow, and test the workflow. These tools provide end-to-end functionality and are highly customizable, with the power to make your team quickly productive.

 

4. Application Lifecycle Differences

The lifecycle for Windows Store application is significant different with Android application. To get the fast and fluid response, and reduce the power consuming of the system, the Windows Store application is well designed so that only the visible application can consume the CPU time. The applications which are invisible will fall into the suspend state. They can be terminated without any notification when the system wants to recycle the resources. On the contrary, Android always keeps the invisible application running unless the system resource is not enough.

Multitasking in Android and Windows Store

In addition to the application in the foreground, Android can keep applications running in the background using the Service component. The Service component runs in the background even if your application is not in foreground until the application completes or stopService is called.

Windows Store applications suspend any invisible processes. The suspended process still stays in the memory but can’t get CPU time unless it gets resumed. It means that by default any task will be stopped if the application is switched out. But we still allow the application to perform actions even when it is not in the foreground. It allows the application to play background audio, transfer files, execute scheduled tasks and notifications such as alarms and reminders without launching the application.

Windows Store Application State Transitions

Following graph illustrates the transitions between app execution states. When you click the tile of an application, the application is activated from “not running” state to “running” state. Then the application exclusively owns the entire screen. The application will suspend when the user switches away from it and resume when the user switches back to it. Once the suspend event is fired, your app has 5 seconds to store off state. In the suspend mode, your main thread of execution is suspended but the app is still held in memory. The user may come back to your app shortly, and then the thread is resumed and that state that you stored is never used. But, at some point, Windows may have too much in memory and need to terminate a process. If your app is terminated, then you can use the state that you stored to restore your app to the way it was when it was terminated on the next time that it is run.

 

Figure: Windows Store Application Life Cycle

 

In Android, you save application state when an activity is paused. You can use the method onSaveInstanceState to access the event, and use the method onRestoreInstanceState to restore the event to previous state.

 

Life Cycle of a Windows Store Application

The following events take place during the life cycle of a Windows Store application. Windows Store application developer needs to take appropriate actions in response to each of these events:

Suspend

In general, Windows Store apps stop running when the user switches to another app. Windows suspends your app when it is not in the foreground. When your app is suspended, it is frozen in memory. It can’t run in this state, but Windows can instantly resume it when the user returns to it. In this way, Windows gives the foreground app better use of system resources and ensures apps in the background can’t drain the user’s battery.

When your app moves out of the foreground, Windows waits for a few seconds to allow quick switching back to the app, then tries to suspend the app. Your app must be registered for the suspending or checkpoint (JavaScript) events, which it receives when Windows wants to suspend it. This is an important time for your app; use this opportunity to save app data that must be kept in persistent storage. Usually, your app is resumed as is, and you won’t need your persisted data because they are still in memory. But you need to store this data in case Windows terminates your app to free system resources. Save enough app data to bring users back to the place in your app where they were when the app was suspended. This way, your customers perceive your app as always alive and running.

If your app doesn’t return from its suspending event handler within 5 seconds of receiving the suspending event, Windows will terminate it. It’s important not to do any heavy operations in your suspending event handler. Save your app data and return quickly.

Resume

When your app is resumed, it continues from the state that it was in when Windows suspended it. To be specific: app data and state are kept in memory while the app is suspended, so when it’s resumed, everything is as it was when the app was suspended. You don’t need to restore any saved data explicitly when receiving the resuming event.

But you want your app to appear to be always alive. For this, it has to be connected and display the latest data. It is possible that your app stays in a suspended state for quite a long time before it is resumed. Data or network connections could become stale and may need to be refreshed when your app resumes. When a user brings your app back to the foreground, your app receives a resuming event. You can refresh the app content and reconnect network resources when your app receives this event.

Activation

Activation is all about how your app gets launched. It serves many purposes in a Windows Store app. Windows can terminate your app after it has been suspended for a number of reasons. For example, the user manually closes your app, or signs out, or the system is running low on resources (some apps, such as games, can be pretty resource intensive!). If the user launches your app after Windows terminated it, it receives an activated event and the user sees your app’s splash screen until the app is activated. You can use this event to determine whether your app needs to restore the data it saved when it was last suspended, or if you must load your app’s default data.

Comparing Life-cycle Methods

A Page in Windows Store application is similar to an Activity in Android. We can compare the Android lifecycle method with Windows 8’s state-change events and Page methods.

Windows Store Application Events

Windows 8 Page Methods

Android Methods

OnLaunch

 InitializeComponent()

onCreate()

 

 

onStart(), onResume()

Suspending

 NavigatedFrom

onStop(), onPause()

Resuming

 NavigatedTo

onResume()

 

 

onDestroy()

When you launch an application in Android, you can use the onCreate method to initialize the components. Then the application receives Start and Resume events, which allows you to load settings and private data. The previous application receives the Pause and Stop events accordingly, which allow you to store settings and private data.

In Windows 8, you can call InitializeComponent method in the constructor of the Page class after the application receives OnLaunched event. The application also receives Resuming events when it’s activated and gets Suspending event when it’s switched out. You can also do the similar work to persist the application data in these two event handlers.

You can switch the pages or activities inside the process. In Android, when one activity replaces another, the current activity invokes the onPause and onStop callbacks. And the new activity invokes the onResume callback. This behavior is similar to the Page-to-Page navigation in Windows Store application. In Windows Store application, while moving from one page to another, the current page invokes the OnNavigatedFrom callback and the new page invokes the OnNavigatedTo callback.

 

Summary

In this chapter we had a look at the different states of application life cycle in Android and Windows 8. We also compared methods for saving application state data in Android and Windows 8.

5. Storing Data and Preferences

Windows 8 features a very comprehensive system of managing data for applications. This section compares the data management features of Windows 8 and Android.

Local Data Storage

Both Android and Windows 8 provide three different ways for the application to store data locally:

  1. 1.      Application settings
  2. 2.      Internal Storage
  3. 3.      External Storage
  4. 4.      Database (supported by the third party)

 

Purpose

Android

Windows 8

Application Settings

SharedPreferences

ApplicationDataContainer

Application Private Data

Internal storage

StorageFolder

Shared Application Data

External storage

Known Libraries

SD Storage

File Picker

Relational Database

SQLite

SQLite*

* Microsoft doesn’t officially support local database in Windows Store application. SQLite is supported by SQLite team. We don’t discuss it in this document.

The difference is that Android application can access not only internal storage but also external storage. Just the same as Android, the internal storage area are private to each application. But for Windows Store application, only the specified external folder can be accessed, such as Documents, Pictures and Music folders. If you want to access an arbitrary file in the system, you should use a file picker to select this file.

In both operating systems, the application developer has to manage the data that gets stored during application installation or reinstallation. The developer has to modify and migrate data if the application is updated; the OS does not manage data for the application.

Both operating systems delete the application files and folders in the private store when the application is uninstalled. 

Application Settings

The following table shows how we store application settings on the two platforms.

 

Android

Windows Store Application

Container class:

SharedPreferences

ApplicationDataContainer

Fetching method:

getSharedPreferences/getPreferences

ApplicationData.Current.LocalSettings

 

 

ApplicationData.Current.RoamingSettings

Fetching data time:

onCreate()

OnResuming()

Writing data time:

 onStop()

OnSuspending()

Android provides SharedPreferences class to save and retrieve persistent key-value pairs of primitive data types. This data survives across application sessions. Similarly, Windows 8 provides the ApplicationDataContainer class to persist key-value pairs. The ApplicationDataContainer data is stored in the application specific folder.  

getSharedPreferences or getPreferences is used to get the container object in Android. In Windows Store application, you can get it through ApplicationData.Current.LocalSettings property. The application setting can also roam to other devices with the help of RoamingSettings property.

Generally you should load settings when the application is activated and store settings when the application exits. Android applications fetch the data in onCreate function and write data in onStop function. While Windows Store applications fetch settings in OnResuming function and write data in OnSuspending function.

Internal Storage

Android applications can create and use files and folders within the internal storage that is private to an application. Similarly, Windows Store applications can create files and folders in the StorageFolder. They use StorageFolder and StorageFile classes for folder and file operations.

Windows 8 provides four different folders to store the private data:

  • · LocalFolder: It’s used to save persistent data that exists only on the current device.
  • · TemporaryFolder: The data in the TemporaryFolder works like a cache. It can be removed by the system at the anytime. 
  • · RoamingFolder: The data in the RoamingFolder exists on all devices on which the user has installed the application.
  • · InstalledLocation: It’s a read only folder which contains the application package. 

External Storage

You can save files to a shared external storage in an Android compatible device. The shared external storage can also include the internal removable storage. Android allows you to access and modify the files that are saved to the external storage by connecting to a computer

Windows Store application doesn’t provide the general external storage due to security reason. You can access some known libraries, such as Documents, Videos, Music and Pictures folders. But you should declare the capabilities in the manifest file before accessing. You can also access SD storage through RemovableDevices property.

For security reason, the Windows Store application can’t access arbitrary files in the device implicitly. If you do want to access a specified file, you should use file picker to select this file explicitly. Then the application gets the permission to access this file.

Summary

In this chapter, we compared data storage guidelines for Windows 8 and Android. We also looked at the storage apis in Windows 8 and Android.