WIP : System settings API
authorRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Wed, 7 Aug 2013 12:25:57 +0000 (15:25 +0300)
committerRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Wed, 7 Aug 2013 16:12:45 +0000 (19:12 +0300)
Based on Sudarsana "Babu" Nagineni's work.
Getting and setting for the following properties-
"HOME_SCREEN", "LOCK_SCREEN","INCOMING_CALL","NOTIFICATION_EMAIL"

examples/index.html
examples/system_setting.html [new file with mode: 0644]
packaging/tizen-extensions-crosswalk.spec
system_setting/system_setting.gyp [new file with mode: 0644]
system_setting/system_setting_api.js [new file with mode: 0644]
system_setting/system_setting_context.cc [new file with mode: 0644]
system_setting/system_setting_context.h [new file with mode: 0644]
system_setting/system_setting_context_desktop.cc [new file with mode: 0644]
system_setting/system_setting_context_mobile.cc [new file with mode: 0644]
tizen-wrt.gyp

index 44aa963..314391e 100644 (file)
@@ -5,6 +5,7 @@
 <a href="networkbearerselection.html"><h5>- Network Bearer Selection Test</h5></a>
 <a href="notification.html"><h5>- Notification Test</h5></a>
 <a href="system_info.html"><h5>- SystemInfo Test</h5></a>
+<a href="system_setting.html"><h5>- SystemSetting Test</h5></a>
 <a href="time.html"><h5>- Time Test</h5></a>
 <a href="power.html"><h5>- Power API Test</h5></a>
 <a href="bluetooth.html"><h5>- Bluetooth Test</h5></a>
diff --git a/examples/system_setting.html b/examples/system_setting.html
new file mode 100644 (file)
index 0000000..9c0bae7
--- /dev/null
@@ -0,0 +1,79 @@
+<h1>System Setting API Test</h1>
+<body>
+  <br>
+  <div id="dbg"></div>
+  <br>
+  <button id="button1">Click 1</button><br><br>
+  <button id="button2">Click 2</button><br><br>
+  <button id="button3">Click 3</button><br><br>
+  <button id="button4">Click 4</button><br><br>
+  <button id="button5">Click 5</button><br><br>
+  <button id="button6">Click 6</button><br><br>
+  <button id="button7">Click 7</button><br><br>
+  <button id="button8">Click 8</button><br><br>
+  <pre id="console"></pre>
+  <script src="js/js-test-pre.js"></script>
+</body>
+<script>
+  var api_system_setting = tizen.systemsetting;
+  var output = document.getElementById("output");
+
+  function dbgOut(msg) {
+    el = document.getElementById("dbg");
+    el.innerHTML = msg + "<br/>" + dbg.innerHTML;
+  }
+
+  function handle(button, text, callback) {
+    var b = document.getElementById(button);
+    b.innerText = text;
+    b.addEventListener("click", callback);
+  }
+
+  // Define the set error callback.
+  function errorCallback(error) {
+    dbgOut("Fail to setProperty" + error);
+  }
+
+  // Define the success callback
+  function successCallback() {
+    dbgOut("Success to setProperty");
+  }
+
+  // Define the get success callback
+  function getPropertySuccessCallback(value) {
+    dbgOut("Success to getProperty " + value);
+  }
+
+  handle("button1", "Set home screen", function () {
+    tizen.systemsetting.setProperty("HOME_SCREEN",
+      "/opt/usr/media/Images/image16.jpg", successCallback, errorCallback);
+  });
+  handle("button2", "Set lock screen", function () {
+    tizen.systemsetting.setProperty("LOCK_SCREEN",
+      "/opt/usr/media/Images/image7.jpg", successCallback, errorCallback);
+  });
+  handle("button3", "Set call ringtone", function () {
+    api_system_setting.setProperty("INCOMING_CALL", "test_incoming.mp3",
+      successCallback, errorCallback);
+  });
+  handle("button4", "Set email alert tone ", function () {
+    api_system_setting.setProperty("NOTIFICATION_EMAIL", "test_email.mp3",
+      successCallback, errorCallback);
+  });
+  handle("button5", "Get HOME_SCREEN", function () {
+    api_system_setting.getProperty("HOME_SCREEN", getPropertySuccessCallback,
+      errorCallback);
+  });
+  handle("button6", "Get LOCK_SCREEN", function () {
+    api_system_setting.getProperty("LOCK_SCREEN", getPropertySuccessCallback,
+      errorCallback);
+  });
+  handle("button7", "Get INCOMING_CALL", function () {
+    api_system_setting.getProperty("INCOMING_CALL",
+      getPropertySuccessCallback, errorCallback);
+  });
+  handle("button8", "Get NOTIFICATION_EMAIL", function () {
+    api_system_setting.getProperty("NOTIFICATION_EMAIL",
+      getPropertySuccessCallback, errorCallback);
+  });
+</script>
index 8fb536d..56bcb3e 100644 (file)
@@ -13,6 +13,7 @@ BuildRequires: python
 BuildRequires: pkgconfig(capi-network-connection)
 BuildRequires: pkgconfig(capi-system-device)
 BuildRequires: pkgconfig(capi-system-power)
