Revert "Add Test cases for app shortcut, managedapps"
[profile/tv/apps/dotnet/home.git] / LibTVRefCommonTizen / Ports / ApplicationManagerPort.cs
old mode 100644 (file)
new mode 100755 (executable)
index bb98cb7..9e5cb9b
 using System;
 using System.Collections.Generic;
 using System.Threading.Tasks;
-using Xamarin.Forms.Platform.Tizen.Native;
-using Tizen;
 using Tizen.Applications;
 using LibTVRefCommonPortable.Utils;
 
 namespace LibTVRefCommonTizen.Ports
 {
+    /// <summary>
+    /// Handles the ApplicationsManager APIs
+    /// </summary>
     public class ApplicationManagerPort : IApplicationManagerAPIs
     {
+        /// <summary>
+        /// Defines the default app icon
+        /// If the app icon is not exist, shows the default app icon
+        /// </summary>
         private static String DefaultAppIcon = "AppIcon.png";
 
+        /// <summary>
+        /// The constructor of this class
+        /// Adds the EventHandler for ApplicationLaunched
+        /// </summary>
         public ApplicationManagerPort()
         {
             ApplicationManager.ApplicationLaunched += new EventHandler<ApplicationLaunchedEventArgs>(OnApplicationLaunched);
         }
 
+        /// <summary>
+        /// Arguments for the event that is raised when the application is launched
+        /// </summary>
+        /// <param name="sender">The source of the event</param>
+        /// <param name="args">An object that contains no event data</param>
+        /// <remarks>
+        /// https://msdn.microsoft.com/en-us/library/system.eventhandler(v=vs.110).aspx
+        /// </remarks>
         void OnApplicationLaunched(object sender, EventArgs args)
         {
             ApplicationLaunchedEventArgs launchedEventArgs = args as ApplicationLaunchedEventArgs;
-            DebuggingUtils.Dbg(launchedEventArgs.ApplicationRunningContext.ApplicationId + " launched");
+            DbgPort.D(launchedEventArgs.ApplicationRunningContext.ApplicationId + " launched");
         }
 
-        public async Task<Dictionary<string, string[]>> GetRecentApplications()
+        /// <summary>
+        /// Clears all recent applications
+        /// </summary>
+        public void DeleteAllRecentApplication()
         {
-            // RUA가 지원되지 않으므로 일단 설치된 app들을 가져와 본다
-            // TODO: RUA로 대체한다
-            Dictionary<string, string[]> resultList = new Dictionary<string, string[]>();
-            var applicationList = await ApplicationManager.GetInstalledApplicationsAsync();
-            string[] result;
+            RecentApplicationControl.DeleteAll();
+        }
+
+        /// <summary>
+        /// Removes the specified application with the app ID
+        /// </summary>
+        /// <param name="appId">An application ID that is removed</param>
+        public void DeleteRecentApplication(string appId)
+        {
+            IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
+            string pkgId = PackageManager.GetPackageIdByApplicationId(appId);
 
-            foreach (ApplicationInfo appInfo in applicationList)
+            foreach (var item in recentApps)
             {
-                if (appInfo.Label == null ||
-                    appInfo.ApplicationId == null)
+                if (item.PackageId.Equals(pkgId))
                 {
-                    continue;
+                    RecentApplicationControl controller = item.Controller;
+                    controller.Delete();
                 }
+            }
+        }
+
+        /// <summary>
+        /// Gets the information of the recent applications
+        /// </summary>
+        /// <returns>The list of the recent applications</returns>
+        public IEnumerable<RecentApp> GetRecentApplications()
+        {
+            List<RecentApp> resultList = new List<RecentApp>();
+            try
+            {
+                IEnumerable<RecentApplicationInfo> recentApps = ApplicationManager.GetRecentApplications();
+
+                foreach (var app in recentApps)
+                {
+                    if (app.IsNoDisplay ||
+                        app.ApplicationId == null ||
+                        app.ApplicationId.Length < 1)
+                    {
+                        continue;
+                    }
 
-                result = new string[3];
-                result[0] = appInfo.Label;
-                result[1] = appInfo.ApplicationId;
-                result[2] = (System.IO.File.Exists(appInfo.IconPath)) ? appInfo.IconPath : DefaultAppIcon;
-                resultList.Add(appInfo.ApplicationId, result);
+                    resultList.Add(new RecentApp()
+                    {
+                        InstanceID = app.InstanceId,
+                        InstanceLabel = app.InstanceName,
+                        AppID = app.ApplicationId,
+                        Applabel = (app.Label == null || app.Label.Length < 1) ? "No Name" : app.Label,
+                        IconPath = app.IconPath,
+                        LaunchedTime = app.LaunchTime,
+                        Uri = app.Uri,
+                    });
+                }
+            }
+            catch (InvalidOperationException)
+            {
+                DbgPort.E("Failed to get the information of the recent applications");
+                return null;
             }
 
             return resultList;
         }
 
-        public Dictionary<string, string> GetInstalledApplication(string applicationId)
+        /// <summary>
+        /// Gets the information of the specified application with the app ID
+        /// </summary>
+        /// <param name="appID">The app Id to get</param>
+        /// <returns>The information of the installed application</returns>
+        public Dictionary<string, string> GetInstalledApplication(string appID)
         {
             Dictionary<string, string> result = null;
             ApplicationInfo appInfo = null;
 
             try
             {
-                appInfo = ApplicationManager.GetInstalledApplication(applicationId);
+                appInfo = ApplicationManager.GetInstalledApplication(appID);
                 if (appInfo == null)
                 {
                     DbgPort.D("GetInstalledApplication failed");
@@ -86,13 +150,17 @@ namespace LibTVRefCommonTizen.Ports
             }
             catch (Exception exception)
             {
-                DbgPort.E("Exception " + applicationId + " :" + exception.Message);
+                DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
                 return null;
             }
 
             return result;
         }
 
+        /// <summary>
+        /// Gets the information of the installed applications asynchronously
+        /// </summary>
+        /// <returns>The list of the installed applications</returns>
         public async Task<Dictionary<string, string[]>> GetAllInstalledApplication()
         {
             try
@@ -123,11 +191,12 @@ namespace LibTVRefCommonTizen.Ports
                         continue;
                     }
 
-                    result = new string[3];
+                    result = new string[4];
 
                     result[0] = appInfo.Label;
                     result[1] = appInfo.ApplicationId;
                     result[2] = (System.IO.File.Exists(appInfo.IconPath)) ? appInfo.IconPath : DefaultAppIcon;
+                    result[3] = "" + pkgInfo.InstalledTime;
                     resultList.Add(appInfo.ApplicationId, result);
                 }
 
@@ -135,9 +204,56 @@ namespace LibTVRefCommonTizen.Ports
             }
             catch (Exception exception)
             {
-                DbgPort.E(exception.Message);
+                DbgPort.E("Failed to get the all installed applications : " + exception.Message);
                 return null;
             }
         }
