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;
21 using LibTVRefCommonPortable.DataModels;
22 using LibTVRefCommonPortable.Utils;
23 using System.Threading.Tasks;
25 namespace LibTVRefCommonPortable.Models
28 /// A class provides Application related information to ViewModel.
29 /// The TVHome shows the Pinned app list when the App Home Menu is pressed,
30 /// by invoking AppShortcutController's API to retrieve the pinned app list.
31 /// The TVApps shows the installed the Tizen UI apps in the main screen.
32 /// To provides the installed apps, the TVApps invokes a AppShortcutController's API.
34 public class AppShortcutController
37 /// The app ID of TV reference home application
39 // TODO : change application later
40 public static string TVHomeAppID = "org.tizen.xahome";
43 /// The app ID of TV reference apps-tray application
45 public static string TVAppsAppID = "org.tizen.xaapps";
48 /// The app ID of TV reference apps-tray application
50 public static string MediaHubAppID = "org.tizen.xamediahub";
53 /// A default app icon for no icon applications.
55 private static String DefaultAppIcon = "AppIcon.png";
58 private bool IsNonPinnableApps(string appID)
60 if (appID.CompareTo(TVHomeAppID) == 0 ||
61 appID.CompareTo(TVAppsAppID) == 0 ||
62 appID.CompareTo(MediaHubAppID) == 0)
71 /// A method provides installed app list.
72 /// The returned app list has only Tizen UI apps not the system app or no display apps.
74 /// <returns>A installed app list.</returns>
75 public async Task<IEnumerable<AppShortcutInfo>> GetInstalledApps()
77 List<AppShortcutInfo> appShortcutInfoList = new List<AppShortcutInfo>();
79 var installedAppList = await ApplicationManagerUtils.GetAllInstalledApplication();
81 foreach (KeyValuePair<string, string[]> item in installedAppList)
83 var defaultStateDescription = new StateDescription()
85 Label = item.Value[0],
86 IconPath = item.Value[2],
87 Action = new AppControlAction()
93 var appShortcutInfo = new AppShortcutInfo()
95 IsRemovable = ApplicationManagerUtils.GetAppInfoRemovable(item.Key),
96 // TODO : Fill these correctly by using Tizen Device APIs
97 Installed = DateUtils.GetRandomDate(),
98 LastUsed = DateUtils.GetRandomDate(),
101 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
102 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
103 appShortcutInfo.AppID = item.Key;
104 appShortcutInfoList.Add(appShortcutInfo);
107 return appShortcutInfoList;
111 /// A method appends a All Apps Shortcut and a MediaHub Shortcut at first in the given App Shortcut list.
112 /// Actually this method is used for making the Pinned App Shortcut panel of the TVHome
114 /// <seealso cref="GetDefaultShortcuts"/>
115 /// <seealso cref="GetPinnedAppsWithDefaultShortcuts"/>
116 /// <param name="returnPinnedAppsInfo">A App Shortcut list contains the additional Shortcuts.</param>
117 private void AddAllAppsAndMediaHubShortcut(ref List<ShortcutInfo> returnPinnedAppsInfo)
119 var allAppsStateDescription = new StateDescription()
122 IconPath = "ic_tizen_home_list_allapps_normal.png",
123 Action = new AppControlAction
129 var allAppsShortcutInfo = new AppShortcutInfo();
131 allAppsShortcutInfo.StateDescriptions.Add("default", allAppsStateDescription);
132 allAppsShortcutInfo.CurrentStateDescription = allAppsStateDescription;
134 returnPinnedAppsInfo.Insert(0, allAppsShortcutInfo);
136 var mediaHubStateDescription = new StateDescription()
139 IconPath = "ic_tizen_home_list_mediahub_normal.png",
140 Action = new AppControlAction
142 AppID = MediaHubAppID,
146 var mediaHubShortcutInfo = new AppShortcutInfo();
147 mediaHubShortcutInfo.StateDescriptions.Add("default", mediaHubStateDescription);
148 mediaHubShortcutInfo.CurrentStateDescription = mediaHubStateDescription;
150 returnPinnedAppsInfo.Insert(1, mediaHubShortcutInfo);
154 /// A method appends a Add Pin Shortcut in the given App Shortcut list.
156 /// <seealso cref="GetDefaultShortcuts"/>
157 /// <seealso cref="GetPinnedAppsWithDefaultShortcuts"/>
158 /// <param name="returnPinnedAppsInfo">A App Shortcut list contains the additional Shortcuts.</param>
159 private void AppendAddPinShortcut(ref List<ShortcutInfo> returnPinnedAppsInfo)
161 var addPinStateDescription = new StateDescription()
164 IconPath = "ic_tizen_home_list_addpin_normal.png",
165 Action = new CommandAction()
167 NextStateDescription = "default",
168 Command = new Command<string>((key) =>
170 AppControlUtils.SendAddAppRequestToApps();
172 CommandParameter = "",
176 var addPinShortcutInfo = new AppShortcutInfo();
178 addPinShortcutInfo.StateDescriptions.Add("default", addPinStateDescription);
179 addPinShortcutInfo.CurrentStateDescription = addPinStateDescription;
181 returnPinnedAppsInfo.Add(addPinShortcutInfo);
185 /// A method provides Pinned App Shortcut list by retrieving from the AppShortcutStorage.
186 /// This method provides only the Pinned App Shortcut list not including any additional Shortcuts
187 /// such as the All Apps, the Media Hub, and the Add Pin.
189 /// <returns>A Pinned App Shortcut list.</returns>
190 private async Task<List<ShortcutInfo>> GetPinnedApps()
192 IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
194 List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
196 foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
198 if (IsNonPinnableApps(appShortcutInfo.AppID))
203 Dictionary<string, string> appInfo = ApplicationManagerUtils.GetInstalledApplication(appShortcutInfo.AppID);
213 if (appInfo.TryGetValue("Label", out appLabel) == false)
215 appLabel = "No Name";
218 appInfo.TryGetValue("IconPath", out appIconPath);
220 var defaultStateDescription = new StateDescription()
223 IconPath = appIconPath ?? DefaultAppIcon,
224 Action = new AppControlAction
226 AppID = appShortcutInfo.AppID,
230 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
231 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
232 appShortcutInfo.IsPinned = true;
233 returnPinnedAppsInfo.Add(appShortcutInfo);
236 return returnPinnedAppsInfo;
240 /// A method provides a App Shortcut list which contains default App Shortcuts
241 /// such as the All Apps, the Media Hub, and the Add Pin.
243 /// <returns>A default App Shortcut list.</returns>
244 public IEnumerable<ShortcutInfo> GetDefaultShortcuts()
246 List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
248 AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
249 AppendAddPinShortcut(ref returnPinnedAppsInfo);
251 return returnPinnedAppsInfo;
255 /// A method provides the App Shortcut list with the default App Shortcut icons.
256 /// The pinned apps, the All Apps, the MediaHub, and the Add Pin are included in the return App Shortcut list.
258 /// <returns>App Shortcut list.</returns>
259 public async Task<IEnumerable<ShortcutInfo>> GetPinnedAppsWithDefaultShortcuts()
261 List<ShortcutInfo> returnPinnedAppsInfo = await GetPinnedApps();
263 AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
264 AppendAddPinShortcut(ref returnPinnedAppsInfo);
266 return returnPinnedAppsInfo;
270 /// A method provides only pinned app's app ID as a hash table.
272 /// <returns>A hash table includes pinned app's app ID.</returns>
273 public async Task<Dictionary<string, string>> GetPinnedAppsAppIDs()
275 IApplicationManagerAPIs applicationManagerPort = DependencyService.Get<IApplicationManagerAPIs>();
276 IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
278 Dictionary<string, string> pinnedAppsDictionary = new Dictionary<string, string>();
280 foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
282 if (IsNonPinnableApps(appShortcutInfo.AppID))
287 pinnedAppsDictionary.Add(appShortcutInfo.AppID, appShortcutInfo.AppID);
290 return pinnedAppsDictionary;
294 /// A method updates the pinned App list by using the AppShortcutStorage.
296 /// <param name="pinnedAppsInfo">A pinned app list.</param>
297 public void UpdatePinnedApps(IEnumerable<AppShortcutInfo> pinnedAppsInfo)
299 AppShortcutStorage.Write(pinnedAppsInfo);
303 /// A listener to get notification of the AppShortcutStorage.
305 /// <param name="eventListener">A Event Handler for the nonfiction of AppShortutStorage.</param>
306 public void AddFileSystemChangedListener(EventHandler<EventArgs> eventListener)
308 if (AppShortcutStorage.Instance != null)
310 AppShortcutStorage.Instance.AddStorageChangedListener(eventListener);
314 DebuggingUtils.Dbg("AppShortcutStorage Instance is NULL");