Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

platform.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. // The last resume event that was received that had the result of a plugin call.
  22. var lastResumeEvent = null;
  23. module.exports = {
  24. id: 'android',
  25. bootstrap: function() {
  26. var channel = require('cordova/channel'),
  27. cordova = require('cordova'),
  28. exec = require('cordova/exec'),
  29. modulemapper = require('cordova/modulemapper');
  30. // Get the shared secret needed to use the bridge.
  31. exec.init();
  32. // TODO: Extract this as a proper plugin.
  33. modulemapper.clobbers('cordova/plugin/android/app', 'navigator.app');
  34. var APP_PLUGIN_NAME = Number(cordova.platformVersion.split('.')[0]) >= 4 ? 'CoreAndroid' : 'App';
  35. // Inject a listener for the backbutton on the document.
  36. var backButtonChannel = cordova.addDocumentEventHandler('backbutton');
  37. backButtonChannel.onHasSubscribersChange = function() {
  38. // If we just attached the first handler or detached the last handler,
  39. // let native know we need to override the back button.
  40. exec(null, null, APP_PLUGIN_NAME, "overrideBackbutton", [this.numHandlers == 1]);
  41. };
  42. // Add hardware MENU and SEARCH button handlers
  43. cordova.addDocumentEventHandler('menubutton');
  44. cordova.addDocumentEventHandler('searchbutton');
  45. function bindButtonChannel(buttonName) {
  46. // generic button bind used for volumeup/volumedown buttons
  47. var volumeButtonChannel = cordova.addDocumentEventHandler(buttonName + 'button');
  48. volumeButtonChannel.onHasSubscribersChange = function() {
  49. exec(null, null, APP_PLUGIN_NAME, "overrideButton", [buttonName, this.numHandlers == 1]);
  50. };
  51. }
  52. // Inject a listener for the volume buttons on the document.
  53. bindButtonChannel('volumeup');
  54. bindButtonChannel('volumedown');
  55. // The resume event is not "sticky", but it is possible that the event
  56. // will contain the result of a plugin call. We need to ensure that the
  57. // plugin result is delivered even after the event is fired (CB-10498)
  58. var cordovaAddEventListener = document.addEventListener;
  59. document.addEventListener = function(evt, handler, capture) {
  60. cordovaAddEventListener(evt, handler, capture);
  61. if (evt === 'resume' && lastResumeEvent) {
  62. handler(lastResumeEvent);
  63. }
  64. };
  65. // Let native code know we are all done on the JS side.
  66. // Native code will then un-hide the WebView.
  67. channel.onCordovaReady.subscribe(function() {
  68. exec(onMessageFromNative, null, APP_PLUGIN_NAME, 'messageChannel', []);
  69. exec(null, null, APP_PLUGIN_NAME, "show", []);
  70. });
  71. }
  72. };
  73. function onMessageFromNative(msg) {
  74. var cordova = require('cordova');
  75. var action = msg.action;
  76. switch (action)
  77. {
  78. // Button events
  79. case 'backbutton':
  80. case 'menubutton':
  81. case 'searchbutton':
  82. // App life cycle events
  83. case 'pause':
  84. // Volume events
  85. case 'volumedownbutton':
  86. case 'volumeupbutton':
  87. cordova.fireDocumentEvent(action);
  88. break;
  89. case 'resume':
  90. if(arguments.length > 1 && msg.pendingResult) {
  91. if(arguments.length === 2) {
  92. msg.pendingResult.result = arguments[1];
  93. } else {
  94. // The plugin returned a multipart message
  95. var res = [];
  96. for(var i = 1; i < arguments.length; i++) {
  97. res.push(arguments[i]);
  98. }
  99. msg.pendingResult.result = res;
  100. }
  101. // Save the plugin result so that it can be delivered to the js
  102. // even if they miss the initial firing of the event
  103. lastResumeEvent = msg;
  104. }
  105. cordova.fireDocumentEvent(action, msg);
  106. break;
  107. default:
  108. throw new Error('Unknown event action ' + action);
  109. }
  110. }