webview.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * webview接口
  3. */
  4. /*global Common: true, $: false,jQuery:false,template:false,console:false , starcorExt:false,starcor:false*/
  5. (function(){
  6. var oldStarcorExt = starcorExt;
  7. starcorExt = {
  8. _setCallbackResult: function(idx,ret) {
  9. oldStarcorExt._setCallbackResult(idx,ret);
  10. },
  11. _execAndroidFunc: function(func,args) {
  12. return oldStarcorExt._execAndroidFunc(func,args);
  13. }
  14. };
  15. //
  16. starcorExt._invokeCallback = function(name, args, returnIdx) {
  17. var func = this._callbacks[name];
  18. console.log("starcorExt._invokeCallback func:"+func+"args:"+args);
  19. if(args!=null){
  20. var item0 = args[0];
  21. console.log("item0.type:"+typeof(item0.type));
  22. if(typeof(item0.type)=="undefined"){
  23. args=JSON.parse(args);
  24. console.log("args:"+args);
  25. }
  26. }
  27. var ret = null;
  28. try {
  29. var invokeArgs = [];
  30. for( var idx in args ) {
  31. var item = args[idx];
  32. console.log("item:"+item+"idx:"+idx);
  33. console.log("item.type:"+item.type+",item.value:"+item.value);
  34. switch(item.type) {
  35. case "number":
  36. invokeArgs.push(Number(item.value));
  37. break;
  38. case "string":
  39. invokeArgs.push(String(item.value));
  40. break;
  41. case "boolean":
  42. invokeArgs.push("true".toLowerCase() == item.value);
  43. break;
  44. }
  45. }
  46. ret = func.apply(this, invokeArgs);
  47. } catch (e) {}
  48. starcorExt._setCallbackResult(returnIdx, ret);
  49. };
  50. starcorExt._addCallback = function(callback) {
  51. var callbackIdx = String(starcorExt._callback_counter++);
  52. this._callbacks[callbackIdx] = callback;
  53. return callbackIdx;
  54. };
  55. starcorExt._callback_counter = 0;
  56. starcorExt._callbacks = {};
  57. // 调用浏览器功能,参数依cmd类型而定
  58. starcorExt.exec = function() {
  59. var args = [];
  60. for(var idx=0;idx<arguments.length;++idx) {
  61. var val = arguments[idx];
  62. switch (typeof val) {
  63. case "number":
  64. args.push({"type":"number", "value":val});
  65. break;
  66. case "string":
  67. args.push({"type":"string", "value":val});
  68. break;
  69. case "bool":
  70. args.push({"type":"boolean", "value":val});
  71. break;
  72. case "object":
  73. args.push({"type":"object", "value":val});
  74. break;
  75. case "function":
  76. args.push({"type": "callback", "value": this._addCallback(val)});
  77. break;
  78. }
  79. }
  80. var cmd = args.shift();
  81. if(typeof this._execAndroidFunc === "undefined"){
  82. return null;
  83. }
  84. return this._execAndroidFunc(cmd.value, JSON.stringify(args));
  85. };
  86. // 返回版本信息
  87. starcorExt.getVersion = function() {
  88. return this.exec("getVersion");
  89. };
  90. // 关闭浏览器或浏览器所在的页面
  91. starcorExt.closeBrwoser = function(reason) {
  92. if (reason)
  93. return this.exec("closeBrowser",reason);
  94. };
  95. // 输出日志信息, info可为一个或多个, 多个输入参数以空格分隔
  96. starcorExt.log = function(tag, info) {
  97. var logMsg = [];
  98. for(var idx=1;idx<arguments.length;++idx) {
  99. var item = arguments[idx];
  100. if (item instanceof Array || typeof(item) == "object") {
  101. logMsg.push(JSON.stringfy(item));
  102. } else {
  103. logMsg.push(item);
  104. }
  105. }
  106. this.exec("log", tag, logMsg.join(" "));
  107. };
  108. // 调整浏览器尺寸,调整后,浏览器保持在屏幕上居中位置
  109. starcorExt.resizeBrowser = function(width, height) {
  110. return this.exec("resizeBrowser", width, height);
  111. };
  112. starcorExt.moveBrowser = function(x, y) {
  113. return this.exec("moveBrowser", x, y);
  114. };
  115. starcorExt.moveBrowserEx = function(x, y, width, height) {
  116. return this.exec("moveBrowserEx", x, y, width, height);
  117. };
  118. starcorExt.getBrowserPosition = function() {
  119. var posInfo = this.exec("getBrowserPosition");
  120. posInfo = posInfo.split(",");
  121. return {
  122. x:posInfo[0],
  123. y:posInfo[1],
  124. width:posInfo[2],
  125. height:posInfo[3]
  126. };
  127. };
  128. starcorExt.getScreenSize = function() {
  129. var posInfo = this.exec("getScreenSize");
  130. posInfo = posInfo.split(",");
  131. return {
  132. width:posInfo[0],
  133. height:posInfo[1]
  134. };
  135. };
  136. // 设置事件回调
  137. // handlerType 回调类型, KeyEvent, Message
  138. // handler 回调函数, 其形式由handlerType决定
  139. //-----------------------------------------------------------------------------
  140. // handler为空表示取消对事件的监听
  141. // handler如果处理了事件, 返回true, 否则事件交由浏览器处理
  142. starcorExt.setHandler = function(handlerType, handler) {
  143. return this.exec("setHandler", handlerType, handler);
  144. };
  145. // 获取焦点
  146. // webview获取焦点后,所有按键事件都将转发给页面处理
  147. starcorExt.requestFocus = function() {
  148. return this.exec("requestFocus");
  149. };
  150. // 释放焦点
  151. // direction: 将焦点释放给当前webview在某个方向上的相邻元素,如无相邻元素,则焦点不变化
  152. starcorExt.releaseFocus = function(direction) {
  153. return this.exec("releaseFocus", direction);
  154. };
  155. // 是否有焦点
  156. starcorExt.hasFocus = function() {
  157. return this.exec("hasFocus");
  158. };
  159. // 读取系统属性
  160. // propName:
  161. // user.name 读取用户名,未登录时值为空
  162. // user.token 读取用户token,未登录时值为空
  163. // user.id 读取用户id,未登录时值为空
  164. // app.name 读取应用名
  165. // app.version 读取应用版本
  166. // stbid stb_id机顶盒编码,即机顶盒串号(联通)
  167. starcorExt.readSystemProp = function(propName) {
  168. return String(this.exec("readSystemProp", propName));
  169. };
  170. // 设置按键事件回调, handler形如function(action, keyCode, keyName, metaState)
  171. // action可以为keyUp, keyDown
  172. // keyName为按键名称, 如: up, down, left, right, a, b, c, d, 1, 2, 3, 4, enter, return, UNKNOWN
  173. // keyCode为按键对应的键值
  174. // metaState为控制建对应状态,ctrl, alt, shift等
  175. starcorExt.setKeyEventHandler = function(handler) {
  176. return this.setHandler("KeyEvent", handler);
  177. };
  178. // 设置消息回调,handler形如function(msg, extInfo)
  179. // msg:
  180. // onFocusChanged, true/false - webview焦点变化,true表示获取焦点,false表示失去焦点
  181. starcorExt.setMessageHandler = function(handler) {
  182. return this.setHandler("Message", handler);
  183. };
  184. // 发送消息给应用
  185. // msg:
  186. // onLogin, extInfo: {name:"user name", id:"user id", token:"login token"}
  187. // onPurchases, extInfo: {id:"item id", result:"success"/"failed", msg:"err message"}
  188. starcorExt.sendMessage = function(msg, extInfo) {
  189. return this.exec("sendMessage", msg, extInfo);
  190. };
  191. // 发送Android Intent
  192. // mode:
  193. // sendBroadcast 发送广播
  194. // startActivity 打开应用
  195. // startService 启动服务
  196. starcorExt.sendIntent = function(mode, intent) {
  197. return this.exec("sendIntent", mode,intent);
  198. };
  199. // 打开子浏览器(弹出方式)
  200. // 同时只能打开一个子浏览器
  201. starcorExt.openBrowser = function(url) {
  202. return this.exec("openBrowser", url);
  203. };
  204. /*设置小视频窗口位置
  205. x:起始x坐标
  206. y:起始y坐标
  207. width:宽度
  208. height:高度
  209. */
  210. starcorExt.setVideoWindowPosition = function(x,y,width,height) {
  211. return this.exec("setVideoWindowPosition", x,y,width,height);
  212. };
  213. /*播放视频,其中playParams为json数据,示例如下:
  214. var playParams = {
  215. video_type:"0",
  216. video_id:"b836aef6c9b851958199d3b5a1a77050",
  217. video_name:"青云志",
  218. media_asset_id:"TVseries",
  219. category_id:"1000015",
  220. ui_style:"0",
  221. video_index:"26",
  222. quality:"hd",
  223. video_all_index:"27",
  224. video_new_index:"26",
  225. };
  226. */
  227. starcorExt.playVideo = function(playParams) {
  228. return this.exec("playVideo", playParams);
  229. };
  230. //停止播放视频
  231. starcorExt.stopVideo = function() {
  232. return this.exec("stopVideo");
  233. };
  234. //web页面是否有输入框,有输入框则设置为“true”,没有则设置成“false”
  235. //调用示例:Webview.setInputTypeTextFlag("false");
  236. starcorExt.setInputTypeTextFlag = function(flag) {
  237. return this.exec("setInputTypeTextFlag",flag);
  238. };
  239. window.Webview = starcorExt;
  240. })();