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 ;
import android.content.Context ;
2022-03-31 21:31:40 +02:00
import android.hardware.usb.UsbManager ;
2022-03-27 22:51:53 +02:00
import android.location.LocationListener ;
import android.location.LocationManager ;
2022-03-30 18:05:58 +02:00
import androidx.appcompat.app.AppCompatActivity ;
2022-03-27 22:51:53 +02:00
import android.os.Bundle ;
import android.util.Log ;
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 ;
public class MainActivity extends AppCompatActivity {
private TextView output ;
private LocationManager locationManager ;
private boolean doRun = true ;
private final Context cntxToastInternGNSSUpdate = this ;
final String [ ] perms = { Manifest . permission . ACCESS_FINE_LOCATION , Manifest . permission . INTERNET } ;
private boolean bChange = true ;
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setContentView ( R . layout . activity_main ) ;
startLocationInit ( ) ;
2022-03-31 21:31:40 +02:00
UsbManager manager = ( UsbManager ) getSystemService ( Context . USB_SERVICE ) ;
Log . i ( " HAG " , " init app " ) ;
ExternGNSS tmp = new ExternGNSS ( manager , this ) ;
2022-03-27 22:51:53 +02:00
}
2022-03-31 21:31:40 +02:00
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 ) ;
}
@AfterPermissionGranted ( 123 )
private void startLocationInit ( ) {
if ( EasyPermissions . hasPermissions ( this , perms ) ) {
// Already have permission, do the thing
Toast . makeText ( cntxToastInternGNSSUpdate , " Permission granted! " , Toast . LENGTH_SHORT ) . show ( ) ;
} else {
// Do not have permissions, request them now
EasyPermissions . requestPermissions ( this , " We require location access!!! " ,
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-03-31 21:31:40 +02:00
public void setExternGnss ( final String str ) {
2022-03-27 22:51:53 +02:00
Runnable myRun = new Runnable ( ) {
public void run ( ) {
2022-03-31 21:31:40 +02:00
final TextView v = ( TextView ) findViewById ( R . id . externGNSSTextView ) ;
2022-03-27 22:51:53 +02:00
v . append ( str ) ;
}
} ;
runOnUiThread ( myRun ) ;
}
@SuppressLint ( " MissingPermission " )
@Override
protected void onResume ( ) {
super . onResume ( ) ;
if ( ! EasyPermissions . hasPermissions ( this , perms ) ) {
// Dummy thread ...
2022-03-31 21:31:40 +02:00
/ *
2022-03-27 22:51:53 +02:00
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 ( ) ;
2022-03-31 21:31:40 +02:00
* /
2022-03-27 22:51:53 +02:00
} else {
2022-03-31 21:31:40 +02:00
if ( locationManager ! = null ) {
locationManager . requestLocationUpdates ( LocationManager . GPS_PROVIDER , 0 , 0 , locationListener ) ;
}
2022-03-27 22:51:53 +02:00
// Worker-Thread
new Thread (
new Runnable ( ) {
@Override
public void run ( ) {
2022-03-31 21:31:40 +02:00
while ( doRun & & ( locationManager ! = null ) ) {
2022-03-30 20:44:43 +02:00
if ( locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) ! = null ) {
2022-03-30 22:27:47 +02:00
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLatNumber ) , String . format ( " %.6f " , locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLatitude ( ) ) ) ;
setEditTextLabel ( ( EditText ) findViewById ( R . id . interngnssLongNumber ) , String . format ( " %.6f " , locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLongitude ( ) ) ) ;
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 " ) ) ) ;
} else {
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 " ) ;
}
2022-03-27 22:51:53 +02:00
runOnUiThread ( new Runnable ( ) {
@Override
public void run ( ) {
//Update
}
} ) ;
try {
sleep ( 10000 ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
}
} ) . start ( ) ;
}
}
/ * *
* Stop the updates when Activity is paused
* /
@Override
protected void onPause ( ) {
super . onPause ( ) ;
doRun = false ;
}
LocationListener locationListener = new LocationListener ( ) {
@Override
public void onLocationChanged ( android . location . Location location ) {
//double latitude = location.getLatitude();
//double longitude = location.getLongitude();
//String msg = "New Latitude: " + latitude + " New Longitude: " + longitude;
Toast . makeText ( cntxToastInternGNSSUpdate , " Internal GNSS updated " , Toast . LENGTH_LONG ) . show ( ) ;
}
@Override
// This callback will never be invoked on Android Q and above.
public void onStatusChanged ( String provider , int status , Bundle extras ) {
// Toast.makeText(mContext, "Status Changed", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled ( String provider ) {
}
@Override
public void onProviderDisabled ( String provider ) {
}
} ;
}