Skip to content

Commit

Permalink
Begin implementation of location.
Browse files Browse the repository at this point in the history
  • Loading branch information
residuum committed Oct 4, 2022
1 parent 33c5e5a commit 9b83b20
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 67 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 32
compileSdkVersion 33

defaultConfig {
applicationId "org.sensors2.osc"
targetSdkVersion 32
targetSdkVersion 33
minSdkVersion 14
versionCode 8
versionName "0.5.2"
versionCode 9
versionName "0.6.0"
}
buildTypes {
release {
Expand All @@ -34,8 +34,8 @@ dependencies {
implementation 'com.github.hoijui.JavaOSC:javaosc-core:javaosc-0.4'
implementation 'org.apmem.tools:layouts:1.10@aar'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.SensorApps:Common:4dbf4d31c5'
implementation 'androidx.fragment:fragment:1.5.3'
implementation 'com.github.SensorApps:Common:466e47404d'
implementation 'androidx.preference:preference:1.2.0'
implementation 'com.google.android.material:material:1.6.1'
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public void onServiceConnected(ComponentName name, IBinder service) {
dispatcher.addSensorConfiguration(sensorConfiguration);
}

// Setup location
SensorConfiguration location = new SensorConfiguration();
location.setSend(true);
location.setSensorType(0);
location.setOscParam("location");
dispatcher.addSensorConfiguration(location);

// Hookup sensor activity buttons with service
for (SensorFragment sensorFragment : StartUpActivity.this.sensorFragments) {
sensorFragment.setSensorService(StartUpActivity.this.sensorService);
Expand Down
53 changes: 49 additions & 4 deletions app/src/main/java/org/sensors2/osc/dispatch/SensorService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sensors2.osc.dispatch;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
Expand All @@ -8,18 +9,23 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.nfc.NfcAdapter;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;

import org.sensors2.common.dispatch.DataDispatcher;
import org.sensors2.common.dispatch.Measurement;
import org.sensors2.common.nfc.NfcActivity;
import org.sensors2.common.sensors.Parameters;
import org.sensors2.common.sensors.SensorActivity;
Expand All @@ -31,6 +37,8 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;

/**
Expand All @@ -42,8 +50,10 @@ public class SensorService extends Service implements SensorActivity, SensorEven
private static final String NOTIFICATION_CHANNEL = "org.sensors2.osc";
public final int NOTIFICATION_ID = 1;
private final OscBinder binder = new OscBinder();
private final BackgroundLocationListener locationListener;
private OscDispatcher dispatcher;
private SensorManager sensorManager;
private LocationManager locationManager;
private SensorCommunication sensorCommunication;
private NfcAdapter nfcAdapter;
private boolean isSendingData = false;
Expand All @@ -52,6 +62,7 @@ public class SensorService extends Service implements SensorActivity, SensorEven

public SensorService() {
super();
this.locationListener = new BackgroundLocationListener();
}

@SuppressLint("WakelockTimeout")
Expand Down Expand Up @@ -80,6 +91,7 @@ public void startSendingData() {
}
startForeground(NOTIFICATION_ID, makeNotification());
this.isSendingData = true;
startLocation();
}
}

Expand Down Expand Up @@ -113,6 +125,7 @@ private void setUpSending() {
if (this.dispatcher == null) {
this.dispatcher = new OscDispatcher();
this.sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
for (org.sensors2.osc.sensors.Parameters parameters : org.sensors2.osc.sensors.Parameters.GetSensors(sensorManager, getApplicationContext())) {
SensorConfiguration sensorConfig = new SensorConfiguration();
sensorConfig.setSensorType(parameters.getSensorType());
Expand All @@ -126,8 +139,11 @@ private void setUpSending() {
private Notification makeNotification() {
Intent notificationIntent = new Intent(this, StartUpActivity.class);

PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
int intentFlag = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intentFlag = PendingIntent.FLAG_IMMUTABLE;
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, intentFlag);

return new NotificationCompat.Builder(SensorService.this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getText(R.string.app_name))
Expand All @@ -139,13 +155,13 @@ private Notification makeNotification() {
.build();
}


public void stopSendingData() {
if (this.wakeLock != null && this.wakeLock.isHeld()) {
this.wakeLock.release();
}
if (isSendingData) {
stopForeground(true);
this.locationManager.removeUpdates(this.locationListener);
sensorManager.unregisterListener(this);
isSendingData = false;
}
Expand Down Expand Up @@ -224,4 +240,33 @@ public SensorService getService() {
return SensorService.this;
}
}
}
private void startLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Location location;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
location = this.locationManager.getLastKnownLocation(LocationManager.FUSED_PROVIDER);
} else {
Location networkLocation = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location gpsLocation = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (networkLocation == null){
location = gpsLocation;
} else if (gpsLocation == null){
location = networkLocation;
} else {
location = networkLocation.getTime() > gpsLocation.getTime() ? networkLocation : gpsLocation;
}
}
if (location != null){
this.dispatcher.dispatch(new Measurement(location));
}
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, this.locationListener);
}
}

private class BackgroundLocationListener implements LocationListener{
@Override
public void onLocationChanged(@NonNull Location location) {
SensorService.this.dispatcher.dispatch(new Measurement(location));
}
}
}
129 changes: 71 additions & 58 deletions example/puredata/osc-test.pd
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
#X msg 20 48 listen 9000;
#X obj 20 94 oscparse;
#X obj 20 116 list trim;
#X obj 19 25 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
-1;
#X obj 19 25 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000
#000000;
#X obj 19 3 loadbang;
#X floatatom 289 285 5 0 0 0 - - -, f 5;
#X floatatom 378 284 5 0 0 0 - - -, f 5;
#X floatatom 20 205 5 0 0 0 - - -, f 5;
#X floatatom 60 205 5 0 0 0 - - -, f 5;
#X floatatom 101 205 5 0 0 0 - - -, f 5;
#X floatatom 199 212 5 0 0 0 - - -, f 5;
#X floatatom 239 212 5 0 0 0 - - -, f 5;
#X floatatom 280 211 5 0 0 0 - - -, f 5;
#X floatatom 468 205 5 0 0 0 - - -, f 5;
#X floatatom 501 205 5 0 0 0 - - -, f 5;
#X floatatom 534 205 5 0 0 0 - - -, f 5;
#X floatatom 558 326 5 0 0 0 - - -, f 5;
#X floatatom 591 326 5 0 0 0 - - -, f 5;
#X floatatom 624 326 5 0 0 0 - - -, f 5;
#X floatatom 566 205 5 0 0 0 - - -, f 5;
#X floatatom 648 205 5 0 0 0 - - -, f 5;
#X floatatom 681 205 5 0 0 0 - - -, f 5;
#X floatatom 714 205 5 0 0 0 - - -, f 5;
#X floatatom 737 325 5 0 0 0 - - -, f 5;
#X floatatom 770 325 5 0 0 0 - - -, f 5;
#X floatatom 803 325 5 0 0 0 - - -, f 5;
#X floatatom 289 285 5 0 0 0 - - - 0;
#X floatatom 378 284 5 0 0 0 - - - 0;
#X floatatom 20 205 5 0 0 0 - - - 0;
#X floatatom 60 205 5 0 0 0 - - - 0;
#X floatatom 101 205 5 0 0 0 - - - 0;
#X floatatom 199 212 5 0 0 0 - - - 0;
#X floatatom 239 212 5 0 0 0 - - - 0;
#X floatatom 280 211 5 0 0 0 - - - 0;
#X floatatom 468 205 5 0 0 0 - - - 0;
#X floatatom 501 205 5 0 0 0 - - - 0;
#X floatatom 534 205 5 0 0 0 - - - 0;
#X floatatom 558 326 5 0 0 0 - - - 0;
#X floatatom 591 326 5 0 0 0 - - - 0;
#X floatatom 624 326 5 0 0 0 - - - 0;
#X floatatom 566 205 5 0 0 0 - - - 0;
#X floatatom 648 205 5 0 0 0 - - - 0;
#X floatatom 681 205 5 0 0 0 - - - 0;
#X floatatom 714 205 5 0 0 0 - - - 0;
#X floatatom 737 325 5 0 0 0 - - - 0;
#X floatatom 770 325 5 0 0 0 - - - 0;
#X floatatom 803 325 5 0 0 0 - - - 0;
#X text 32 156 accelerometer;
#X text 96 255 gyroscope;
#X text 199 163 magneticfield;
Expand All @@ -36,22 +36,22 @@
#X text 647 160 linearacceleration;
#X text 585 278 gravity;
#X text 735 279 orientation;
#X floatatom 661 560 5 0 0 0 - - -, f 5;
#X floatatom 694 560 5 0 0 0 - - -, f 5;
#X floatatom 749 559 5 0 0 0 - - -, f 5;
#X floatatom 782 559 5 0 0 0 - - -, f 5;
#X floatatom 837 559 5 0 0 0 - - -, f 5;
#X floatatom 870 559 5 0 0 0 - - -, f 5;
#X floatatom 925 559 5 0 0 0 - - -, f 5;
#X floatatom 958 559 5 0 0 0 - - -, f 5;
#X floatatom 1013 559 5 0 0 0 - - -, f 5;
#X floatatom 1046 559 5 0 0 0 - - -, f 5;
#X floatatom 1101 559 5 0 0 0 - - -, f 5;
#X floatatom 1134 559 5 0 0 0 - - -, f 5;
#X floatatom 1189 559 5 0 0 0 - - -, f 5;
#X floatatom 1222 559 5 0 0 0 - - -, f 5;
#X floatatom 1277 559 5 0 0 0 - - -, f 5;
#X floatatom 1310 559 5 0 0 0 - - -, f 5;
#X floatatom 661 560 5 0 0 0 - - - 0;
#X floatatom 694 560 5 0 0 0 - - - 0;
#X floatatom 749 559 5 0 0 0 - - - 0;
#X floatatom 782 559 5 0 0 0 - - - 0;
#X floatatom 837 559 5 0 0 0 - - - 0;
#X floatatom 870 559 5 0 0 0 - - - 0;
#X floatatom 925 559 5 0 0 0 - - - 0;
#X floatatom 958 559 5 0 0 0 - - - 0;
#X floatatom 1013 559 5 0 0 0 - - - 0;
#X floatatom 1046 559 5 0 0 0 - - - 0;
#X floatatom 1101 559 5 0 0 0 - - - 0;
#X floatatom 1134 559 5 0 0 0 - - - 0;
#X floatatom 1189 559 5 0 0 0 - - - 0;
#X floatatom 1222 559 5 0 0 0 - - - 0;
#X floatatom 1277 559 5 0 0 0 - - - 0;
#X floatatom 1310 559 5 0 0 0 - - - 0;
#X obj 1296 608 print wtf;
#X text 757 512 finger 2;
#X text 838 510 finger 3;
Expand All @@ -65,9 +65,9 @@
touch8, f 101;
#X obj 20 175 unpack f f f;
#X obj 199 183 unpack f f f;
#X floatatom 109 314 5 0 0 0 - - -, f 5;
#X floatatom 149 314 5 0 0 0 - - -, f 5;
#X floatatom 190 314 5 0 0 0 - - -, f 5;
#X floatatom 109 314 5 0 0 0 - - - 0;
#X floatatom 149 314 5 0 0 0 - - - 0;
#X floatatom 190 314 5 0 0 0 - - - 0;
#X obj 109 284 unpack f f f;
#X obj 468 176 unpack f f f f, f 15;
#X obj 558 297 unpack f f f, f 13;
Expand All @@ -81,15 +81,22 @@ touch8, f 101;
#X obj 1101 530 unpack f f;
#X obj 1189 530 unpack f f;
#X obj 1277 530 unpack f f;
#X obj 20 139 route accelerometer gyroscope magneticfield light proximity
rotationvector gravity linearacceleration orientation inclination,
f 150;
#X floatatom 794 214 5 0 0 0 - - -, f 5;
#X floatatom 794 214 5 0 0 0 - - - 0;
#X text 788 192 inclination;
#X obj 20 139 route accelerometer gyroscope magneticfield light proximity
rotationvector gravity linearacceleration orientation inclination location
, f 150;
#X obj 1034 241 unpack f f f f f;
#X floatatom 1004 264 5 0 0 0 - - - 0;
#X floatatom 1039 265 5 0 0 0 - - - 0;
#X floatatom 1074 266 5 0 0 0 - - - 0;
#X floatatom 1109 267 5 0 0 0 - - - 0;
#X floatatom 1145 267 5 0 0 0 - - - 0;
#X text 1032 222 location;
#X connect 0 0 2 0;
#X connect 1 0 0 0;
#X connect 2 0 3 0;
#X connect 3 0 80 0;
#X connect 3 0 82 0;
#X connect 4 0 1 0;
#X connect 5 0 4 0;
#X connect 61 0 72 0;
Expand Down Expand Up @@ -139,14 +146,20 @@ f 150;
#X connect 78 1 49 0;
#X connect 79 0 50 0;
#X connect 79 1 51 0;
#X connect 80 0 62 0;
#X connect 80 1 67 0;
#X connect 80 2 63 0;
#X connect 80 3 6 0;
#X connect 80 4 7 0;
#X connect 80 5 68 0;
#X connect 80 6 69 0;
#X connect 80 7 70 0;
#X connect 80 8 71 0;
#X connect 80 9 81 0;
#X connect 80 10 61 0;
#X connect 82 0 62 0;
#X connect 82 1 67 0;
#X connect 82 2 63 0;
#X connect 82 3 6 0;
#X connect 82 4 7 0;
#X connect 82 5 68 0;
#X connect 82 6 69 0;
#X connect 82 7 70 0;
#X connect 82 8 71 0;
#X connect 82 9 80 0;
#X connect 82 10 83 0;
#X connect 82 11 61 0;
#X connect 83 0 84 0;
#X connect 83 1 85 0;
#X connect 83 2 86 0;
#X connect 83 3 87 0;
#X connect 83 4 88 0;

0 comments on commit 9b83b20

Please sign in to comment.