add apps manager gadget
authorYurii Zinchuk/Tizen Services & IoT (PLT) /SRPOL/Engineer/Samsung Electronics <y.zinchuk@samsung.com>
Fri, 7 Jul 2023 14:57:40 +0000 (16:57 +0200)
committerYurii Zinchuk/Tizen Services & IoT (PLT) /SRPOL/Engineer/Samsung Electronics <y.zinchuk@samsung.com>
Wed, 12 Jul 2023 07:38:14 +0000 (09:38 +0200)
SettingMainGadget/SettingMainGadget/Apps/AppsManagerGadget.cs [new file with mode: 0644]
SettingMainGadget/SettingMainGadget/AppsGadget.cs
SettingMainGadget/SettingMainGadget/MainMenuProvider.cs

diff --git a/SettingMainGadget/SettingMainGadget/Apps/AppsManagerGadget.cs b/SettingMainGadget/SettingMainGadget/Apps/AppsManagerGadget.cs
new file mode 100644 (file)
index 0000000..cb9c5be
--- /dev/null
@@ -0,0 +1,300 @@
+using SettingCore;
+using SettingCore.Views;
+using SettingMainGadget.TextResources;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using Tizen.Applications;
+using Tizen.NUI;
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+
+namespace Setting.Menu.Apps
+{
+    public class AppsManagerGadget : MenuGadget
+    {
+        public override string ProvideTitle() => NUIGadgetResourceManager.GetString(nameof(Resources.IDS_ST_BODY_APPLICATION_MANAGER));
+
+        private string defaultIcon = System.IO.Path.Combine(Application.Current.DirectoryInfo.Resource, "default_app_icon.svg");
+
+        private View content;
+        private View installedAppsContent;
+        private View runningAppsContent;
+        private View allAppsContent;
+
+        private Loading installedAppsIndicator;
+        private PackageSizeInformation packageSizeInfo;
+
+        private List<Package> allPackages = new List<Package>();
+        private Dictionary<Package, TextWithIconListItem> installedApps = new Dictionary<Package, TextWithIconListItem>();
+        private Dictionary<Package, TextWithIconListItem> allApps = new Dictionary<Package, TextWithIconListItem>();
+        private Dictionary<ApplicationInfo, TextWithIconListItem> runningApps = new Dictionary<ApplicationInfo, TextWithIconListItem>();
+
+        protected override View OnCreate()
+        {
+            base.OnCreate();
+
+            content = new View
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                },
+            };
+
+            AddTabs();
+            OnPageAppeared += AddTabsContent;
+
+            return content;
+        }
+
+        private void AddTabs()
+        {
+            var tabView = new TabView()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            // installed apps tab
+
+            var installedAppsTabButton = new TabButton()
+            {
+                Text = NUIGadgetResourceManager.GetString(nameof(Resources.IDS_ST_BODY_DOWNLOADS))
+            };
+
+            installedAppsContent = new View()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                    VerticalAlignment = VerticalAlignment.Center,
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                },
+            };
+
+            installedAppsIndicator = new Loading();
+            installedAppsIndicator.Play();
+            installedAppsContent.Add(installedAppsIndicator);
+
+            // running apps tab
+
+            var runningAppsTabButton = new TabButton()
+            {
+                Text = NUIGadgetResourceManager.GetString(nameof(Resources.IDS_ST_BODY_RUNNING))
+            };
+
+            runningAppsContent = CreateScrollableBase();
+
+            // all apps tab
+
+            var allAppsTabButton = new TabButton()
+            {
+                Text = NUIGadgetResourceManager.GetString(nameof(Resources.IDS_ST_OPT_ALL))
+            };
+
+            allAppsContent = CreateScrollableBase();
+
+            tabView.AddTab(installedAppsTabButton, installedAppsContent);
+            tabView.AddTab(runningAppsTabButton, runningAppsContent);
+            tabView.AddTab(allAppsTabButton, allAppsContent);
+
+            content.Add(tabView);
+        }
+
+        private void AddTabsContent()
+        {
+            allPackages = PackageManager.GetPackages().ToList();
+
+            AddInstalledApps(installedAppsContent);
+            _ = AddRunningAppsAsync(runningAppsContent);
+            AddAllApps(allAppsContent);
+        }
+
+        private void AddInstalledApps(View content)
+        {
+            var packages = allPackages.Where(a => a.IsPreloaded == false && !string.IsNullOrEmpty(a.Label) && a.PackageType != PackageType.WGT).OrderBy(x => x.Label).ToList();
+
+            if (packages.Count() == 0)
+            {
+                var noAppsLabel = new TextLabel
+                {
+                    WidthSpecification = LayoutParamPolicies.MatchParent,
+                    VerticalAlignment = VerticalAlignment.Center,
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                    Text = NUIGadgetResourceManager.GetString(nameof(Resources.IDS_FP_BODY_NO_APPLICATIONS)),
+                    PixelSize = 24.SpToPx(),
+                    TextColor = IsLightTheme ? new Color("#CACACA") : new Color("#666666"),
+                };
+                var infoLabel = new TextLabel
+                {
+                    WidthSpecification = LayoutParamPolicies.MatchParent,
+                    VerticalAlignment = VerticalAlignment.Center,
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                    MultiLine = true,
+                    Ellipsis = true,
+                    Text = NUIGadgetResourceManager.GetString(nameof(Resources.IDS_ST_BODY_AFTER_YOU_DOWNLOAD_AND_INSTALL_APPLICATIONS_APPLICATIONS_WILL_BE_SHOWN_HERE)),
+                    PixelSize = 24.SpToPx(),
+                    Margin = new Extents(16, 16, 0, 0).SpToPx(),
+                    TextColor = IsLightTheme ? new Color("#CACACA") : new Color("#666666"),
+                };
+
+                content.Add(noAppsLabel);
+                content.Add(infoLabel);
+
+                installedAppsIndicator.Stop();
+                installedAppsIndicator.Unparent();
+                installedAppsIndicator.Dispose();
+
+                return;
+            }
+
+            var scrollView = CreateScrollableBase();
+
+            scrollView.Add(CreateAppSizeLabel());
+
+            foreach (var package in packages)
+            {
+                var iconPath = File.Exists(package.IconPath) ? package.IconPath : defaultIcon;
+
+                var appItem = new TextWithIconListItem(package.Label, Color.Transparent, iconPath: iconPath, subText: NUIGadgetResourceManager.GetString(nameof(Resources.IDS_SM_SBODY_CALCULATING_ING)));
+                appItem.Clicked += (s, e) =>
+                {
+                    // TODO : goto app info by AppId
+                };
+
+                installedApps.Add(package, appItem);
+
+                scrollView.Add(appItem);
+                content.Add(scrollView);
+            }
+
+            installedAppsIndicator.Stop();
+            installedAppsIndicator.Unparent();
+            installedAppsIndicator.Dispose();
+
+            _ = UpdateSizeInfo(installedApps);
+        }
+
+        private async Task AddRunningAppsAsync(View content)
+        {
+            var runningApplicationsContexts = await ApplicationManager.GetAllRunningApplicationsAsync();
+
+            content.Add(CreateAppSizeLabel());
+
+            var applicationInfoList = new List<ApplicationInfo>();
+
+            foreach (var application in runningApplicationsContexts)
+            {
+                applicationInfoList.Add(new ApplicationInfo(application.ApplicationId));
+            }
+
+            applicationInfoList = applicationInfoList.OrderBy(x => x.Label).ToList();
+
+            foreach (var applicationInfo in applicationInfoList)
+            {
+                var iconPath = File.Exists(applicationInfo.IconPath) ? applicationInfo.IconPath : defaultIcon;
+
+                var appItem = new TextWithIconListItem(applicationInfo.Label, Color.Transparent, iconPath: iconPath, subText: NUIGadgetResourceManager.GetString(nameof(Resources.IDS_SM_SBODY_CALCULATING_ING)));
+                appItem.Clicked += (s, e) =>
+                {
+                    // TODO : goto app info by AppId
+                };
+
+                runningApps.Add(applicationInfo, appItem);
+                content.Add(appItem);
+            }
+
+            UpdateRAMSizeInfo(runningApps);
+        }
+
+        private void AddAllApps(View content)
+        {
+            var packages = allPackages.Where(x => !string.IsNullOrEmpty(x.Label) && x.PackageType != PackageType.WGT).OrderBy(x => x.Label).ToList();
+
+            content.Add(CreateAppSizeLabel());
+
+            foreach (var package in packages)
+            {
+                var iconPath = File.Exists(package.IconPath) ? package.IconPath : defaultIcon;
+
+                var appItem = new TextWithIconListItem(package.Label, Color.Transparent, iconPath: iconPath, subText: NUIGadgetResourceManager.GetString(nameof(Resources.IDS_SM_SBODY_CALCULATING_ING)));
+                appItem.Clicked += (s, e) =>
+                {
+                    // TODO : goto app info by AppId
+                };
+
+                allApps.Add(package, appItem);
+                content.Add(appItem);
+            }
+
+            _ = UpdateSizeInfo(allApps);
+        }
+
+        private async Task UpdateSizeInfo(Dictionary<Package, TextWithIconListItem> packages)
+        {
+            foreach (var package in packages)
+            {
+                packageSizeInfo = await package.Key.GetSizeInformationAsync();
+                package.Value.SubText = GetSizeString(packageSizeInfo.AppSize);
+            }
+        }
+
+        private void UpdateRAMSizeInfo(Dictionary<ApplicationInfo, TextWithIconListItem> infos)
+        {
+            foreach (var info in infos)
+            {
+                var appContext = new ApplicationRunningContext(info.Key.ApplicationId);
+                var processMemmory = new Tizen.System.ProcessMemoryUsage(new List<int> { appContext.ProcessId });
+                processMemmory.Update(new List<int> { appContext.ProcessId });
+                info.Value.SubText = GetSizeString(processMemmory.GetVsz(appContext.ProcessId));
+            }
+        }
+
+        private string GetSizeString(double size)
+        {
+            string[] suffixes = { "Bytes", "KB", "MB", "GB" };
+            int counter = 0;
+
+            while (Math.Round(size / 1024, 2) >= 1)
+            {
+                size = size / 1024;
+                counter++;
+            }
+
+            return string.Format("{0:0.##} {1}", size, suffixes[counter]);
+        }
+
+        private ScrollableBase CreateScrollableBase()
+        {
+            return new ScrollableBase()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+                ScrollingDirection = ScrollableBase.Direction.Vertical,
+                HideScrollbar = false,
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                },
+            };
+        }
+
+        private TextLabel CreateAppSizeLabel()
+        {
+            return new TextLabel
+            {
+                Text = "App size", // TODO : add translation to Resources 
+                TextColor = IsLightTheme ? new Color("#83868F") : new Color("#666666"),
+                PixelSize = 24.SpToPx(),
+                Margin = new Extents(20, 0, 16, 16).SpToPx(),
+            };
+        }
+    }
+}
index 1a3df131b8ba104ea61cc6b169e4da6de94510bb..2645249577718144523c430147c6f1d7f720c47e 100644 (file)
@@ -51,7 +51,7 @@ namespace Setting.Menu
             sections.Add(MainMenuProvider.Apps_AppsManager, appsManagerItem);
             appsManagerItem.Clicked += (o, e) =>
             {
-                // TODO : NavigateTo App Manager
+                NavigateTo(MainMenuProvider.Apps_AppsManager);
             };
             content.Add(appsManagerItem);
 
index ff6cf10021158b1e0e51d08b75eb1a302427c938..59ea94ef5159e0dfa4db4426333a70a742883e2a 100644 (file)
@@ -127,7 +127,7 @@ namespace SettingMainGadget
                 new SettingMenu(path: About_DeviceStatus_cpu_usage, defaultOrder: 40),
                 // apps
                 new SettingMenu(path: Apps, defaultOrder: 80, type: typeof(Setting.Menu.AppsGadget)),
-                new SettingMenu(path: Apps_AppsManager, defaultOrder: 10),
+                new SettingMenu(path: Apps_AppsManager, defaultOrder: 10, typeof(Setting.Menu.Apps.AppsManagerGadget)),
                 new SettingMenu(path: Apps_DefaultApps, defaultOrder: 20),
                 //storage
                 new SettingMenu(path: Storage, defaultOrder: 120, type: typeof(Setting.Menu.StorageGadget)),