Skip to content

Commit

Permalink
Changes to pass Android context via static methods rather than config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrobin committed Mar 20, 2019
1 parent df2515c commit 821476b
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 49 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -51,7 +51,7 @@ subprojects {
substitute module('org.sensorhub:sensorhub-service-video') with project(':sensorhub-service-video')
substitute module('org.vast.opengis:ogc-services-sps') with project(':ogc-services-sps')

// override objenesis version to get new android instantiatiors
// override objenesis version to get new android instantiators
substitute module('uk.com.robust-it:cloning:1.9.1') with module('uk.com.robust-it:cloning:1.9.10')
}
}
Expand Down
1 change: 0 additions & 1 deletion sensorhub-android-app/build.gradle
Expand Up @@ -5,7 +5,6 @@ ext.details = 'OSH demo app for Android'

dependencies {
api project(':sensorhub-android-lib')
api 'javax.xml.stream:stax-api:1.0-2'
}

android {
Expand Down
Expand Up @@ -138,8 +138,6 @@ protected void updateConfig(SharedPreferences prefs, String runName)
if (sensorsConfig.activateBackCamera || sensorsConfig.activateFrontCamera)
showVideo = true;
sensorsConfig.videoCodec = prefs.getString("video_codec", AndroidSensorsConfig.JPEG_CODEC);
sensorsConfig.androidContext = this.getApplicationContext();
sensorsConfig.camPreviewTexture = boundService.getVideoTexture();
sensorsConfig.runName = runName;
sensorhubConfig.add(sensorsConfig);
addSosTConfig(sensorsConfig, sosUser, sosPwd);
Expand Down
7 changes: 2 additions & 5 deletions sensorhub-android-lib/build.gradle
Expand Up @@ -9,22 +9,19 @@ dependencies {
//compile 'org.sensorhub:sensorhub-comm-ble:' + oshCoreVersion
//compile 'org.sensorhub:sensorhub-driver-trupulse:[0.1,2.0)'
//compile 'org.sensorhub:sensorhub-driver-angelsensor:[0.1,2.0)'
api project(':sensorhub-core')
api project(':sensorhub-service-swe')
api project(':sensorhub-comm-ble')
api project(':sensorhub-android-service')
api project(':sensorhub-driver-trupulse')
api project(':sensorhub-driver-angelsensor')
api project(':sensorhub-driver-android')
api project(':sensorhub-android-flirone')
//api project(':sensorhub-android-dji')
implementation 'org.slf4j:slf4j-android:1.6.1-RC1'
api 'javax.xml.stream:stax-api:1.0-2'
}

configurations {
compile {
// exclude stuff from APK
exclude group: "javax.xml.stream"
//exclude group: "javax.xml.stream"
exclude group: "javax.servlet"
exclude group: "xml-apis"
exclude group: "org.eclipse.jetty"
Expand Down
5 changes: 5 additions & 0 deletions sensorhub-android-service/AndroidManifest.xml
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.sensorhub.android.service"
android:versionCode="1"
android:versionName="1.0" >
</manifest>
38 changes: 38 additions & 0 deletions sensorhub-android-service/build.gradle
@@ -0,0 +1,38 @@
apply plugin: 'com.android.library'

description = 'OSH Android Service'
ext.details = 'Android service encapsulating an OSH instance'

dependencies {
//api 'org.sensorhub:sensorhub-core:' + oshCoreVersion
//api 'org.sensorhub:sensorhub-driver-videocam:[1.0,2.0)'
//api 'org.sensorhub:sensorhub-process-vecmath:[1.0,2.0)'
api project(':sensorhub-core')
api project(':sensorhub-comm-ble')
api project(':sensorhub-service-swe')
api 'javax.xml.stream:stax-api:1.0-2'
}

android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion

defaultConfig {
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
}

lintOptions {
abortOnError false
}

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}

File renamed without changes.
@@ -1,17 +1,17 @@
/***************************** BEGIN LICENSE BLOCK ***************************
The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
Copyright (C) 2012-2015 Sensia Software LLC. All Rights Reserved.
******************************* END LICENSE BLOCK ***************************/

/***************************** BEGIN LICENSE BLOCK ***************************
The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
Copyright (C) 2012-2015 Sensia Software LLC. All Rights Reserved.
******************************* END LICENSE BLOCK ***************************/

package org.sensorhub.android;

import java.io.File;
Expand Down
Expand Up @@ -51,11 +51,13 @@ public class SensorHubService extends Service
private HandlerThread msgThread;
private Handler msgHandler;
SensorHub sensorhub;
SurfaceTexture videoTex;
boolean hasVideo;
static Context context;
static SurfaceTexture videoTex;


public class LocalBinder extends Binder {
public class LocalBinder extends Binder
{
SensorHubService getService() {
return SensorHubService.this;
}
Expand All @@ -67,6 +69,13 @@ public void onCreate() {

try
{
// keep handle to Android context so it can be retrieved by OSH components
SensorHubService.context = getApplicationContext();

// create video surface texture here so it's not destroyed when pausing the app
SensorHubService.videoTex = new SurfaceTexture(1);
SensorHubService.videoTex.detachFromGLContext();

// load external dex file containing stax API
//Dexter.loadFromAssets(this.getApplicationContext(), "stax-api-1.0-2.dex");

Expand All @@ -79,10 +88,6 @@ public void onCreate() {
dbf.setNamespaceAware(true);
XMLImplFinder.setDOMImplementation(dbf.newDocumentBuilder().getDOMImplementation());

// create video surface texture here so it's not destroyed when pausing the app
videoTex = new SurfaceTexture(1);
videoTex.detachFromGLContext();

// start handler thread
msgThread = new HandlerThread("SensorHubService", Process.THREAD_PRIORITY_BACKGROUND);
msgThread.start();
Expand Down Expand Up @@ -145,7 +150,9 @@ public void onDestroy()
{
stopSensorHub();
msgThread.quitSafely();
videoTex.release();
SensorHubService.videoTex.release();
SensorHubService.videoTex = null;
SensorHubService.context = null;
}


Expand All @@ -162,15 +169,21 @@ public SensorHub getSensorHub()
}


public SurfaceTexture getVideoTexture()
public boolean hasVideo()
{
return hasVideo;
}


public static SurfaceTexture getVideoTexture()
{
return videoTex;
}


public boolean hasVideo()
public static Context getContext()
{
return hasVideo;
return context;
}

}
File renamed without changes.
1 change: 1 addition & 0 deletions sensorhub-driver-android/build.gradle
Expand Up @@ -10,6 +10,7 @@ dependencies {
api project(':sensorhub-core')
api project(':sensorhub-driver-videocam')
api project(':sensorhub-process-vecmath')
api project(':sensorhub-android-service')
}

android {
Expand Down
Expand Up @@ -31,7 +31,7 @@
public class AndroidSensorsConfig extends SensorConfig
{
public final static String JPEG_CODEC = "JPEG";
public final static String H264_CODEC = "H264";
public final static String H264_CODEC = "H264";

public boolean activateAccelerometer = false;
public boolean activateGyrometer = false;
Expand All @@ -46,22 +46,11 @@ public class AndroidSensorsConfig extends SensorConfig

public String deviceName;
public String runName;
public String runDescription;

public transient Context androidContext;
public transient SurfaceTexture camPreviewTexture;

public String runDescription;


public AndroidSensorsConfig()
{
this.moduleClass = AndroidSensorsDriver.class.getCanonicalName();
}


@Override
public ModuleConfig clone()
{
return this; // disable clone for now as it crashes Android app
}
}
Expand Up @@ -18,13 +18,16 @@
import java.util.List;
import javax.xml.namespace.QName;

import android.graphics.SurfaceTexture;
import android.os.Handler;
import android.os.HandlerThread;
import net.opengis.gml.v32.AbstractFeature;
import net.opengis.sensorml.v20.PhysicalComponent;
import net.opengis.sensorml.v20.PhysicalSystem;
import net.opengis.sensorml.v20.SpatialFrame;
import net.opengis.sensorml.v20.impl.SpatialFrameImpl;

import org.sensorhub.android.SensorHubService;
import org.sensorhub.api.common.SensorHubException;
import org.sensorhub.api.sensor.ISensorDataInterface;
import org.sensorhub.api.sensor.SensorException;
Expand Down Expand Up @@ -71,7 +74,7 @@ public AndroidSensorsDriver()
@Override
public synchronized void init() throws SensorHubException
{
Context androidContext = config.androidContext;
Context androidContext = SensorHubService.getContext();

// generate identifiers
String deviceID = Secure.getString(androidContext.getContentResolver(), Secure.ANDROID_ID);
Expand Down Expand Up @@ -185,10 +188,11 @@ protected void createCameraOutputs(Context androidContext) throws SensorExceptio
if ( (info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK && config.activateBackCamera) ||
(info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT && config.activateFrontCamera))
{
SurfaceTexture camPreviewTexture = SensorHubService.getVideoTexture();
if (AndroidSensorsConfig.JPEG_CODEC.equals(config.videoCodec))
useCamera(new AndroidCameraOutputMJPEG(this, cameraId, config.camPreviewTexture), cameraId);
useCamera(new AndroidCameraOutputMJPEG(this, cameraId, camPreviewTexture), cameraId);
else if (AndroidSensorsConfig.H264_CODEC.equals(config.videoCodec))
useCamera(new AndroidCameraOutputH264(this, cameraId, config.camPreviewTexture), cameraId);
useCamera(new AndroidCameraOutputH264(this, cameraId, camPreviewTexture), cameraId);
else
throw new SensorException("Unsupported codec " + config.videoCodec);
}
Expand Down

0 comments on commit 821476b

Please sign in to comment.