Commit 756d9335 authored by Owais Chunawala's avatar Owais Chunawala

Add location FAB functionality

parent c2d729ee
......@@ -41,15 +41,16 @@ dependencies {
compile "net.openid:appauth:${appAuthVersion}"
testCompile "junit:junit:4.12"
compile "com.android.support:design:${supportLibVersion}"
compile "com.google.android.gms:play-services-maps:${playServicesVersion}"
compile "com.android.support:support-v4:${supportLibVersion}"
compile "com.jakewharton:butterknife:${butterKnifeVersion}"
compile "com.google.android.gms:play-services-gcm:${playServicesVersion}"
compile "com.google.android.gms:play-services-gcm:11.6.2"
annotationProcessor "com.jakewharton:butterknife-compiler:${butterKnifeVersion}"
compile "com.squareup.retrofit2:retrofit:${retrofitVersion}"
compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
compile "com.squareup.okhttp3:okhttp:${okhttpVersion}"
compile "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"
compile "com.squareup.picasso:picasso:${picassoVersion}"
compile 'com.google.android.gms:play-services-maps:11.6.2'
compile 'com.google.android.gms:play-services-location:11.6.2'
}
apply plugin: 'com.google.gms.google-services'
package in.ac.iitb.gymkhana.iitbapp.fragment;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
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;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.List;
import in.ac.iitb.gymkhana.iitbapp.R;
public class MapFragment extends Fragment implements OnMapReadyCallback {
import static android.content.Context.LOCATION_SERVICE;
public class MapFragment extends Fragment implements OnMapReadyCallback, LocationListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
SupportMapFragment gMapFragment;
GoogleMap googleMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
private FloatingActionButton locationButton;
private Location currentLocation;
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......@@ -31,12 +77,160 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
return view;
}
@Override
public void onStart(){
super.onStart();
locationButton = (FloatingActionButton) getActivity().findViewById(R.id.location_button);
locationButton.setImageResource(R.drawable.ic_my_location_black_24dp);
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN);
locationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("MapFragment", "Location button pressed" );
try {
LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
return;
}
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
getActivity(), 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
}
currentLocation = getLastKnownLocation();
if (currentLocation != null) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 17);
googleMap.animateCamera(cameraUpdate);
locationButton.getDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.colorPrimary), PorterDuff.Mode.SRC_IN);
}
} catch (Exception e){
checkLocationPermission();
Toast.makeText(getContext(), "Please turn on Location from the Settings", Toast.LENGTH_SHORT).show();
}
}
});
}
private Location getLastKnownLocation() {
//Create a location manager object instance
LocationManager mLocationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
//Get all the different providers which can give current location info
List<String> providers = mLocationManager.getProviders(true);
//Initialising the location to be null
Location bestLocation = null;
//Creating a for loop to go through all the location providers and get the location
for (String provider : providers) {
//if permission is not granted, then get the permission
if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
//Get the last known location from the data provider
Location l = mLocationManager.getLastKnownLocation(provider);
//If the last know location provided by the data provider is null then ignore the provider and move to the next one.
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
@Override
public void onMapReady(GoogleMap gMap) {
googleMap = gMap;
// googleMap.setMyLocationEnabled(true);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
checkLocationPermission();
} else {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
}
locationButton.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#FFFFFF")));
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Location Permission already granted
buildGoogleApiClient();
googleMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
} else {
buildGoogleApiClient();
googleMap.setMyLocationEnabled(true);
}
googleMap.getUiSettings().setZoomGesturesEnabled(true);
LatLngBounds iitbBounds = new LatLngBounds(new LatLng(19.1249000, 72.9046000), new LatLng(19.143522, 72.920000));
......@@ -49,4 +243,107 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
.title("Marker in IITB"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(iitb));
}
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(getActivity())
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//move map camera
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap .animateCamera(CameraUpdateFactory.zoomTo(17));
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
googleMap.setMyLocationEnabled(true);
}
}
return;
}
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
}
......@@ -15,7 +15,7 @@
<!--TODO: Change colours-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/location"
android:id="@+id/location_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
......
......@@ -8,4 +8,8 @@
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="Cancel">Cancel</string>
<string name="gps_network_not_enabled">Location is not enabled. Please turn on your location from the settings.</string>
<string name="open_location_settings">Open Location Settings</string>
</resources>
......@@ -17,4 +17,5 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true
# TODO Replace Key and put in local:properties
GOOGLE_MAPS_API_KEY=AIzaSyB72DhM4E1NW0F-_SLnYtYYvz0rT9kne-U
#GOOGLE_MAPS_API_KEY=AIzaSyB72DhM4E1NW0F-_SLnYtYYvz0rT9kne-U
GOOGLE_MAPS_API_KEY=AIzaSyC31o6h06Z36V8M0lXgkGj4Y1cmNRVV9s0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment