Add function to check shared data is valid in complication provider 32/186532/7
authorSukHyung, Kang <shine.kang@samsung.com>
Fri, 10 Aug 2018 06:18:24 +0000 (15:18 +0900)
committerhyunho <hhstark.kang@samsung.com>
Fri, 17 Aug 2018 08:38:34 +0000 (17:38 +0900)
Change-Id: Iffee2667a54b3e62c4c70557292a98f150581b39
Signed-off-by: SukHyung, Kang <shine.kang@samsung.com>
Signed-off-by: hyunho <hhstark.kang@samsung.com>
watchface-complication-provider/include/watchface-complication-provider.h
watchface-complication-provider/watchface-complication-provider.cc

index 66c35f5..8d0d399 100644 (file)
@@ -518,6 +518,54 @@ int watchface_complication_provider_data_set_extra_data(bundle *shared_data,
                const char *extra_data);
 
 /**
+ * @brief Checks whether shared_data is valid or not.
+ * @details Developer can check the shared data is valid. If the following mandatory data
+ *          is not set in shared_data, it's not valid.\n
+ *          #WATCHFACE_COMPLICATION_TYPE_SHORT_TEXT : short text\n
+ *          #WATCHFACE_COMPLICATION_TYPE_LONG_TEXT : long text\n
+ *          #WATCHFACE_COMPLICATION_TYPE_RANGED_VALUE : current, min, max\n
+ *          #WATCHFACE_COMPLICATION_TYPE_TIME : timestamp\n
+ *          #WATCHFACE_COMPLICATION_TYPE_ICON : icon path\n
+ *          #WATCHFACE_COMPLICATION_TYPE_IMAGE : image path\n
+ * @since_tizen 5.0
+ * @param[in] shared_data The data which will be shared with watch application
+ * @param[out] is_valid The value that tell the shared_data is valid or not.
+ * @return #WATCHFACE_COMPLICATION_ERROR_NONE on success,
+ *         otherwise an error code (see #watchface_complication_error_e) on failure
+ * @retval #WATCHFACE_COMPLICATION_ERROR_NONE Successful
+ * @retval #WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @see watchface_complication_type_e
+ * @see watchface_complication_provider_data_set_short_text()
+ * @see watchface_complication_provider_data_set_long_text()
+ * @see watchface_complication_provider_data_set_ranged_value()
+ * @see watchface_complication_provider_data_set_timestamp()
+ * @see watchface_complication_provider_data_set_icon_path()
+ * @see watchface_complication_provider_data_set_image_path()
+ * @par Sample code:
+ * @code
+#include <watchface-complication-provider.h>
+void _watchface_complication_provider_update_requested_cb(const char *provider_id,
+               const char *req_appid, watchface_complication_type_e type,
+               const bundle *context, bundle *shared_data, void *user_data)
+{
+       char num_str[128] = {0, };
+       int num = rand();
+       bool is_valid;
+
+       if (type == WATCHFACE_COMPLICATION_TYPE_SHORT_TEXT) {
+               snprintf(num_str, sizeof(num_str), "air %d", num % 200);
+               watchface_complication_provider_data_set_short_text(
+                       shared_data, num_str);
+
+               watchface_complication_provider_data_is_valid(shared_data, &is_valid);
+       }
+}
+ * @endcode
+ */
+int watchface_complication_provider_data_is_valid(bundle *shared_data,
+                       bool *is_valid);
+
+/**
  * @brief Gets touch event type that is transferred from watchface.
  * @since_tizen 5.0
  * @remarks @a event_type is the type of touch event transftered from the watchface
index 1f25d55..12e9e07 100644 (file)
@@ -39,6 +39,7 @@
 #endif
 
 #define LOG_TAG "WATCHFACE_COMPLICATION"
+#define MAX_MANDATORY_FIELDS 3
 
 static int _add_bundle_data(bundle* shared_data, const char* key,
     const char* value);
@@ -116,6 +117,11 @@ class WatchComplicationProviderStub : public ComplicationProvider {
 
 static std::map<std::string, WatchComplicationProviderStub*> __providers;
 
+struct complication_mandatory_fields {
+  watchface_complication_type_e type;
+  const char* mandatory_fields[MAX_MANDATORY_FIELDS];
+};
+
 extern "C" EXPORT_API int watchface_complication_provider_add_update_requested_cb(
     const char* provider_id,
     watchface_complication_provider_update_requested_cb cb,
@@ -527,6 +533,56 @@ int watchface_complication_provider_data_set_extra_data(
   return _add_bundle_data(shared_data, EXTRA_DATA_KEY, extra_data);
 }
 
+static struct complication_mandatory_fields _mandatory_fields[] {
+  {WATCHFACE_COMPLICATION_TYPE_SHORT_TEXT, {SHORT_TEXT_KEY, NULL, NULL}},
+  {WATCHFACE_COMPLICATION_TYPE_LONG_TEXT, {LONG_TEXT_KEY, NULL, NULL}},
+  {WATCHFACE_COMPLICATION_TYPE_RANGED_VALUE,
+    {RANGE_CUR_KEY, RANGE_MAX_KEY, RANGE_MIN_KEY}},
+  {WATCHFACE_COMPLICATION_TYPE_TIME, {TIME_KEY, NULL, NULL}},
+  {WATCHFACE_COMPLICATION_TYPE_ICON, {ICON_KEY, NULL, NULL}},
+  {WATCHFACE_COMPLICATION_TYPE_IMAGE, {IMAGE_KEY, NULL, NULL}}
+};
+
+extern "C" EXPORT_API int watchface_complication_provider_data_is_valid(
+    bundle* shared_data, bool*is_valid) {
+  int ret;
+  int type_count;
+  watchface_complication_type_e type;
+  char* value = NULL;
+
+  if (shared_data == NULL || is_valid == NULL) {
+    LOGE("Invalid param");
+    return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+  }
+
+  ret = _get_data_type(shared_data, &type);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
+
+  type_count  = sizeof(_mandatory_fields) /
+                sizeof(struct complication_mandatory_fields);
+
+  for (int i = 0; i < type_count; i++) {
+    if (type == _mandatory_fields[i].type) {
+      for (int idx = 0; idx < MAX_MANDATORY_FIELDS
+            && _mandatory_fields[i].mandatory_fields[idx] != NULL; idx++) {
+        ret = bundle_get_str(shared_data,
+              _mandatory_fields[i].mandatory_fields[idx], &value);
+        if (ret != BUNDLE_ERROR_NONE) {
+          LOGE("Invalid data, missing field : %s",
+                _mandatory_fields[i].mandatory_fields[idx]);
+          *is_valid = false;
+          return WATCHFACE_COMPLICATION_ERROR_NONE;
+        }
+      }
+      *is_valid = true;
+      return WATCHFACE_COMPLICATION_ERROR_NONE;
+    }
+  }
+
+  return WATCHFACE_COMPLICATION_ERROR_INVALID_PARAMETER;
+}
+
 static int _get_value_from_touch_launch_data(app_control_h handle,
     const char* key, char** value) {
   bundle* data = NULL;