Skip to content

Instantly share code, notes, and snippets.

@vudunguit
Created September 26, 2016 08:35
Show Gist options
  • Save vudunguit/6fe23db92a33475bd4c3836d41e4ed4b to your computer and use it in GitHub Desktop.
Save vudunguit/6fe23db92a33475bd4c3836d41e4ed4b to your computer and use it in GitHub Desktop.
LocationHelper is a class help deal with location service
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
/**
* Created by CUSDungVT on 9/16/2016.
*/
public class LocationHelper {
private Activity mActivity;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private OnLocationCallback mOnLocationCallback;
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "LocationListener => onLocationChanged");
mOnLocationCallback.onLoadWithLocation(location);
}
};
private GoogleApiClient.ConnectionCallbacks mConnectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.e(TAG, "GoogleApiClient => onConnected");
try {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
mOnLocationCallback.onLoadWithLocation(location);
}
} catch (SecurityException e) {
e.printStackTrace();
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, "GoogleApiClient => onConnectionSuspended");
mGoogleApiClient.connect();
}
};
private GoogleApiClient.OnConnectionFailedListener mOnConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "GoogleApiClient => onConnectionFailed");
}
};
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public static final String TAG = LocationHelper.class.getSimpleName();
public LocationHelper(Activity activity, OnLocationCallback onLocationCallback){
this.mActivity = activity;
this.mOnLocationCallback = onLocationCallback;
}
public void start(){
if(mGoogleApiClient != null && !mGoogleApiClient.isConnected()){
mGoogleApiClient.connect();
}
}
public void stop(){
// only stop if it's connected, otherwise we crash
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
// Disconnecting the client invalidates it.
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationListener);
mGoogleApiClient.disconnect();
}
}
private void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationListener);
}
public void init(){
buildGoogleApiClient();
createLocationRequest(
UPDATE_INTERVAL_IN_MILLISECONDS,
FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS,
LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
private synchronized void buildGoogleApiClient() {
Log.e(TAG, "Building GoogleApiClient");
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(mConnectionCallbacks)
.addOnConnectionFailedListener(mOnConnectionFailedListener)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
private void createLocationRequest(long interval, long fastestInterval, int priority) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(interval);
mLocationRequest.setFastestInterval(fastestInterval);
mLocationRequest.setPriority(priority);
}
public boolean isLocationEnabled() {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(mActivity.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(mActivity.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
public void openSettingsScreen() {
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mActivity.startActivity(viewIntent);
}
public interface OnLocationCallback{
void onLoadWithLocation(Location location);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment