Revert "Add Test cases for app shortcut, managedapps"
[profile/tv/apps/dotnet/home.git] / LibTVRefCommonPortable / Models / AppShortcutController.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
20 using LibTVRefCommonPortable.DataModels;
21 using LibTVRefCommonPortable.Utils;
22 using System.Threading.Tasks;
23
24 namespace LibTVRefCommonPortable.Models
25 {
26     /// <summary>
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.
32     /// </summary>
33     public class AppShortcutController
34     {
35         /// <summary>
36         /// A default app icon for no icon applications.
37         /// </summary>
38         private static String DefaultAppIcon = "AppIcon.png";
39
40         /// <summary>
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.
43         /// </summary>
44         /// <returns>An installed app list.</returns>
45         public async Task<IEnumerable<AppShortcutInfo>> GetInstalledApps()
46         {
47             List<AppShortcutInfo> appShortcutInfoList = new List<AppShortcutInfo>();
48
49             var installedAppList = await ApplicationManagerUtils.Instance.GetAllInstalledApplication();
50
51             foreach (KeyValuePair<string, string[]> item in installedAppList)
52             {
53                 if (ManagedApps.IsNonPinnableApps(item.Key))
54                 {
55                     continue;
56                 }
57
58                 var defaultStateDescription = new StateDescription()
59                 {
60                     Label = item.Value[0],
61                     IconPath = item.Value[2],
62                     Action = new AppControlAction()
63                     {
64                         AppID = item.Key,
65                     }
66                 };
67
68                 long longDate;
69                 if (long.TryParse(item.Value[3], out longDate) == false)
70                 {
71                     longDate = long.MinValue;
72                 }
73
74                 var appShortcutInfo = new AppShortcutInfo()
75                 {
76                     IsRemovable = ApplicationManagerUtils.Instance.GetAppInfoRemovable(item.Key),
77                     Installed = new DateTime(longDate),
78                 };
79
80                 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
81                 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
82                 appShortcutInfo.AppID = item.Key;
83                 appShortcutInfoList.Add(appShortcutInfo);
84             }
85
86             return appShortcutInfoList;
87         }
88
89         /// <summary>
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
92         /// </summary>
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)
97         {
98             var allAppsShortcutInfo = new AppShortcutInfo()
99             {
100                 StateDescriptions =
101                 {
102                     {
103                         "default",
104                         new StateDescription
105                         {
106                             Label = "All apps",
107                             IconPath = "ic_home_menu_apps_138.png",
108                             Action = new AppControlAction
109                             {
110                                 AppID = ManagedApps.TVAppsAppID,
111                             }
112                         }
113                     },
114                     {
115                         "focused",
116                         new StateDescription
117                         {
118                             Label = "All apps",
119                             IconPath = "ic_home_menu_apps_182.png",
120                             Action = new AppControlAction
121                             {
122                                 AppID = ManagedApps.TVAppsAppID,
123                             }
124                         }
125                     },
126                 }
127             };
128
129             allAppsShortcutInfo.UpdateState();
130             returnPinnedAppsInfo.Insert(0, allAppsShortcutInfo);
131
132             var mediaHubShortcutInfo = new AppShortcutInfo()
133             {
134                 StateDescriptions =
135                 {
136                     {
137                         "default",
138                         new StateDescription
139                         {
140                             Label = "Media Hub",
141                             IconPath = "ic_launcher_mediahub_138.png",
142                             Action = new AppControlAction
143                             {
144                                 AppID = ManagedApps.MediaHubAppID,
145                             }
146                         }
147                     },
148                     {
149                         "focused",
150                         new StateDescription
151                         {
152                             Label = "Media Hub",
153                             IconPath = "ic_launcher_mediahub_182.png",
154                             Action = new AppControlAction
155                             {
156                                 AppID = ManagedApps.MediaHubAppID,
157                             }
158                         }
159                     },
160                 }
161             };
162
163             mediaHubShortcutInfo.UpdateState();
164             returnPinnedAppsInfo.Insert(1, mediaHubShortcutInfo);
165         }
166
167         /// <summary>
168         /// A method appends an Add Pin Shortcut in the given App Shortcut list.
169         /// </summary>
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)
174         {
175             var addPinCommandAction = new CommandAction()
176             {
177                 NextStateDescription = "default",
178                 CommandParameter = "",
179             };
180
181             addPinCommandAction.Command += new EventHandler<string>((s, e) =>
182             {
183                 AppControlUtils.SendAddAppRequestToApps();
184             });
185
186             var addPinShortcutInfo = new AppShortcutInfo()
187             {
188                 StateDescriptions =
189                 {
190                     {
191                         "default",
192                         new StateDescription
193                         {
194                             Label = "Add pin",
195                             IconPath = "ic_home_menu_addpin_138.png",
196                             Action = addPinCommandAction,
197                         }
198                     },
199                     {
200                         "focused",
201                         new StateDescription
202                         {
203                             Label = "Add pin",
204                             IconPath = "ic_home_menu_addpin_182.png",
205                             Action = addPinCommandAction,
206                         }
207                     },
208                 }
209             };
210
211             addPinShortcutInfo.UpdateState();
212             returnPinnedAppsInfo.Add(addPinShortcutInfo);
213         }
214
215         /// <summary>
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.
219         /// </summary>
220         /// <returns>A Pinned App Shortcut list.</returns>
221         private async Task<List<ShortcutInfo>> GetPinnedApps()
222         {
223             IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
224
225             List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
226
227             foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
228             {
229                 if (ManagedApps.IsNonPinnableApps(appShortcutInfo.AppID))
230                 {
231                     continue;
232                 }
233
234                 Dictionary<string, string> appInfo = ApplicationManagerUtils.Instance.GetInstalledApplication(appShortcutInfo.AppID);
235
236                 if (appInfo == null)
237                 {
238                     continue;
239                 }
240
241                 string appLabel;
242                 string appIconPath;
243
244                 if (appInfo.TryGetValue("Label", out appLabel) == false)
245                 {
246                     appLabel = "No Name";
247                 }
248
249                 appInfo.TryGetValue("IconPath", out appIconPath);
250
251                 var defaultStateDescription = new StateDescription()
252                 {
253                     Label = appLabel,
254                     IconPath = appIconPath ?? DefaultAppIcon,
255                     Action = new AppControlAction
256                     {
257                         AppID = appShortcutInfo.AppID,
258                     }
259                 };
260
261                 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
262                 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
263                 appShortcutInfo.IsPinned = true;
264                 returnPinnedAppsInfo.Add(appShortcutInfo);
265             }
266
267             return returnPinnedAppsInfo;
268         }
269
270         /// <summary>
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.
273         /// </summary>
274         /// <returns>A default App Shortcut list.</returns>
275         public IEnumerable<ShortcutInfo> GetDefaultShortcuts()
276         {
277             List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
278
279             AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
280             AppendAddPinShortcut(ref returnPinnedAppsInfo);
281
282             return returnPinnedAppsInfo;
283         }
284
285         /// <summary>
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.
288         /// </summary>
289         /// <returns>App Shortcut list.</returns>
290         public async Task<IEnumerable<ShortcutInfo>> GetPinnedAppsWithDefaultShortcuts()
291         {
292             List<ShortcutInfo> returnPinnedAppsInfo = await GetPinnedApps();
293
294             AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
295             AppendAddPinShortcut(ref returnPinnedAppsInfo);
296
297             return returnPinnedAppsInfo;
298         }
299
300         /// <summary>
301         /// A method provides only pinned app's app ID as a hash table.
302         /// </summary>
303         /// <returns>A hash table includes pinned app's app ID.</returns>
304         public async Task<Dictionary<string, string>> GetPinnedAppsAppIDs()
305         {
306             IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
307             Dictionary<string, string> pinnedAppsDictionary = new Dictionary<string, string>();
308
309             foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
310             {
311                 if (ManagedApps.IsNonPinnableApps(appShortcutInfo.AppID))
312                 {
313                     continue;
314                 }
315
316                 pinnedAppsDictionary.Add(appShortcutInfo.AppID, appShortcutInfo.AppID);
317             }
318
319             return pinnedAppsDictionary;
320         }
321
322         /// <summary>
323         /// A method updates the pinned App list by using the AppShortcutStorage.
324         /// </summary>
325         /// <param name="pinnedAppsInfo">A pinned app list.</param>
326         public void UpdatePinnedApps(IEnumerable<AppShortcutInfo> pinnedAppsInfo)
327         {
328             AppShortcutStorage.Write(pinnedAppsInfo);
329         }
330
331         /// <summary>
332         /// A listener to get notification of the AppShortcutStorage.
333         /// </summary>
334         /// <param name="eventListener">A Event Handler for the nonfiction of AppShortutStorage.</param>
335         public void AddFileSystemChangedListener(EventHandler<EventArgs> eventListener)
336         {
337             if (AppShortcutStorage.Instance != null)
338             {
339                 AppShortcutStorage.Instance.AddStorageChangedListener(eventListener);
340             }
341             else
342             {
343                 DebuggingUtils.Dbg("AppShortcutStorage Instance is NULL");
344             }
345         }
346     }
347 }