Last active
February 11, 2023 11:10
-
-
Save tanerjn/9b23306b25320478259802befc091d1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package connect5g.com.a5geo; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.content.ContextCompat; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.RadioGroup; | |
import android.widget.Toast; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GoogleApiAvailability; | |
import com.google.android.gms.common.api.GoogleApiClient; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationResult; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
import java.nio.file.StandardWatchEventKinds; | |
import static connect5g.com.a5geo.Main.REQUEST_DENIED; | |
import static connect5g.com.a5geo.Main.permissionsList; | |
/* Fused services is a combination of location fetching over GPS and network providers, optimal performance for battery-drain & accuracy. */ | |
class FusedLocation implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ | |
private final static long LOCATION_UPDATE_INTERVAL = 1000*3; | |
private final static long LOC_FAST_UPDATE_INTERVAL = 1000*2; | |
public int locationAccuracy = 102; | |
private final static String TAG_FUSED="FUSED"; | |
private final static String TAG_RESUME="RESUME"; | |
private final static String TAG_UPDATE="UPDATE"; | |
private final static String TAG_STOP="STOP"; | |
private final static String TAG_FAIL="FAIL"; | |
private boolean isServiceAllowed ; | |
public FusedLocationProviderClient fusedLocationProviderClient; | |
public String sFusedLatitude, sFusedLongtitude; | |
private Context context; | |
private LocationRequest fLocationRequest; | |
public LocationCallback fLocationCallback; | |
public GoogleApiClient fGoogleApiClient; | |
private Location fActualLocation; | |
private Location fActuallLocation; | |
private Main mainActivity = new Main(); | |
public FusedLocation(Context context){ | |
this.context = context; | |
if(!isGooglePlayServicesAvailable(context)){ | |
Log.e(TAG_FUSED,"play services unavailable"); | |
} else Log.i(TAG_FUSED, "play services available"); | |
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context); | |
getLocationRequest(); | |
createApiClient(); | |
fGoogleApiClient = new GoogleApiClient.Builder(context) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
fGoogleApiClient.connect(); | |
if( getFusedLocation() != null) { | |
sFusedLatitude = String.valueOf(getFusedLocation().getLatitude()); | |
sFusedLongtitude = String.valueOf(getFusedLocation().getLongitude()); | |
} | |
onStart(); | |
} | |
protected void onStart() { | |
fGoogleApiClient.connect(); | |
if(fGoogleApiClient.isConnected()){ | |
Log.d(TAG_FUSED, "api client connected"); | |
}else Log.d(TAG_FUSED, "api client disconnected"); | |
} | |
protected void onStop() { | |
// fGoogleApiClient.disconnect(); | |
Log.d(TAG_FUSED, TAG_STOP); | |
} | |
protected void onResume() { | |
if( isGooglePlayServicesAvailable(context)){ | |
Log.d(TAG_FUSED, TAG_RESUME); | |
createApiClient().connect(); | |
} | |
} | |
private void startLocationUpdates(){ | |
if(checkPermission()){ | |
fusedLocationProviderClient.requestLocationUpdates(fLocationRequest, fLocationCallback, null); | |
} | |
} | |
protected void getLocationRequest(){ | |
Log.i(TAG_FUSED, "location request"); | |
fLocationRequest = new LocationRequest(); | |
fLocationRequest.setInterval(LOCATION_UPDATE_INTERVAL); | |
fLocationRequest.setFastestInterval(LOC_FAST_UPDATE_INTERVAL); | |
fLocationRequest.setPriority(locationAccuracy); | |
/* location accuracy | |
* PRIORITY_HIGH_ACCURACY: 100 | |
* PRIORITY_BALANCED_POWER_ACCURACY: 102 | |
* PRIORITY_LOW_POWER: 104 | |
* */ | |
} | |
protected Location getActuallLocation(){ | |
if(checkPermission()) { | |
fLocationCallback = new LocationCallback(){ | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
super.onLocationResult(locationResult); | |
fActuallLocation = locationResult.getLocations().get(0); | |
} | |
}; | |
fusedLocationProviderClient.requestLocationUpdates(fLocationRequest, fLocationCallback, null); | |
Log.i(TAG_FUSED, "getLocationRequest"); | |
} | |
return fActuallLocation; | |
} | |
private boolean isGooglePlayServicesAvailable(Context context) { | |
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); | |
int result = apiAvailability.isGooglePlayServicesAvailable(context); | |
if(result != ConnectionResult.SUCCESS ){ | |
apiAvailability.getErrorDialog((Activity) context, result, 2).show(); | |
return false; | |
} | |
Log.i(TAG_FUSED, "play service accessed"); | |
return true; | |
} | |
protected Location getFusedLocation(){ | |
if( checkPermission()){ | |
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(mainActivity, new OnSuccessListener<Location>() { | |
//FIXME: LifecycleCallback with tag TaskOnStop Callback already added to this fragment. | |
@Override | |
public void onSuccess(Location location) { | |
if( location != null ){ | |
Log.i(TAG_FUSED, "fused location update"); | |
fActualLocation = location; | |
} | |
} | |
}); | |
}else{ | |
Log.e(TAG_FUSED, "No location update"); | |
} | |
return fActualLocation; | |
} | |
protected String buildFusedCoordinates(){ | |
fGoogleApiClient.connect(); | |
getLocationRequest(); | |
if((getFusedLocation())!= null) { | |
sFusedLatitude = String.valueOf(getFusedLocation().getLatitude()); | |
sFusedLongtitude = String.valueOf(getFusedLocation().getLongitude()); | |
} | |
return " "+ sFusedLatitude+" "+sFusedLongtitude; | |
} | |
public GoogleApiClient createApiClient(){ | |
/* can be in onCreate */ | |
return new GoogleApiClient.Builder(context) | |
.addApi(LocationServices.API) | |
.addConnectionCallbacks(this) | |
.addOnConnectionFailedListener(this) | |
.build(); | |
} | |
@Override | |
public void onLocationChanged(Location location) { | |
Log.d(TAG_FUSED, "Location Changed"); | |
getFusedLocation(); | |
fActualLocation = location; | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
Log.i(TAG_FUSED, "Status Changed"); | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.i(TAG_FUSED, "Provider Enabled"); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.i(TAG_FUSED, "Provider Disabled"); | |
} | |
@Override | |
public void onConnected(@Nullable Bundle bundle) { | |
Log.i(TAG_FUSED, "Connected"); | |
getFusedLocation(); | |
} | |
@Override | |
public void onConnectionSuspended(int i) { | |
Log.i(TAG_FUSED, "Connection Suspended"); | |
} | |
@Override | |
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
Log.i(TAG_FUSED, "Connection Failed"); | |
} | |
public boolean checkPermission() { | |
if (ContextCompat.checkSelfPermission(context, permissionsList[0]) == PackageManager.PERMISSION_GRANTED ){ | |
if (ContextCompat.checkSelfPermission(context, permissionsList[1]) == PackageManager.PERMISSION_GRANTED){ | |
if (ContextCompat.checkSelfPermission(context, permissionsList[2]) == PackageManager.PERMISSION_GRANTED) { | |
isServiceAllowed = true; | |
} | |
} | |
} | |
else if (ContextCompat.checkSelfPermission(context, permissionsList[0]) == PackageManager.PERMISSION_DENIED || | |
ContextCompat.checkSelfPermission(context, permissionsList[2]) == PackageManager.PERMISSION_DENIED){ | |
Toast.makeText(context, "Location share is not permitted, application will not function as supposed to.", Toast.LENGTH_LONG).show(); | |
Log.i(REQUEST_DENIED, "DENIAL OF SERVICE REQUEST."); | |
isServiceAllowed = false; | |
} | |
return isServiceAllowed; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment