Refactor Badge structure 31/140631/9
authorSeungha Son <seungha.son@samsung.com>
Tue, 30 May 2017 09:16:51 +0000 (18:16 +0900)
committerSeungha Son <seungha.son@samsung.com>
Thu, 10 Aug 2017 00:12:22 +0000 (09:12 +0900)
 - Change parameter of methods of BadgeManager class from individual
   infomation to Badge instance.
 - Adds NotSupported exception.

Signed-off-by: Seungha Son <seungha.son@samsung.com>
Change-Id: I1f140d1995cfffb6cb6bd8f33b0fd8ed4621b32a

Tizen.Applications.Badge/Tizen.Applications/Badge.cs
Tizen.Applications.Badge/Tizen.Applications/BadgeControl.cs
Tizen.Applications.Badge/Tizen.Applications/BadgeErrorFactory.cs
Tizen.Applications.Badge/Tizen.Applications/BadgeEventArgs.cs

index 89b317a..2361942 100755 (executable)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Licensed under the Apache License, Version 2.0 (the License);
+ * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 namespace Tizen.Applications
 {
     /// <summary>
-    /// Immutable class for getting information of the badge.
+    /// The class containing common properties of the Badge.
     /// </summary>
     public class Badge
     {
-        private readonly string _appId;
-        private readonly int _count;
-        private readonly bool _isDisplay;
+        private int count = 0;
 
-        internal Badge(string appid, int count, bool isDisplay)
+        /// <summary>
+        /// Initializes a new instance of the Badge class.
+        /// </summary>
+        /// <since_tizen> 3 </since_tizen>
+        /// <param name="appId">Application ID</param>
+        /// <param name="count">Count value</param>
+        /// <param name="visible">True if it should be displayed</param>
+        /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
+        public Badge(string appId, int count = 1, bool visible = true)
         {
-            _appId = appid;
-            _count = count;
-            _isDisplay = isDisplay;
+            if (IsNegativeNumber(count))
+            {
+                throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "The count must be positive number");
+            }
+            AppId = appId;
+            this.count = count;
+            Visible = visible;
         }
 
         /// <summary>
@@ -40,7 +50,16 @@ namespace Tizen.Applications
         {
             get
             {
-                return _count;
+                return count;
+            }
+            set
+            {
+                if (IsNegativeNumber(value))
+                {
+                    throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "The count must be positive number");
+                }
+
+                count = value;
             }
         }
 
@@ -48,24 +67,17 @@ namespace Tizen.Applications
         /// Property for the application ID of the badge.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        public string AppId
-        {
-            get
-            {
-                return _appId;
-            }
-        }
+        public string AppId { get; set; }
 
         /// <summary>
-        /// Property for the flag of 'display'.
+        /// Property for display visibility. True if the badge display visible, otherwise false..
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        public bool IsDisplay
+        public bool Visible{ get; set; }
+
+        private bool IsNegativeNumber(int number)
         {
-            get
-            {
-                return _isDisplay;
-            }
+            return number < 0;
         }
     }
 }
