Remove default pinned app.
[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 a App Shortcut list.
69         /// </summary>
70         /// <returns>A App Shortcut list.</returns>
71         public static async Task<IEnumerable<AppShortcutInfo>> Read()
72         {
73             IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
74
75             if (fileSystem.IsFileExist(storagePath) == false)
76             {
77                 DebuggingUtils.Err("Set Default Pinned Apps" + storagePath);
78                 List<AppShortcutInfo> result = new List<AppShortcutInfo>();
79                 Write(result);
80                 return result;
81             }
82
83             for (int i = 0; i < 5; i++)
84             {
85                 if (fileSystem.IsFileReady(storagePath))
86                 {
87                     break;
88                 }
89                 else if (i >= 4)
90                 {
91                     DebuggingUtils.Err("Can't open storage" + storagePath);
92                     return new List<AppShortcutInfo>();
93                 }
94
95                 await Task.Delay(100);
96                 DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + storagePath);
97             }
98
99             using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Open))
100             {
101                 Debug.Assert(fileStream != null);
102
103                 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
104                 StreamReader streamReader = new StreamReader(fileStream);
105                 List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
106
107                 return list;
108             }
109         }
110
111         /// <summary>
112         /// A method updates App Shortcuts of the storage
113         /// </summary>
114         /// <param name="pinnedAppInfo">A App Shortcuts that pinned by a user.</param>
115         /// <returns>A status of storage update.</returns>
116         public static bool Write(IEnumerable<AppShortcutInfo> pinnedAppInfo)
117         {
118             IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
119
120             using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Create))
121             {
122                 Debug.Assert(fileStream != null);
123
124                 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
125                 StreamWriter streamWriter = new StreamWriter(fileStream);
126                 serializer.Serialize(streamWriter, pinnedAppInfo);
127                 streamWriter.Flush();
128             }
129
130             return true;
131         }
132
133         /// <summary>
134         /// A method sets a event listener for the storage watcher
135         /// </summary>
136         /// <param name="eventListener">A event handler for the storage event </param>
137         public void AddStorageChangedListener(EventHandler<EventArgs> eventListener)
138         {
139             fileSystemWatcher.CustomChanged += eventListener;
140         }
141     }
142 }