Skip to content

Commit

Permalink
Update location update behaviour. Change app version to 0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vshkl committed May 21, 2017
1 parent 1d099d6 commit f30825a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "by.vshkl.translate2"
minSdkVersion 19
targetSdkVersion 25
versionCode 2
versionName "0.5.0"
versionCode 3
versionName "0.5.1"
vectorDrawables.useSupportLibrary = true
resConfigs "en", "ru", "be"
buildConfigField("String", "ENCRYPTED_PREFERENCES_PWD", ENCRYPTED_PREFERENCES_PWD)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-feature android:name="android.hardware.location.network"/>
<uses-feature android:name="android.hardware.location.gps"/>

<application
android:name=".App"
android:allowBackup="false"
Expand Down
67 changes: 45 additions & 22 deletions app/src/main/java/by/vshkl/translate2/ui/activity/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -44,6 +45,7 @@
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
Expand Down Expand Up @@ -99,7 +101,7 @@
public class MapActivity extends MvpAppCompatActivity implements MapView, ConnectionCallbacks, OnMapReadyCallback,
OnMapClickListener, OnMarkerClickListener, OnQueryChangeListener, OnSearchListener, OnBindSuggestionCallback,
OnFocusChangeListener, OnMenuItemClickListener, OnDrawerItemClickListener, OnDrawerItemLongClickListener,
StopBookmarkListener, AppUpdateListener {
StopBookmarkListener, AppUpdateListener, LocationListener {

private static final String TAG = "MapActivity";

Expand All @@ -119,6 +121,7 @@ public class MapActivity extends MvpAppCompatActivity implements MapView, Connec
private BottomSheetBehavior bottomSheetBehavior;
private Drawer ndStopBookmarks;
private boolean firstConnect = true;
private boolean requestLocationUpdates = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -131,6 +134,7 @@ protected void onCreate(Bundle savedInstanceState) {
initializeGoogleApiClient();
initializeNavigationDrawer();
initializeSearchView();

}

@Override
Expand All @@ -141,6 +145,7 @@ protected void onResume() {

@Override
protected void onPause() {
stopLocationUpdates();
googleApiClient.disconnect();
super.onPause();
}
Expand Down Expand Up @@ -174,6 +179,7 @@ public void onBackPressed() {

@OnClick(R.id.fb_location)
void onLocationClicked() {
requestLocationUpdates = true;
MapActivityPermissionsDispatcher.updateCoordinatesWithCheck(this);
}

Expand All @@ -190,7 +196,7 @@ void onBookmarkClicked() {
public void onConnected(@Nullable Bundle bundle) {
if (firstConnect) {
firstConnect = false;
showUserLocation();
startLocationUpdates();
}
}

Expand Down Expand Up @@ -222,6 +228,19 @@ public boolean onMarkerClick(Marker marker) {
return false;
}

@Override
public void onLocationChanged(Location location) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, Constants.ZOOM_POSITION));
}

@Override
public void onSearchTextChanged(String oldQuery, String newQuery) {
if (!oldQuery.isEmpty() && newQuery.isEmpty()) {
Expand Down Expand Up @@ -345,13 +364,18 @@ public void showToast(int messageId) {
@Override
public void placeMarkers(final List<Stop> stops) {
if (map != null) {
map.setOnCameraMoveListener(() -> addItemsToMap(stops, map.getCameraPosition().zoom));
map.setOnCameraMoveListener(() -> {
addItemsToMap(stops, map.getCameraPosition().zoom);
requestLocationUpdates = false;
stopLocationUpdates();
});
}
}

@Override
public void showSelectedStop(final Stop stop, final boolean bookmarked, final boolean fromNavDrawer) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(stop.getLatitude(), stop.getLongitude()), Constants.ZOOM_POSITION),
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(stop.getLatitude(), stop.getLongitude()),
Constants.ZOOM_POSITION),
new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
Expand Down Expand Up @@ -402,7 +426,7 @@ void updateCoordinates() {
boolean hasProvider = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
|| lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (hasProvider) {
showUserLocation();
startLocationUpdates();
} else {
turnOnLocation();
}
Expand Down Expand Up @@ -518,23 +542,6 @@ private void setupMap() {
map.setOnMarkerClickListener(this);
}

private void showUserLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}

if (googleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, getLocationRequest(), location -> {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
map.setMyLocationEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, Constants.ZOOM_POSITION));
});
}
}

private void loadWebView(String url) {
CookieManager.getInstance().setCookie("", CookieUtils.getCookies(getApplicationContext()));
wvDashboard.clearHistory();
Expand Down Expand Up @@ -622,6 +629,22 @@ private void turnOnLocation() {
});
}

private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
if (googleApiClient.isConnected() && requestLocationUpdates) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, getLocationRequest(), this);
}
}

private void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}

private LocationRequest getLocationRequest() {
if (locationRequest == null) {
locationRequest = LocationRequest.create();
Expand Down

0 comments on commit f30825a

Please sign in to comment.