+
+        /// <summary>
+        /// Checks whether application is removable
+        /// </summary>
+        /// <param name="appID">The app ID to get</param>
+        /// <returns>If the application is removable, true; otherwise, false</returns>
+        public bool GetAppInfoRemovable(string appID)
+        {
+            ApplicationInfo appInfo = null;
+
+            try
+            {
+                appInfo = ApplicationManager.GetInstalledApplication(appID);
+                if (appInfo == null)
+                {
+                    DbgPort.D("Failed to get the installed application(" + appID + ")");
+                    return false;
+                }
+
+                return !appInfo.IsPreload;
+            }
+            catch (Exception exception)
+            {
+                DbgPort.E("Failed to get the installed application(" + appID + ") :" + exception.Message);
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// Gets the app ID by the app label
+        /// </summary>
+        /// <param name="appLabel">the app label to get</param>
+        /// <returns>the app ID of the app label</returns>
+        public async Task<string> GetAppIDbyAppLabel(string appLabel)
+        {
+            IEnumerable<ApplicationInfo> installedList = await ApplicationManager.GetInstalledApplicationsAsync();
+
+            foreach (var app in installedList)
+            {
+                if (app.Label.Equals(appLabel))
+                {
+                    return app.ApplicationId;
+                }
+            }
+
+            return null;
+        }
     }
 }
\ No newline at end of file