2022-03-30 18:05:58 +02:00
package fmaps.grabow.com.externgnss ;
2022-03-27 22:51:53 +02:00
import android.Manifest ;
import android.annotation.SuppressLint ;
import android.content.Context ;
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 ;
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 ( ) ;
}
// 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 ( ) ;
// Get the output UI
output = ( TextView ) findViewById ( R . id . interngnss ) ;
// Test print
///printlnOnView(output, "Running!");
locationManager = ( LocationManager ) getSystemService ( Context . LOCATION_SERVICE ) ;
//List<String> providers = locationManager.getAllProviders();
//for (String temp : providers) {
// System.out.println(temp);
//printProvider(temp);
//}
} else {
// Do not have permissions, request them now
EasyPermissions . requestPermissions ( this , " We require location access!!! " ,
123 , perms ) ;
}
}
/ *
private void printProvider ( String provider ) {
LocationProvider info = locationManager . getProvider ( provider ) ;
output . append ( " Name: " + info . getName ( ) + " \ n " ) ;
output . append ( " Accuracy: " + info . getAccuracy ( ) + " \ n " ) ;
output . append ( " PowerReq.: " + info . getPowerRequirement ( ) + " \ n " ) ;
output . append ( " CellIDReq.: " + info . requiresCell ( ) + " \ n " ) ;
output . append ( " NetworkReq.: " + info . requiresNetwork ( ) + " \ n " ) ;
output . append ( " SatReq.: " + info . requiresSatellite ( ) + " \ n \ n " ) ;
}
* /
// Die Nachfolgenden Methoden können Sie aus einem Worker-Thread aufrufen
private void printlnOnView ( final TextView v , final String str ) {
Runnable myRun = new Runnable ( ) {
public void run ( ) {
v . setText ( str + " \ n " ) ;
}
} ;
runOnUiThread ( myRun ) ;
}
/ *
private void printOnView ( final TextView v , final String str ) {
Runnable myRun = new Runnable ( ) {
public void run ( ) {
v . append ( str ) ;
}
} ;
runOnUiThread ( myRun ) ;
}
public void addToString ( Location loc ) {
//Verwendet die Variablen centerLat & centerLng
String time = loc . getTime ( ) + " " ;
String latStr = Double . toString ( loc . getLatitude ( ) ) ;
String lngStr = Double . toString ( loc . getLongitude ( ) ) ;
String acc = loc . getAccuracy ( ) + " " ;
String numOfSat = loc . getExtras ( ) . getInt ( " satellites " ) + " " ;
String satStr = " " ;
String distStr = " " ;
// Bestimme die Distanz (Fix <-> Center)
Location locationA = new Location ( " Center " ) ;
locationA . setLatitude ( Double . valueOf ( centerLat ) ) ;
locationA . setLongitude ( Double . valueOf ( centerLng ) ) ;
Location locationB = new Location ( " Fix " ) ;
locationB . setLatitude ( Double . valueOf ( latStr ) ) ;
locationB . setLongitude ( Double . valueOf ( lngStr ) ) ;
distStr = locationA . distanceTo ( locationB ) + " " ;
logString = logString + time + " ; " ; logString = logString + numOfSat + " ; " ; logString = logString + distStr +
" ; " ; logString = logString + acc + " ; " ; logString = logString + latStr + " ; " ; logString = logString + lngStr
+ " ; " ; logString = logString + centerLat + " ; " ; logString = logString + centerLng + " ; " ; logString =
logString + satStr + " ; " ; logString = logString + " \ n " ;
}
* /
@SuppressLint ( " MissingPermission " )
@Override
protected void onResume ( ) {
super . onResume ( ) ;
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 ) {
// Log.i("HAG", "ok");
//
// Aufgabe 3
//
// Geben Sie via printOnView die getLastKnownLocation() von
// Network-Provider und GPS-Provider aus
//
printlnOnView ( output , " Lat: " + locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLatitude ( )
+ " Long: " + locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getLongitude ( )
+ " Accuracy: " + locationManager . getLastKnownLocation ( LocationManager . GPS_PROVIDER ) . getAccuracy ( )
) ;
//
// Aufgabe 4
//
// Erzeugen Sie eine WebView und zeigen Sie mittels der WebView
// alternierend die Location von Network und GPS auf der Karte an
//
//
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 ) {
}
} ;
}