-/*
+/*
* Copyright (c) 2017 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.1 (the "License");
*/
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 LibTVRefCommmonPortable.DataModels;
-using LibTVRefCommmonPortable.Utils;
+using LibTVRefCommonPortable.DataModels;
using Xamarin.Forms;
+using System.Threading.Tasks;
+using System.Diagnostics;
-namespace LibTVRefCommmonPortable.Utils
+namespace LibTVRefCommonPortable.Utils
{
+ /// <summary>
+ /// A class manages App Shortcuts by using subsystem.
+ /// </summary>
public class AppShortcutStorage
{
+ /// <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",
- });
+ IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
- pinnedAppsInfo.Add(new AppShortcutInfo()
+ if (fileSystem.IsFileExist(StoragePath) == false)
{
- AppID = "org.tizen.ode",
- });
+ 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.apps",
- });
+ if (fileSystem.IsFileReady(StoragePath))
+ {
+ break;
+ }
+ else if (i >= 4)
+ {
+ DebuggingUtils.Err("Can't open storage" + StoragePath);
+ return new List<AppShortcutInfo>();
+ }
+
+ await Task.Delay(100);
+ DebuggingUtils.Dbg("[" + i + "/5] Waiting for Writing" + StoragePath);
+ }
- pinnedAppsInfo.Add(new AppShortcutInfo()
+ using (Stream fileStream = fileSystem.OpenFile(StoragePath, UtilFileMode.Open))
{
- AppID = "org.tizen.home",
- });
+ Debug.Assert(fileStream != null);
- return pinnedAppsInfo;
- }
+ XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
+ StreamReader streamReader = new StreamReader(fileStream);
+ List<AppShortcutInfo> list = (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
- public static List<AppShortcutInfo> ReadFromFile(string filePath)
- {
- IFileSystemAPIs fileSystem = DependencyService.Get<IFileSystemAPIs>();
- XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
- Stream fileStream = fileSystem.OpenFile(filePath, UtilFileMode.Open);
- if (fileStream == null)
- {
- DebuggingUtils.Dbg("OpenFile failed : " + filePath);
- // Below lines are just for sample
- List<AppShortcutInfo> result = GetSampleList();
- WriteToFile(result, filePath);
- return result;
+ return list;
}
-
- StreamReader streamReader = new StreamReader(fileStream);
- return (List<AppShortcutInfo>)serializer.Deserialize(streamReader);
}
- public static bool WriteToFile(IEnumerable<AppShortcutInfo> pinnedAppInfo, string filePath)
+ /// <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 = fileSystem.OpenFile(filePath, UtilFileMode.OpenOrCreate);
- if (fileStream == null)
+
+ using (Stream fileStream = fileSystem.OpenFile(StoragePath, UtilFileMode.Create))
{
- return false;
+ Debug.Assert(fileStream != null);
+
+ XmlSerializer serializer = new XmlSerializer(typeof(List<AppShortcutInfo>));
+ StreamWriter streamWriter = new StreamWriter(fileStream);
+ serializer.Serialize(streamWriter, pinnedAppInfo);
+ streamWriter.Flush();
}
- StreamWriter streamWriter = new StreamWriter(fileStream);
- serializer.Serialize(streamWriter, pinnedAppInfo);
- streamWriter.Flush();
- 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;
}