12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- namespace Nxr.Internal
- {
- /// <summary>
- ///
- /// </summary>
- public class NibiruService
- {
- private const string NibiruSDKClassName = "com.nibiru.lib.vr.NibiruVR";
- private const string ServiceClassName = "com.nibiru.service.NibiruService";
- protected AndroidJavaObject androidActivity;
- protected AndroidJavaClass nibiruSDKClass;
- protected AndroidJavaObject nibiruOsServiceObject;
- protected AndroidJavaObject nibiruSensorServiceObject;
- protected AndroidJavaObject nibiruVoiceServiceObject;
- protected AndroidJavaObject nibiruGestureServiceObject;
- protected AndroidJavaObject nibiruVRServiceObject;
- protected AndroidJavaObject nibiruCameraServiceObject;
- protected AndroidJavaObject nibiruMarkerServiceObject;
- protected CameraPreviewHelper cameraPreviewHelper;
- protected AndroidJavaObject cameraDeviceObject;
- protected AndroidJavaObject audioManager;
- public int HMDCameraId;
- public int ControllerCameraId;
- private bool isCameraPreviewing = false;
- private string systemDevice = "";
- public void Init()
- {
- #if UNITY_ANDROID
- try
- {
- using (AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- androidActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
- audioManager = androidActivity.Call<AndroidJavaObject>("getSystemService",
- new AndroidJavaObject("java.lang.String", "audio"));
- }
- }
- catch (AndroidJavaException e)
- {
- androidActivity = null;
- Debug.LogError("Exception while connecting to the Activity: " + e);
- return;
- }
- nibiruSDKClass = BaseAndroidDevice.GetClass(NibiruSDKClassName);
- //
- // systemDevice = nibiruSDKClass.CallStatic<string>("getSystemProperty", "ro.product.device", "");
- nibiruOsServiceObject = nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruOSService", androidActivity);
- nibiruSensorServiceObject =
- nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruSensorService", androidActivity);
- nibiruVoiceServiceObject =
- nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruVoiceService", androidActivity);
- nibiruGestureServiceObject =
- nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruGestureService", androidActivity);
- nibiruVRServiceObject = nibiruSDKClass.CallStatic<AndroidJavaObject>("getUsingNibiruVRServiceGL");
- nibiruCameraServiceObject =
- nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruCameraService", androidActivity);
- nibiruMarkerServiceObject =
- nibiruSDKClass.CallStatic<AndroidJavaObject>("getNibiruMarkerService", androidActivity);
- HMDCameraId = nibiruCameraServiceObject.Call<int>("getHMDCameraId");
- ControllerCameraId = nibiruCameraServiceObject.Call<int>("getControllerCameraId");
- UpdateVoiceLanguage();
- // Debug.Log("nibiruOsServiceObject is "+ nibiruOsServiceObject.Call<AndroidJavaObject>("getClass").Call<string>("getName"));
- // Debug.Log("nibiruSensorServiceObject is " + nibiruSensorServiceObject.Call<AndroidJavaObject>("getClass").Call<string>("getName"));
- NibiruTask.NibiruTaskApi.Init();
- IsCaptureEnabled = -1;
- // 默认触发请求权限:
- RequsetPermission(new string[]
- {
- NxrGlobal.Permission.CAMERA,
- NxrGlobal.Permission.WRITE_EXTERNAL_STORAGE,
- NxrGlobal.Permission.READ_EXTERNAL_STORAGE,
- NxrGlobal.Permission.ACCESS_NETWORK_STATE,
- NxrGlobal.Permission.ACCESS_COARSE_LOCATION,
- NxrGlobal.Permission.BLUETOOTH,
- NxrGlobal.Permission.BLUETOOTH_ADMIN,
- NxrGlobal.Permission.INTERNET,
- NxrGlobal.Permission.GET_TASKS,
- });
- #endif
- }
- public static int NKEY_SYS_HANDLE = 0;
- public static int NKEY_APP_HANDLE = 1;
- /// <summary>
- /// Handle N key event
- /// 0=system handle
- /// 1=app handle
- /// </summary>
- /// <param name="mode"></param>
- public void RegHandleNKey(int mode)
- {
- if (nibiruVRServiceObject != null)
- {
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("regHandleNKey", mode); }));
- }
- else
- {
- Debug.LogError("regHandleNKey failed, nibiruVRServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Enable fps statistics
- /// </summary>
- /// <param name="isEnabled"></param>
- public void SetEnableFPS(bool isEnabled)
- {
- if (nibiruVRServiceObject != null)
- {
- nibiruVRServiceObject.Call("setEnableFPS", isEnabled);
- }
- else
- {
- Debug.LogError("SetEnableFPS failed, nibiruVRServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Get fps : 0=app,1=dtr
- /// </summary>
- /// <returns></returns>
- public float[] GetFPS()
- {
- if (nibiruVRServiceObject != null)
- {
- return nibiruVRServiceObject.Call<float[]>("getFPS");
- }
- else
- {
- Debug.LogError("SetEnableFPS failed, nibiruVRServiceObject is null !!!");
- }
- return new float[] {-1, -1};
- }
- /// <summary>
- /// Register virtual mouse service
- /// </summary>
- /// <param name="serviceStatus"></param>
- public void RegisterVirtualMouseService(OnVirtualMouseServiceStatus serviceStatus)
- {
- if (nibiruOsServiceObject != null)
- {
- nibiruOsServiceObject.Call("registerVirtualMouseManagerService",
- new NibiruVirtualMouseServiceListener(serviceStatus));
- }
- else
- {
- Debug.LogError("RegisterVirtualMouseService failed, nibiruOsServiceObject is null !!!");
- }
- }
- /// <summary>
- /// UnRegister virtual mouse service
- /// </summary>
- public void UnRegisterVirtualMouseService()
- {
- if (nibiruOsServiceObject != null)
- {
- nibiruOsServiceObject.Call("unRegisterVirtaulMouseManagerService");
- }
- else
- {
- Debug.LogError("UnRegisterVirtualMouseService failed, nibiruOsServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Set enable virtual mouse
- /// </summary>
- /// <param name="enabled"></param>
- /// <returns></returns>
- public bool SetEnableVirtualMouse(bool enabled)
- {
- if (nibiruOsServiceObject != null)
- {
- return nibiruOsServiceObject.Call<bool>("setEnableVirtualMouse", enabled);
- }
- else
- {
- Debug.LogError("SetEnableVirtualMouse failed, nibiruOsServiceObject is null !!!");
- return false;
- }
- }
- public CameraPreviewHelper InitCameraPreviewHelper()
- {
- cameraPreviewHelper = new CameraPreviewHelper();
- return cameraPreviewHelper;
- }
- public CameraPreviewHelper CameraPreviewHelper
- {
- get { return cameraPreviewHelper; }
- }
- /// <summary>
- /// Get camera status async
- /// </summary>
- public void GetCameraStatus()
- {
- int cameraId = HMDCameraId;
- if (nibiruCameraServiceObject != null)
- {
- nibiruCameraServiceObject.Call("getCameraStatus", cameraId, new CameraStatusCallback());
- }
- else
- {
- Debug.LogError("GetCameraStatus failed, because nibiruCameraServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Open camera
- /// </summary>
- /// <returns> NibiruCameraDevice</returns>
- private AndroidJavaObject OpenCamera()
- {
- int cameraId = HMDCameraId;
- if (nibiruCameraServiceObject != null && cameraDeviceObject == null)
- {
- cameraDeviceObject = nibiruCameraServiceObject.Call<AndroidJavaObject>("openCamera", cameraId);
- return cameraDeviceObject;
- }
- else if (cameraDeviceObject != null)
- {
- return cameraDeviceObject;
- }
- else
- {
- Debug.LogError("OpenCamera failed, because nibiruCameraServiceObject is null !!!");
- return null;
- }
- }
- /// <summary>
- /// Get current cameraId
- /// </summary>
- /// <returns></returns>
- public CAMERA_ID GetCurrentCameraId()
- {
- return (CAMERA_ID) HMDCameraId;
- }
- /// <summary>
- /// Start camera preView
- /// </summary>
- public void StartCameraPreView()
- {
- StartCameraPreView(false);
- }
- /// <summary>
- /// Start camera preView
- /// </summary>
- /// <param name="triggerFocus">trigger focus</param>
- public void StartCameraPreView(bool triggerFocus)
- {
- OpenCamera();
- AndroidJavaObject surfaceTextureObject = cameraPreviewHelper.GetSurfaceTexture();
- cameraDeviceObject.Call<bool>("startPreviewWithBestSize", surfaceTextureObject);
- if (triggerFocus)
- {
- DoCameraAutoFocus();
- }
- isCameraPreviewing = true;
- }
- /// <summary>
- /// Stop camera preView
- /// </summary>
- public void StopCamereaPreView()
- {
- if (nibiruCameraServiceObject != null)
- {
- isCameraPreviewing = false;
- nibiruCameraServiceObject.Call("stopPreview");
- cameraDeviceObject = null;
- }
- else
- {
- Debug.LogError("StopCamereaPreView failed, because nibiruCameraServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Determine whether the camera is in preview.
- /// </summary>
- /// <returns></returns>
- public bool IsCameraPreviewing()
- {
- return isCameraPreviewing;
- }
- public void SetCameraPreviewing(bool enabled)
- {
- isCameraPreviewing = true;
- }
- public void DoCameraAutoFocus()
- {
- if (cameraDeviceObject != null)
- {
- cameraDeviceObject.Call("doAutoFocus");
- }
- else
- {
- Debug.LogError("DoCameraAutoFocus failed, because cameraDeviceObject is null !!!");
- }
- }
- public void EnableVoiceService(bool enabled)
- {
- if (nibiruVoiceServiceObject != null)
- {
- nibiruVoiceServiceObject.Call("setEnableVoice", enabled);
- }
- else
- {
- Debug.LogError("EnableVoiceService failed, because nibiruVoiceServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Start voice recording
- /// </summary>
- public void StartVoiceRecording()
- {
- if (nibiruVoiceServiceObject != null)
- {
- nibiruVoiceServiceObject.Call("startRecording");
- }
- else
- {
- Debug.LogError("StartVoiceRecording failed, because nibiruVoiceServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Stop voice recording
- /// </summary>
- public void StopVoiceRecording()
- {
- if (nibiruVoiceServiceObject != null)
- {
- nibiruVoiceServiceObject.Call("stopRecording");
- }
- else
- {
- Debug.LogError("StopVoiceRecording failed, because nibiruVoiceServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Cancel voice recognizer
- /// </summary>
- public void CancelVoiceRecognizer()
- {
- if (nibiruVoiceServiceObject != null)
- {
- nibiruVoiceServiceObject.Call("cancelRecognizer");
- }
- else
- {
- Debug.LogError("CancelVoiceRecognizer failed, because nibiruVoiceServiceObject is null !!!");
- }
- }
- public bool IsSupportVoice()
- {
- if (nibiruVoiceServiceObject != null)
- {
- return nibiruVoiceServiceObject.Call<bool>("isMicrophoneVoice");
- }
- else
- {
- Debug.LogError("IsSupportVoice failed, because nibiruVoiceServiceObject is null !!!");
- }
- return false;
- }
- public bool IsSupport6DOF()
- {
- if (nibiruVRServiceObject != null)
- {
- return nibiruVRServiceObject.Call<bool>("isSupport6Dof");
- }
- else
- {
- Debug.LogError("IsSupport6DOF failed, because nibiruVRServiceObject is null !!!");
- }
- return false;
- }
- public bool IsSupportGesture()
- {
- if (nibiruGestureServiceObject != null)
- {
- return nibiruGestureServiceObject.Call<bool>("isCameraGesture");
- }
- else
- {
- Debug.LogError("isSupportGesture failed, because nibiruGestureServiceObject is null !!!");
- }
- return false;
- }
- public void UpdateVoiceLanguage()
- {
- if (nibiruVoiceServiceObject != null)
- {
- nibiruVoiceServiceObject.Call("setVoicePID", (int) NxrGlobal.voiceLanguage);
- }
- else
- {
- Debug.LogError("UpdateVoiceLanguage failed, because nibiruVoiceServiceObject is null !!!");
- }
- }
- /// <summary>
- /// Control camera status of gesture service:false-Turn off camera occupation,true-Turn on camera occupation
- /// </summary>
- /// <param name="enabled"></param>
- public void EnableGestureService(bool enabled)
- {
- if (nibiruGestureServiceObject != null)
- {
- nibiruGestureServiceObject.Call("setEnableGesture", enabled);
- }
- else
- {
- Debug.LogError("EnableGestureService failed, because nibiruGestureServiceObject is null !!!");
- }
- }
- public bool IsCameraGesture()
- {
- if (nibiruGestureServiceObject != null)
- {
- return nibiruGestureServiceObject.Call<bool>("isCameraGesture");
- }
- return false;
- }
- public delegate void OnSensorDataChanged(NibiruSensorEvent sensorEvent);
- /// <summary>
- /// The callback when sensor data changes.
- /// </summary>
- public static OnSensorDataChanged OnSensorDataChangedHandler;
- class NibiruSensorDataListenerCallback : AndroidJavaProxy
- {
- public NibiruSensorDataListenerCallback() : base(
- "com.nibiru.service.NibiruSensorService$INibiruSensorDataListener")
- {
- }
- public void onSensorDataChanged(AndroidJavaObject sensorEventObject)
- {
- float x = sensorEventObject.Get<float>("x");
- float y = sensorEventObject.Get<float>("y");
- float z = sensorEventObject.Get<float>("z");
- long timestamp = sensorEventObject.Get<long>("timestamp");
- AndroidJavaObject locationObject = sensorEventObject.Get<AndroidJavaObject>("sensorLocation");
- AndroidJavaObject typeObject = sensorEventObject.Get<AndroidJavaObject>("sensorType");
- SENSOR_LOCATION sensorLocation = (SENSOR_LOCATION) locationObject.Call<int>("ordinal");
- SENSOR_TYPE sensorType = (SENSOR_TYPE) typeObject.Call<int>("ordinal");
- NibiruSensorEvent sensorEvent = new NibiruSensorEvent(x, y, z, timestamp, sensorType, sensorLocation);
- // sensorEvent.printLog();
- // 用Loom的方法在Unity主线程中调用Text组件
- Loom.QueueOnMainThread((param) =>
- {
- if (OnSensorDataChangedHandler != null)
- {
- OnSensorDataChangedHandler((NibiruSensorEvent) param);
- }
- }, sensorEvent);
- }
- }
- private NibiruSensorDataListenerCallback nibiruSensorDataListenerCallback;
- public void RegisterSensorListener(SENSOR_TYPE type, SENSOR_LOCATION location)
- {
- if (nibiruSensorServiceObject != null)
- {
- if (nibiruSensorDataListenerCallback == null)
- {
- nibiruSensorDataListenerCallback = new NibiruSensorDataListenerCallback();
- }
- // UI线程执行
- RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
- {
- AndroidJavaClass locationClass =
- BaseAndroidDevice.GetClass("com.nibiru.service.NibiruSensorService$SENSOR_LOCATION");
- AndroidJavaObject locationObj =
- locationClass.CallStatic<AndroidJavaObject>("valueOf", location.ToString());
- AndroidJavaClass typeClass =
- BaseAndroidDevice.GetClass("com.nibiru.service.NibiruSensorService$SENSOR_TYPE");
- AndroidJavaObject typeObj = typeClass.CallStatic<AndroidJavaObject>("valueOf", type.ToString());
- nibiruSensorServiceObject.Call<bool>("registerSensorListener", typeObj, locationObj,
- nibiruSensorDataListenerCallback);
- Debug.Log("registerSensorListener=" + type.ToString() + "," + location.ToString());
- }
- ));
- }
- else
- {
- Debug.LogError("RegisterControllerSensor failed, nibiruSensorServiceObject is null !");
- }
- }
- public void UnRegisterSensorListener()
- {
- if (nibiruSensorServiceObject != null)
- {
- // UI线程执行
- RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
- {
- nibiruSensorServiceObject.Call("unregisterSensorListenerAll");
- }
- ));
- }
- else
- {
- Debug.LogError("UnRegisterSensorListener failed, nibiruSensorServiceObject is null !");
- }
- }
- //4.1 获取屏幕亮度值:
- /// <summary>
- /// Get system's brightness value
- /// </summary>
- /// <returns></returns>
- public int GetBrightnessValue()
- {
- int BrightnessValue = 0;
- #if UNITY_ANDROID
- BaseAndroidDevice.CallObjectMethod<int>(ref BrightnessValue, nibiruOsServiceObject, "getBrightnessValue");
- #endif
- return BrightnessValue;
- }
- //4.2 调节屏幕亮度:
- /// <summary>
- /// Set system's brightness value
- /// </summary>
- /// <returns></returns>
- public void SetBrightnessValue(int value)
- {
- if (nibiruOsServiceObject == null) return;
- #if UNITY_ANDROID
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() =>
- {
- BaseAndroidDevice.CallObjectMethod(nibiruOsServiceObject, "setBrightnessValue", value, 200.01f);
- }));
- #endif
- }
- //4.3 获取当前2D/3D显示模式:
- /// <summary>
- /// Get display mode 2d/3d
- /// </summary>
- /// <returns></returns>
- public DISPLAY_MODE GetDisplayMode()
- {
- if (nibiruOsServiceObject == null) return DISPLAY_MODE.MODE_2D;
- AndroidJavaObject androidObject = nibiruOsServiceObject.Call<AndroidJavaObject>("getDisplayMode");
- int mode = androidObject.Call<int>("ordinal");
- return (DISPLAY_MODE) mode;
- }
- //4.4 切换2D/3D显示模式:
- /// <summary>
- /// Set display mode 2d/3d
- /// </summary>
- /// <param name="displayMode"></param>
- public void SetDisplayMode(DISPLAY_MODE displayMode)
- {
- if (nibiruOsServiceObject != null)
- {
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() =>
- {
- nibiruOsServiceObject.Call("setDisplayMode", (int) displayMode);
- }));
- }
- }
- // 渠道ID
- /// <summary>
- /// Get system's channel code
- /// </summary>
- /// <returns></returns>
- public string GetChannelCode()
- {
- if (nibiruOsServiceObject == null) return "NULL";
- return nibiruOsServiceObject.Call<string>("getChannelCode");
- }
- // 型号
- /// <summary>
- /// Get device's model
- /// </summary>
- /// <returns></returns>
- public string GetModel()
- {
- if (nibiruOsServiceObject == null) return "NULL";
- return nibiruOsServiceObject.Call<string>("getModel");
- }
- // 系统OS版本
- /// <summary>
- /// Get system's os version
- /// </summary>
- /// <returns></returns>
- public string GetOSVersion()
- {
- if (nibiruOsServiceObject == null) return "NULL";
- return nibiruOsServiceObject.Call<string>("getOSVersion");
- }
- // 系统OS版本号
- /// <summary>
- /// Get system's service version
- /// </summary>
- /// <returns></returns>
- public int GetOSVersionCode()
- {
- if (nibiruOsServiceObject == null) return -1;
- return nibiruOsServiceObject.Call<int>("getOSVersionCode");
- }
- // 系统服务版本
- /// <summary>
- /// Get system's service version code
- /// </summary>
- /// <returns></returns>
- public string GetServiceVersionCode()
- {
- if (nibiruOsServiceObject == null) return "NULL";
- return nibiruOsServiceObject.Call<string>("getServiceVersionCode");
- }
- // 获取厂家软件版本:(对应驱动板软件版本号)
- /// <summary>
- /// Get system's vendor SW version
- /// </summary>
- /// <returns></returns>
- public string GetVendorSWVersion()
- {
- if (nibiruOsServiceObject == null) return "NULL";
- return nibiruOsServiceObject.Call<string>("getVendorSWVersion");
- }
- // 控制touchpad是否显示 value为true表示显示,false表示不显示
- /// <summary>
- /// Control whether touchpad is displayed. true-display false-not display
- /// </summary>
- /// <param name="isEnable"></param>
- public void SetEnableTouchCursor(bool isEnable)
- {
- RunOnUIThread(androidActivity, new AndroidJavaRunnable(() =>
- {
- if (nibiruOsServiceObject != null)
- {
- nibiruOsServiceObject.Call("setEnableTouchCursor", isEnable);
- }
- }));
- }
- /// <summary>
- /// Get the value of the distance sensor.
- /// </summary>
- /// <returns></returns>
- public int GetProximityValue()
- {
- if (nibiruSensorServiceObject == null) return -1;
- return nibiruSensorServiceObject.Call<int>("getProximityValue");
- }
- /// <summary>
- /// Get the value of light perception.
- /// </summary>
- /// <returns></returns>
- public int GetLightValue()
- {
- if (nibiruSensorServiceObject == null) return -1;
- return nibiruSensorServiceObject.Call<int>("getLightValue");
- }
- // UI线程中运行
- public void RunOnUIThread(AndroidJavaObject activityObj, AndroidJavaRunnable r)
- {
- activityObj.Call("runOnUiThread", r);
- }
- public delegate void CameraIdle();
- public delegate void CameraBusy();
- /// <summary>
- /// The callback when Camera is idle.
- /// </summary>
- public static CameraIdle OnCameraIdle;
- /// <summary>
- /// The callback when Camera is busy.
- /// </summary>
- public static CameraBusy OnCameraBusy;
- public delegate void OnRecorderSuccess();
- public delegate void OnRecorderFailed();
- public static OnRecorderSuccess OnRecorderSuccessHandler;
- public static OnRecorderFailed OnRecorderFailedHandler;
- class CameraStatusCallback : AndroidJavaProxy
- {
- public CameraStatusCallback() : base("com.nibiru.lib.vr.listener.NVRCameraStatusListener")
- {
- }
- public void cameraBusy()
- {
- // 从Android UI线程回调过来的,加入到Unity主线程处理
- // NxrViewer.Instance.TriggerCameraStatus(1);
- Loom.QueueOnMainThread((param) =>
- {
- if (OnCameraBusy != null)
- {
- OnCameraBusy();
- }
- }, 1);
- Debug.Log("cameraBusy");
- }
- public void cameraIdle()
- {
- // 从Android UI线程回调过来的,加入到Unity主线程处理
- // NxrViewer.Instance.TriggerCameraStatus(0);
- Loom.QueueOnMainThread((param) =>
- {
- if (OnCameraIdle != null)
- {
- OnCameraIdle();
- }
- }, 0);
- Debug.Log("cameraIdle");
- }
- }
- class CaptureCallback : AndroidJavaProxy
- {
- public CaptureCallback() : base("com.nibiru.lib.vr.listener.NVRVideoCaptureListener")
- {
- }
- public void onSuccess()
- {
- // 从Android UI线程回调过来的,加入到Unity主线程处理
- // NxrViewer.Instance.TriggerCaptureStatus(1);
- Loom.QueueOnMainThread((param) =>
- {
- if (OnRecorderSuccessHandler != null)
- {
- OnRecorderSuccessHandler();
- }
- }, 1);
- }
- public void onFailed()
- {
- // 从Android UI线程回调过来的,加入到Unity主线程处理
- // NxrViewer.Instance.TriggerCaptureStatus(0);
- Loom.QueueOnMainThread((param) =>
- {
- if (OnRecorderFailedHandler != null)
- {
- OnRecorderFailedHandler();
- }
- }, 0);
- }
- }
- public int IsCaptureEnabled { set; get; }
- public static int BIT_RATE = 4000000;
- /// <summary>
- /// Start capture
- /// </summary>
- /// <param name="path"></param>
- public void StartCapture(string path)
- {
- StartCapture(path, -1);
- }
- /// <summary>
- /// Start capture
- /// </summary>
- /// <param name="path"></param>
- /// <param name="seconds"></param>
- public void StartCapture(string path, int seconds)
- {
- StartCapture(path, BIT_RATE, seconds);
- }
- private static int videoSize = (int) VIDEO_SIZE.V720P;
- // private static int captureCameraId = (int)CAMERA_ID.FRONT;
- /// <summary>
- /// Start capture
- /// </summary>
- /// <param name="path"></param>
- /// <param name="bitRate"></param>
- /// <param name="seconds"></param>
- public void StartCapture(string path, int bitRate, int seconds)
- {
- IsCaptureEnabled = 1;
- nibiruSDKClass.CallStatic("startCaptureForUnity", new CaptureCallback(), path, bitRate, seconds, videoSize,
- HMDCameraId);
- }
- public static void SetCaptureVideoSize(VIDEO_SIZE video_Size)
- {
- videoSize = (int) video_Size;
- }
- /// <summary>
- /// Stop capture
- /// </summary>
- public void StopCapture()
- {
- nibiruSDKClass.CallStatic("stopCaptureForUnity");
- IsCaptureEnabled = 0;
- }
- public bool CaptureDrawFrame(int textureId, int frameId)
- {
- if (IsCaptureEnabled <= -3)
- {
- return false;
- }
- else if (IsCaptureEnabled <= 0 && IsCaptureEnabled >= -2)
- {
- // 在stop后,多执行3次,用于内部处理stop的逻辑。
- IsCaptureEnabled--;
- }
- return nibiruSDKClass.CallStatic<bool>("onDrawFrameForUnity", textureId, frameId);
- }
- private const int STREAM_VOICE_CALL = 0;
- private const int STREAM_SYSTEM = 1;
- private const int STREAM_RING = 2;
- private const int STREAM_MUSIC = 3;
- private const int STREAM_ALARM = 4;
- private const int STREAM_NOTIFICATION = 5;
- private const string currentVolume = "getStreamVolume"; //当前音量
- private const string maxVolume = "getStreamMaxVolume"; //最大音量
- public int GetVolumeValue()
- {
- if (audioManager == null) return 0;
- return audioManager.Call<int>(currentVolume, STREAM_MUSIC);
- }
- public int GetMaxVolume()
- {
- if (audioManager == null) return 1;
- return audioManager.Call<int>(maxVolume, STREAM_MUSIC);
- }
- public void EnabledMarkerAutoFocus(bool enabled)
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- }
- else if (isMarkerRecognizeRunning)
- {
- nibiruMarkerServiceObject.Call(enabled ? "doAutoFocus" : "stopAutoFocus");
- }
- }
- /// <summary>
- /// Set marker recognize cameraId
- /// </summary>
- /// <param name="cameraID"></param>
- private void SetMarkerRecognizeCameraId(int cameraID)
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- }
- else
- {
- nibiruMarkerServiceObject.Call("setCameraId", cameraID);
- }
- }
- private bool isMarkerRecognizeRunning;
- public bool IsMarkerRecognizeRunning
- {
- get { return isMarkerRecognizeRunning; }
- set { isMarkerRecognizeRunning = value; }
- }
- /// <summary>
- /// Start marker recognize
- /// </summary>
- public void StartMarkerRecognize()
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- }
- else if (!isMarkerRecognizeRunning)
- {
- // 默认使用前置相机
- SetMarkerRecognizeCameraId(HMDCameraId);
- // 焦距,具体不同机器可以需要微调 16 , 640 * 480
- nibiruMarkerServiceObject.Call("setCameraZoom", NxrGlobal.GetMarkerCameraZoom());
- nibiruMarkerServiceObject.Call("setPreviewSize", 640, 480);
- nibiruMarkerServiceObject.Call("startMarkerRecognize");
- isMarkerRecognizeRunning = true;
- }
- }
- /// <summary>
- /// Stop marker recognize
- /// </summary>
- public void StopMarkerRecognize()
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- }
- else if (isMarkerRecognizeRunning)
- {
- nibiruMarkerServiceObject.Call("stopMarkerRecognize");
- isMarkerRecognizeRunning = false;
- }
- }
- /// <summary>
- /// Get the ViewMatrix of Marker.
- /// </summary>
- /// <returns></returns>
- public float[] GetMarkerViewMatrix()
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- return null;
- }
- else
- {
- float[] result = nibiruMarkerServiceObject.Call<float[]>("getMarkerViewMatrix");
- if (result == null || result.Length == 0) return null;
- // 全是0
- if (IsAllZero(result)) return null;
- return result;
- }
- }
- public static bool IsAllZero(float[] array)
- {
- for (int i = 0, l = array.Length; i < l; i++)
- {
- if (array[i] != 0) return false;
- }
- return true;
- }
- public float[] GetMarkerViewMatrix(int eyeType)
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- return null;
- }
- else
- {
- float[] result = nibiruMarkerServiceObject.Call<float[]>("getMarkerViewMatrix", eyeType);
- if (result == null || result.Length == 0) return null;
- // 全是0
- if (IsAllZero(result)) return null;
- return result;
- }
- }
- public float[] GetMarkerProjectionMatrix()
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("nibiruMarkerServiceObject is null");
- return null;
- }
- else
- {
- float[] projArr = nibiruMarkerServiceObject.Call<float[]>("getProjection");
- if (projArr == null || projArr.Length == 0)
- return null;
- return projArr;
- }
- }
- public string GetMarkerDetectStatus()
- {
- if (nibiruMarkerServiceObject == null)
- {
- Debug.LogError("GetMarkerDetectStatus failed, nibiruMarkerServiceObject is null");
- return "-1";
- }
- string res = nibiruMarkerServiceObject.Call<string>("getParameters", "p_detect_status");
- return res == null ? "-1" : res;
- }
- public delegate void OnVirtualMouseServiceStatus(bool succ);
- public class NibiruVirtualMouseServiceListener : AndroidJavaProxy
- {
- OnVirtualMouseServiceStatus _OnVirtualMouseServiceStatus;
- public NibiruVirtualMouseServiceListener(OnVirtualMouseServiceStatus onVirtualMouseServiceStatus) : base(
- "com.nibiru.service.NibiruVirtualMouseManager$VirtualMouseServiceListener")
- {
- _OnVirtualMouseServiceStatus = onVirtualMouseServiceStatus;
- }
- public void onServiceRegisterResult(bool succ)
- {
- if (_OnVirtualMouseServiceStatus != null)
- {
- _OnVirtualMouseServiceStatus(succ);
- }
- }
- }
- public void PauseGestureService()
- {
- if (nibiruGestureServiceObject != null)
- {
- nibiruGestureServiceObject.Call("onPause");
- }
- else
- {
- Debug.LogError("onPause failed, because nibiruGestureServiceObject is null !!!");
- }
- }
- public void ResumeGestureService()
- {
- if (nibiruGestureServiceObject != null)
- {
- nibiruGestureServiceObject.Call("onResume");
- }
- else
- {
- Debug.LogError("onResume failed, because nibiruGestureServiceObject is null !!!");
- }
- }
- private AndroidJavaObject javaArrayFromCS(string[] values)
- {
- AndroidJavaClass arrayClass = new AndroidJavaClass("java.lang.reflect.Array");
- AndroidJavaObject arrayObject = arrayClass.CallStatic<AndroidJavaObject>("newInstance",
- new AndroidJavaClass("java.lang.String"), values.Count());
- for (int i = 0; i < values.Count(); ++i)
- {
- arrayClass.CallStatic("set", arrayObject, i, new AndroidJavaObject("java.lang.String", values[i]));
- }
- return arrayObject;
- }
- /// <summary>
- /// Request permission
- /// </summary>
- /// <param name="names">NxrGlobal.Permission</param>
- public void RequsetPermission(string[] names)
- {
- if (nibiruOsServiceObject != null)
- {
- nibiruOsServiceObject.Call("requestPermission", javaArrayFromCS(names));
- }
- }
- /// <summary>
- /// Get QCOM of product device.
- /// </summary>
- /// <returns></returns>
- public QCOMProductDevice GetQCOMProductDevice()
- {
- if ("msm8996".Equals(systemDevice))
- {
- return QCOMProductDevice.QCOM_820;
- }
- else if ("msm8998".Equals(systemDevice))
- {
- return QCOMProductDevice.QCOM_835;
- }
- else if ("sdm710".Equals(systemDevice))
- {
- return QCOMProductDevice.QCOM_XR1;
- }
- else if ("sdm845".Equals(systemDevice))
- {
- return QCOMProductDevice.QCOM_845;
- }
- return QCOMProductDevice.QCOM_UNKNOW;
- }
- public void LockToCur()
- {
- if (nibiruVRServiceObject != null)
- {
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("lockTracker"); }));
- }
- else
- {
- Debug.LogError("LockToCur failed, nibiruVRServiceObject is null !!!");
- }
- }
- public void LockToFront()
- {
- if (nibiruVRServiceObject != null)
- {
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("lockTrackerToFront"); }));
- }
- else
- {
- Debug.LogError("LockToFront failed, nibiruVRServiceObject is null !!!");
- }
- }
- public void UnLock()
- {
- if (nibiruVRServiceObject != null)
- {
- RunOnUIThread(androidActivity,
- new AndroidJavaRunnable(() => { nibiruVRServiceObject.Call("unlockTracker"); }));
- }
- else
- {
- Debug.LogError("UnLock failed, nibiruVRServiceObject is null !!!");
- }
- }
- }
- }
|