2 * Copyright (c) 2017 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20 using Tizen.Applications;
21 using LibTVRefCommonPortable.Utils;
23 namespace LibTVRefCommonTizen.Ports
26 /// Handles the ApplicationsManager APIs
28 public class ApplicationManagerPort : IApplicationManagerAPIs
31 /// Defines the default app icon
32 /// If the app icon is not exist, shows the default app icon
34 private static String DefaultAppIcon = "AppIcon.png";
37 /// The constructor of this class
38 /// Adds the EventHandler for ApplicationLaunched
40 public ApplicationManagerPort()
42 ApplicationManager.ApplicationLaunched += new EventHandler<ApplicationLaunchedEventArgs>(OnApplicationLaunched);
46 /// Arguments for the event that is raised when the application is launched
48 /// <param name="sender">The source of the event</param>
49 /// <param name="args">An object that contains no event data</param>
51 /// https://msdn.microsoft.com/en-us/library/system.eventhandler(v=vs.110).aspx
53 void OnApplicationLaunched(object sender, EventArgs args)
55 ApplicationLaunchedEventArgs launchedEventArgs = args as ApplicationLaunchedEventArgs;
56 DbgPort.D(launchedEventArgs.ApplicationRunningContext.ApplicationId + " launched");
60 /// Clears all recent applications
62 public void DeleteAllRecentApplication()
64 RecentApplicationControl.DeleteAll();
68 /// Removes the specified application with the app ID
70 /// <param name="appId">An application ID that is removed</param>
71 public void DeleteRecentApplication(string appId)
73 IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
74 string pkgId = PackageManager.GetPackageIdByApplicationId(appId);
76 foreach (var item in recentApps)
78 if (item.PackageId.Equals(pkgId))
80 RecentApplicationControl controller = item.Controller;
87 /// Gets the information of the recent applications
89 /// <returns>The list of the recent applications</returns>
90 public IEnumerable<RecentApp> GetRecentApplications()
92 List<RecentApp> resultList = new List<RecentApp>();
95 IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
97 foreach (var app in recentApps)
99 if (app.IsNoDisplay ||
100 app.ApplicationId == null ||
101 app.ApplicationId.Length < 1)
106 resultList.Add(new RecentApp()
108 InstanceID = app.InstanceId,
109 InstanceLabel = app.InstanceName,
110 AppID = app.ApplicationId,
111 Applabel = (app.Label == null || app.Label.Length < 1) ? "No Name" : app.Label,
112 IconPath = app.IconPath,
113 LaunchedTime = app.LaunchTime,
118 catch (InvalidOperationException)
120 DbgPort.E("Failed to get the information of the recent applications");
128 /// Gets the information of the specified application with the app ID
130 /// <param name="appID">The app Id to get</param>
131 /// <returns>The information of the installed application</returns>
132 public Dictionary<string, string> GetInstalledApplication(string appID)
134 Dictionary<string, string> result = null;
135 ApplicationInfo appInfo = null;
139 appInfo = ApplicationManager.GetInstalledApplication(appID);
142 DbgPort.D("GetInstalledApplication failed");
146 result = new Dictionary<string, string>();
147 result.Add("Label", appInfo.Label);
148 result.Add("ApplicationId", appInfo.ApplicationId);
149 result.Add("IconPath", (System.IO.File.Exists(appInfo.IconPath)) ? appInfo.IconPath : DefaultAppIcon);
151 catch (Exception exception)
153 DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
161 /// Gets the information of the installed applications asynchronously
163 /// <returns>The list of the installed applications</returns>
164 public async Task<Dictionary<string, string[]>> GetAllInstalledApplication()
168 Dictionary<string, string[]> resultList = new Dictionary<string, string[]>();
169 Task<IEnumerable<ApplicationInfo>> task = ApplicationManager.GetInstalledApplicationsAsync();
172 IEnumerable<ApplicationInfo> installedList = await task;
174 foreach (var appInfo in installedList)
176 if (appInfo.Label == null ||
177 appInfo.ApplicationId == null ||
183 Package pkgInfo = PackageManager.GetPackage(appInfo.PackageId);
189 if (pkgInfo.IsSystemPackage)
194 result = new string[4];
196 result[0] = appInfo.Label;
197 result[1] = appInfo.ApplicationId;
198 result[2] = (System.IO.File.Exists(appInfo.IconPath)) ? appInfo.IconPath : DefaultAppIcon;
199 result[3] = "" + pkgInfo.InstalledTime;
200 resultList.Add(appInfo.ApplicationId, result);
205 catch (Exception exception)
207 DbgPort.E("Failed to get the all installed applications : " + exception.Message);
213 /// Checks whether application is removable
215 /// <param name="appID">The app ID to get</param>
216 /// <returns>If the application is removable, true; otherwise, false</returns>
217 public bool GetAppInfoRemovable(string appID)
219 ApplicationInfo appInfo = null;
223 appInfo = ApplicationManager.GetInstalledApplication(appID);
226 DbgPort.D("Failed to get the installed application(" + appID + ")");
230 return !appInfo.IsPreload;
232 catch (Exception exception)
234 DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
240 /// Gets the app ID by the app label
242 /// <param name="appLabel">the app label to get</param>
243 /// <returns>the app ID of the app label</returns>
244 public async Task<string> GetAppIDbyAppLabel(string appLabel)
246 IEnumerable<ApplicationInfo> installedList = await ApplicationManager.GetInstalledApplicationsAsync();
248 foreach (var app in installedList)
250 if (app.Label.Equals(appLabel))
252 return app.ApplicationId;