keyboard_js.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2013 BlackBerry Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <string>
  17. #include "../public/tokenizer.h"
  18. #include "keyboard_js.hpp"
  19. #include "keyboard_ndk.hpp"
  20. #include <sstream>
  21. using namespace std;
  22. /**
  23. * Default constructor.
  24. */
  25. Keyboard_JS::Keyboard_JS(const std::string& id) :
  26. m_id(id) {
  27. m_pLogger = new webworks::Logger("Keyboard_JS", this);
  28. m_pKeyboardController = new webworks::Keyboard_NDK(this);
  29. }
  30. /**
  31. * Keyboard_JS destructor.
  32. */
  33. Keyboard_JS::~Keyboard_JS() {
  34. if (m_pKeyboardController)
  35. delete m_pKeyboardController;
  36. if (m_pLogger)
  37. delete m_pLogger;
  38. }
  39. webworks::Logger* Keyboard_JS::getLog() {
  40. return m_pLogger;
  41. }
  42. /**
  43. * This method returns the list of objects implemented by this native
  44. * extension.
  45. */
  46. char* onGetObjList() {
  47. static char name[] = "Keyboard_JS";
  48. return name;
  49. }
  50. /**
  51. * This method is used by JNext to instantiate the Keyboard_JS object when
  52. * an object is created on the JavaScript server side.
  53. */
  54. JSExt* onCreateObject(const string& className, const string& id) {
  55. if (className == "Keyboard_JS") {
  56. return new Keyboard_JS(id);
  57. }
  58. return NULL;
  59. }
  60. /**
  61. * Method used by JNext to determine if the object can be deleted.
  62. */
  63. bool Keyboard_JS::CanDelete() {
  64. return true;
  65. }
  66. /**
  67. * It will be called from JNext JavaScript side with passed string.
  68. * This method implements the interface for the JavaScript to native binding
  69. * for invoking native code. This method is triggered when JNext.invoke is
  70. * called on the JavaScript side with this native objects id.
  71. */
  72. string Keyboard_JS::InvokeMethod(const string& command) {
  73. // format must be: "command callbackId params"
  74. size_t commandIndex = command.find_first_of(" ");
  75. std::string strCommand = command.substr(0, commandIndex);
  76. size_t callbackIndex = command.find_first_of(" ", commandIndex + 1);
  77. std::string callbackId = command.substr(commandIndex + 1, callbackIndex - commandIndex - 1);
  78. std::string arg = command.substr(callbackIndex + 1, command.length());
  79. // based on the command given, run the appropriate method in keyboard_ndk.cpp
  80. if (strCommand == "showKeyboard") {
  81. m_pKeyboardController->callKeyboardEmail();
  82. return "Show Keyboard";
  83. } else if (strCommand == "closeKeyboard") {
  84. m_pKeyboardController->cancelKeyboard();
  85. return "Cancel Keyboard";
  86. }
  87. else if(strCommand == "startService"){
  88. m_pKeyboardController->keyboardStartThread();
  89. return "Starting Service";
  90. }
  91. strCommand.append(";");
  92. strCommand.append(command);
  93. return strCommand;
  94. }
  95. // Notifies JavaScript of an event
  96. void Keyboard_JS::NotifyEvent(const std::string& event) {
  97. std::string eventString = m_id + " ";
  98. eventString.append(event);
  99. SendPluginEvent(eventString.c_str(), m_pContext);
  100. }