[Device] Added Displays and Vibrators property.
authorPraveen Gattu <gattu.p@samsung.com>
Tue, 24 May 2016 10:15:39 +0000 (15:45 +0530)
committerPraveen Gattu <gattu.p@samsung.com>
Tue, 24 May 2016 10:24:48 +0000 (15:54 +0530)
Change-Id: I74f53b1c3d49504cf1e8cbd8eb3f254e7dba3320
Signed-off-by: Praveen Gattu <gattu.p@samsung.com>
Tizen.System/Device/Display.cs
Tizen.System/Device/Haptic.cs

index ae60a19..321f278 100644 (file)
@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-
 namespace Tizen.System
 {
     /// <summary>
@@ -29,8 +28,8 @@ namespace Tizen.System
     public class Display
     {
         private readonly int _displayId;
-        private static readonly Dictionary<int, Display> s_displays = new Dictionary<int, Display>();
         private static readonly object s_lock = new object();
+        private static Lazy<IReadOnlyList<Display>> _lazyDisplays;
         private Display(int deviceNumber)
         {
             _displayId = deviceNumber;
@@ -52,7 +51,30 @@ namespace Tizen.System
                 return number;
             }
         }
+        /// <summary>
+        /// Get all the avaialble vibrators.
+        /// </summary>
+        public static IReadOnlyList<Display> Displays
+        {
+            get
+            {
+                _lazyDisplays = new Lazy<IReadOnlyList<Display>>(() => GetAllDisplayes());
+                return _lazyDisplays.Value;
+            }
+        }
+
 
+        private static IReadOnlyList<Display> GetAllDisplayes()
+        {
+            List<Display> displays = new List<Display>();
+
+            for (int i = 0; i < NumberOfDisplays; i++)
+            {
+                displays.Add(new Display(i));
+            }
+            return displays;
+
+        }
         /// <summary>
         /// The maximum brightness value that can be set.
         /// </summary>
@@ -96,24 +118,6 @@ namespace Tizen.System
             }
         }
         /// <summary>
-        /// Get the Display instance for the given display index.
-        /// </summary>
-        /// <param name="deviceNumber"> The index of the display.
-        /// It can be greater than or equal to 0 and less than the number of display devices </param>
-        public static Display GetDisplay(int deviceNumber)
-        {
-            //TODO: throw an exception for invalid display Id.
-            if(deviceNumber < 0 || deviceNumber >= NumberOfDisplays)
-            {
-                throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid display number");
-            }
-            if (!s_displays.ContainsKey(deviceNumber))
-            {
-                s_displays.Add(deviceNumber, new Display(deviceNumber));
-            }
-            return s_displays[deviceNumber];
-        }
-        /// <summary>
         /// The current display state.
         /// </summary>
         public static DisplayState State
index 6af4d7e..4bd431f 100644 (file)
@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-
 namespace Tizen.System
 {
     /// <summary>
@@ -9,9 +8,10 @@ namespace Tizen.System
     public class Vibrator : IDisposable
     {
         private readonly int _vibratorId;
-        private static readonly Dictionary<int, Vibrator> s_vibrators = new Dictionary<int, Vibrator>();
         private IntPtr _hapticHandle;
         private bool _disposedValue = false;
+        private static Lazy<IReadOnlyList<Vibrator>> _lazyVibrators;
+        private static int _maxcount = 0;
         private Vibrator(int id)
         {
             _vibratorId = id;
@@ -20,39 +20,27 @@ namespace Tizen.System
             {
                 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
             }
+            res = (DeviceError)Interop.Device.DeviceHapticGetCount(out _maxcount);
+            if (res != DeviceError.None)
+            {
+                Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
+            }
+
         }
 
         ~Vibrator()
         {
             Dispose(false);
         }
-
         /// <summary>
-        /// Get the Vibrator instance for the given vibrator index.
-        /// </summary>
-        /// <param name="deviceId"> The index of the vibrator.
-        /// It can be greater than or equal to 0 and less than the number of vibrators. </param>
-        public static Vibrator GetVibrator(int deviceNumber)
-        {
-            if (deviceNumber < 0 || deviceNumber >= NumberOfVibrators)
-            {
-                throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid vibrator number");
-            }
-            if (!s_vibrators.ContainsKey(deviceNumber))
-            {
-                s_vibrators.Add(deviceNumber, new Vibrator(deviceNumber));
-            }
-            return s_vibrators[deviceNumber];
-        }
-        /// <summary>
-        /// Gets the number of vibrators.
+        /// Get the number of avaialble vibrators.
         /// </summary>
         public static int NumberOfVibrators
         {
             get
             {
                 int count = 0;
-                DeviceError res = (DeviceError) Interop.Device.DeviceHapticGetCount(out count);
+                DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
                 if (res != DeviceError.None)
                 {
                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
@@ -61,6 +49,33 @@ namespace Tizen.System
             }
         }
         /// <summary>
+        /// Get all the avaialble vibrators.
+        /// </summary>
+        public static IReadOnlyList<Vibrator> Vibrators
+        {
+            get
+            {
+                _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
+                return _lazyVibrators.Value;
+            }
+        }
+        private static IReadOnlyList<Vibrator> GetAllVibrators()
+        {
+            int count = 0;
+            List< Vibrator > vibrators = new List<Vibrator>();
+            DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
+            if (res != DeviceError.None)
+            {
+                throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
+            }
+            for(int i = 0; i< NumberOfVibrators; i++)
+            {
+                vibrators.Add(new Vibrator(i));
+            }
+            return vibrators;
+
+        }
+        /// <summary>
         /// Vibrates during the specified time with a constant intensity.
         /// This function can be used to start monotonous vibration for the specified time.
         /// </summary>