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 sample App Shortcuts.
70 /// <returns>a App Shortcut list</returns>
71 private static List<AppShortcutInfo> GetSampleList()
73 var pinnedAppsInfo = new List<AppShortcutInfo>();
75 pinnedAppsInfo.Add(new AppShortcutInfo()
77 AppID = "org.tizen.settings",
80 pinnedAppsInfo.Add(new AppShortcutInfo()
82 AppID = "org.tizen.apps",
85 pinnedAppsInfo.Add(new AppShortcutInfo()
87 AppID = "org.tizen.home",
90 return pinnedAppsInfo;
94 /// A method provides a App Shortcut list.
96 /// <returns>A App Shortcut list.</returns>
97 public static async Task<IEnumerable<AppShortcutInfo>> Read()
99 IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
101 if (fileSystem.IsFileExist(storagePath) == false)
103 // TODO : Modify default pinned Apps
104 DebuggingUtils.Err("Set Default Pinned Apps" + storagePath);
105 List<AppShortcutInfo> result = GetSampleList();
110 for (int i = 0; i < 5; i++)
112 if (fileSystem.IsFileReady(storagePath))
118 DebuggingUtils.Err("Can't open storage" + storagePath);
119 return new List<AppShortcutInfo>();
122 await Task.Delay(100);
123 DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + storagePath);
126 using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Open))
128 Debug.Assert(fileStream != null);
130 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
131 StreamReader streamReader = new StreamReader(fileStream);
132 List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
139 /// A method updates App Shortcuts of the storage
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)
145 IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
147 using (Stream fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Create))
149 Debug.Assert(fileStream != null);
151 XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
152 StreamWriter streamWriter = new StreamWriter(fileStream);
153 serializer.Serialize(streamWriter, pinnedAppInfo);
154 streamWriter.Flush();
161 /// A method sets a event listener for the storage watcher
163 /// <param name="eventListener">A event handler for the storage event </param>
164 public void AddStorageChangedListener(EventHandler<EventArgs> eventListener)
166 fileSystemWatcher.CustomChanged += eventListener;