Application Launcher
This guide explains how to create a basic application launcher. The application launcher is the main application that normally starts after the system boots. This app is commonly replaced in the platform adjustment process. It is easy to use the .NET APIs in the application launcher implementation.
Every application launcher must be able to do the following tasks:
- List installed and runnable applications
- Run applications selected by the user
You can also implement advanced functionalities such as the following:
- Grouping installed applications into folders
- Removing applications
- Rearrangement of icons
- Viewing notifications
- Viewing widgets
Prerequisites
To list the installed applications and launch them, the launcher app must have defined privileges in the project manifest file:
xml
Copy
<?xml version="1.0" encoding="utf-8"?>
<ui-application appid="NUIApplicationLauncher">
<!-- Auto generated manifest properties -->
</ui-application>
<privileges>
<privilege>http://tizen.org/privilege/packagemanager.info</privilege>
<privilege>http://tizen.org/privilege/appmanager.launch</privilege>
</privileges>
Manage app launcher
The following steps illustrate how to implement the simple application launcher using:
- NUI for the view implementation
- Package Manager to gather installed applications
- TizenFX AppControl to launch an app on touch event
Figure: NUI application launcher
-
To use mentioned API’s, the following namespaces have to be included:
C#Copyusing System; using System.Collections.Generic; using Tizen.Applications; using Tizen.NUI; using Tizen.NUI.BaseComponents;
-
To group application launcher responsibilities, three classes in the
NUIApplicationLauncher
namespace are defined which represent the main application structure:C#Copynamespace NUIApplicationLauncher { //Each touch event needs additional data like application ID to launch application partial class ApplicationIconClickedEventArgs : EventArgs { //... } //Application Icon component with ImageView and Label. Also OnTouch Event is handler here. class ApplicationIcon : View { //... } //Main Application Code class Program : NUIApplication { //... } }
-
To add an application ID field, create the event arguments derived class. The application ID is used by
AppControl
to launch the application:C#Copypartial class ApplicationIconClickedEventArgs : EventArgs { public string AppId = ""; public ApplicationIconClickedEventArgs(string id) { AppId = id; } }
-
The
ApplicationIcon
class stores the application ID. TheIcon
component andOriginSize
are used to resizeIcon
when it is in the pressed state. TheApplicationIconClicked
is invoked when touch changes its state to finished:C#Copyclass ApplicationIcon : View { private string AppId; private ImageView Icon; private Size2D OriginSize; public event EventHandler<ApplicationIconClickedEventArgs> ApplicationIconClicked; }
-
The
ApplicationIcon
constructor is responsible for the following:- Set
AppId
andOriginSize
. - Create view components:
Label
which is used to show an application name andIcon
which load resource frompath
string and show loaded image. - Create
Layout
of theApplicationIcon
. In this case, the vertical linear layout is used. - Setup the
TouchEvent
handler.
C#Copypublic ApplicationIcon(string name, string path, Size2D size, string id) { this.AppId = id; OriginSize = new Size2D(size.Width - 40, size.Height - 40); this.Size2D = size; TextLabel label = new TextLabel() { PointSize = 6, Text = name, MultiLine = true, Size2D = new Size2D(size.Width, 40), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; Icon = new ImageView() { Size2D = new Size2D(size.Height - 40, size.Height - 40), ResourceUrl = path, }; this.Layout = new LinearLayout() { HorizontalAlignment = HorizontalAlignment.Center, LinearOrientation = LinearLayout.Orientation.Vertical }; this.Add(Icon); this.Add(label); this.TouchEvent += OnTouchEvent; }
Figure: Application icons
- Set
-
OnTouchEvent
reads state from theTouchEventArgs
. If touch is inPointStateType.Down
state, the application icon is resized. Otherwise, it returns to normal size and the predefined event is invoked with the properAppId
:C#Copypublic bool OnTouchEvent(object sender, TouchEventArgs args) { var state = args.Touch.GetState(0); switch (state) { case PointStateType.Down: Icon.Size2D = new Size2D((int)(OriginSize.Width * 1.1f), (int)(OriginSize.Height * 1.1f)); break; case PointStateType.Finished: Icon.Size2D = OriginSize; ApplicationIconClicked.Invoke(this, new ApplicationIconClickedEventArgs(this.AppId)); break; default: break; } return false; }
-
The
Program
class derived from NUIApplication handles all necessary system events (Application Lifecycle). TheAppLauncher
is created inInitialize()
method and used in an icon touch handler:C#Copyclass Program : NUIApplication { private AppControl AppLauncher; //... }
-
The
OnCreate()
method callsInitialize()
function before the main application loop starts:C#Copyprotected override void OnCreate() { base.OnCreate(); Initialize(); }
-
The
Initialize()
method set up key listener,AppControl
object, and the application background:C#Copyvoid Initialize() { var appWindow = Window.Instance; appWindow.KeyEvent += OnKeyEvent; AppLauncher = new AppControl(); ImageView background = new ImageView(DirectoryInfo.Resource + "/images/bg.png"); //Setting the background parameters so that it occupies the entire application window background.Size2D = new Size2D(appWindow.Size.Width, appWindow.Size.Height); background.Position2D = new Position2D(0, 0); appWindow.GetDefaultLayer().Add(background); }
-
In the next step, the grid component for application icons is created. Grid spacing and columns number are defined in the GridLayout object. The
appGrid
component width and height is set to fill its parent:C#CopyView appGrid = new View() { WidthResizePolicy = ResizePolicyType.FillToParent, HeightResizePolicy = ResizePolicyType.FillToParent, Layout = new GridLayout() { Columns = 8, GridOrientation = GridLayout.Orientation.Horizontal, RowSpacing = 35f, ColumnSpacing = 12f, Padding = new Extents(30, 30, 30, 30) } };
-
The PackageManager.GetPackages() is used to obtain all installed packages. Each package may contain several applications. Hence,
pkg.GetApplications()
is used.ApplicationInfo
object is used to filter apps that must be displayed. In this case,NUIApplicationLauncher
app with no icon or app withIsNoDisplay
parameter will not be inserted intoappGrid
:C#CopyIEnumerable<Package> packageList = PackageManager.GetPackages(); foreach (var pkg in packageList) { var list = pkg.GetApplications(); foreach (var app in list) { if (!app.IsNoDisplay && app.IconPath != null && app.Label != "NUIApplicationLauncher") { var icon = new ApplicationIcon(app.Label, app.IconPath, new Size2D((), 139), app.ApplicationId); icon.ApplicationIconClicked += OnAppIconClicked; appGrid.Add(icon); } } }
-
Once
appGrid
object is created, it must be inserted in the application window:C#CopyappWindow.GetDefaultLayer().Add(appGrid);
-
Clicked event set up
AppLauncher
and pass it to AppControlSendLaunchRequest()
API. Now, the selected applications get started:C#Copypublic void OnAppIconClicked(object sender, ApplicationIconClickedEventArgs args) { AppLauncher.ApplicationId = args.AppId; AppLauncher.Operation = AppControlOperations.Default; AppControl.SendLaunchRequest(AppLauncher); }
-
The following code shows how to implement
back
button clicked event. In this case application exits, but normally the launcher app doesn’t call theExit()
API:C#Copypublic void OnKeyEvent(object sender, Window.KeyEventArgs args) { Tizen.Log.Info(LogTag, args.Key.ToString()); if (args.Key.State == Key.StateType.Down && (args.Key.KeyPressedName == "XF86Back" || args.Key.KeyPressedName == "Escape")) { Exit(); } }
-
The common main code of the NUI application:
C#Copystatic void Main(string[] args) { var app = new Program(); app.Run(args); }
For full source code of the example, see here.
Related information
- Dependencies
- Tizen 4.0 and Higher