*/
using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Xml;
using System.Xml.Serialization;
using System.IO;
using LibTVRefCommonPortable.DataModels;
-using LibTVRefCommonPortable.Utils;
using Xamarin.Forms;
+using System.Threading.Tasks;
+using System.Diagnostics;
namespace LibTVRefCommonPortable.Utils
{
+ /// <summary>
+ /// A class manages App Shortcuts by using subsystem.
+ /// </summary>
public class AppShortcutStorage
{
- // TODO : Make this working proeprly, a accessing below directory is not permitted.
- // private String storagePath = "/home/owner/apps_rw/TVHome.TizenTV/res/pinned_apps_info.xml";
- private static String storagePath = "/opt/usr/home/owner/share/pinned_apps_info.xml";
-
+ /// <summary>
+ /// A storage path.
+ /// </summary>
+ private static String StoragePath;
+
+ /// <summary>
+ /// A file system watcher which checks if the targeted storage is changed.
+ /// </summary>
private static IFileSystemWatcherAPIs fileSystemWatcher = DependencyService.Get<IFileSystemWatcherAPIs>();
+
+ /// <summary>
+ /// An instance of AppShortcutStorage.
+ /// </summary>
private static AppShortcutStorage instance = new AppShortcutStorage();
+ /// <summary>
+ /// An instance of AppShortcutStorage.
+ /// </summary>
public static AppShortcutStorage Instance
{
get { return instance; }
}
+ /// <summary>
+ /// A constructor of AppShortcutStorage.
+ /// </summary>
private AppShortcutStorage()
{
+ StoragePath = DependencyService.Get<IFileSystemAPIs>()?.PlatformShareStorage + "pinned_apps_info.xml";
+
fileSystemWatcher.Run();
}
- private static List<AppShortcutInfo> GetSampleList()
+ /// <summary>
+ /// A method provides an app Shortcut list.
+ /// </summary>
+ /// <returns>An app Shortcut list.</returns>
+ public static async Task<IEnumerable<AppShortcutInfo>> Read()
{
- var pinnedAppsInfo = new List<AppShortcutInfo>();
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.settings",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.chromium-efl.ubrowser",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.dpm-toolkit",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.infosquare",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.mediahub",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.heremaps-uc",
- });
-
- pinnedAppsInfo.Add(new AppShortcutInfo()
- {
- AppID = "org.tizen.ode",
- });
+ IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
- pinnedAppsInfo.Add(new AppShortcutInfo()
+ if (fileSystem.IsFileExist(StoragePath) == false)
{
- AppID = "org.tizen.apps",
- });
+ DebuggingUtils.Err("Set Default Pinned Apps" + StoragePath);
+ List<AppShortcutInfo> result = new List<AppShortcutInfo>();
+ Write(result);
+ return result;
+ }
- pinnedAppsInfo.Add(new AppShortcutInfo()
+ for (int i = 0; i < 5; i++)
{
- AppID = "org.tizen.home",
- });
-
- return pinnedAppsInfo;
- }
+ if (fileSystem.IsFileReady(StoragePath))
+ {
+ break;
+ }
+ else if (i >= 4)
+ {
+ DebuggingUtils.Err("Can't open storage" + StoragePath);
+ return new List<AppShortcutInfo>();
+ }
- public static List<AppShortcutInfo> Read()
- {
- IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
- XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
- Stream fileStream = null;
+ await Task.Delay(100);
+ DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + StoragePath);
+ }
- try
+ using (Stream fileStream = fileSystem.OpenFile(StoragePath, UtilFileMode.Open))
{
- fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Open);
- if (fileStream == null)
- {
- DebuggingUtils.Dbg("OpenFile failed : " + storagePath);
- // TODO : Remove later, below lines are sample apps for demonstration.
- List<AppShortcutInfo> result = GetSampleList();
- Write(result);
- return result;
- }
+ Debug.Assert(fileStream != null);
+ XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
StreamReader streamReader = new StreamReader(fileStream);
List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
return list;
}
- catch (Exception e)
- {
- // TODO : recover xml file here!
- DebuggingUtils.Err("XML Desearialize is failed, " + storagePath + ", " + e.Message);
- List<AppShortcutInfo> result = GetSampleList();
- Write(result);
- return result;
- }
- finally
- {
- if (fileStream != null)
- {
- fileSystem.CloseFile(fileStream);
- }
- }
}
+ /// <summary>
+ /// A method updates App Shortcuts of the storage
+ /// </summary>
+ /// <param name="pinnedAppInfo">An app Shortcuts that pinned by a user.</param>
+ /// <returns>A status of storage update.</returns>
public static bool Write(IEnumerable<AppShortcutInfo> pinnedAppInfo)
{
IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
- XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
- Stream fileStream = null;
- try
+ using (Stream fileStream = fileSystem.OpenFile(StoragePath, UtilFileMode.Create))
{
- fileStream = fileSystem.OpenFile(storagePath, UtilFileMode.Create);
- if (fileStream == null)
- {
- return false;
- }
+ Debug.Assert(fileStream != null);
+ XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
StreamWriter streamWriter = new StreamWriter(fileStream);
serializer.Serialize(streamWriter, pinnedAppInfo);
streamWriter.Flush();
}
- catch (Exception e)
- {
- DebuggingUtils.Err("XML Searialize is failed, " + storagePath + ", " + e.Message);
- return false;
- }
- finally
- {
- if (fileStream != null)
- {
- fileSystem.CloseFile(fileStream);
- }
-
- }
return true;
}
- public void AddFileSystemChangedListener(EventHandler<EventArgs> eventListener)
+ /// <summary>
+ /// A method sets an event listener for the storage watcher
+ /// </summary>
+ /// <param name="eventListener">An event handler for the storage event </param>
+ public void AddStorageChangedListener(EventHandler<EventArgs> eventListener)
{
fileSystemWatcher.CustomChanged += eventListener;
}