Remove default pinned app.
[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 using Xamarin.Forms;
20
21 using LibTVRefCommonPortable.DataModels;
22 using LibTVRefCommonPortable.Utils;
23 using System.Threading.Tasks;
24
25 namespace LibTVRefCommonPortable.Models
26 {
27     /// <summary>
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.
33     /// </summary>
34     public class AppShortcutController
35     {
36         /// <summary>
37         /// The app ID of TV reference home application
38         /// </summary>
39         // TODO : change application later
40         public static string TVHomeAppID = "org.tizen.xahome";
41
42         /// <summary>
43         /// The app ID of TV reference apps-tray application
44         /// </summary>
45         public static string TVAppsAppID = "org.tizen.xaapps";
46
47         /// <summary>
48         /// The app ID of TV reference apps-tray application
49         /// </summary>
50         public static string MediaHubAppID = "org.tizen.xamediahub";
51
52         /// <summary>
53         /// A default app icon for no icon applications.
54         /// </summary>
55         private static String DefaultAppIcon = "AppIcon.png";
56
57
58         private bool IsNonPinnableApps(string appID)
59         {
60             if (appID.CompareTo(TVHomeAppID) == 0 ||
61                   appID.CompareTo(TVAppsAppID) == 0 ||
62                   appID.CompareTo(MediaHubAppID) == 0)
63             {
64                 return true;
65             }
66
67             return false;
68         }
69
70         /// <summary>
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.
73         /// </summary>
74         /// <returns>A installed app list.</returns>
75         public async Task<IEnumerable<AppShortcutInfo>> GetInstalledApps()
76         {
77             List<AppShortcutInfo> appShortcutInfoList = new List<AppShortcutInfo>();
78
79             var installedAppList = await ApplicationManagerUtils.GetAllInstalledApplication();
80
81             foreach (KeyValuePair<string, string[]> item in installedAppList)
82             {
83                 var defaultStateDescription = new StateDescription()
84                 {
85                     Label = item.Value[0],
86                     IconPath = item.Value[2],
87                     Action = new AppControlAction()
88                     {
89                         AppID = item.Key,
90                     }
91                 };
92
93                 var appShortcutInfo = new AppShortcutInfo()
94                 {
95                     IsRemovable = ApplicationManagerUtils.GetAppInfoRemovable(item.Key),
96                     // TODO : Fill these correctly by using Tizen Device APIs
97                     Installed = DateUtils.GetRandomDate(),
98                     LastUsed = DateUtils.GetRandomDate(),
99                 };
100
101                 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
102                 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
103                 appShortcutInfo.AppID = item.Key;
104                 appShortcutInfoList.Add(appShortcutInfo);
105             }
106
107             return appShortcutInfoList;
108         }
109
110         /// <summary>
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
113         /// </summary>
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)
118         {
119             var allAppsStateDescription = new StateDescription()
120             {
121                 Label = "All apps",
122                 IconPath = "ic_tizen_home_list_allapps_normal.png",
123                 Action = new AppControlAction
124                 {
125                     AppID = TVAppsAppID,
126                 }
127             };
128
129             var allAppsShortcutInfo = new AppShortcutInfo();
130
131             allAppsShortcutInfo.StateDescriptions.Add("default", allAppsStateDescription);
132             allAppsShortcutInfo.CurrentStateDescription = allAppsStateDescription;
133
134             returnPinnedAppsInfo.Insert(0, allAppsShortcutInfo);
135
136             var mediaHubStateDescription = new StateDescription()
137             {
138                 Label = "Media Hub",
139                 IconPath = "ic_tizen_home_list_mediahub_normal.png",
140                 Action = new AppControlAction
141                 {
142                     AppID = MediaHubAppID,
143                 }
144             };
145
146             var mediaHubShortcutInfo = new AppShortcutInfo();
147             mediaHubShortcutInfo.StateDescriptions.Add("default", mediaHubStateDescription);
148             mediaHubShortcutInfo.CurrentStateDescription = mediaHubStateDescription;
149
150             returnPinnedAppsInfo.Insert(1, mediaHubShortcutInfo);
151         }
152
153         /// <summary>
154         /// A method appends a Add Pin Shortcut in the given App Shortcut list.
155         /// </summary>
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)
160         {
161             var addPinStateDescription = new StateDescription()
162             {
163                 Label = "Add pin",
164                 IconPath = "ic_tizen_home_list_addpin_normal.png",
165                 Action = new CommandAction()
166                 {
167                     NextStateDescription = "default",
168                     Command = new Command<string>((key) =>
169                     {
170                         AppControlUtils.SendAddAppRequestToApps();
171                     }),
172                     CommandParameter = "",
173                 }
174             };
175
176             var addPinShortcutInfo = new AppShortcutInfo();
177
178             addPinShortcutInfo.StateDescriptions.Add("default", addPinStateDescription);
179             addPinShortcutInfo.CurrentStateDescription = addPinStateDescription;
180
181             returnPinnedAppsInfo.Add(addPinShortcutInfo);
182         }
183
184         /// <summary>
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.
188         /// </summary>
189         /// <returns>A Pinned App Shortcut list.</returns>
190         private async Task<List<ShortcutInfo>> GetPinnedApps()
191         {
192             IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
193
194             List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
195
196             foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
197             {
198                 if (IsNonPinnableApps(appShortcutInfo.AppID))
199                 {
200                     continue;
201                 }
202
203                 Dictionary<string, string> appInfo = ApplicationManagerUtils.GetInstalledApplication(appShortcutInfo.AppID);
204
205                 if (appInfo == null)
206                 {
207                     continue;
208                 }
209
210                 string appLabel;
211                 string appIconPath;
212
213                 if (appInfo.TryGetValue("Label", out appLabel) == false)
214                 {
215                     appLabel = "No Name";
216                 }
217
218                 appInfo.TryGetValue("IconPath", out appIconPath);
219
220                 var defaultStateDescription = new StateDescription()
221                 {
222                     Label = appLabel,
223                     IconPath = appIconPath ?? DefaultAppIcon,
224                     Action = new AppControlAction
225                     {
226                         AppID = appShortcutInfo.AppID,
227                     }
228                 };
229
230                 appShortcutInfo.StateDescriptions.Add("default", defaultStateDescription);
231                 appShortcutInfo.CurrentStateDescription = defaultStateDescription;
232                 appShortcutInfo.IsPinned = true;
233                 returnPinnedAppsInfo.Add(appShortcutInfo);
234             }
235
236             return returnPinnedAppsInfo;
237         }
238
239         /// <summary>
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.
242         /// </summary>
243         /// <returns>A default App Shortcut list.</returns>
244         public IEnumerable<ShortcutInfo> GetDefaultShortcuts()
245         {
246             List<ShortcutInfo> returnPinnedAppsInfo = new List<ShortcutInfo>();
247
248             AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
249             AppendAddPinShortcut(ref returnPinnedAppsInfo);
250
251             return returnPinnedAppsInfo;
252         }
253
254         /// <summary>
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.
257         /// </summary>
258         /// <returns>App Shortcut list.</returns>
259         public async Task<IEnumerable<ShortcutInfo>> GetPinnedAppsWithDefaultShortcuts()
260         {
261             List<ShortcutInfo> returnPinnedAppsInfo = await GetPinnedApps();
262
263             AddAllAppsAndMediaHubShortcut(ref returnPinnedAppsInfo);
264             AppendAddPinShortcut(ref returnPinnedAppsInfo);
265
266             return returnPinnedAppsInfo;
267         }
268
269         /// <summary>
270         /// A method provides only pinned app's app ID as a hash table.
271         /// </summary>
272         /// <returns>A hash table includes pinned app's app ID.</returns>
273         public async Task<Dictionary<string, string>> GetPinnedAppsAppIDs()
274         {
275             IApplicationManagerAPIs applicationManagerPort = DependencyService.Get<IApplicationManagerAPIs>();
276             IEnumerable<AppShortcutInfo> pinned_apps_info = await AppShortcutStorage.Read();
277
278             Dictionary<string, string> pinnedAppsDictionary = new Dictionary<string, string>();
279
280             foreach (AppShortcutInfo appShortcutInfo in pinned_apps_info)
281             {
282                 if (IsNonPinnableApps(appShortcutInfo.AppID))
283                 {
284                     continue;
285                 }
286
287                 pinnedAppsDictionary.Add(appShortcutInfo.AppID, appShortcutInfo.AppID);
288             }
289
290             return pinnedAppsDictionary;
291         }
292
293         /// <summary>
294         /// A method updates the pinned App list by using the AppShortcutStorage.
295         /// </summary>
296         /// <param name="pinnedAppsInfo">A pinned app list.</param>
297         public void UpdatePinnedApps(IEnumerable<AppShortcutInfo> pinnedAppsInfo)
298         {
299             AppShortcutStorage.Write(pinnedAppsInfo);
300         }
301
302         /// <summary>
303         /// A listener to get notification of the AppShortcutStorage.
304         /// </summary>
305         /// <param name="eventListener">A Event Handler for the nonfiction of AppShortutStorage.</param>
306         public void AddFileSystemChangedListener(EventHandler<EventArgs> eventListener)
307         {
308             if (AppShortcutStorage.Instance != null)
309             {
310                 AppShortcutStorage.Instance.AddStorageChangedListener(eventListener);
311             }
312             else
313             {
314                 DebuggingUtils.Dbg("AppShortcutStorage Instance is NULL");
315             }
316         }
317     }
318 }