Android sensor API: proximity

Source: Internet
Author: User

/*
* Copyright (c) 2011 the rexsee open source project
*
* Licensed under the rexsee license, version 1.0 (the "License ");
* You may not use this file before t in compliance with the license.
* You may obtain a copy of the license
*
* Http://www.rexsee.com/CN/legal/license.html
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the license is distributed on an "as is" basis,
* Without warranties or conditions of any kind, either express or implied.
* See the license for the specific language governing permissions and
* Limitations under the license.
*/

Package rexsee. sensor;

Import rexsee. Core. browser. javascriptinte ** CE;
Import rexsee. Core. browser. rexseebrowser;
Import Android. content. context;
Import Android. Hardware. sensor;
Import Android. Hardware. sensorevent;
Import Android. Hardware. sensoreventlistener;
Import Android. Hardware. sensormanager;

Public class rexseesensorproximity implements extends criptinte ** ce {

Private Static final string inte ** ce_name = "proximity ";
@ Override
Public String getinte ** CENAME (){
Return mbrowser. application. Resources. prefix + inte ** ce_name;
}
@ Override
Public javascriptinte ** ce getinheritinte ** Ce (rexseebrowser childbrowser ){
Return this;
}
@ Override
Public javascriptinte ** ce getnewinte ** Ce (rexseebrowser childbroce ){
Return new rexseesensorproximity (childbroity );
}

Public static final string event_onproximitychanged = "onproximitychanged ";

Private Final context mcontext;
Private Final rexseebrowser mbrowser;
Private Final sensormanager msensormanager;
Private Final sensoreventlistener msensorlistener;
Private Final sensor msensor;

Private int mrate = sensormanager. sensor_delay_normal;
Private int mcycle = 100; // milliseconds
Private int meventcycle = 100; // milliseconds
Private float maccuracy = 0;

Private long lastupdate =-1;
Private long lastevent =-1;

Private float value =-999f;

Public rexseesensorproximity (rexseebrowser browser ){
Mcontext = browser. getcontext ();
Mbrowser = browser;
Browser. eventlist. Add (event_onproximitychanged );

Msensormanager = (sensormanager) mcontext. getsystemservice (context. sensor_service );

Msensor = msensormanager. getdefasensensor (sensor. type_proximity );

Msensorlistener = new sensoreventlistener (){
@ Override
Public void onaccuracychanged (sensor, int accuracy ){
}
@ Override
Public void onsensorchanged (sensorevent event ){
If (event. sensor. GetType ()! = Sensor. type_proximity) return;
Long curtime = system. currenttimemillis ();
If (lastupdate =-1 | (curtime-lastupdate)> mcycle ){
Lastupdate = curtime;
Float lastvalue = value;
Value = event. Values [sensormanager. data_x];
If (lastevent =-1 | (curtime-lastevent)> meventcycle ){
If (math. Abs (value-lastvalue)> maccuracy ){
Lastevent = curtime;
Mbrowser. eventlist. Run (event_onproximitychanged );
}
}
}
}
};

}

Public String getlastknownvalue (){
Return (value =-999 )? "Null": String. valueof (value );
}

Public void setrate (string rate ){
Mrate = sensorrate. getint (rate );
}
Public String getrate (){
Return sensorrate. getstring (mrate );
}
Public void setcycle (INT milliseconds ){
Mcycle = milliseconds;
}
Public int getcycle (){
Return mcycle;
}
Public void seteventcycle (INT milliseconds ){
Meventcycle = milliseconds;
}
Public int geteventcycle (){
Return meventcycle;
}
Public void setaccuracy (float value ){
Maccuracy = math. Abs (value );
}
Public float getaccuracy (){
Return maccuracy;
}

Public Boolean isready (){
Return (msensor = NULL )? False: true;
}
Public void start (){
If (isready ()){
Msensormanager. registerlistener (msensorlistener, msensor, mrate );
} Else {
Mbrowser. Exception (getinte ** CENAME (), "proximity sensor is not found .");
}
}
Public void stop (){
If (isready ()){
Msensormanager. unregisterlistener (msensorlistener );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.