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;
20 using LibTVRefCommonPortable.DataModels;
21 using LibTVRefCommonPortable.Utils;
22 using System.Threading.Tasks;
24 namespace LibTVRefCommonPortable.Models
27 /// A class provides Application related information to ViewModel.
28 /// The TVHome shows the Pinned app list when the App Home Menu is pressed,
29 /// by invoking AppShortcutController's API to retrieve the pinned app list.
30 /// The TVApps shows the installed the Tizen UI apps in the main screen.
31 /// To provides the installed apps, the TVApps invokes an AppShortcutController's API.
33 public class AppShortcutController
36 /// A default app icon for no icon applications.
38 private static String DefaultAppIcon = "AppIcon.png";
41 /// A method provides installed app list.
42 /// The returned app list has only Tizen UI apps not the system app or no display apps.
44 /// <returns>An installed app list.</returns>
45 public async Task<IEnumerable<AppShortcutInfo>> GetInstalledApps()
47 List<AppShortcutInfo> appShortcutInfoList = new List<AppShortcutInfo>();
49 var installedAppList = await ApplicationManagerUtils.Instance.GetAllInstalledApplication();
51 foreach (KeyValuePair<string, string[]> item in installedAppList)
53 if (ManagedApps.IsNonPinnableApps(item.Key))
58 var defaultStateDescription = new StateDescription()
60 Label = item.Value[0],
61 IconPath = item.Value[2],
62 Action = new AppControlAction()
69 if (long.TryParse(item.Value[3], out longDate) == false)
71 longDate = long.MinValue;
74 var appShortcutInfo = new AppShortcutInfo()
76 IsRemovable = ApplicationManagerUtils.Instance.GetAppInfoRemovable(item.Key),
77 Installed = new DateTime(longDate),
80 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
81 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
82 appShortcutInfo.AppID = item.Key;
83 appShortcutInfoList.Add(appShortcutInfo);
86 return appShortcutInfoList;
90 /// A method appends an All Apps Shortcut and a MediaHub Shortcut at first in the given App Shortcut list.
91 /// Actually this method is used for making the Pinned App Shortcut panel of the TVHome
93 /// <seealso cref="GetDefaultShortcuts"/>
94 /// <seealso cref="GetPinnedAppsWithDefaultShortcuts"/>
95 /// <param name="returnPinnedAppsInfo">An App Shortcut list contains the additional Shortcuts.</param>
96 private void AddAllAppsAndMediaHubShortcut(ref List<ShortcutInfo> returnPinnedAppsInfo)
98 var allAppsShortcutInfo = new AppShortcutInfo()
107 IconPath = "ic_home_menu_apps_138.png",
108 Action = new AppControlAction
110 AppID = ManagedApps.TVAppsAppID,
119 IconPath = "ic_home_menu_apps_182.png",
120 Action = new AppControlAction
122 AppID = ManagedApps.TVAppsAppID,
129 allAppsShortcutInfo.UpdateState();
130 returnPinnedAppsInfo.Insert(0, allAppsShortcutInfo);
132 var mediaHubShortcutInfo = new AppShortcutInfo()
141 IconPath = "ic_launcher_mediahub_138.png",
142 Action = new AppControlAction
144 AppID = ManagedApps.MediaHubAppID,
153 IconPath = "ic_launcher_mediahub_182.png",
154 Action = new AppControlAction
156 AppID = ManagedApps.MediaHubAppID,
163 mediaHubShortcutInfo.UpdateState();
164 returnPinnedAppsInfo.Insert(1, mediaHubShortcutInfo);
168 /// A method appends an Add Pin Shortcut in the given App Shortcut list.
170 /// <seealso cref="GetDefaultShortcuts"/>
171 /// <seealso cref="GetPinnedAppsWithDefaultShortcuts"/>
172 /// <param name="returnPinnedAppsInfo">An App Shortcut list contains the additional Shortcuts.</param>
173 private void AppendAddPinShortcut(ref List<ShortcutInfo> returnPinnedAppsInfo)
175 var addPinCommandAction = new CommandAction()
177 NextStateDescription = "default",
178 CommandParameter = "",
181 addPinCommandAction.Command += new EventHandler<string>((s, e) =>
183 AppControlUtils.SendAddAppRequestToApps();
186 var addPinShortcutInfo = new AppShortcutInfo()
195 IconPath = "ic_home_menu_addpin_138.png",
196 Action = addPinCommandAction,
204 IconPath = "ic_home_menu_addpin_182.png",
205 Action = addPinCommandAction,
211 addPinShortcutInfo.UpdateState();
212 returnPinnedAppsInfo.Add(addPinShortcutInfo);
216 /// A method provides Pinned App Shortcut list by retrieving from the AppShortcutStorage.
217 /// This method provides only the Pinned App Shortcut list not including any additional Shortcuts
218 /// such as the All Apps, the Media Hub, and the Add Pin.
220 /// <returns>A Pinned App Shortcut list.</returns>
221 private async Task<List<ShortcutInfo>> GetPinnedApps()
223 IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
225 List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
227 foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
229 if (ManagedApps.IsNonPinnableApps(appShortcutInfo.AppID))
234 Dictionary<string, string> appInfo = ApplicationManagerUtils.Instance.GetInstalledApplication(appShortcutInfo.AppID);
244 if (appInfo.TryGetValue("Label", out appLabel) == false)
246 appLabel = "No Name";
249 appInfo.TryGetValue("IconPath", out appIconPath);
251 var defaultStateDescription = new StateDescription()
254 IconPath = appIconPath ?? DefaultAppIcon,
255 Action = new AppControlAction
257 AppID = appShortcutInfo.AppID,
261 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
262 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
263 appShortcutInfo.IsPinned = true;
264 returnPinnedAppsInfo.Add(appShortcutInfo);
267 return returnPinnedAppsInfo;
271 /// A method provides an App Shortcut list which contains default App Shortcuts
272 /// such as the All Apps, the Media Hub, and the Add Pin.
274 /// <returns>A default App Shortcut list.</returns>
275 public IEnumerable<ShortcutInfo> GetDefaultShortcuts()
277 List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
279 AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
280 AppendAddPinShortcut(ref returnPinnedAppsInfo);
282 return returnPinnedAppsInfo;
286 /// A method provides the App Shortcut list with the default App Shortcut icons.
287 /// The pinned apps, the All Apps, the MediaHub, and the Add Pin are included in the return App Shortcut list.
289 /// <returns>App Shortcut list.</returns>
290 public async Task<IEnumerable<ShortcutInfo>> GetPinnedAppsWithDefaultShortcuts()
292 List<ShortcutInfo> returnPinnedAppsInfo = await GetPinnedApps();
294 AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
295 AppendAddPinShortcut(ref returnPinnedAppsInfo);
297 return returnPinnedAppsInfo;
301 /// A method provides only pinned app's app ID as a hash table.
303 /// <returns>A hash table includes pinned app's app ID.</returns>
304 public async Task<Dictionary<string, string>> GetPinnedAppsAppIDs()
306 IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
307 Dictionary<string, string> pinnedAppsDictionary = new Dictionary<string, string>();
309 foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
311 if (ManagedApps.IsNonPinnableApps(appShortcutInfo.AppID))
316 pinnedAppsDictionary.Add(appShortcutInfo.AppID, appShortcutInfo.AppID);
319 return pinnedAppsDictionary;
323 /// A method updates the pinned App list by using the AppShortcutStorage.
325 /// <param name="pinnedAppsInfo">A pinned app list.</param>
326 public void UpdatePinnedApps(IEnumerable<AppShortcutInfo> pinnedAppsInfo)
328 AppShortcutStorage.Write(pinnedAppsInfo);
332 /// A listener to get notification of the AppShortcutStorage.
334 /// <param name="eventListener">A Event Handler for the nonfiction of AppShortutStorage.</param>
335 public void AddFileSystemChangedListener(EventHandler<EventArgs> eventListener)
337 if (AppShortcutStorage.Instance != null)
339 AppShortcutStorage.Instance.AddStorageChangedListener(eventListener);
343 DebuggingUtils.Dbg("AppShortcutStorage Instance is NULL");