+BuildRequires: pkgconfig(capi-system-system-settings)
 BuildRequires: pkgconfig(dbus-glib-1)
 BuildRequires: pkgconfig(glib-2.0)
 BuildRequires: pkgconfig(libudev)
diff --git a/system_setting/system_setting.gyp b/system_setting/system_setting.gyp
new file mode 100644 (file)
index 0000000..84a7e39
--- /dev/null
@@ -0,0 +1,31 @@
+{
+  'includes':[
+    '../common/common.gypi',
+  ],
+  'targets': [
+    {
+      'target_name': 'tizen_systemsetting',
+      'type': 'loadable_module',
+      'sources': [
+        'system_setting_api.js',
+        'system_setting_context.cc',
+        'system_setting_context.h',
+        'system_setting_context_desktop.cc',
+        'system_setting_context_mobile.cc',
+      ],
+      'includes': [
+        '../common/pkg-config.gypi',
+      ],
+      'conditions': [
+        ['extension_host_os=="mobile"', {
+          'variables': {
+            'packages': [
+              'capi-system-system-settings',
+              'vconf',
+            ]
+          },
+        }],
+      ],
+    },
+  ],
+}
diff --git a/system_setting/system_setting_api.js b/system_setting/system_setting_api.js
new file mode 100644 (file)
index 0000000..a2cb0c7
--- /dev/null
@@ -0,0 +1,70 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+var systemSettingTypes = {
+  "HOME_SCREEN": 0,
+  "LOCK_SCREEN": 1,
+  "INCOMING_CALL": 2,
+  "NOTIFICATION_EMAIL": 3
+};
+var _callbacks = {};
+var _next_reply_id = 0;
+extension.setMessageListener(function (message) {
+  var m = JSON.parse(message);
+  var _reply_id = m._reply_id;
+  var _error = m._error;
+  var handler = _callbacks[_reply_id];
+  if (handler) {
+    delete m._reply_id;
+    delete m._error;
+    delete _callbacks[_reply_id];
+    if (_error === 0) {
+      handler[0](m._file);
+    } else if (handler[1]) {
+      handler[1](new tizen.WebAPIError(_error));
+    }
+    delete m._file;
+  } else {
+    console.log(
+      'Invalid reply_id received from xwalk.systemsetting extension:' +
+      _reply_id);
+  }
+});
+
+exports.setProperty = function (type, proposedPath, successCallback, errorCallback) {
+  if (typeof type !== 'string' || typeof proposedPath !== 'string') {
+    throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
+  }
+  if (!systemSettingTypes.hasOwnProperty(type) || !successCallback) {
+    throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
+  }
+  if (typeof (successCallback) !== "function" ||
+    (errorCallback && typeof (errorCallback) !== "function")) {
+    throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
+  }
+  var id = (_next_reply_id++).toString();
+  _callbacks[id] = [successCallback, errorCallback];
+  extension.postMessage(JSON.stringify({
+    "cmd": 'SetProperty',
+    '_type': systemSettingTypes[type],
+    '_file': proposedPath,
+    "_reply_id": id,
+  }));
+}
+
+exports.getProperty = function (type, successCallback, errorCallback) {
+  if (!successCallback) {
+    throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
+  }
+  if (typeof type !== 'string' || typeof (successCallback) !== "function" ||
+    (errorCallback && typeof (errorCallback) !== "function")) {
+    throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
+  }
+  var id = (_next_reply_id++).toString();
+  _callbacks[id] = [successCallback, errorCallback];
+  extension.postMessage(JSON.stringify({
+    "cmd": 'GetProperty',
+    '_type': systemSettingTypes[type],
+    "_reply_id": id,
+  }));
+}
\ No newline at end of file
diff --git a/system_setting/system_setting_context.cc b/system_setting/system_setting_context.cc
new file mode 100644 (file)
index 0000000..a8aa742
--- /dev/null
@@ -0,0 +1,57 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "system_setting/system_setting_context.h"
+
+#include <string>
+#include "common/picojson.h"
+
+CXWalkExtension* xwalk_extension_init(int32_t api_version) {
+  return ExtensionAdapter<SystemSettingContext>::Initialize();
+}
+
+SystemSettingContext::SystemSettingContext(ContextAPI* api)
+    : api_(api) {}
+
+SystemSettingContext::~SystemSettingContext() {
+  delete api_;
+}
+
+const char SystemSettingContext::name[] = "tizen.systemsetting";
+
+// This will be generated from system_setting_api.js.
+extern const char kSource_system_setting_api[];
+
+const char* SystemSettingContext::GetJavaScript() {
+  return kSource_system_setting_api;
+}
+
+void SystemSettingContext::HandleMessage(const char* message) {
+  picojson::value v;
+
+  std::string err;
+  picojson::parse(v, message, message + strlen(message), &err);
+  if (!err.empty()) {
+    std::cout << "Ignoring message.\n";
+    return;
+  }
+
+  std::string cmd = v.get("cmd").to_str();
+  if (cmd == "SetProperty")
+    HandleSetProperty(v);
+  else if (cmd == "GetProperty")
+    HandleGetProperty(v);
+}
+
+void SystemSettingContext::OnPropertyHandled(const char* reply_id,
+                                             const char* value, int ret) {
+  picojson::value::object o;
+  o["_reply_id"] = picojson::value(reply_id);
+  if (value)
+    o["_file"] = picojson::value(value);
+  o["_error"] = picojson::value(static_cast<double>(ret));
+
+  picojson::value v(o);
+  api_->PostMessage(v.serialize().c_str());
+}
diff --git a/system_setting/system_setting_context.h b/system_setting/system_setting_context.h
new file mode 100644 (file)
index 0000000..5ef51bd
--- /dev/null
@@ -0,0 +1,40 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SYSTEM_SETTING_SYSTEM_SETTING_CONTEXT_H_
+#define SYSTEM_SETTING_SYSTEM_SETTING_CONTEXT_H_
+
+#include "common/extension_adapter.h"
+
+namespace picojson {
+class value;
+}
+
+class SystemSettingContext {
+ public:
+  explicit SystemSettingContext(ContextAPI* api);
+  ~SystemSettingContext();
+
+  // ExtensionAdapter implementation.
+  static const char name[];
+  static const char* GetJavaScript();
+  void HandleMessage(const char* message);
+  void HandleSyncMessage(const char* message) {}
+
+ private:
+  enum SystemSettingType {
+    HOME_SCREEN = 0,
+    LOCK_SCREEN = 1,
+    INCOMING_CALL = 2,
+    NOTIFICATION_EMAIL = 3,
+  };
+
+  void HandleSetProperty(const picojson::value& msg);
+  void HandleGetProperty(const picojson::value& msg);
+  void OnPropertyHandled(const char* reply_id, const char* value, int ret);
+
+  ContextAPI* api_;
+};
+
+#endif  // SYSTEM_SETTING_SYSTEM_SETTING_CONTEXT_H_
diff --git a/system_setting/system_setting_context_desktop.cc b/system_setting/system_setting_context_desktop.cc
new file mode 100644 (file)
index 0000000..a4f7b04
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "system_setting/system_setting_context.h"
+#include "common/picojson.h"
+
+void SystemSettingContext::HandleSetProperty(const picojson::value& msg) {
+  SystemSettingType type = static_cast<SystemSettingType>
+    (msg.get("_type").get<double>());
+  const char* value = msg.get("_file").to_str().c_str();
+  const char* reply_id = msg.get("_reply_id").to_str().c_str();
+
+  OnPropertyHandled(reply_id, value, 0);
+}
+
+void SystemSettingContext::HandleGetProperty(const picojson::value& msg) {
+  SystemSettingType type = static_cast<SystemSettingType>
+    (msg.get("_type").get<double>());
+  const char* reply_id = msg.get("_reply_id").to_str().c_str();
+
+  OnPropertyHandled(reply_id, "test.png", 0);
+}
diff --git a/system_setting/system_setting_context_mobile.cc b/system_setting/system_setting_context_mobile.cc
new file mode 100644 (file)
index 0000000..2270c60
--- /dev/null
@@ -0,0 +1,66 @@
+// Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "system_setting/system_setting_context.h"
+
+#include <system_settings.h>
+#include <vconf.h>
+#include "common/picojson.h"
+
+void SystemSettingContext::HandleSetProperty(const picojson::value& msg) {
+  SystemSettingType type = static_cast<SystemSettingType>
+    (msg.get("_type").get<double>());
+  const char* value = msg.get("_file").to_str().c_str();
+  const char* reply_id = msg.get("_reply_id").to_str().c_str();
+  system_settings_key_e key;
+  switch (type) {
+    case HOME_SCREEN:
+      key = SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN;
+      break;
+    case LOCK_SCREEN:
+      key = SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN;
+      break;
+    case INCOMING_CALL:
+      key = SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE;
+      break;
+    case NOTIFICATION_EMAIL:
+      key = SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE;
+      break;
+  default:
+    std::cout<< "Invalid Key : should not reach here";
+    break;
+  }
+
+  int ret = system_settings_set_value_string(key, value);
+  OnPropertyHandled(reply_id, value, ret);
+}
+
+void SystemSettingContext::HandleGetProperty(const picojson::value& msg) {
+  SystemSettingType type = static_cast<SystemSettingType>
+    (msg.get("_type").get<double>());
+  const char* reply_id = msg.get("_reply_id").to_str().c_str();
+  system_settings_key_e key;
+  switch (type) {
+    case HOME_SCREEN:
+      key = SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN;
+      break;
+    case LOCK_SCREEN:
+      key = SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN;
+      break;
+    case INCOMING_CALL:
+      key = SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE;
+      break;
+    case NOTIFICATION_EMAIL:
+      key = SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE;
+      break;
+  default:
+    std::cout<< "Invalid Key :should not reach here";
+    break;
+  }
+
+  char* value = NULL;
+  int ret = system_settings_get_value_string(key, &value);
+  OnPropertyHandled(reply_id, value, ret);
+  free(value);
+}
index 5a0a60f..ba99b96 100644 (file)
@@ -9,6 +9,7 @@
         'notification/notification.gyp:*',
         'power/power.gyp:*',
         'system_info/system_info.gyp:*',
+        'system_setting/system_setting.gyp:*',
         'time/time.gyp:*',
         'tizen/tizen.gyp:*',
       ],