Java tutorial
/* This file was modified from or inspired by Apache Cordova. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 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 com.polyvi.xface.extension; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import com.polyvi.xface.util.XLog; public class XCompassExt extends XExtension implements SensorEventListener { /** XCompassExt ??js??? */ private static final String COMMAND_GET_HEADING = "getHeading"; private static final int STOPPED = 0; private static final int STARTING = 1; private static final int RUNNING = 2; private static final int ERROR_FAILED_TO_START = 3; private static final long TIMEOUT = 30000; //sensor?TIMEOUTsensor?? private int mStatus; //sensor? private float mLatestHeading; //?heading private long mTimeStamp; //?heading private long mLastAccessTime; //?sensor private SensorManager mSensorManager = null; //Compass Sensor private Sensor mSensor = null; //Compass Sensor public XCompassExt() { mLatestHeading = 0; mTimeStamp = 0; mStatus = STOPPED; mLastAccessTime = 0; } @Override public XExtensionResult exec(String action, JSONArray args, XCallbackContext callbackCtx) { XExtensionResult.Status status = XExtensionResult.Status.OK; if (action.equals(COMMAND_GET_HEADING)) { if (mStatus != RUNNING) { int sensorStatus = start(); if (sensorStatus == ERROR_FAILED_TO_START) { return new XExtensionResult(XExtensionResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START); } //sensor?2s long timeout = 2000; while ((mStatus == STARTING) && (timeout > 0)) { timeout = timeout - 100; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } if (timeout == 0) { return new XExtensionResult(XExtensionResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START); } } return new XExtensionResult(status, getCompassHeading()); } else { return new XExtensionResult(XExtensionResult.Status.INVALID_ACTION); } } @Override public void sendAsyncResult(String result) { } @Override public boolean isAsync(String action) { if (action.equals(COMMAND_GET_HEADING)) { //sensor?? if (mStatus == RUNNING) { return false; } } return true; } /** * ?compass sensor */ private int start() { if ((mStatus == RUNNING) || (mStatus == STARTING)) { return mStatus; } //FIXME:SENSOR_SERVICE?hang? // ? if (null == mSensorManager) { mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE); } List<Sensor> list = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION); if (list != null && list.size() > 0) { mSensor = list.get(0); mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); mLastAccessTime = System.currentTimeMillis(); mStatus = STARTING; } else { mStatus = ERROR_FAILED_TO_START; } return mStatus; } /** * ??compass sensor */ private void stop() { if (mStatus != STOPPED) { mSensorManager.unregisterListener(this); } mStatus = STOPPED; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } /** ?SenserEvent */ @Override public void onSensorChanged(SensorEvent event) { float heading = event.values[0]; mTimeStamp = System.currentTimeMillis(); mLatestHeading = heading; mStatus = RUNNING; // ?TIMEOUT?????compass sensor?? if ((mTimeStamp - mLastAccessTime) > TIMEOUT) { stop(); } } /** * CompassHeadingJSONJavaScript * * @return CompassHeadingJSON */ private JSONObject getCompassHeading() { mLastAccessTime = System.currentTimeMillis(); JSONObject obj = new JSONObject(); try { obj.put("magneticHeading", mLatestHeading); obj.put("trueHeading", mLatestHeading); //accuracytruemagnetic //?magnetictrue?accuracy0 obj.put("headingAccuracy", 0); obj.put("timestamp", mTimeStamp); } catch (JSONException e) { String className = XCompassExt.class.getSimpleName(); XLog.e(className, e.getMessage()); } return obj; } }