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.
17 using System.Collections.Generic;
18 using System.Xml.Serialization;
21 using LibTVRefCommonPortable.DataModels;
24 using System.Threading.Tasks;
25 using System.Diagnostics;
27 namespace LibTVRefCommonPortable.Utils
30 /// A class manages App Shortcuts by using subsystem.
32 public class AppShortcutStorage
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";
42 /// A file system watcher which checks if the targeted storage is changed.
44 private static IFileSystemWatcherAPIs fileSystemWatcher = DependencyService.Get<IFileSystemWatcherAPIs>();
47 /// A instance of AppShortcutStorage.
49 private static AppShortcutStorage instance = new AppShortcutStorage();
52 /// A instance of AppShortcutStorage.
54 public static AppShortcutStorage Instance
56 get { return instance; }
60 /// A constructor of AppShortcutStorage.
62 private AppShortcutStorage()
64 fileSystemWatcher.Run();
68 /// A method provides a App Shortcut list.
70 /// <returns>A App Shortcut list.</returns>
71 public static async Task<IEnumerable<AppShortcutInfo>> Read()
73 IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
75 if (fileSystem.IsFileExist(storagePath) == false)
77 DebuggingUtils.Err("Set Default Pinned Apps" + storagePath);
78 List<AppShortcutInfo> result = new List<AppShortcutInfo>();
83 for (int i = 0; i < 5; i++)
85 if (fileSystem.IsFileReady(storagePath))
91 DebuggingUtils.Err("Can't open storage" + storagePath);
92 return new List<AppShortcutInfo>();
95 await Task.Delay(100);
96 DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + storagePath);
99 using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Open))
101 Debug.Assert(fileStream != null);
103 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
104 StreamReader streamReader = new StreamReader(fileStream);
105 List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
112 /// A method updates App Shortcuts of the storage
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)
118 IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
120 using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Create))
122 Debug.Assert(fileStream != null);
124 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
125 StreamWriter streamWriter = new StreamWriter(fileStream);
126 serializer.Serialize(streamWriter, pinnedAppInfo);
127 streamWriter.Flush();
134 /// A method sets a event listener for the storage watcher
136 /// <param name="eventListener">A event handler for the storage event </param>
137 public void AddStorageChangedListener(EventHandler<EventArgs> eventListener)
139 fileSystemWatcher.CustomChanged += eventListener;