Revert "Add Test cases for app shortcut, managedapps"
[profile/tv/apps/dotnet/home.git] / LibTVRefCommonTizen / Ports / ApplicationManagerPort.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20 using Tizen.Applications;
21 using LibTVRefCommonPortable.Utils;
22
23 namespace LibTVRefCommonTizen.Ports
24 {
25     /// <summary>
26     /// Handles the ApplicationsManager APIs
27     /// </summary>
28     public class ApplicationManagerPort : IApplicationManagerAPIs
29     {
30         /// <summary>
31         /// Defines the default app icon
32         /// If the app icon is not exist, shows the default app icon
33         /// </summary>
34         private static String DefaultAppIcon = "AppIcon.png";
35
36         /// <summary>
37         /// The constructor of this class
38         /// Adds the EventHandler for ApplicationLaunched
39         /// </summary>
40         public ApplicationManagerPort()
41         {
42             ApplicationManager.ApplicationLaunched += new EventHandler<ApplicationLaunchedEventArgs>(OnApplicationLaunched);
43         }
44
45         /// <summary>
46         /// Arguments for the event that is raised when the application is launched
47         /// </summary>
48         /// <param name="sender">The source of the event</param>
49         /// <param name="args">An object that contains no event data</param>
50         /// <remarks>
51         /// https://msdn.microsoft.com/en-us/library/system.eventhandler(v=vs.110).aspx
52         /// </remarks>
53         void OnApplicationLaunched(object sender, EventArgs args)
54         {
55             ApplicationLaunchedEventArgs launchedEventArgs = args as ApplicationLaunchedEventArgs;
56             DbgPort.D(launchedEventArgs.ApplicationRunningContext.ApplicationId + " launched");
57         }
58
59         /// <summary>
60         /// Clears all recent applications
61         /// </summary>
62         public void DeleteAllRecentApplication()
63         {
64             RecentApplicationControl.DeleteAll();
65         }
66
67         /// <summary>
68         /// Removes the specified application with the app ID
69         /// </summary>
70         /// <param name="appId">An application ID that is removed</param>
71         public void DeleteRecentApplication(string appId)
72         {
73             IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
74             string pkgId = PackageManager.GetPackageIdByApplicationId(appId);
75
76             foreach (var item in recentApps)
77             {
78                 if (item.PackageId.Equals(pkgId))
79                 {
80                     RecentApplicationControl controller = item.Controller;
81                     controller.Delete();
82                 }
83             }
84         }
85
86         /// <summary>
87         /// Gets the information of the recent applications
88         /// </summary>
89         /// <returns>The list of the recent applications</returns>
90         public IEnumerable<RecentApp> GetRecentApplications()
91         {
92             List<RecentApp> resultList = new List<RecentApp>();
93             try
94             {
95                 IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
96
97                 foreach (var app in recentApps)
98                 {
99                     if (app.IsNoDisplay ||
100                         app.ApplicationId == null ||
101                         app.ApplicationId.Length < 1)
102                     {
103                         continue;
104                     }
105
106                     resultList.Add(new RecentApp()
107                     {
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,
114                         Uri = app.Uri,
115                     });
116                 }
117             }
118             catch (InvalidOperationException)
119             {
120                 DbgPort.E("Failed to get the information of the recent applications");
121                 return null;
122             }
123
124             return resultList;
125         }
126
127         /// <summary>
128         /// Gets the information of the specified application with the app ID
129         /// </summary>
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)
133         {
134             Dictionary<string, string> result = null;
135             ApplicationInfo appInfo = null;
136
137             try
138             {
139                 appInfo = ApplicationManager.GetInstalledApplication(appID);
140                 if (appInfo == null)
141                 {
142                     DbgPort.D("GetInstalledApplication failed");
143                     return null;
144                 }
145
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);
150             }
151             catch (Exception exception)
152             {
153                 DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
154                 return null;
155             }
156
157             return result;
158         }
159
160         /// <summary>
161         /// Gets the information of the installed applications asynchronously
162         /// </summary>
163         /// <returns>The list of the installed applications</returns>
164         public async Task<Dictionary<string, string[]>> GetAllInstalledApplication()
165         {
166             try
167             {
168                 Dictionary<string, string[]> resultList = new Dictionary<string, string[]>();
169                 Task<IEnumerable<ApplicationInfo>> task = ApplicationManager.GetInstalledApplicationsAsync();
170                 string[] result;
171
172                 IEnumerable<ApplicationInfo> installedList = await task;
173
174                 foreach (var appInfo in installedList)
175                 {
176                     if (appInfo.Label == null ||
177                     appInfo.ApplicationId == null ||
178                     appInfo.IsNoDisplay)
179                     {
180                         continue;
181                     }
182
183                     Package pkgInfo = PackageManager.GetPackage(appInfo.PackageId);
184                     if (pkgInfo == null)
185                     {
186                         continue;
187                     }
188
189                     if (pkgInfo.IsSystemPackage)
190                     {
191                         continue;
192                     }
193
194                     result = new string[4];
195
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);
201                 }
202
203                 return resultList;
204             }
205             catch (Exception exception)
206             {
207                 DbgPort.E("Failed to get the all installed applications : " + exception.Message);
208                 return null;
209             }
210         }
211
212         /// <summary>
213         /// Checks whether application is removable
214         /// </summary>
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)
218         {
219             ApplicationInfo appInfo = null;
220
221             try
222             {
223                 appInfo = ApplicationManager.GetInstalledApplication(appID);
224                 if (appInfo == null)
225                 {
226                     DbgPort.D("Failed to get the installed application(" + appID + ")");
227                     return false;
228                 }
229
230                 return !appInfo.IsPreload;
231             }
232             catch (Exception exception)
233             {
234                 DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
235                 return false;
236             }
237         }
238
239         /// <summary>
240         /// Gets the app ID by the app label
241         /// </summary>
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)
245         {
246             IEnumerable<ApplicationInfo> installedList = await ApplicationManager.GetInstalledApplicationsAsync();
247
248             foreach (var app in installedList)
249             {
250                 if (app.Label.Equals(appLabel))
251                 {
252                     return app.ApplicationId;
253                 }
254             }
255
256             return null;
257         }
258     }
259 }