shithub: drawterm

Download patch

ref: 9fc8db1c35ac193cfff906149b9a66ae7e8fe5ab
parent: 207a95163f5a047e1f8ed8b77f1c4ab2b08edb64
author: glenda <[email protected]>
date: Fri Nov 11 17:55:50 EST 2022

privacy notice, nmea bullshit

--- /dev/null
+++ b/PRIVACY
@@ -1,0 +1,1 @@
+this Android app does not collect or report any user data to anyone. the only connections made by this app should be to your own servers.
\ No newline at end of file
--- a/gui-android/cpp/android.c
+++ b/gui-android/cpp/android.c
@@ -67,15 +67,13 @@
 		return 0;
 	}
 	jclass clazz = (*env)->GetObjectClass(env, mainActivityObj);
-	jmethodID methodID = (*env)->GetMethodID(env, clazz, "getLocation", "()Ljava/lang/String;");
+	jmethodID methodID = (*env)->GetMethodID(env, clazz, "getNmea", "()Ljava/lang/String;");
 	jstring str = (*env)->CallObjectMethod(env, mainActivityObj, methodID);
-	char *s = (*env)->GetStringUTFChars(env, str, NULL);
+	const char *s = (*env)->GetStringUTFChars(env, str, NULL);
 	long ret = strlen(s);
-	if (ret > (n-1))
-		ret = n-1;
+	if (ret > n)
+		ret = n;
 	memcpy(a, s, ret);
-	a[ret] = '\n';
-	ret++;
 	(*env)->ReleaseStringUTFChars(env, str, s);
 	(*jvm)->DetachCurrentThread(jvm);
 	return ret;
@@ -82,6 +80,37 @@
 }
 
 void
+startlocation()
+{
+	JNIEnv *env;
+	jint rs = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
+	if(rs != JNI_OK) {
+		__android_log_print(ANDROID_LOG_WARN, "drawterm", "AttachCurrentThread returned error: %d", rs);
+		return;
+	}
+	jclass clazz = (*env)->GetObjectClass(env, mainActivityObj);
+	jmethodID methodID = (*env)->GetMethodID(env, clazz, "startNmea", "()V");
+	(*env)->CallVoidMethod(env, mainActivityObj, methodID);
+	(*jvm)->DetachCurrentThread(jvm);
+	return;
+}
+
+void
+stoplocation()
+{
+	JNIEnv *env;
+	jint rs = (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6);
+	if(rs != JNI_OK) {
+		__android_log_print(ANDROID_LOG_WARN, "drawterm", "GetEnv returned error: %d", rs);
+		return;
+	}
+	jclass clazz = (*env)->GetObjectClass(env, mainActivityObj);
+	jmethodID methodID = (*env)->GetMethodID(env, clazz, "stopNmea", "()V");
+	(*env)->CallVoidMethod(env, mainActivityObj, methodID);
+	return;
+}
+
+void
 show_notification(char *buf)
 {
 	JNIEnv *env;
@@ -137,8 +166,11 @@
 }
 
 void
-getcolor(ulong v, ulong *r, ulong *g, ulong *b)
+getcolor(ulong i, ulong *r, ulong *g, ulong *b)
 {
+	ulong v;
+
+	v = cmap2rgb(i);
 	*r = (v>>16)&0xFF;
 	*g = (v>>8)&0xFF;
 	*b = v&0xFF;
@@ -152,9 +184,6 @@
 	int x, y, o, b;
 	ARect bounds;
 
-	if (window == NULL)
-		return;
-
 	memset(&buffer, 0, sizeof(buffer));
 
 	bounds.left = r.min.x;
@@ -162,6 +191,9 @@
 	bounds.right = r.max.x;
 	bounds.bottom = r.max.y;
 
+	if (window == NULL)
+		return;
+
 	if (ANativeWindow_lock(window, &buffer, &bounds) != 0) {
 		__android_log_print(ANDROID_LOG_WARN, "drawterm", "Unable to lock window buffer");
 		return;
@@ -172,6 +204,8 @@
 	r.max.x = bounds.right;
 	r.max.y = bounds.bottom;
 
+//	__android_log_print(ANDROID_LOG_WARN, "drawterm", "flushmemscreen 1");
+
 	pixels = (uint8_t*)buffer.bits;
 	for (y = r.min.y; y < r.max.y; y++)
 		for (x = r.min.x; x < r.max.x; x++) {
@@ -182,6 +216,8 @@
 			pixels[b+1] = gscreen->data->bdata[o+1];
 			pixels[b+0] = gscreen->data->bdata[o+2];
 		}
+
+//	__android_log_print(ANDROID_LOG_WARN, "drawterm", "flushmemscreen 2");
 
 	if (ANativeWindow_unlockAndPost(window) != 0) {
 		__android_log_print(ANDROID_LOG_WARN, "drawterm", "Unable to unlock and post window buffer");
--- a/gui-android/cpp/devandroid.c
+++ b/gui-android/cpp/devandroid.c
@@ -11,6 +11,8 @@
 void take_picture(int id);
 int num_cameras();
 long getlocation(char *a, long n);
+void startlocation();
+void stoplocation();
 
 int Ncameras = 0;
 
@@ -27,6 +29,7 @@
 	Qcompass,
 	Qnotify,
 	Qlocation,
+	Qlocationctl,
 	Ntab,
 	Qcam		= 8,
 };
@@ -39,6 +42,7 @@
 	"compass",	{Qcompass},		0,	0444,
 	"notify",	{Qnotify},		0,	0222,
 	"location",	{Qlocation},		0,	0444,
+	"locationctl",	{Qlocationctl},		0,	0222,
 	"camNNNN",	{Qcam},			0,	0444,
 };
 
@@ -133,6 +137,9 @@
 			take_picture(i);
 		}
 	}
