AndroidDevice.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_ANDROID
  15. using UnityEngine;
  16. /// @cond
  17. namespace Nxr.Internal
  18. {
  19. public class AndroidDevice : NxrDevice
  20. {
  21. //
  22. private const string ActivityListenerClass =
  23. "com.nibiru.lib.xr.unity.NibiruVRUnityService";
  24. // sdk-class
  25. private const string NibiruVRClass = "com.nibiru.lib.vr.NibiruVR";
  26. private static AndroidJavaObject activityListener, nibiruVR;
  27. AndroidJavaObject nibiruVRService = null;
  28. public override void Init()
  29. {
  30. SetApplicationState();
  31. ConnectToActivity();
  32. base.Init();
  33. }
  34. protected override void ConnectToActivity()
  35. {
  36. base.ConnectToActivity();
  37. if (androidActivity != null && activityListener == null)
  38. {
  39. activityListener = Create(ActivityListenerClass);
  40. }
  41. if (androidActivity != null && nibiruVR == null)
  42. {
  43. nibiruVR = Create(NibiruVRClass);
  44. }
  45. }
  46. public override void TurnOff()
  47. {
  48. CallStaticMethod(activityListener, "shutdownBroadcast");
  49. }
  50. public override void Reboot()
  51. {
  52. CallStaticMethod(activityListener, "rebootBroadcast");
  53. }
  54. public override long CreateNibiruVRService()
  55. {
  56. string hmdType = "NONE";
  57. CallStaticMethod(ref hmdType, nibiruVR, "getMetaData", androidActivity, "HMD_TYPE");
  58. if (hmdType != null)
  59. {
  60. NxrViewer.Instance.HmdType = hmdType.Equals("AR") ? HMD_TYPE.AR : (hmdType.Equals("VR") ? HMD_TYPE.VR : HMD_TYPE.NONE);
  61. }
  62. string initParams = "";
  63. long pointer = 0;
  64. CallStaticMethod(ref initParams, nibiruVR, "initNibiruVRServiceForUnity", androidActivity);
  65. // -1207076736_0_1_1_1_20.0_20.0
  66. Debug.Log("initParams is " + initParams + ",hmdType is " + hmdType);
  67. string[] data = initParams.Split('_');
  68. pointer = long.Parse(data[0]);
  69. NxrGlobal.supportDtr = (int.Parse(data[1]) == 1 ? true : false);
  70. NxrGlobal.distortionEnabled = (int.Parse(data[2]) == 1 ? true : false);
  71. NxrGlobal.useNvrSo = (int.Parse(data[3]) == 1 ? true : false);
  72. if (data.Length >= 5)
  73. {
  74. NxrGlobal.offaxisDistortionEnabled = (int.Parse(data[4]) == 1 ? true : false);
  75. }
  76. // 6dof
  77. if (NxrViewer.Instance.TrackerPosition)
  78. {
  79. CallStaticMethod(nibiruVR, "setTrackingModeForUnity", (int)TRACKING_MODE.POSITION);
  80. }
  81. int meshSizeX = -1;
  82. if (data.Length >= 6)
  83. {
  84. meshSizeX = (int)float.Parse(data[5]);
  85. }
  86. int meshSizeY = -1;
  87. if (data.Length >= 7)
  88. {
  89. meshSizeY = (int)float.Parse(data[6]);
  90. }
  91. if (data.Length >= 8)
  92. {
  93. float fps = float.Parse(data[7]);
  94. // 防止从系统获取的刷新率出现异常,此处保证最低为60
  95. NxrGlobal.refreshRate = Mathf.Max(60, fps > 0 ? fps : 0);
  96. }
  97. if (meshSizeX > 0 && meshSizeY > 0)
  98. {
  99. NxrGlobal.meshSize = new int[] { meshSizeX, meshSizeY };
  100. }
  101. string channelCode = "";
  102. CallStaticMethod<string>(ref channelCode, nibiruVR, "getChannelCode");
  103. NxrGlobal.channelCode = channelCode;
  104. // 系统支持
  105. int[] allVersion = new int[] { -1, -1, -1, -1 };
  106. CallStaticMethod(ref allVersion, nibiruVR, "getVersionForUnity");
  107. NxrGlobal.soVersion = allVersion[0];
  108. NxrGlobal.jarVersion = allVersion[1];
  109. NxrGlobal.platPerformanceLevel = allVersion[2];
  110. NxrGlobal.platformID = allVersion[3];
  111. NxrSDKApi.Instance.IsSptMultiThreadedRendering = NxrGlobal.soVersion >= 414;
  112. NxrGlobal.isVR9Platform = NxrGlobal.platformID == (int)PLATFORM.PLATFORM_VR9;
  113. if (NxrGlobal.isVR9Platform)
  114. {
  115. NxrGlobal.distortionEnabled = false;
  116. NxrGlobal.supportDtr = true;
  117. NxrViewer.Instance.SwitchControllerMode(false);
  118. }
  119. if(!NxrSDKApi.Instance.IsSptMultiThreadedRendering && SystemInfo.graphicsMultiThreaded)
  120. {
  121. AndroidLog("*****Warning******\n\n System Does Not Support Unity MultiThreadedRendering !!! \n\n*****Warning******");
  122. AndroidLog("Support Unity MultiThreadedRendering Need V2 Version >=414, Currently Is " + NxrGlobal.soVersion + " !!!");
  123. }
  124. Debug.Log("AndDev->Service : [pointer]=" + pointer + ", [dtrSpt] =" + NxrGlobal.supportDtr + ", [DistEnabled]=" +
  125. NxrGlobal.distortionEnabled + ", [useNvrSo]=" + NxrGlobal.useNvrSo + ", [code]=" + channelCode + ", [jar]=" + NxrGlobal.jarVersion + ", [so]=" + NxrGlobal.soVersion
  126. + ", [platform id]=" + NxrGlobal.platformID + ", [pl]=" + NxrGlobal.platPerformanceLevel + ",[offaxisDist]=" + NxrGlobal.offaxisDistortionEnabled + ",[mesh]=" + meshSizeX +
  127. "*" + meshSizeY + ",[fps]=" + NxrGlobal.refreshRate + "," + channelCode);
  128. // 读取cardboard参数
  129. string cardboardParams = "";
  130. CallStaticMethod<string>(ref cardboardParams, nibiruVR, "getNibiruVRConfigFull");
  131. if (cardboardParams.Length > 0)
  132. {
  133. Debug.Log("cardboardParams is " + cardboardParams);
  134. string[] profileData = cardboardParams.Split('_');
  135. for (int i = 0; i < NxrGlobal.dftProfileParams.Length; i++)
  136. {
  137. if (i >= profileData.Length) break;
  138. if (profileData[i] == null || profileData[i].Length == 0) continue;
  139. NxrGlobal.dftProfileParams[i] = float.Parse(profileData[i]);
  140. }
  141. }
  142. else
  143. {
  144. Debug.Log("Nxr->AndroidDevice->getNibiruVRConfigFull Failed ! ");
  145. }
  146. // offaxis distortion
  147. if (NxrGlobal.offaxisDistortionEnabled)
  148. {
  149. string offaxisParams = "";
  150. CallStaticMethod<string>(ref offaxisParams, nibiruVR, "getOffAxisDistortionConfig");
  151. if (offaxisParams != null && offaxisParams.Length > 0)
  152. {
  153. NxrGlobal.offaxisDistortionConfigData = offaxisParams;
  154. // Debug.LogError(offaxisParams);
  155. }
  156. string sdkParams = "";
  157. CallStaticMethod<string>(ref sdkParams, nibiruVR, "getSDKConfig");
  158. if (sdkParams != null && sdkParams.Length > 0)
  159. {
  160. NxrGlobal.sdkConfigData = sdkParams;
  161. string[] linesCN = sdkParams.Split('\n');
  162. //key=value
  163. foreach (string line in linesCN)
  164. {
  165. if (line == null || line.Length <= 1)
  166. {
  167. continue;
  168. }
  169. string[] keyAndValue = line.Split('=');
  170. //Debug.Log("line=" + line);
  171. if (keyAndValue[0].Contains("oad_offset_x1"))
  172. {
  173. NxrGlobal.offaxisOffset[0] = int.Parse(keyAndValue[1]);
  174. }
  175. else if (keyAndValue[0].Contains("oad_offset_x2"))
  176. {
  177. NxrGlobal.offaxisOffset[1] = int.Parse(keyAndValue[1]);
  178. }
  179. else if (keyAndValue[0].Contains("oad_offset_y1"))
  180. {
  181. NxrGlobal.offaxisOffset[2] = int.Parse(keyAndValue[1]);
  182. }
  183. else if (keyAndValue[0].Contains("oad_offset_y2"))
  184. {
  185. NxrGlobal.offaxisOffset[3] = int.Parse(keyAndValue[1]);
  186. }
  187. }
  188. }
  189. Debug.Log("Offaxis Offset : " + NxrGlobal.offaxisOffset[0] + "," + NxrGlobal.offaxisOffset[1] + "," + NxrGlobal.offaxisOffset[2] + "," + NxrGlobal.offaxisOffset[3]);
  190. }
  191. // Debug.LogError("AndroidDevice-Ptr=" +this.GetHashCode());
  192. return pointer;
  193. }
  194. public override void SetDisplayQuality(int level)
  195. {
  196. CallStaticMethod(nibiruVR, "setDisplayQualityForUnity", level);
  197. }
  198. public override bool GazeApi(GazeTag tag, string param)
  199. {
  200. bool show = false;
  201. CallStaticMethod<bool>(ref show, nibiruVR, "gazeApiForUnity", (int)tag, param);
  202. return show;
  203. }
  204. public override void SetSplitScreenModeEnabled(bool enabled)
  205. {
  206. }
  207. public override void AndroidLog(string msg)
  208. {
  209. CallStaticMethod(activityListener, "log", msg);
  210. }
  211. public override void SetSystemParameters(string key, string value)
  212. {
  213. if (nibiruVR != null)
  214. {
  215. CallStaticMethod(nibiruVR, "setSystemParameters", key, value);
  216. }
  217. }
  218. public override void OnApplicationPause(bool pause)
  219. {
  220. base.OnApplicationPause(pause);
  221. //CallObjectMethod(activityListener, "onPause", pause);
  222. if (!pause && androidActivity != null)
  223. {
  224. RunOnUIThread(androidActivity, new AndroidJavaRunnable(runOnUiThread));
  225. }
  226. }
  227. public override void AppQuit()
  228. {
  229. if (androidActivity != null)
  230. {
  231. RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
  232. {
  233. androidActivity.Call("finish");
  234. }));
  235. }
  236. }
  237. public override void OnApplicationQuit()
  238. {
  239. base.OnApplicationQuit();
  240. }
  241. void runOnUiThread()
  242. {
  243. //mActivity.getWindow().addFlags(128);
  244. //mActivity.getWindow().getDecorView().setSystemUiVisibility(5894);
  245. AndroidJavaObject androidWindow = androidActivity.Call<AndroidJavaObject>("getWindow");
  246. androidWindow.Call("addFlags", 128);
  247. AndroidJavaObject androidDecorView = androidWindow.Call<AndroidJavaObject>("getDecorView");
  248. androidDecorView.Call("setSystemUiVisibility", 5894);
  249. }
  250. public override void SetIsKeepScreenOn(bool keep)
  251. {
  252. if (androidActivity != null)
  253. {
  254. RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
  255. {
  256. SetScreenOn(keep);
  257. }));
  258. }
  259. }
  260. //if(enable) {
  261. // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  262. //} else {
  263. // getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  264. //}
  265. void SetScreenOn(bool enable)
  266. {
  267. if (enable)
  268. {
  269. AndroidJavaObject androidWindow = androidActivity.Call<AndroidJavaObject>("getWindow");
  270. androidWindow.Call("addFlags", 128);
  271. }
  272. else
  273. {
  274. AndroidJavaObject androidWindow = androidActivity.Call<AndroidJavaObject>("getWindow");
  275. androidWindow.Call("clearFlags", 128);
  276. }
  277. }
  278. private void SetApplicationState()
  279. {
  280. }
  281. /// <summary>
  282. /// * @param path
  283. /// * @param type23d 0=2d,1=3d
  284. /// * @param mode 0=normal,1=360,2=180,3=fullmode
  285. /// * @param decode 0=hardware,1=software
  286. /// </summary>
  287. public override void ShowVideoPlayer(string path, int type2D3D, int mode, int decode)
  288. {
  289. CallStaticMethod(nibiruVR, "showVideoPlayer", path, type2D3D, mode, decode);
  290. }
  291. //public override void DismissVideoPlayer()
  292. //{
  293. // CallStaticMethod(nibiruVR, "dismissVideoPlayer");
  294. //}
  295. void InitNibiruVRService()
  296. {
  297. if (nibiruVRService == null)
  298. {
  299. // getNibiruVRService
  300. CallStaticMethod<AndroidJavaObject>(ref nibiruVRService, nibiruVR, "getNibiruVRService", null);
  301. }
  302. }
  303. public override void SetIpd(float ipd)
  304. {
  305. InitNibiruVRService();
  306. if (nibiruVRService != null)
  307. {
  308. CallObjectMethod(nibiruVRService, "setIpd", ipd);
  309. }
  310. else
  311. {
  312. Debug.LogError("SetIpd failed, because nibiruVRService is null !!!!");
  313. }
  314. }
  315. public override void SetTimeWarpEnable(bool enabled)
  316. {
  317. InitNibiruVRService();
  318. if (nibiruVRService != null)
  319. {
  320. CallObjectMethod(nibiruVRService, "setTimeWarpEnable", enabled);
  321. }
  322. else
  323. {
  324. Debug.LogError("SetTimeWarpEnable failed, because nibiruVRService is null !!!!");
  325. }
  326. }
  327. /// <summary>
  328. /// Not currently supported.
  329. /// </summary>
  330. /// <returns></returns>
  331. //public override void SetEnableSyncFrame(bool enabled)
  332. //{
  333. // InitNibiruVRService();
  334. // if (nibiruVRService != null)
  335. // {
  336. // CallObjectMethod(nibiruVRService, "setEnableSyncFrame", enabled);
  337. // }
  338. // else
  339. // {
  340. // Debug.LogError("SetEnableSyncFrame failed, because nibiruVRService is null !!!!");
  341. // }
  342. //}
  343. //public override string GetSyncFrameUrl()
  344. //{
  345. // InitNibiruVRService();
  346. // if (nibiruVRService != null)
  347. // {
  348. // return nibiruVRService.Call<string>("getSyncFrameUrl");
  349. // }
  350. // else
  351. // {
  352. // Debug.LogError("GetSyncFrameUrl failed, because nibiruVRService is null !!!!");
  353. // }
  354. // return null;
  355. //}
  356. //public override bool IsSyncFrameEnabled()
  357. //{
  358. // InitNibiruVRService();
  359. // if (nibiruVRService != null)
  360. // {
  361. // return nibiruVRService.Call<bool>("isSyncFrameEnabled");
  362. // }
  363. // return false;
  364. //}
  365. //public override bool IsSyncFrameSupported()
  366. //{
  367. // InitNibiruVRService();
  368. // if (nibiruVRService != null)
  369. // {
  370. // return nibiruVRService.Call<bool>("isSyncFrameSupported");
  371. // }
  372. // return false;
  373. //}
  374. public override string GetStoragePath() { return GetAndroidStoragePath(); }
  375. public override void SetCameraNearFar(float near, float far)
  376. {
  377. CallStaticMethod(nibiruVR, "setProjectionNearFarForUnity", near, far);
  378. }
  379. public override void StopCapture()
  380. {
  381. CallStaticMethod(nibiruVR, "stopCaptureForUnity");
  382. }
  383. public override void OnDrawFrameCapture(int frameId)
  384. {
  385. CallStaticMethod(nibiruVR, "onDrawFrameForUnity", frameId);
  386. }
  387. public override NxrInstantNativeApi.NibiruDeviceType GetSixDofControllerPrimaryDeviceType()
  388. {
  389. string result = "3";
  390. CallStaticMethod<string>(ref result, nibiruVR, "getSystemProperty", "nxr.ctrl.primaryhand", "3");
  391. Debug.Log("primaryhand_" + result);
  392. int type = int.Parse(result);
  393. // 1 = left, 0 = right
  394. if (type == 0)
  395. {
  396. return NxrInstantNativeApi.NibiruDeviceType.RightController;
  397. } else if(type == 1)
  398. {
  399. return NxrInstantNativeApi.NibiruDeviceType.LeftController;
  400. }
  401. return NxrInstantNativeApi.NibiruDeviceType.None;
  402. }
  403. public override void SetSixDofControllerPrimaryDeviceType(NxrInstantNativeApi.NibiruDeviceType deviceType)
  404. {
  405. int type = -1;
  406. if(deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  407. {
  408. type = 1;
  409. } else if(deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  410. {
  411. type = 0;
  412. }
  413. if (type >=0) CallStaticMethod(nibiruVR, "setSystemProperty", "nxr.ctrl.primaryhand", "" + type);
  414. }
  415. public override int GetControllerTipState()
  416. {
  417. string result = "0";
  418. CallStaticMethod<string>(ref result, nibiruVR, "getSystemProperty", "nxr.ctrl.calib.tip", "0");
  419. int state = int.Parse(result);
  420. // 1-手柄校准提示框已弹出 , 0-未弹出
  421. return state;
  422. }
  423. public override void SetControllerTipState(int state)
  424. {
  425. CallStaticMethod(nibiruVR, "setSystemProperty", "nxr.ctrl.calib.tip", "" + state);
  426. }
  427. public override int GetEnableSystemDialog()
  428. {
  429. string result = "0";
  430. CallStaticMethod<string>(ref result, nibiruVR, "getSystemProperty", "persist.nsr.sysui.enable", "0");
  431. int enableSystemDialog = int.Parse(result);
  432. return enableSystemDialog;
  433. }
  434. public override void SetEnableSystemDialog(int enableSystemDialog)
  435. {
  436. CallStaticMethod(nibiruVR, "setSystemProperty", "persist.nsr.sysui.enable", "" +enableSystemDialog);
  437. }
  438. }
  439. }
  440. /// @endcond
  441. #endif