94157ee389cef834f5ba636027338c0e7c491b0c
[profile/tv/apps/dotnet/home.git] / LibTVRefCommonPortable / Utils / AppShortcutStorage.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 using System;
17 using System.Collections.Generic;
18 using System.Xml.Serialization;
19 using System.IO;
20
21 using LibTVRefCommonPortable.DataModels;
22
23 using Xamarin.Forms;
24 using System.Threading.Tasks;
25 using System.Diagnostics;
26
27 namespace LibTVRefCommonPortable.Utils
28 {
29     /// <summary>
30     /// A class manages App Shortcuts by using subsystem.
31     /// </summary>
32     public class AppShortcutStorage
33     {
34         /// <summary>
35         /// A storage path.
36         /// </summary>
37         // TODO : Make this working properly, a accessing below directory is not permitted.
38         // private String storagePath = "/home/owner/apps_rw/xahome/res/pinned_apps_info.xml";
39         private static String storagePath = "/opt/usr/home/owner/share/pinned_apps_info.xml";
40
41         /// <summary>
42         /// A file system watcher which checks if the targeted storage is changed.
43         /// </summary>
44         private static IFileSystemWatcherAPIs fileSystemWatcher = DependencyService.Get<IFileSystemWatcherAPIs>();
45
46         /// <summary>
47         /// A instance of AppShortcutStorage.
48         /// </summary>
49         private static AppShortcutStorage instance = new AppShortcutStorage();
50
51         /// <summary>
52         /// A instance of AppShortcutStorage.
53         /// </summary>
54         public static AppShortcutStorage Instance
55         {
56             get { return instance; }
57         }
58
59         /// <summary>
60         /// A constructor of AppShortcutStorage.
61         /// </summary>
62         private AppShortcutStorage()
63         {
64             fileSystemWatcher.Run();
65         }
66
67         /// <summary>
68         /// A method provides sample App Shortcuts.
69         /// </summary>
70         /// <returns>a App Shortcut list</returns>
71         private static List<AppShortcutInfo> GetSampleList()
72         {
73             var pinnedAppsInfo = new List<AppShortcutInfo>();
74
75             pinnedAppsInfo.Add(new AppShortcutInfo()
76             {
77                 AppID = "org.tizen.settings",
78             });
79
80             pinnedAppsInfo.Add(new AppShortcutInfo()
81             {
82                 AppID = "org.tizen.apps",
83             });
84
85             pinnedAppsInfo.Add(new AppShortcutInfo()
86             {
87                 AppID = "org.tizen.home",
88             });
89
90             return pinnedAppsInfo;
91         }
92
93         /// <summary>
94         /// A method provides a App Shortcut list.
95         /// </summary>
96         /// <returns>A App Shortcut list.</returns>
97         public static async Task<IEnumerable<AppShortcutInfo>> Read()
98         {
99             IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
100
101             if (fileSystem.IsFileExist(storagePath) == false)
102             {
103                 // TODO : Modify default pinned Apps
104                 DebuggingUtils.Err("Set Default Pinned Apps" + storagePath);
105                 List<AppShortcutInfo> result = GetSampleList();
106                 Write(result);
107                 return result;
108             }
109
110             for (int i = 0; i < 5; i++)
111             {
112                 if (fileSystem.IsFileReady(storagePath))
113                 {
114                     break;
115                 }
116                 else if (i >= 4)
117                 {
118                     DebuggingUtils.Err("Can't open storage" + storagePath);
119                     return new List<AppShortcutInfo>();
120                 }
121
122                 await Task.Delay(100);
123                 DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + storagePath);
124             }
125
126             using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Open))
127             {
128                 Debug.Assert(fileStream != null);
129
130                 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
131                 StreamReader streamReader = new StreamReader(fileStream);
132                 List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
133
134                 return list;
135             }
136         }
137
138         /// <summary>
139         /// A method updates App Shortcuts of the storage
140         /// </summary>
141         /// <param name="pinnedAppInfo">A App Shortcuts that pinned by a user.</param>
142         /// <returns>A status of storage update.</returns>
143         public static bool Write(IEnumerable<AppShortcutInfo> pinnedAppInfo)
144         {
145             IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
146
147             using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Create))
148             {
149                 Debug.Assert(fileStream != null);
150
151                 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
152                 StreamWriter streamWriter = new StreamWriter(fileStream);
153                 serializer.Serialize(streamWriter, pinnedAppInfo);
154                 streamWriter.Flush();
155             }
156
157             return true;
158         }
159
160         /// <summary>
161         /// A method sets a event listener for the storage watcher
162         /// </summary>
163         /// <param name="eventListener">A event handler for the storage event </param>
164         public void AddStorageChangedListener(EventHandler<EventArgs> eventListener)
165         {
166             fileSystemWatcher.CustomChanged += eventListener;
167         }
168     }
169 }