+	else if (c->qid.path & Qlocation) {
+		startlocation();
+	}
 	c = devopen(c, omode, androiddir, nelem(androiddir) + Ncameras - 1, androidgen);
 
 	return c;
@@ -147,6 +154,9 @@
 		camlen = 0;
 		camopen = 0;
 	}
+	else if (c->qid.path & Qlocation) {
+		stoplocation();
+	}
 }
 
 long
@@ -214,7 +224,9 @@
 		case Qcompass:
 			return readsensor(a, n, ASENSOR_TYPE_MAGNETIC_FIELD);
 		case Qlocation:
-			return getlocation(a, n);
+			while ((l = getlocation(a, n)) == 0)
+				usleep(250 * 1000);
+			return l;
 		case Qdir:
 			return devdirread(c, a, n, androiddir, nelem(androiddir) + Ncameras - 1, androidgen);
 	}
@@ -233,6 +245,8 @@
 			str[n] = '\0';
 			show_notification(str);
 			free(str);
+			return n;
+		case Qlocationctl:
 			return n;
 		default:
 			error(Eperm);
--- a/gui-android/java/org/echoline/drawterm/MainActivity.java
+++ b/gui-android/java/org/echoline/drawterm/MainActivity.java
@@ -52,6 +52,8 @@
 import android.location.Location;
 import android.location.LocationManager;
 import android.location.LocationListener;
+import android.location.GpsStatus.NmeaListener;
+import android.location.Criteria;
 
 import java.io.File;
 import java.util.Map;
@@ -70,6 +72,10 @@
 	private int notificationId;
 	private byte []jpegBytes;
 	private String location;
+	private String nmeaMessages = "";
+	private LocationListener locationListener;
+	private HandlerThread locationThread;
+	private boolean nlStarted = false;
 
 	static {
 		System.loadLibrary("drawterm");
@@ -104,24 +110,33 @@
 		return false;
 	}
 
-	public String getLocation() {
-		LocationManager locationManager = ((LocationManager)getSystemService(Context.LOCATION_SERVICE));
+	public void startNmea() {
+		LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 
-		location = "";
+		if (!nlStarted) {
+			NmeaListener nl = new NmeaListener() {
+				@Override
+				public void onNmeaReceived(long timestamp, String nmea) {
+					nmeaMessages += nmea;
+				}
+			};
 
-		HandlerThread mBackgroundThread = new HandlerThread("Location Background");
-		mBackgroundThread.start();
+			lm.addNmeaListener(nl);
 
-		locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
+			nlStarted = true;
+		}
+
+		locationThread = new HandlerThread("Location Background");
+		locationThread.start();
+
+		locationListener = new LocationListener() {
 			@Override
 			public void onLocationChanged(Location l) {
-				location = l.getLatitude() + "\t" + l.getLongitude() + "\t" + l.getAltitude();
-				mBackgroundThread.quitSafely();
 			}
 
 			@Override
 			public void onProviderDisabled(String p) {
-				mBackgroundThread.quitSafely();
+				locationThread.quitSafely();
 			}
 
 			@Override
@@ -131,15 +146,30 @@
 			@Override
 			public void onStatusChanged(String p, int i, Bundle b) {
 			}
-		}, mBackgroundThread.getLooper());
+		};
 
+		lm.requestLocationUpdates(lm.GPS_PROVIDER, 1l, 0.01f, locationListener, locationThread.getLooper());
+	}
+
+	public String getNmea() {
+		String ret = new String(nmeaMessages);
+
+		nmeaMessages = "";
+
+		return ret;
+	}
+
+	public void stopNmea() {
+		locationThread.quitSafely();
 		try {
-			mBackgroundThread.join();
+			locationThread.join();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
 
-		return location;
+		LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
+
+		lm.removeUpdates(locationListener);
 	}
 
 	public void showNotification(String text) {
@@ -316,7 +346,6 @@
 		}
 		ll.setAdapter(la);
 
-		setDTSurface(null);
 		dtrunning = false;
 	}
 
@@ -539,7 +568,6 @@
 	@Override
 	public void onDestroy()
 	{
-		setDTSurface(null);
 		dtrunning = false;
 		exitDT();
 		super.onDestroy();