NibiruService.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. namespace Nxr.Internal
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public class NibiruService
  13. {
  14. private const string NibiruSDKClassName = "com.nibiru.lib.vr.NibiruVR";
  15. private const string ServiceClassName = "com.nibiru.service.NibiruService";
  16. protected AndroidJavaObject androidActivity;
  17. protected AndroidJavaClass nibiruSDKClass;
  18. protected AndroidJavaObject nibiruOsServiceObject;
  19. protected AndroidJavaObject nibiruSensorServiceObject;
  20. protected AndroidJavaObject nibiruVoiceServiceObject;
  21. protected AndroidJavaObject nibiruGestureServiceObject;
  22. protected AndroidJavaObject nibiruVRServiceObject;
  23. protected AndroidJavaObject nibiruCameraServiceObject;
  24. protected AndroidJavaObject nibiruMarkerServiceObject;
  25. protected CameraPreviewHelper cameraPreviewHelper;
  26. protected AndroidJavaObject cameraDeviceObject;
  27. protected AndroidJavaObject audioManager;
  28. public int HMDCameraId;
  29. public int ControllerCameraId;
  30. private bool isCameraPreviewing = false;
  31. private string systemDevice = "";
  32. public void Init()
  33. {
  34. #if UNITY_ANDROID
  35. try
  36. {
  37. using (AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  38. {
  39. androidActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
  40. audioManager = androidActivity.Call<AndroidJavaObject>("getSystemService",
  41. new AndroidJavaObject("java.lang.String", "audio"));
  42. }
  43. }
  44. catch (AndroidJavaException e)
  45. {
  46. androidActivity = null;
  47. Debug.LogError("Exception while connecting to the Activity: " + e);
  48. return;
  49. }
  50. nibiruSDKClass = BaseAndroidDevice.GetClass(NibiruSDKClassName);
  51. //
  52. // systemDevice = nibiruSDKClass.CallStatic<string>("getSystemProperty", "ro.product.device", "");
  53. nibiruOsServiceObject = nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruOSService", androidActivity);
  54. nibiruSensorServiceObject =
  55. nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruSensorService", androidActivity);
  56. nibiruVoiceServiceObject =
  57. nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruVoiceService", androidActivity);
  58. nibiruGestureServiceObject =
  59. nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruGestureService", androidActivity);
  60. nibiruVRServiceObject = nibiruSDKClass.CallStatic<AndroidJavaObject>("getUsingNibiruVRServiceGL");
  61. nibiruCameraServiceObject =
  62. nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruCameraService", androidActivity);
  63. nibiruMarkerServiceObject =
  64. nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruMarkerService", androidActivity);
  65. HMDCameraId = nibiruCameraServiceObject.Call<int>("getHMDCameraId");
  66. ControllerCameraId = nibiruCameraServiceObject.Call<int>("getControllerCameraId");
  67. UpdateVoiceLanguage();
  68. // Debug.Log("nibiruOsServiceObject is "+ nibiruOsServiceObject.Call<AndroidJavaObject>("getClass").Call<string>("getName"));
  69. // Debug.Log("nibiruSensorServiceObject is " + nibiruSensorServiceObject.Call<AndroidJavaObject>("getClass").Call<string>("getName"));
  70. NibiruTask.NibiruTaskApi.Init();
  71. IsCaptureEnabled = -1;
  72. // 默认触发请求权限:
  73. RequsetPermission(new string[]
  74. {
  75. NxrGlobal.Permission.CAMERA,
  76. NxrGlobal.Permission.WRITE_EXTERNAL_STORAGE,
  77. NxrGlobal.Permission.READ_EXTERNAL_STORAGE,
  78. NxrGlobal.Permission.ACCESS_NETWORK_STATE,
  79. NxrGlobal.Permission.ACCESS_COARSE_LOCATION,
  80. NxrGlobal.Permission.BLUETOOTH,
  81. NxrGlobal.Permission.BLUETOOTH_ADMIN,
  82. NxrGlobal.Permission.INTERNET,
  83. NxrGlobal.Permission.GET_TASKS,
  84. });
  85. #endif
  86. }
  87. public static int NKEY_SYS_HANDLE = 0;
  88. public static int NKEY_APP_HANDLE = 1;
  89. /// <summary>
  90. /// Handle N key event
  91. /// 0=system handle
  92. /// 1=app handle
  93. /// </summary>
  94. /// <param name="mode"></param>
  95. public void RegHandleNKey(int mode)
  96. {
  97. if (nibiruVRServiceObject != null)
  98. {
  99. RunOnUIThread(androidActivity,
  100. new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("regHandleNKey", mode); }));
  101. }
  102. else
  103. {
  104. Debug.LogError("regHandleNKey failed, nibiruVRServiceObject is null !!!");
  105. }
  106. }
  107. /// <summary>
  108. /// Enable fps statistics
  109. /// </summary>
  110. /// <param name="isEnabled"></param>
  111. public void SetEnableFPS(bool isEnabled)
  112. {
  113. if (nibiruVRServiceObject != null)
  114. {
  115. nibiruVRServiceObject.Call("setEnableFPS", isEnabled);
  116. }
  117. else
  118. {
  119. Debug.LogError("SetEnableFPS failed, nibiruVRServiceObject is null !!!");
  120. }
  121. }
  122. /// <summary>
  123. /// Get fps : 0=app,1=dtr
  124. /// </summary>
  125. /// <returns></returns>
  126. public float[] GetFPS()
  127. {
  128. if (nibiruVRServiceObject != null)
  129. {
  130. return nibiruVRServiceObject.Call<float[]>("getFPS");
  131. }
  132. else
  133. {
  134. Debug.LogError("SetEnableFPS failed, nibiruVRServiceObject is null !!!");
  135. }
  136. return new float[] {-1, -1};
  137. }
  138. /// <summary>
  139. /// Register virtual mouse service
  140. /// </summary>
  141. /// <param name="serviceStatus"></param>
  142. public void RegisterVirtualMouseService(OnVirtualMouseServiceStatus serviceStatus)
  143. {
  144. if (nibiruOsServiceObject != null)
  145. {
  146. nibiruOsServiceObject.Call("registerVirtualMouseManagerService",
  147. new NibiruVirtualMouseServiceListener(serviceStatus));
  148. }
  149. else
  150. {
  151. Debug.LogError("RegisterVirtualMouseService failed, nibiruOsServiceObject is null !!!");
  152. }
  153. }
  154. /// <summary>
  155. /// UnRegister virtual mouse service
  156. /// </summary>
  157. public void UnRegisterVirtualMouseService()
  158. {
  159. if (nibiruOsServiceObject != null)
  160. {
  161. nibiruOsServiceObject.Call("unRegisterVirtaulMouseManagerService");
  162. }
  163. else
  164. {
  165. Debug.LogError("UnRegisterVirtualMouseService failed, nibiruOsServiceObject is null !!!");
  166. }
  167. }
  168. /// <summary>
  169. /// Set enable virtual mouse
  170. /// </summary>
  171. /// <param name="enabled"></param>
  172. /// <returns></returns>
  173. public bool SetEnableVirtualMouse(bool enabled)
  174. {
  175. if (nibiruOsServiceObject != null)
  176. {
  177. return nibiruOsServiceObject.Call<bool>("setEnableVirtualMouse", enabled);
  178. }
  179. else
  180. {
  181. Debug.LogError("SetEnableVirtualMouse failed, nibiruOsServiceObject is null !!!");
  182. return false;
  183. }
  184. }
  185. public CameraPreviewHelper InitCameraPreviewHelper()
  186. {
  187. cameraPreviewHelper = new CameraPreviewHelper();
  188. return cameraPreviewHelper;
  189. }
  190. public CameraPreviewHelper CameraPreviewHelper
  191. {
  192. get { return cameraPreviewHelper; }
  193. }
  194. /// <summary>
  195. /// Get camera status async
  196. /// </summary>
  197. public void GetCameraStatus()
  198. {
  199. int cameraId = HMDCameraId;
  200. if (nibiruCameraServiceObject != null)
  201. {
  202. nibiruCameraServiceObject.Call("getCameraStatus", cameraId, new CameraStatusCallback());
  203. }
  204. else
  205. {
  206. Debug.LogError("GetCameraStatus failed, because nibiruCameraServiceObject is null !!!");
  207. }
  208. }
  209. /// <summary>
  210. /// Open camera
  211. /// </summary>
  212. /// <returns> NibiruCameraDevice</returns>
  213. private AndroidJavaObject OpenCamera()
  214. {
  215. int cameraId = HMDCameraId;
  216. if (nibiruCameraServiceObject != null && cameraDeviceObject == null)
  217. {
  218. cameraDeviceObject = nibiruCameraServiceObject.Call<AndroidJavaObject>("openCamera", cameraId);
  219. return cameraDeviceObject;
  220. }
  221. else if (cameraDeviceObject != null)
  222. {
  223. return cameraDeviceObject;
  224. }
  225. else
  226. {
  227. Debug.LogError("OpenCamera failed, because nibiruCameraServiceObject is null !!!");
  228. return null;
  229. }
  230. }
  231. /// <summary>
  232. /// Get current cameraId
  233. /// </summary>
  234. /// <returns></returns>
  235. public CAMERA_ID GetCurrentCameraId()
  236. {
  237. return (CAMERA_ID) HMDCameraId;
  238. }
  239. /// <summary>
  240. /// Start camera preView
  241. /// </summary>
  242. public void StartCameraPreView()
  243. {
  244. StartCameraPreView(false);
  245. }
  246. /// <summary>
  247. /// Start camera preView
  248. /// </summary>
  249. /// <param name="triggerFocus">trigger focus</param>
  250. public void StartCameraPreView(bool triggerFocus)
  251. {
  252. OpenCamera();
  253. AndroidJavaObject surfaceTextureObject = cameraPreviewHelper.GetSurfaceTexture();
  254. cameraDeviceObject.Call<bool>("startPreviewWithBestSize", surfaceTextureObject);
  255. if (triggerFocus)
  256. {
  257. DoCameraAutoFocus();
  258. }
  259. isCameraPreviewing = true;
  260. }
  261. /// <summary>
  262. /// Stop camera preView
  263. /// </summary>
  264. public void StopCamereaPreView()
  265. {
  266. if (nibiruCameraServiceObject != null)
  267. {
  268. isCameraPreviewing = false;
  269. nibiruCameraServiceObject.Call("stopPreview");
  270. cameraDeviceObject = null;
  271. }
  272. else
  273. {
  274. Debug.LogError("StopCamereaPreView failed, because nibiruCameraServiceObject is null !!!");
  275. }
  276. }
  277. /// <summary>
  278. /// Determine whether the camera is in preview.
  279. /// </summary>
  280. /// <returns></returns>
  281. public bool IsCameraPreviewing()
  282. {
  283. return isCameraPreviewing;
  284. }
  285. public void SetCameraPreviewing(bool enabled)
  286. {
  287. isCameraPreviewing = true;
  288. }
  289. public void DoCameraAutoFocus()
  290. {
  291. if (cameraDeviceObject != null)
  292. {
  293. cameraDeviceObject.Call("doAutoFocus");
  294. }
  295. else
  296. {
  297. Debug.LogError("DoCameraAutoFocus failed, because cameraDeviceObject is null !!!");
  298. }
  299. }
  300. public void EnableVoiceService(bool enabled)
  301. {
  302. if (nibiruVoiceServiceObject != null)
  303. {
  304. nibiruVoiceServiceObject.Call("setEnableVoice", enabled);
  305. }
  306. else
  307. {
  308. Debug.LogError("EnableVoiceService failed, because nibiruVoiceServiceObject is null !!!");
  309. }
  310. }
  311. /// <summary>
  312. /// Start voice recording
  313. /// </summary>
  314. public void StartVoiceRecording()
  315. {
  316. if (nibiruVoiceServiceObject != null)
  317. {
  318. nibiruVoiceServiceObject.Call("startRecording");
  319. }
  320. else
  321. {
  322. Debug.LogError("StartVoiceRecording failed, because nibiruVoiceServiceObject is null !!!");
  323. }
  324. }
  325. /// <summary>
  326. /// Stop voice recording
  327. /// </summary>
  328. public void StopVoiceRecording()
  329. {
  330. if (nibiruVoiceServiceObject != null)
  331. {
  332. nibiruVoiceServiceObject.Call("stopRecording");
  333. }
  334. else
  335. {
  336. Debug.LogError("StopVoiceRecording failed, because nibiruVoiceServiceObject is null !!!");
  337. }
  338. }
  339. /// <summary>
  340. /// Cancel voice recognizer
  341. /// </summary>
  342. public void CancelVoiceRecognizer()
  343. {
  344. if (nibiruVoiceServiceObject != null)
  345. {
  346. nibiruVoiceServiceObject.Call("cancelRecognizer");
  347. }
  348. else
  349. {
  350. Debug.LogError("CancelVoiceRecognizer failed, because nibiruVoiceServiceObject is null !!!");
  351. }
  352. }
  353. public bool IsSupportVoice()
  354. {
  355. if (nibiruVoiceServiceObject != null)
  356. {
  357. return nibiruVoiceServiceObject.Call<bool>("isMicrophoneVoice");
  358. }
  359. else
  360. {
  361. Debug.LogError("IsSupportVoice failed, because nibiruVoiceServiceObject is null !!!");
  362. }
  363. return false;
  364. }
  365. public bool IsSupport6DOF()
  366. {
  367. if (nibiruVRServiceObject != null)
  368. {
  369. return nibiruVRServiceObject.Call<bool>("isSupport6Dof");
  370. }
  371. else
  372. {
  373. Debug.LogError("IsSupport6DOF failed, because nibiruVRServiceObject is null !!!");
  374. }
  375. return false;
  376. }
  377. public bool IsSupportGesture()
  378. {
  379. if (nibiruGestureServiceObject != null)
  380. {
  381. return nibiruGestureServiceObject.Call<bool>("isCameraGesture");
  382. }
  383. else
  384. {
  385. Debug.LogError("isSupportGesture failed, because nibiruGestureServiceObject is null !!!");
  386. }
  387. return false;
  388. }
  389. public void UpdateVoiceLanguage()
  390. {
  391. if (nibiruVoiceServiceObject != null)
  392. {
  393. nibiruVoiceServiceObject.Call("setVoicePID", (int) NxrGlobal.voiceLanguage);
  394. }
  395. else
  396. {
  397. Debug.LogError("UpdateVoiceLanguage failed, because nibiruVoiceServiceObject is null !!!");
  398. }
  399. }
  400. /// <summary>
  401. /// Control camera status of gesture service:false-Turn off camera occupation,true-Turn on camera occupation
  402. /// </summary>
  403. /// <param name="enabled"></param>
  404. public void EnableGestureService(bool enabled)
  405. {
  406. if (nibiruGestureServiceObject != null)
  407. {
  408. nibiruGestureServiceObject.Call("setEnableGesture", enabled);
  409. }
  410. else
  411. {
  412. Debug.LogError("EnableGestureService failed, because nibiruGestureServiceObject is null !!!");
  413. }
  414. }
  415. public bool IsCameraGesture()
  416. {
  417. if (nibiruGestureServiceObject != null)
  418. {
  419. return nibiruGestureServiceObject.Call<bool>("isCameraGesture");
  420. }
  421. return false;
  422. }
  423. public delegate void OnSensorDataChanged(NibiruSensorEvent sensorEvent);
  424. /// <summary>
  425. /// The callback when sensor data changes.
  426. /// </summary>
  427. public static OnSensorDataChanged OnSensorDataChangedHandler;
  428. class NibiruSensorDataListenerCallback : AndroidJavaProxy
  429. {
  430. public NibiruSensorDataListenerCallback() : base(
  431. "com.nibiru.service.NibiruSensorService$INibiruSensorDataListener")
  432. {
  433. }
  434. public void onSensorDataChanged(AndroidJavaObject sensorEventObject)
  435. {
  436. float x = sensorEventObject.Get<float>("x");
  437. float y = sensorEventObject.Get<float>("y");
  438. float z = sensorEventObject.Get<float>("z");
  439. long timestamp = sensorEventObject.Get<long>("timestamp");
  440. AndroidJavaObject locationObject = sensorEventObject.Get<AndroidJavaObject>("sensorLocation");
  441. AndroidJavaObject typeObject = sensorEventObject.Get<AndroidJavaObject>("sensorType");
  442. SENSOR_LOCATION sensorLocation = (SENSOR_LOCATION) locationObject.Call<int>("ordinal");
  443. SENSOR_TYPE sensorType = (SENSOR_TYPE) typeObject.Call<int>("ordinal");
  444. NibiruSensorEvent sensorEvent = new NibiruSensorEvent(x, y, z, timestamp, sensorType, sensorLocation);
  445. // sensorEvent.printLog();
  446. // 用Loom的方法在Unity主线程中调用Text组件
  447. Loom.QueueOnMainThread((param) =>
  448. {
  449. if (OnSensorDataChangedHandler != null)
  450. {
  451. OnSensorDataChangedHandler((NibiruSensorEvent) param);
  452. }
  453. }, sensorEvent);
  454. }
  455. }
  456. private NibiruSensorDataListenerCallback nibiruSensorDataListenerCallback;
  457. public void RegisterSensorListener(SENSOR_TYPE type, SENSOR_LOCATION location)
  458. {
  459. if (nibiruSensorServiceObject != null)
  460. {
  461. if (nibiruSensorDataListenerCallback == null)
  462. {
  463. nibiruSensorDataListenerCallback = new NibiruSensorDataListenerCallback();
  464. }
  465. // UI线程执行
  466. RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
  467. {
  468. AndroidJavaClass locationClass =
  469. BaseAndroidDevice.GetClass("com.nibiru.service.NibiruSensorService$SENSOR_LOCATION");
  470. AndroidJavaObject locationObj =
  471. locationClass.CallStatic<AndroidJavaObject>("valueOf", location.ToString());
  472. AndroidJavaClass typeClass =
  473. BaseAndroidDevice.GetClass("com.nibiru.service.NibiruSensorService$SENSOR_TYPE");
  474. AndroidJavaObject typeObj = typeClass.CallStatic<AndroidJavaObject>("valueOf", type.ToString());
  475. nibiruSensorServiceObject.Call<bool>("registerSensorListener", typeObj, locationObj,
  476. nibiruSensorDataListenerCallback);
  477. Debug.Log("registerSensorListener=" + type.ToString() + "," + location.ToString());
  478. }
  479. ));
  480. }
  481. else
  482. {
  483. Debug.LogError("RegisterControllerSensor failed, nibiruSensorServiceObject is null !");
  484. }
  485. }
  486. public void UnRegisterSensorListener()
  487. {
  488. if (nibiruSensorServiceObject != null)
  489. {
  490. // UI线程执行
  491. RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
  492. {
  493. nibiruSensorServiceObject.Call("unregisterSensorListenerAll");
  494. }
  495. ));
  496. }
  497. else
  498. {
  499. Debug.LogError("UnRegisterSensorListener failed, nibiruSensorServiceObject is null !");
  500. }
  501. }
  502. //4.1 获取屏幕亮度值:
  503. /// <summary>
  504. /// Get system's brightness value
  505. /// </summary>
  506. /// <returns></returns>
  507. public int GetBrightnessValue()
  508. {
  509. int BrightnessValue = 0;
  510. #if UNITY_ANDROID
  511. BaseAndroidDevice.CallObjectMethod<int>(ref BrightnessValue, nibiruOsServiceObject, "getBrightnessValue");
  512. #endif
  513. return BrightnessValue;
  514. }
  515. //4.2 调节屏幕亮度:
  516. /// <summary>
  517. /// Set system's brightness value
  518. /// </summary>
  519. /// <returns></returns>
  520. public void SetBrightnessValue(int value)
  521. {
  522. if (nibiruOsServiceObject == null) return;
  523. #if UNITY_ANDROID
  524. RunOnUIThread(androidActivity,
  525. new AndroidJavaRunnable(() =>
  526. {
  527. BaseAndroidDevice.CallObjectMethod(nibiruOsServiceObject, "setBrightnessValue", value, 200.01f);
  528. }));
  529. #endif
  530. }
  531. //4.3 获取当前2D/3D显示模式:
  532. /// <summary>
  533. /// Get display mode 2d/3d
  534. /// </summary>
  535. /// <returns></returns>
  536. public DISPLAY_MODE GetDisplayMode()
  537. {
  538. if (nibiruOsServiceObject == null) return DISPLAY_MODE.MODE_2D;
  539. AndroidJavaObject androidObject = nibiruOsServiceObject.Call<AndroidJavaObject>("getDisplayMode");
  540. int mode = androidObject.Call<int>("ordinal");
  541. return (DISPLAY_MODE) mode;
  542. }
  543. //4.4 切换2D/3D显示模式:
  544. /// <summary>
  545. /// Set display mode 2d/3d
  546. /// </summary>
  547. /// <param name="displayMode"></param>
  548. public void SetDisplayMode(DISPLAY_MODE displayMode)
  549. {
  550. if (nibiruOsServiceObject != null)
  551. {
  552. RunOnUIThread(androidActivity,
  553. new AndroidJavaRunnable(() =>
  554. {
  555. nibiruOsServiceObject.Call("setDisplayMode", (int) displayMode);
  556. }));
  557. }
  558. }
  559. // 渠道ID
  560. /// <summary>
  561. /// Get system's channel code
  562. /// </summary>
  563. /// <returns></returns>
  564. public string GetChannelCode()
  565. {
  566. if (nibiruOsServiceObject == null) return "NULL";
  567. return nibiruOsServiceObject.Call<string>("getChannelCode");
  568. }
  569. // 型号
  570. /// <summary>
  571. /// Get device's model
  572. /// </summary>
  573. /// <returns></returns>
  574. public string GetModel()
  575. {
  576. if (nibiruOsServiceObject == null) return "NULL";
  577. return nibiruOsServiceObject.Call<string>("getModel");
  578. }
  579. // 系统OS版本
  580. /// <summary>
  581. /// Get system's os version
  582. /// </summary>
  583. /// <returns></returns>
  584. public string GetOSVersion()
  585. {
  586. if (nibiruOsServiceObject == null) return "NULL";
  587. return nibiruOsServiceObject.Call<string>("getOSVersion");
  588. }
  589. // 系统OS版本号
  590. /// <summary>
  591. /// Get system's service version
  592. /// </summary>
  593. /// <returns></returns>
  594. public int GetOSVersionCode()
  595. {
  596. if (nibiruOsServiceObject == null) return -1;
  597. return nibiruOsServiceObject.Call<int>("getOSVersionCode");
  598. }
  599. // 系统服务版本
  600. /// <summary>
  601. /// Get system's service version code
  602. /// </summary>
  603. /// <returns></returns>
  604. public string GetServiceVersionCode()
  605. {
  606. if (nibiruOsServiceObject == null) return "NULL";
  607. return nibiruOsServiceObject.Call<string>("getServiceVersionCode");
  608. }
  609. // 获取厂家软件版本:(对应驱动板软件版本号)
  610. /// <summary>
  611. /// Get system's vendor SW version
  612. /// </summary>
  613. /// <returns></returns>
  614. public string GetVendorSWVersion()
  615. {
  616. if (nibiruOsServiceObject == null) return "NULL";
  617. return nibiruOsServiceObject.Call<string>("getVendorSWVersion");
  618. }
  619. // 控制touchpad是否显示 value为true表示显示,false表示不显示
  620. /// <summary>
  621. /// Control whether touchpad is displayed. true-display false-not display
  622. /// </summary>
  623. /// <param name="isEnable"></param>
  624. public void SetEnableTouchCursor(bool isEnable)
  625. {
  626. RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
  627. {
  628. if (nibiruOsServiceObject != null)
  629. {
  630. nibiruOsServiceObject.Call("setEnableTouchCursor", isEnable);
  631. }
  632. }));
  633. }
  634. /// <summary>
  635. /// Get the value of the distance sensor.
  636. /// </summary>
  637. /// <returns></returns>
  638. public int GetProximityValue()
  639. {
  640. if (nibiruSensorServiceObject == null) return -1;
  641. return nibiruSensorServiceObject.Call<int>("getProximityValue");
  642. }
  643. /// <summary>
  644. /// Get the value of light perception.
  645. /// </summary>
  646. /// <returns></returns>
  647. public int GetLightValue()
  648. {
  649. if (nibiruSensorServiceObject == null) return -1;
  650. return nibiruSensorServiceObject.Call<int>("getLightValue");
  651. }
  652. // UI线程中运行
  653. public void RunOnUIThread(AndroidJavaObject activityObj, AndroidJavaRunnable r)
  654. {
  655. activityObj.Call("runOnUiThread", r);
  656. }
  657. public delegate void CameraIdle();
  658. public delegate void CameraBusy();
  659. /// <summary>
  660. /// The callback when Camera is idle.
  661. /// </summary>
  662. public static CameraIdle OnCameraIdle;
  663. /// <summary>
  664. /// The callback when Camera is busy.
  665. /// </summary>
  666. public static CameraBusy OnCameraBusy;
  667. public delegate void OnRecorderSuccess();
  668. public delegate void OnRecorderFailed();
  669. public static OnRecorderSuccess OnRecorderSuccessHandler;
  670. public static OnRecorderFailed OnRecorderFailedHandler;
  671. class CameraStatusCallback : AndroidJavaProxy
  672. {
  673. public CameraStatusCallback() : base("com.nibiru.lib.vr.listener.NVRCameraStatusListener")
  674. {
  675. }
  676. public void cameraBusy()
  677. {
  678. // 从Android UI线程回调过来的,加入到Unity主线程处理
  679. // NxrViewer.Instance.TriggerCameraStatus(1);
  680. Loom.QueueOnMainThread((param) =>
  681. {
  682. if (OnCameraBusy != null)
  683. {
  684. OnCameraBusy();
  685. }
  686. }, 1);
  687. Debug.Log("cameraBusy");
  688. }
  689. public void cameraIdle()
  690. {
  691. // 从Android UI线程回调过来的,加入到Unity主线程处理
  692. // NxrViewer.Instance.TriggerCameraStatus(0);
  693. Loom.QueueOnMainThread((param) =>
  694. {
  695. if (OnCameraIdle != null)
  696. {
  697. OnCameraIdle();
  698. }
  699. }, 0);
  700. Debug.Log("cameraIdle");
  701. }
  702. }
  703. class CaptureCallback : AndroidJavaProxy
  704. {
  705. public CaptureCallback() : base("com.nibiru.lib.vr.listener.NVRVideoCaptureListener")
  706. {
  707. }
  708. public void onSuccess()
  709. {
  710. // 从Android UI线程回调过来的,加入到Unity主线程处理
  711. // NxrViewer.Instance.TriggerCaptureStatus(1);
  712. Loom.QueueOnMainThread((param) =>
  713. {
  714. if (OnRecorderSuccessHandler != null)
  715. {
  716. OnRecorderSuccessHandler();
  717. }
  718. }, 1);
  719. }
  720. public void onFailed()
  721. {
  722. // 从Android UI线程回调过来的,加入到Unity主线程处理
  723. // NxrViewer.Instance.TriggerCaptureStatus(0);
  724. Loom.QueueOnMainThread((param) =>
  725. {
  726. if (OnRecorderFailedHandler != null)
  727. {
  728. OnRecorderFailedHandler();
  729. }
  730. }, 0);
  731. }
  732. }
  733. public int IsCaptureEnabled { set; get; }
  734. public static int BIT_RATE = 4000000;
  735. /// <summary>
  736. /// Start capture
  737. /// </summary>
  738. /// <param name="path"></param>
  739. public void StartCapture(string path)
  740. {
  741. StartCapture(path, -1);
  742. }
  743. /// <summary>
  744. /// Start capture
  745. /// </summary>
  746. /// <param name="path"></param>
  747. /// <param name="seconds"></param>
  748. public void StartCapture(string path, int seconds)
  749. {
  750. StartCapture(path, BIT_RATE, seconds);
  751. }
  752. private static int videoSize = (int) VIDEO_SIZE.V720P;
  753. // private static int captureCameraId = (int)CAMERA_ID.FRONT;
  754. /// <summary>
  755. /// Start capture
  756. /// </summary>
  757. /// <param name="path"></param>
  758. /// <param name="bitRate"></param>
  759. /// <param name="seconds"></param>
  760. public void StartCapture(string path, int bitRate, int seconds)
  761. {
  762. IsCaptureEnabled = 1;
  763. nibiruSDKClass.CallStatic("startCaptureForUnity", new CaptureCallback(), path, bitRate, seconds, videoSize,
  764. HMDCameraId);
  765. }
  766. public static void SetCaptureVideoSize(VIDEO_SIZE video_Size)
  767. {
  768. videoSize = (int) video_Size;
  769. }
  770. /// <summary>
  771. /// Stop capture
  772. /// </summary>
  773. public void StopCapture()
  774. {
  775. nibiruSDKClass.CallStatic("stopCaptureForUnity");
  776. IsCaptureEnabled = 0;
  777. }
  778. public bool CaptureDrawFrame(int textureId, int frameId)
  779. {
  780. if (IsCaptureEnabled <= -3)
  781. {
  782. return false;
  783. }
  784. else if (IsCaptureEnabled <= 0 && IsCaptureEnabled >= -2)
  785. {
  786. // 在stop后,多执行3次,用于内部处理stop的逻辑。
  787. IsCaptureEnabled--;
  788. }
  789. return nibiruSDKClass.CallStatic<bool>("onDrawFrameForUnity", textureId, frameId);
  790. }
  791. private const int STREAM_VOICE_CALL = 0;
  792. private const int STREAM_SYSTEM = 1;
  793. private const int STREAM_RING = 2;
  794. private const int STREAM_MUSIC = 3;
  795. private const int STREAM_ALARM = 4;
  796. private const int STREAM_NOTIFICATION = 5;
  797. private const string currentVolume = "getStreamVolume"; //当前音量
  798. private const string maxVolume = "getStreamMaxVolume"; //最大音量
  799. public int GetVolumeValue()
  800. {
  801. if (audioManager == null) return 0;
  802. return audioManager.Call<int>(currentVolume, STREAM_MUSIC);
  803. }
  804. public int GetMaxVolume()
  805. {
  806. if (audioManager == null) return 1;
  807. return audioManager.Call<int>(maxVolume, STREAM_MUSIC);
  808. }
  809. public void EnabledMarkerAutoFocus(bool enabled)
  810. {
  811. if (nibiruMarkerServiceObject == null)
  812. {
  813. Debug.LogError("nibiruMarkerServiceObject is null");
  814. }
  815. else if (isMarkerRecognizeRunning)
  816. {
  817. nibiruMarkerServiceObject.Call(enabled ? "doAutoFocus" : "stopAutoFocus");
  818. }
  819. }
  820. /// <summary>
  821. /// Set marker recognize cameraId
  822. /// </summary>
  823. /// <param name="cameraID"></param>
  824. private void SetMarkerRecognizeCameraId(int cameraID)
  825. {
  826. if (nibiruMarkerServiceObject == null)
  827. {
  828. Debug.LogError("nibiruMarkerServiceObject is null");
  829. }
  830. else
  831. {
  832. nibiruMarkerServiceObject.Call("setCameraId", cameraID);
  833. }
  834. }
  835. private bool isMarkerRecognizeRunning;
  836. public bool IsMarkerRecognizeRunning
  837. {
  838. get { return isMarkerRecognizeRunning; }
  839. set { isMarkerRecognizeRunning = value; }
  840. }
  841. /// <summary>
  842. /// Start marker recognize
  843. /// </summary>
  844. public void StartMarkerRecognize()
  845. {
  846. if (nibiruMarkerServiceObject == null)
  847. {
  848. Debug.LogError("nibiruMarkerServiceObject is null");
  849. }
  850. else if (!isMarkerRecognizeRunning)
  851. {
  852. // 默认使用前置相机
  853. SetMarkerRecognizeCameraId(HMDCameraId);
  854. // 焦距,具体不同机器可以需要微调 16 , 640 * 480
  855. nibiruMarkerServiceObject.Call("setCameraZoom", NxrGlobal.GetMarkerCameraZoom());
  856. nibiruMarkerServiceObject.Call("setPreviewSize", 640, 480);
  857. nibiruMarkerServiceObject.Call("startMarkerRecognize");
  858. isMarkerRecognizeRunning = true;
  859. }
  860. }
  861. /// <summary>
  862. /// Stop marker recognize
  863. /// </summary>
  864. public void StopMarkerRecognize()
  865. {
  866. if (nibiruMarkerServiceObject == null)
  867. {
  868. Debug.LogError("nibiruMarkerServiceObject is null");
  869. }
  870. else if (isMarkerRecognizeRunning)
  871. {
  872. nibiruMarkerServiceObject.Call("stopMarkerRecognize");
  873. isMarkerRecognizeRunning = false;
  874. }
  875. }
  876. /// <summary>
  877. /// Get the ViewMatrix of Marker.
  878. /// </summary>
  879. /// <returns></returns>
  880. public float[] GetMarkerViewMatrix()
  881. {
  882. if (nibiruMarkerServiceObject == null)
  883. {
  884. Debug.LogError("nibiruMarkerServiceObject is null");
  885. return null;
  886. }
  887. else
  888. {
  889. float[] result = nibiruMarkerServiceObject.Call<float[]>("getMarkerViewMatrix");
  890. if (result == null || result.Length == 0) return null;
  891. // 全是0
  892. if (IsAllZero(result)) return null;
  893. return result;
  894. }
  895. }
  896. public static bool IsAllZero(float[] array)
  897. {
  898. for (int i = 0, l = array.Length; i < l; i++)
  899. {
  900. if (array[i] != 0) return false;
  901. }
  902. return true;
  903. }
  904. public float[] GetMarkerViewMatrix(int eyeType)
  905. {
  906. if (nibiruMarkerServiceObject == null)
  907. {
  908. Debug.LogError("nibiruMarkerServiceObject is null");
  909. return null;
  910. }
  911. else
  912. {
  913. float[] result = nibiruMarkerServiceObject.Call<float[]>("getMarkerViewMatrix", eyeType);
  914. if (result == null || result.Length == 0) return null;
  915. // 全是0
  916. if (IsAllZero(result)) return null;
  917. return result;
  918. }
  919. }
  920. public float[] GetMarkerProjectionMatrix()
  921. {
  922. if (nibiruMarkerServiceObject == null)
  923. {
  924. Debug.LogError("nibiruMarkerServiceObject is null");
  925. return null;
  926. }
  927. else
  928. {
  929. float[] projArr = nibiruMarkerServiceObject.Call<float[]>("getProjection");
  930. if (projArr == null || projArr.Length == 0)
  931. return null;
  932. return projArr;
  933. }
  934. }
  935. public string GetMarkerDetectStatus()
  936. {
  937. if (nibiruMarkerServiceObject == null)
  938. {
  939. Debug.LogError("GetMarkerDetectStatus failed, nibiruMarkerServiceObject is null");
  940. return "-1";
  941. }
  942. string res = nibiruMarkerServiceObject.Call<string>("getParameters", "p_detect_status");
  943. return res == null ? "-1" : res;
  944. }
  945. public delegate void OnVirtualMouseServiceStatus(bool succ);
  946. public class NibiruVirtualMouseServiceListener : AndroidJavaProxy
  947. {
  948. OnVirtualMouseServiceStatus _OnVirtualMouseServiceStatus;
  949. public NibiruVirtualMouseServiceListener(OnVirtualMouseServiceStatus onVirtualMouseServiceStatus) : base(
  950. "com.nibiru.service.NibiruVirtualMouseManager$VirtualMouseServiceListener")
  951. {
  952. _OnVirtualMouseServiceStatus = onVirtualMouseServiceStatus;
  953. }
  954. public void onServiceRegisterResult(bool succ)
  955. {
  956. if (_OnVirtualMouseServiceStatus != null)
  957. {
  958. _OnVirtualMouseServiceStatus(succ);
  959. }
  960. }
  961. }
  962. public void PauseGestureService()
  963. {
  964. if (nibiruGestureServiceObject != null)
  965. {
  966. nibiruGestureServiceObject.Call("onPause");
  967. }
  968. else
  969. {
  970. Debug.LogError("onPause failed, because nibiruGestureServiceObject is null !!!");
  971. }
  972. }
  973. public void ResumeGestureService()
  974. {
  975. if (nibiruGestureServiceObject != null)
  976. {
  977. nibiruGestureServiceObject.Call("onResume");
  978. }
  979. else
  980. {
  981. Debug.LogError("onResume failed, because nibiruGestureServiceObject is null !!!");
  982. }
  983. }
  984. private AndroidJavaObject javaArrayFromCS(string[] values)
  985. {
  986. AndroidJavaClass arrayClass = new AndroidJavaClass("java.lang.reflect.Array");
  987. AndroidJavaObject arrayObject = arrayClass.CallStatic<AndroidJavaObject>("newInstance",
  988. new AndroidJavaClass("java.lang.String"), values.Count());
  989. for (int i = 0; i < values.Count(); ++i)
  990. {
  991. arrayClass.CallStatic("set", arrayObject, i, new AndroidJavaObject("java.lang.String", values[i]));
  992. }
  993. return arrayObject;
  994. }
  995. /// <summary>
  996. /// Request permission
  997. /// </summary>
  998. /// <param name="names">NxrGlobal.Permission</param>
  999. public void RequsetPermission(string[] names)
  1000. {
  1001. if (nibiruOsServiceObject != null)
  1002. {
  1003. nibiruOsServiceObject.Call("requestPermission", javaArrayFromCS(names));
  1004. }
  1005. }
  1006. /// <summary>
  1007. /// Get QCOM of product device.
  1008. /// </summary>
  1009. /// <returns></returns>
  1010. public QCOMProductDevice GetQCOMProductDevice()
  1011. {
  1012. if ("msm8996".Equals(systemDevice))
  1013. {
  1014. return QCOMProductDevice.QCOM_820;
  1015. }
  1016. else if ("msm8998".Equals(systemDevice))
  1017. {
  1018. return QCOMProductDevice.QCOM_835;
  1019. }
  1020. else if ("sdm710".Equals(systemDevice))
  1021. {
  1022. return QCOMProductDevice.QCOM_XR1;
  1023. }
  1024. else if ("sdm845".Equals(systemDevice))
  1025. {
  1026. return QCOMProductDevice.QCOM_845;
  1027. }
  1028. return QCOMProductDevice.QCOM_UNKNOW;
  1029. }
  1030. public void LockToCur()
  1031. {
  1032. if (nibiruVRServiceObject != null)
  1033. {
  1034. RunOnUIThread(androidActivity,
  1035. new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("lockTracker"); }));
  1036. }
  1037. else
  1038. {
  1039. Debug.LogError("LockToCur failed, nibiruVRServiceObject is null !!!");
  1040. }
  1041. }
  1042. public void LockToFront()
  1043. {
  1044. if (nibiruVRServiceObject != null)
  1045. {
  1046. RunOnUIThread(androidActivity,
  1047. new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("lockTrackerToFront"); }));
  1048. }
  1049. else
  1050. {
  1051. Debug.LogError("LockToFront failed, nibiruVRServiceObject is null !!!");
  1052. }
  1053. }
  1054. public void UnLock()
  1055. {
  1056. if (nibiruVRServiceObject != null)
  1057. {
  1058. RunOnUIThread(androidActivity,
  1059. new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("unlockTracker"); }));
  1060. }
  1061. else
  1062. {
  1063. Debug.LogError("UnLock failed, nibiruVRServiceObject is null !!!");
  1064. }
  1065. }
  1066. }
  1067. }