Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decongest MapActivity #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 23 additions & 67 deletions library/src/main/java/io/ona/kujaku/activities/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import io.ona.kujaku.adapters.InfoWindowObject;
import io.ona.kujaku.adapters.holders.InfoWindowViewHolder;
import io.ona.kujaku.helpers.MapBoxStyleStorage;
import io.ona.kujaku.location.GoogleLocationClient;
import io.ona.kujaku.sorting.Sorter;
import io.ona.kujaku.utils.Permissions;
import io.ona.kujaku.utils.exceptions.InvalidMapBoxStyleException;
Expand All @@ -83,7 +84,7 @@
* <p>
* Created by Ephraim Kigamba - [email protected]
*/
public class MapActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
public class MapActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener{
private static final int PERMISSIONS_REQUEST_CODE = 342;
private MapView mapView;
private String currentStylePath;
Expand Down Expand Up @@ -115,7 +116,6 @@ public class MapActivity extends AppCompatActivity implements MapboxMap.OnMapCli
private double minZoom = -1;
private Bundle savedInstanceState;

private GoogleApiClient googleApiClient;
private Location lastLocation;
private boolean waitingForLocation = true;
private boolean googleApiClientInitialized = false;
Expand All @@ -124,14 +124,27 @@ public class MapActivity extends AppCompatActivity implements MapboxMap.OnMapCli
private LatLng focusedLocation;
private boolean focusedOnOfMyLocation = false;

private GoogleLocationClient googleLocationClient;

//Todo: Move reading data to another Thread

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
initializeViews();
googleLocationClient = new GoogleLocationClient(this, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
lastLocation = location;

if (waitingForLocation) {
focusOnMyLocation(mapboxMap);
waitingForLocation = false;
}
}
});

initializeViews();
screenWidth = getScreenWidth(this);

Bundle bundle = getIntentExtras();
Expand Down Expand Up @@ -168,8 +181,6 @@ private void initializeMapActivityAfterPermissionsSupplied(Bundle savedInstanceS
minZoom = bundle.getDouble(Constants.PARCELABLE_KEY_MIN_ZOOM);
}



if (stylesArray != null) {
currentStylePath = stylesArray[0];
if (currentStylePath != null && !currentStylePath.isEmpty()) {
Expand Down Expand Up @@ -265,7 +276,8 @@ public void onScroll() {
}

lastLocation = null;
initGoogleApiClient();
googleApiClientInitialized = true;
googleLocationClient.initGoogleApiClient();
focusOnMyLocationImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -424,7 +436,10 @@ public void showAlertDialog(int title, int message,
protected void onResume() {
super.onResume();
if (mapView != null) mapView.onResume();
if (googleApiClientInitialized) initGoogleApiClient();
if (googleApiClientInitialized) {
googleApiClientInitialized = true;
googleLocationClient.initGoogleApiClient();
}
}

@Override
Expand All @@ -443,7 +458,7 @@ protected void onStop() {
protected void onPause() {
super.onPause();
if (mapView != null) mapView.onPause();
disconnectGoogleApiClient();
googleLocationClient.disconnectGoogleApiClient();
}

@Override
Expand Down Expand Up @@ -755,65 +770,6 @@ private void focusOnMyLocation(@NonNull MapboxMap mapboxMap) {
}
}

private void initGoogleApiClient() {
googleApiClientInitialized = true;
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}

googleApiClient.connect();
}

private void disconnectGoogleApiClient() {
if (googleApiClient != null) {
googleApiClient.disconnect();
}
}

// GPS - Location Stuff
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

try {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
} catch (SecurityException e) {
Log.e(TAG, Log.getStackTraceString(e));
Toast.makeText(this, "Sorry but we could not get your location since the app does not have permissions to access your Location", Toast.LENGTH_LONG)
.show();
}
}

@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this, R.string.msg_location_retrieval_taking_longer_than_expected, Toast.LENGTH_LONG)
.show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, R.string.msg_could_not_find_your_location, Toast.LENGTH_LONG)
.show();
}

@Override
public void onLocationChanged(Location location) {
lastLocation = location;

if (waitingForLocation) {
waitingForLocation = false;
focusOnMyLocation(mapboxMap);
}
}

private void changeTargetIcon(int drawableIcon) {
changeDrawable(focusOnMyLocationImgBtn, drawableIcon);
}
Expand Down
109 changes: 109 additions & 0 deletions library/src/main/java/io/ona/kujaku/location/GoogleLocationClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.ona.kujaku.location;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

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;

import io.ona.kujaku.R;

/**
* Created by Ephraim Kigamba - [email protected] on 06/02/2018.
*/

public class GoogleLocationClient implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private GoogleApiClient googleApiClient;
private Location lastLocation;

private static final String TAG = GoogleLocationClient.class.getName();

private Context context;
private LocationListener locationListener;

private long locationRequestInterval = 5000;
private long locationRequestFastestInterval = 1000;

public GoogleLocationClient(@NonNull Context context, LocationListener locationListener) {
this.context = context;
this.locationListener = locationListener;
}

public void initGoogleApiClient() {
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}

googleApiClient.connect();
}

public void disconnectGoogleApiClient() {
if (googleApiClient != null) {
googleApiClient.disconnect();
}
}

// GPS - Location Stuff
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(locationRequestInterval);
locationRequest.setFastestInterval(locationRequestFastestInterval);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

try {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
} catch (SecurityException e) {
Log.e(TAG, Log.getStackTraceString(e));
Toast.makeText(context, "Sorry but we could not get your location since the app does not have permissions to access your Location", Toast.LENGTH_LONG)
.show();
}
}

@Override
public void onConnectionSuspended(int i) {
Toast.makeText(context, R.string.msg_location_retrieval_taking_longer_than_expected, Toast.LENGTH_LONG)
.show();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(context, R.string.msg_could_not_find_your_location, Toast.LENGTH_LONG)
.show();
}

@Override
public void onLocationChanged(Location location) {
lastLocation = location;

if (locationListener != null) {
locationListener.onLocationChanged(location);
}
}

public void setLocationRequestInterval(long locationRequestInterval) {
this.locationRequestInterval = locationRequestInterval;
}

public void setLocationRequestFastestInterval(long locationRequestFastestInterval) {
this.locationRequestFastestInterval = locationRequestFastestInterval;
}

public Location getLastLocation() {
return lastLocation;
}
}