Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

528 строки
15 KiB

  1. require("preload.Plugin");
  2. -- android
  3. if cc.Application:getInstance():getTargetPlatform() == 3 then
  4. PluginDevice = cc.PluginManager:getInstance():createPlugin("device" , "com/ddgame/plugin/Device");
  5. -- 弹出普通确认框
  6. function deviceAlert(message , title , func)
  7. -- 普通确认框
  8. local params =
  9. {
  10. message;
  11. title or PLN.DLG_TITLE;
  12. 0;
  13. func or 0;
  14. }
  15. PluginDevice:callMemberMethod("showDialog" , "(Ljava/lang/String;Ljava/lang/String;II)V" , params);
  16. end
  17. -- 确认取消确认框
  18. -- @message 消息内容
  19. -- @title 消息标题
  20. -- @func 回调函数,function(value),确认时value="1",取消时value="0"
  21. function deviceMessageBox(message , title , func)
  22. -- 确认取消确认框
  23. local params =
  24. {
  25. message;
  26. title or PLN.DLG_TITLE;
  27. 1;
  28. func;
  29. }
  30. PluginDevice:callMemberMethod("showDialog" , "(Ljava/lang/String;Ljava/lang/String;II)V" , params);
  31. end
  32. -- 输入框
  33. -- @title 标题
  34. -- @defaultValue 默认值
  35. -- @maxLength 最多输入多少字符
  36. -- @func 回调函数,function(value)确认时value=输入值,取消时value=""
  37. function deviceInputBox(title , defaultValue , maxLength , func)
  38. -- 确认取消确认框
  39. local params =
  40. {
  41. title;
  42. defaultValue;
  43. 6; -- 所有内容
  44. 4; -- 任意字符
  45. 0; -- 默认按钮
  46. maxLength; -- 12个字符
  47. func;
  48. }
  49. PluginDevice:callMemberMethod("showInputBoxDialog" , "(Ljava/lang/String;Ljava/lang/String;IIIII)V" , params);
  50. end
  51. -- 发送一个通知
  52. -- loop 通知是否循环
  53. -- absoluteTime 当loop为false时,这个值生效,而且是个绝对 2015:06:15:12:00:00
  54. -- timeString 当loop为true时,这个值生效,为一个循环时间例如:21:00:00
  55. -- period 当loop为true时,这个值表示间隔时间(单位为毫秒)
  56. -- tickerText 标签
  57. -- title 提示标题
  58. -- contentText 提示的内容
  59. function deviceSendNotification(loop, absoluteTime, timeString, period, tickerText, title, contentText)
  60. local params =
  61. {
  62. Nloop = loop,
  63. NabsoluteTime = absoluteTime,
  64. NtimeString = timeString,
  65. Nperiod = period,
  66. NtickerText = tickerText,
  67. Ntitle = title,
  68. NcontentText = contentText,
  69. }
  70. print("通知传入的参数:", table.tostring(params))
  71. PluginDevice:callVoid("addTimerNotification", params);
  72. end
  73. -- 开始所有的通知
  74. function deviceStartAllNotification()
  75. PluginDevice:callVoid("startAllNotifications");
  76. end
  77. -- 停止所有的通知
  78. function deviceStopAllNotification()
  79. PluginDevice:callVoid("stopAllNotifications");
  80. end
  81. -- 清空下所有的通知
  82. function deviceClearAllNotificaiton()
  83. PluginDevice:callVoid("clearAllNotifications");
  84. end
  85. function getSystemPath(key)
  86. local path = PluginDevice:callString("getSystemDir", tostring(key));
  87. print("getSystemPath()", key, path)
  88. return path
  89. end
  90. function copyFile(sourcePath, targetPath)
  91. print("PluginDevice::copyFile()", sourcePath, targetPath)
  92. local params =
  93. {
  94. sourcePath = sourcePath,
  95. targetPath = targetPath,
  96. }
  97. PluginDevice:callVoid("copyFile", params);
  98. end
  99. -- ios
  100. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  101. PluginDevice = cc.PluginManager:getInstance():createPlugin("device" , "PluginDevice");
  102. --网络权限判断
  103. function checkNetWorkPermission(callback)
  104. -- return PluginDevice:callString("checkNetWorkPermission");
  105. local developerInfo =
  106. {
  107. callback = callback, -- 回调函数
  108. };
  109. PluginDevice:callVoid("checkNetWorkPermission", developerInfo)
  110. end
  111. -- 弹出普通确认框
  112. function deviceAlert(message , title , func)
  113. -- 普通确认框
  114. local params =
  115. {
  116. message;
  117. title or PLN.DLG_TITLE;
  118. 0;
  119. func or 0;
  120. }
  121. PluginDevice:callMemberMethod("showDialog" , unpack(params));
  122. end
  123. -- 确认取消确认框
  124. -- @message 消息内容
  125. -- @title 消息标题
  126. -- @func 回调函数,function(value),确认时value="1",取消时value="0"
  127. function deviceMessageBox(message , title , func)
  128. -- 确认取消确认框
  129. local params =
  130. {
  131. message;
  132. title or PLN.DLG_TITLE;
  133. 1;
  134. func;
  135. }
  136. PluginDevice:callMemberMethod("showDialog" , unpack(params));
  137. end
  138. -- 输入框
  139. -- @title 标题
  140. -- @defaultValue 默认值
  141. -- @maxLength 最多输入多少字符
  142. -- @func 回调函数,function(value)确认时value=输入值,取消时value=""
  143. function deviceInputBox(title , defaultValue , maxLength , func)
  144. -- 确认取消确认框
  145. local params =
  146. {
  147. title;
  148. defaultValue;
  149. title;
  150. false;
  151. maxLength; -- 12个字符
  152. 0; -- 默认的键盘类型
  153. 9; -- 确定按钮
  154. func;
  155. }
  156. PluginDevice:callMemberMethod("showInputBoxDialog" , unpack(params));
  157. end
  158. -- 发送一个通知
  159. -- loop 通知是否循环
  160. -- remainSeconds 当loop为false时,这个值生效,而且是个绝对的时间戳,单位为毫秒(2015-06-11对应的时间戳就是585822221222毫秒)
  161. -- timeString 当loop为true时,这个值生效,为一个循环时间例如:21:00:00
  162. -- period 当loop为true时,这个值表示间隔时间(单位为毫秒)
  163. -- tickerText 标签
  164. -- title 提示标题
  165. -- contentText 提示的内容
  166. function deviceSendNotification(loop, remainSeconds, timeString, period, tickerText, title, contentText)
  167. print("deviceSendNotification", loop, remainSeconds, timeString, period, tickerText, title, contentText)
  168. end
  169. -- 开始所有的通知
  170. function deviceStartAllNotification()
  171. print("deviceStartAllNotification")
  172. end
  173. -- 停止所有的通知
  174. function deviceStopAllNotification()
  175. print("deviceStopAllNotification")
  176. end
  177. -- 清空下所有的通知
  178. function deviceClearAllNotificaiton()
  179. print("deviceClearAllNotificaiton")
  180. end
  181. else
  182. -- 弹出普通确认框
  183. function deviceAlert(message , title , func)
  184. print("deviceAlert(" , message , title , func , ")");
  185. end
  186. -- 确认取消确认框
  187. -- @message 消息内容
  188. -- @title 消息标题
  189. -- @func 回调函数,function(value),确认时value="1",取消时value="0"
  190. function deviceMessageBox(message , title , func)
  191. print("deviceMessageBox(" , message , title , func , ")");
  192. end
  193. -- 输入框
  194. -- @title 标题
  195. -- @defaultValue 默认值
  196. -- @maxLength 最多输入多少字符
  197. -- @func 回调函数,function(value)确认时value=输入值,取消时value=""
  198. function deviceInputBox(title , defaultValue , maxLength , func)
  199. --print("deviceInputBox(" , title , defaultValue , maxLength , func , ")");
  200. -- 确认取消确认框
  201. app:showWinInputDialog(title , defaultValue , maxLength , func)
  202. end
  203. -- 发送一个通知
  204. -- loop 通知是否循环
  205. -- remainSeconds 当loop为false时,这个值生效,而且是个绝对的时间戳,单位为毫秒(2015-06-11对应的时间戳就是585822221222毫秒)
  206. -- timeString 当loop为true时,这个值生效,为一个循环时间例如:21:00:00
  207. -- period 当loop为true时,这个值表示间隔时间(单位为毫秒)
  208. -- tickerText 标签
  209. -- title 提示标题
  210. -- contentText 提示的内容
  211. function deviceSendNotification(loop, remainSeconds, timeString, period, tickerText, title, contentText)
  212. print("deviceSendNotification", loop, remainSeconds, timeString, period, tickerText, title, contentText)
  213. end
  214. -- 开始所有的通知
  215. function deviceStartAllNotification()
  216. print("deviceStartAllNotification")
  217. end
  218. -- 停止所有的通知
  219. function deviceStopAllNotification()
  220. print("deviceStopAllNotification")
  221. end
  222. -- 清空下所有的通知
  223. function deviceClearAllNotificaiton()
  224. print("deviceClearAllNotificaiton")
  225. end
  226. end
  227. -- 把设备信息打印到日志里
  228. function devicePrintBaseInfo()
  229. if PluginDevice then
  230. local allNames = PluginDevice:callString("getBaseInfoNames");
  231. local names = split(allNames , ",")
  232. for i, v in pairs(names) do
  233. -- 这里不需要打印位置信息
  234. if v ~= "location" then
  235. local value = PluginDevice:callString("getInfoValue" , v);
  236. print(v , " = " , value);
  237. end
  238. end
  239. end
  240. end
  241. -- 获取设备的CPU型号
  242. function getDeviceCPUType()
  243. if PluginDevice then
  244. if cc.Application:getInstance():getTargetPlatform() == 3 then
  245. return PluginDevice:callString("getInfoValue" , "CPU_ABI")
  246. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  247. return PluginDevice:callString("getInfoValue" , "CPUType")
  248. end
  249. end
  250. print("找不到设备的CPU_ABI字段,使用默认的armeabi-v7a")
  251. return "armeabi-v7a"
  252. end
  253. --获取mac地址
  254. function getWifiMAC()
  255. if PluginDevice then
  256. return PluginDevice:callString("getInfoValue" , "WifiMAC");
  257. end
  258. print("找不到设备的WifiMAC字段,使用默认的00:00:00");
  259. return "00:00:00";
  260. end
  261. --获取手机型号
  262. function getLocalizedModel()
  263. if PluginDevice then
  264. if cc.Application:getInstance():getTargetPlatform() == 3 then
  265. return PluginDevice:callString("getInfoValue" , "model");
  266. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  267. return PluginDevice:callString("getInfoValue" , "localizedModel");
  268. end
  269. end
  270. local machinetype = ""
  271. if cc.Application:getInstance():getTargetPlatform() == 3 then
  272. machinetype = "android"
  273. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  274. machinetype = "ios"
  275. end
  276. print("找不到设备的localizedModel字段");
  277. return machinetype;
  278. end
  279. --获取系统版本号
  280. function getSystemVersion()
  281. if PluginDevice then
  282. if cc.Application:getInstance():getTargetPlatform() == 3 then
  283. return PluginDevice:callString("getInfoValue" , "version_release");
  284. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  285. return PluginDevice:callString("getInfoValue" , "systemVersion");
  286. end
  287. end
  288. local sysVersion = ""
  289. if cc.Application:getInstance():getTargetPlatform() == 3 then
  290. sysVersion = "1.0"
  291. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  292. sysVersion = "8.0"
  293. end
  294. print("找不到设备的systemVersion字段");
  295. return sysVersion;
  296. end
  297. --获取手机名
  298. function getSystemName()
  299. if PluginDevice then
  300. if cc.Application:getInstance():getTargetPlatform() == 3 then
  301. return PluginDevice:callString("getSystemName");
  302. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  303. return PluginDevice:callString("getInfoValue" , "systemName");
  304. end
  305. end
  306. local sysVersion = ""
  307. if cc.Application:getInstance():getTargetPlatform() == 3 then
  308. sysVersion = "android"
  309. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  310. sysVersion = "ios"
  311. end
  312. print("找不到设备的getSystemName字段");
  313. return sysVersion;
  314. end
  315. --获取网络类型(wifi,2g,3g)
  316. function getNetworkType()
  317. if PluginDevice then
  318. return PluginDevice:callString("getNetwork");
  319. end
  320. print("找不到设备的getNetwork字段");
  321. return "none";
  322. end
  323. --获取设备号
  324. function getDeviceID()
  325. if PluginDevice then
  326. if cc.Application:getInstance():getTargetPlatform() == 3 then
  327. return PluginDevice:callString("getUDID");
  328. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  329. return PluginDevice:callString("getInfoValue" , "OpenUDID");
  330. end
  331. end
  332. print("找不到设备号字段");
  333. return "";
  334. end
  335. --获取包名
  336. function getAppPackageName()
  337. if PluginDevice then
  338. return PluginDevice:callString("getInfoValue" , "packageName");
  339. else
  340. return ""
  341. end
  342. end
  343. -- 获得应用程序版本 - 内部版本号
  344. -- ios : CFBundleVersion(Build) 小版本
  345. -- android : versionCode(int)
  346. function getAppVersionCode()
  347. if PluginDevice then
  348. return PluginDevice:callString("getInfoValue" , "versionCode");
  349. else
  350. return "100"
  351. end
  352. end
  353. -- 获得应用程序版本 - 发布版本号
  354. -- ios : CFBundleShortVersionString(Version) 大版本1.0.0
  355. -- android : versionName(string)
  356. function getAppVersionName()
  357. if PluginDevice then
  358. return PluginDevice:callString("getInfoValue" , "versionName");
  359. else
  360. return "1.0.0"
  361. end
  362. end
  363. function getAppVersionNum()
  364. local version = getAppVersionName();
  365. local versionNum = string.gsub(version, "%p", "");
  366. return tonumber(versionNum)
  367. end
  368. function getAppVersion()
  369. return getAppVersionName()
  370. end
  371. -- 复制内容到剪贴板
  372. function copyStringToClipboard(str)
  373. -- 暂时屏蔽功能
  374. --do return end
  375. local versionCode = getAppVersionCode()
  376. versionCode = tonumber(versionCode) or 0
  377. --[[ if versionCode < 113 then
  378. return
  379. end--]]
  380. if PluginDevice then
  381. PluginDevice:callVoid("copyStringToClipboard", str or "");
  382. end
  383. end
  384. -- 从剪贴板复制内容
  385. function copyStringFromClipboard()
  386. -- 暂时屏蔽功能
  387. --do return "" end
  388. local versionCode = getAppVersionCode()
  389. versionCode = tonumber(versionCode) or 0
  390. --[[ if versionCode < 113 then
  391. return ""
  392. end--]]
  393. if PluginDevice then
  394. return PluginDevice:callString("copyStringFromClipboard") or "";
  395. end
  396. return ""
  397. end
  398. -- 是否支持截屏保存到相册的功能
  399. function isSupportCopyToDcim()
  400. local versionCode = tonumber(getAppVersionCode() or 0)
  401. if versionCode >= 114 then
  402. return true
  403. else
  404. return false
  405. end
  406. end
  407. function isSupportScreenRotate()
  408. local appVersionNum = getAppVersionNum()
  409. return true
  410. end
  411. --判断appversion的版本是否支持某些功能
  412. function isSupportVersion(ver)
  413. local appVersionNum = getAppVersionNum()
  414. if tonumber(appVersionNum)<ver then
  415. return false
  416. end
  417. return true
  418. end
  419. -- 0:竖屏,1:横屏
  420. function setOrientation(orientation)
  421. if PluginDevice then
  422. PluginDevice:callVoid("setOrientation", orientation)
  423. end
  424. end
  425. function getPowerLevel()
  426. if PluginDevice then
  427. if cc.Application:getInstance():getTargetPlatform() == 3 then
  428. local powerLevel = PluginDevice:callString("getInfoValue" , "powerLevel");
  429. return tonumber(powerLevel)
  430. elseif cc.Application:getInstance():getTargetPlatform() == 4 or cc.Application:getInstance():getTargetPlatform() == 5 then
  431. return PluginDevice:callInt("getBatteryLevel")
  432. end
  433. else
  434. return 100
  435. end
  436. end
  437. function getSingleLevel()
  438. if PluginDevice then
  439. return PluginDevice:callInt("getSingleLevel");
  440. else
  441. return 0
  442. end
  443. end
  444. --如果没有ping 同 或者有问题 返回空字符串
  445. function ping(callback)
  446. if PluginDevice then
  447. local params = {
  448. url = "www.baidu.com",
  449. callback = callback,
  450. }
  451. PluginDevice:callVoid("ping",params);
  452. end
  453. end
  454. function getFrameWorkVersion()
  455. local version = 0;
  456. local func = cc.FileUtils.getFrameWorkVersion
  457. if func ~= nil then
  458. version = cc.FileUtils:getInstance():getFrameWorkVersion()
  459. else
  460. print("getFrameWorkVersion() func is nil")
  461. end
  462. return version;
  463. end
  464. local appPackageName = getAppPackageName()
  465. local appVersionName = getAppVersionName()
  466. local appVersionCode = getAppVersionCode()
  467. print("PluginDevice::appPackageName = " .. appPackageName)
  468. print("PluginDevice::appVersionName = " .. appVersionName)
  469. print("PluginDevice::appVersionCode = " .. appVersionCode)