2022-03-30 18:35:05 +02:00
package hendrikschutter.com.externgnss ;
2022-03-27 22:51:53 +02:00
import android.Manifest ;
import android.annotation.SuppressLint ;
2022-04-25 22:55:07 +02:00
import android.app.AlertDialog ;
2022-03-27 22:51:53 +02:00
import android.content.Context ;
2022-04-25 22:55:07 +02:00
import android.content.DialogInterface ;
import android.graphics.Color ;
2022-03-27 22:51:53 +02:00
import android.location.LocationListener ;
import android.location.LocationManager ;
2022-04-25 18:09:07 +02:00
2022-03-30 18:05:58 +02:00
import androidx.appcompat.app.AppCompatActivity ;
2022-04-25 18:09:07 +02:00
2022-03-27 22:51:53 +02:00
import android.os.Bundle ;
import android.util.Log ;
2022-04-25 22:55:07 +02:00
import android.view.View ;
2022-03-30 20:44:43 +02:00
import android.widget.EditText ;
2022-03-27 22:51:53 +02:00
import android.widget.TextView ;
import android.widget.Toast ;
import pub.devrel.easypermissions.AfterPermissionGranted ;
import pub.devrel.easypermissions.EasyPermissions ;
import static java.lang.Thread.sleep ;
2022-04-25 22:55:07 +02:00
import com.google.android.material.floatingactionbutton.FloatingActionButton ;
2022-04-25 17:11:58 +02:00
import java.util.concurrent.BlockingQueue ;
import java.util.concurrent.LinkedBlockingQueue ;
2022-04-25 14:42:06 +02:00
2022-03-27 22:51:53 +02:00
public class MainActivity extends AppCompatActivity {
private TextView output ;
2022-04-25 18:09:07 +02:00
private LocationManager locationManager = null ;
2022-04-25 17:11:58 +02:00
private SerialUSB serialUSB = null ;
private NMEAParser nmeaParser = null ;
2022-03-27 22:51:53 +02:00
private boolean doRun = true ;
private final Context cntxToastInternGNSSUpdate = this ;
2022-04-25 14:42:06 +02:00
final String [ ] perms = { Manifest . permission . ACCESS_FINE_LOCATION , Manifest . permission . INTERNET , } ;
private static final String INTENT_ACTION_GRANT_USB = BuildConfig . APPLICATION_ID + " .GRANT_USB " ;
2022-03-27 22:51:53 +02:00
private boolean bChange = true ;
2022-04-25 17:11:58 +02:00
private BlockingQueue < Byte > rawSerialByteDataQueue = null ;
2022-04-25 18:09:07 +02:00
private double lastInternFixLat , lastInternFixLon ;
2022-04-25 22:55:07 +02:00
private MainActivity activity = this ;
2022-03-27 22:51:53 +02:00
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
2022-04-25 14:42:06 +02:00
2022-04-25 17:11:58 +02:00
// start intern GNSS and display stats on UI
startInternGNSS ( ) ;
2022-04-25 14:42:06 +02:00
2022-04-25 17:11:58 +02:00
// start extern GNSS and display stats on UI
startExternGNSS ( ) ;
2022-03-27 22:51:53 +02:00
}
// From https://github.com/googlesamples/easypermissions
@Override
public void onRequestPermissionsResult ( int requestCode , String [ ] permissions , int [ ] grantResults ) {
super . onRequestPermissionsResult ( requestCode , permissions , grantResults ) ;
// Forward results to EasyPermissions
EasyPermissions . onRequestPermissionsResult ( requestCode , permissions , grantResults , this ) ;
}
2022-04-25 17:11:58 +02:00
2022-04-25 18:09:07 +02:00
private void startExternGNSS ( ) {
2022-04-25 17:11:58 +02:00
rawSerialByteDataQueue = new LinkedBlockingQueue < > ( ) ;
serialUSB = new SerialUSB ( this , rawSerialByteDataQueue ) ;
2022-04-25 22:55:07 +02:00
nmeaParser = new NMEAParser ( ) ;
2022-04-25 17:11:58 +02:00
2022-04-25 18:09:07 +02:00
if ( serialUSB . findSerialDevice ( ) ) {
2022-04-25 17:11:58 +02:00
serialUSB . connect ( 9600 , 0 ) ;
2022-04-25 18:09:07 +02:00
if ( serialUSB . isConnected ( ) ) {
2022-04-25 17:11:58 +02:00
nmeaParser . setReceiveByteStream ( rawSerialByteDataQueue ) ;
2022-04-25 22:55:07 +02:00
( ( FloatingActionButton ) findViewById ( R . id . floatingActionButtonExternGnssInfo ) ) . setOnClickListener ( new View . OnClickListener ( ) {
@Override
public void onClick ( View view ) {
String otgInfo = " OTG-Info: \ n Vendor: " + serialUSB . getUsbDeviceVendorId ( ) + " " +
" \ n Device: " + serialUSB . getDeviceId ( ) +
" \ n Baudrate: " + String . valueOf ( serialUSB . getBaudRate ( ) +
" \ n \ nNMEA: \ n Sentences: " + String . valueOf ( nmeaParser . getSentenceCounter ( ) ) ) ;
AlertDialog . Builder builder = new AlertDialog . Builder ( activity ) ;
builder . setMessage ( otgInfo )
. setPositiveButton ( " Close " , new DialogInterface . OnClickListener ( ) {
public void onClick ( DialogInterface dialog , int id ) {
}
} ) ;
builder . create ( ) . show ( ) ;
}
} ) ;
2022-04-25 17:11:58 +02:00
}
}
}
2022-03-27 22:51:53 +02:00
@AfterPermissionGranted ( 123 )
2022-04-25 17:11:58 +02:00
private void startInternGNSS ( ) {
2022-03-27 22:51:53 +02:00
if ( EasyPermissions . hasPermissions ( this , perms ) ) {
// Already have permission, do the thing
Toast . makeText ( cntxToastInternGNSSUpdate , " Permission granted! " , Toast . LENGTH_SHORT ) . show ( ) ;
locationManager = ( LocationManager ) getSystemService ( Context . LOCATION_SERVICE ) ;
} else {
// Do not have permissions, request them now
2022-04-25 17:11:58 +02:00
EasyPermissions . requestPermissions ( this , " We require location access! " ,
2022-03-27 22:51:53 +02:00
123 , perms ) ;
}
}
2022-03-30 20:44:43 +02:00
private void setEditTextLabel ( final EditText v , final String str ) {
2022-03-27 22:51:53 +02:00
Runnable myRun = new Runnable ( ) {
public void run ( ) {
2022-03-30 20:44:43 +02:00
v . setText ( str ) ;
2022-03-27 22:51:53 +02:00
}
} ;
runOnUiThread ( myRun ) ;
}
2022-04-25 22:55:07 +02:00
private void setTextViewLabel ( final TextView v , final String str ) {
Runnable myRun = new Runnable ( ) {
public void run ( ) {
v . setText ( str ) ;
}
} ;
runOnUiThread ( myRun ) ;
}
2022-03-27 22:51:53 +02:00
@SuppressLint ( " MissingPermission " )
@Override
protected void onResume ( ) {
super . onResume ( ) ;
2022-04-25 22:55:07 +02:00
setInternGnssUiNoSignal ( ) ;
setExternGnssUiNoSignal ( ) ;
setInternGnssUiColor ( Color . GRAY ) ;
setExternGnssUiColor ( Color . GRAY ) ;
2022-03-27 22:51:53 +02:00
if ( ! EasyPermissions . hasPermissions ( this , perms ) ) {
// Dummy thread ...
new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
while ( doRun ) {
Log . i ( " HAG " , " Just do nothing ... " ) ;
try {
sleep ( 2000 ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
}
} ) . start ( ) ;
} else {
locationManager . requestLocationUpdates ( LocationManager . GPS_PROVIDER , 0 , 0 , locationListener ) ;
// Worker-Thread
new Thread (
new Runnable ( ) {
@Override
public void run ( ) {
while ( doRun ) {
2022-03-30 20:44:43 +02:00
if ( locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) ! = null ) {
2022-04-25 22:55:07 +02:00
setInternGnssUiColor ( Color . BLACK ) ;
2022-04-25 18:09:07 +02:00
lastInternFixLat = locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLatitude ( ) ;
lastInternFixLon = locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLongitude ( ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLatNumber ) , String . format ( " %.6f " , lastInternFixLat ) ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLongNumber ) , String . format ( " %.6f " , lastInternFixLon ) ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssAltitudeNumber ) , String . format ( " %.2f " , locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getAltitude ( ) ) + " m " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssAccuracyNumber ) , String . format ( " %.2f " , locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getAccuracy ( ) ) + " m " ) ;
2022-03-30 20:44:43 +02:00
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssSatCountNumber ) , Integer . toString ( locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getExtras ( ) . getInt ( " satellites " ) ) ) ;
2022-04-25 18:09:07 +02:00
} else {
2022-04-25 22:55:07 +02:00
setInternGnssUiColor ( Color . GRAY ) ;
setInternGnssUiNoSignal ( ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . diffGNSSDistanceNumber ) , " no data " ) ;
2022-03-30 20:44:43 +02:00
}
2022-04-25 17:11:58 +02:00
2022-04-25 18:09:07 +02:00
if ( ( serialUSB ! = null ) & & ( nmeaParser ! = null ) ) {
if ( serialUSB . isConnected ( ) ) {
2022-04-25 22:55:07 +02:00
setTextViewLabel ( ( TextView ) findViewById ( R . id . externgnssNameTextView ) , " Extern GNSS receiver " ) ;
2022-04-25 17:11:58 +02:00
nmeaParser . handleReceiveByteStream ( ) ;
if ( nmeaParser . checkFix ( ) = = true ) {
2022-04-25 22:55:07 +02:00
setExternGnssUiColor ( Color . BLACK ) ;
2022-04-25 18:09:07 +02:00
//Log.i("ExternGNSS", "Fix! Lat: " + String.valueOf(nmeaParser.getLatitude()) + " Lon: " + String.valueOf(nmeaParser.getLongitude()));
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssLatNumber ) , String . format ( " %.6f " , nmeaParser . getLatitude ( ) ) ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssLongNumber ) , String . format ( " %.6f " , nmeaParser . getLongitude ( ) ) ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssSatCountNumber ) , Integer . toString ( nmeaParser . getSatCount ( ) ) ) ;
2022-04-25 17:11:58 +02:00
}
2022-04-25 18:09:07 +02:00
} else {
2022-04-25 22:55:07 +02:00
setTextViewLabel ( ( TextView ) findViewById ( R . id . externgnssNameTextView ) , " Extern GNSS receiver (offline) " ) ;
setExternGnssUiColor ( Color . GRAY ) ;
setExternGnssUiNoSignal ( ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . diffGNSSDistanceNumber ) , " no data " ) ;
2022-04-25 17:11:58 +02:00
}
2022-04-25 18:09:07 +02:00
if ( ( locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) ! = null ) & & ( serialUSB ! = null ) & & ( nmeaParser ! = null ) ) {
if ( nmeaParser . checkFix ( ) = = true ) {
setEditTextLabel ( ( EditText ) findViewById ( R . id . diffGNSSDistanceNumber ) , String . format ( " %.6f " , nmeaParser . calcDistance ( lastInternFixLat , lastInternFixLon ) ) + " m " ) ;
}
} else {
setEditTextLabel ( ( EditText ) findViewById ( R . id . diffGNSSDistanceNumber ) , " no signal " ) ;
2022-03-27 22:51:53 +02:00
}
2022-04-25 18:09:07 +02:00
}
2022-03-27 22:51:53 +02:00
try {
2022-04-25 22:55:07 +02:00
sleep ( 1000 ) ;
2022-03-27 22:51:53 +02:00
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
}
} ) . start ( ) ;
}
}
/ * *
* Stop the updates when Activity is paused
* /
@Override
protected void onPause ( ) {
super . onPause ( ) ;
doRun = false ;
}
2022-04-25 22:55:07 +02:00
private void setInternGnssUiColor ( int color ) {
( ( TextView ) findViewById ( R . id . interngnssLatTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . interngnssLongTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . interngnssAccuracyTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . interngnssAltitudeTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . interngnssSatCountTextView ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . interngnssLatNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . interngnssLongNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . interngnssAccuracyNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . interngnssAltitudeNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . interngnssSatCountNumber ) ) . setTextColor ( color ) ;
}
private void setExternGnssUiColor ( int color ) {
( ( TextView ) findViewById ( R . id . externgnssLatTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . externgnssLongTextView ) ) . setTextColor ( color ) ;
( ( TextView ) findViewById ( R . id . externgnssSatCountTextView ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . externgnssLatNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . externgnssLongNumber ) ) . setTextColor ( color ) ;
( ( EditText ) findViewById ( R . id . externgnssSatCountNumber ) ) . setTextColor ( color ) ;
}
private void setInternGnssUiNoSignal ( ) {
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLatNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLongNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssAltitudeNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssAccuracyNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssSatCountNumber ) , " no signal " ) ;
}
private void setExternGnssUiNoSignal ( ) {
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssLatNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssLongNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . externgnssSatCountNumber ) , " no signal " ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . diffGNSSDistanceNumber ) , " no signal " ) ;
}
2022-03-27 22:51:53 +02:00
LocationListener locationListener = new LocationListener ( ) {
@Override
public void onLocationChanged ( android . location . Location location ) {
Toast . makeText ( cntxToastInternGNSSUpdate , " Internal GNSS updated " , Toast . LENGTH_LONG ) . show ( ) ;
}
2022-04-25 18:09:07 +02:00
@Override
2022-03-27 22:51:53 +02:00
// This callback will never be invoked on Android Q and above.
public void onStatusChanged ( String provider , int status , Bundle extras ) {
2022-04-25 18:09:07 +02:00
// Toast.makeText(mContext, "Status Changed", Toast.LENGTH_LONG).show();
2022-03-27 22:51:53 +02:00
}
@Override
public void onProviderEnabled ( String provider ) {
}
@Override
public void onProviderDisabled ( String provider ) {
}
} ;
}