index 85e72a7..3c5b60e 100755 (executable)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Licensed under the Apache License, Version 2.0 (the License);
+ * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
@@ -31,9 +31,11 @@ namespace Tizen.Applications
         /// Event handler for receiving badge events.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
         public static event EventHandler<BadgeEventArgs> Changed
         {
             add
@@ -78,10 +80,13 @@ namespace Tizen.Applications
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         /// <param name="appId">Application ID.</param>
+        /// <returns>The Badge object with inputted application ID</returns>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
         public static Badge Find(string appId)
         {
             uint count;
@@ -107,10 +112,12 @@ namespace Tizen.Applications
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
         /// <param name="appId">Application ID.</param>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="ArgumentException">Thrown when failed because of a an invalid argument.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
         public static void Remove(string appId)
         {
             BadgeError err = Interop.Badge.Remove(appId);
@@ -121,51 +128,58 @@ namespace Tizen.Applications
         }
 
         /// <summary>
-        /// Adds the badge information.
+        /// Removes the badge information.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        /// <param name="appId">Application ID.</param>
-        /// <param name="count">Count value.</param>
-        /// <param name="isDisplay">True if it should be displayed.</param>
+        /// <param name="badge">The Badge object.</param>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
-        public static void Add(string appId, int count = 1, bool isDisplay = true)
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
+        public static void Remove(Badge badge)
         {
-            BadgeError err = Interop.Badge.Add(appId);
-            if (err != BadgeError.None)
+            if (badge == null)
             {
-                throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + appId);
+                throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
             }
 
-            try
-            {
-                Update(appId, count, isDisplay);
-            }
-            catch (Exception e)
-            {
-                Remove(appId);
-                throw e;
-            }
+            Remove(badge.AppId);
         }
 
         /// <summary>
-        /// Updates the badge information.
+        /// Adds the badge information.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        /// <param name="appId">Application ID.</param>
-        /// <param name="count">Count value.</param>
+        /// <param name="badge">The Badge object.</param>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
-        public static void Update(string appId, int count)
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
+        public static void Add(Badge badge)
         {
-            BadgeError err = Interop.Badge.SetCount(appId, (uint)count);
+            if (badge == null)
+            {
+                throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
+            }
+
+            BadgeError err = Interop.Badge.Add(badge.AppId);
             if (err != BadgeError.None)
             {
-                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
+                throw BadgeErrorFactory.GetException(err, "Failed to add badge of " + badge.AppId);
+            }
+
+            try
+            {
+                Update(badge);
+            }
+            catch (Exception e)
+            {
+                Remove(badge.AppId);
+                throw e;
             }
         }
 
@@ -173,45 +187,43 @@ namespace Tizen.Applications
         /// Updates the badge information.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
-        /// <param name="appId">Application ID.</param>
-        /// <param name="isDisplay">True if it should be displayed.</param>
+        /// <param name="badge">The Badge object.</param>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
-        public static void Update(string appId, bool isDisplay)
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
+        public static void Update(Badge badge)
         {
-            BadgeError err = Interop.Badge.SetDisplay(appId, isDisplay ? 1U : 0U);
+            if (badge == null)
+            {
+                throw BadgeErrorFactory.GetException(BadgeError.InvalidParameter, "Invalid Badge object");
+            }
+
+            BadgeError err = Interop.Badge.SetCount(badge.AppId, (uint)badge.Count);
             if (err != BadgeError.None)
             {
-                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + appId);
+                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
             }
-        }
 
-        /// <summary>
-        /// Updates the badge information.
-        /// </summary>
-        /// <since_tizen> 3 </since_tizen>
-        /// <param name="appId">Application ID.</param>
-        /// <param name="count">Count value.</param>
-        /// <param name="isDisplay">True if it should be displayed.</param>
-        /// <exception cref="ArgumentException">Thrown when failed because of invalid argument.</exception>
-        /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
-        /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
-        public static void Update(string appId, int count, bool isDisplay)
-        {
-            Update(appId, count);
-            Update(appId, isDisplay);
+            err = Interop.Badge.SetDisplay(badge.AppId, badge.Visible ? 1U : 0U);
+            if (err != BadgeError.None)
+            {
+                throw BadgeErrorFactory.GetException(err, "Failed to update badge of " + badge.AppId);
+            }
         }
 
         /// <summary>
         /// Gets all the badge information.
         /// </summary>
         /// <since_tizen> 3 </since_tizen>
+        /// <returns>List of all Badge instances.</returns>
+        /// <feature>http://tizen.org/feature/badge</feature>
+        /// <privilege>http://tizen.org/privilege/notification</privilege>
         /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access.</exception>
         /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
-        /// <privilege>http://tizen.org/privilege/notification</privilege>
+        /// <exception cref="NotSupportedException">Thrown when Badge is not supported.</exception>
         public static IEnumerable<Badge> GetBadges()
         {
             IList<Badge> list = new List<Badge>();
index ff0d909..8a7a0b1 100755 (executable)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Licensed under the Apache License, Version 2.0 (the License);
+ * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
@@ -26,6 +26,7 @@ namespace Tizen.Applications
         OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
         PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
         IoError = Tizen.Internals.Errors.ErrorCode.IoError,
+        NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
         DbError = -0x01120000 | 0x01,
         AlreadyExists = -0x01120000 | 0x02,
         DBusError = -0x01120000 | 0x03,
@@ -51,7 +52,11 @@ namespace Tizen.Applications
                     Log.Error(LogTag, msg);
                     return new ArgumentException(ret + " error occurred.");
                 case BadgeError.PermissionDenied:
+                    Log.Error(LogTag, msg);
                     throw new UnauthorizedAccessException("Permission denied (http://tizen.org/privilege/notification)");
+                case BadgeError.NotSupported:
+                    Log.Error(LogTag, msg);
+                    throw new NotSupportedException("Not Supported (http://tizen.org/feature/badge)");
                 default:
                     Log.Error(LogTag, msg);
                     return new InvalidOperationException(ret + " error occurred.");
index bfcc85f..b93cf66 100755 (executable)
@@ -1,14 +1,14 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Licensed under the Apache License, Version 2.0 (the License);
+ * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.