Commit c1756dfd authored by Varun Patil's avatar Varun Patil

Locate on start if already has permission

parent 743b082c
...@@ -658,7 +658,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -658,7 +658,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
// Map // Map
MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag(MapFragment.TAG); MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag(MapFragment.TAG);
if (mapFragment != null && mapFragment.isVisible()) { if (mapFragment != null && mapFragment.isVisible()) {
MapFragment.getMainActivity().setupGPS(); MapFragment.getMainActivity().setupGPS(true);
} }
// File complaint // File complaint
......
...@@ -318,15 +318,22 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -318,15 +318,22 @@ public class MapFragment extends Fragment implements TextWatcher,
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
locate(); locate(true);
} }
}); });
// Setup GPS if already has permission
String permission = Manifest.permission.ACCESS_FINE_LOCATION;
int res = getContext().checkCallingOrSelfPermission(permission);
if (res == PackageManager.PERMISSION_GRANTED) {
locate(false);
}
} }
private void locate() { private void locate(boolean showWarning) {
followingUser = true; followingUser = true;
if (!GPSIsSetup) { if (!GPSIsSetup) {
displayLocationSettingsRequest(); displayLocationSettingsRequest(showWarning);
} else if (user != null) { } else if (user != null) {
if (!campusMapView.isAddedMarker(user)) { if (!campusMapView.isAddedMarker(user)) {
campusMapView.addMarker(user); campusMapView.addMarker(user);
...@@ -985,7 +992,7 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -985,7 +992,7 @@ public class MapFragment extends Fragment implements TextWatcher,
return slidingLayout; return slidingLayout;
} }
public void setupGPS() { public void setupGPS(boolean showWarning) {
if (getView() == null || getActivity() == null) return; if (getView() == null || getActivity() == null) return;
// Permissions stuff // Permissions stuff
if (ContextCompat.checkSelfPermission(getActivity(), if (ContextCompat.checkSelfPermission(getActivity(),
...@@ -996,8 +1003,6 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -996,8 +1003,6 @@ public class MapFragment extends Fragment implements TextWatcher,
MY_PERMISSIONS_REQUEST_LOCATION); MY_PERMISSIONS_REQUEST_LOCATION);
} else { } else {
try { try {
// Create the location request to start receiving updates // Create the location request to start receiving updates
mLocationRequest = new LocationRequest(); mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
...@@ -1019,7 +1024,10 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -1019,7 +1024,10 @@ public class MapFragment extends Fragment implements TextWatcher,
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, myLocationCallback, Looper.myLooper()); fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, myLocationCallback, Looper.myLooper());
GPSIsSetup = true; GPSIsSetup = true;
if (showWarning) {
Toast.makeText(getContext(), "WARNING: Location is in Beta. Use with Caution.", Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), "WARNING: Location is in Beta. Use with Caution.", Toast.LENGTH_LONG).show();
}
} catch (SecurityException ignored) { } catch (SecurityException ignored) {
Toast.makeText(getContext(), "No permission!", Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), "No permission!", Toast.LENGTH_LONG).show();
} }
...@@ -1030,7 +1038,7 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -1030,7 +1038,7 @@ public class MapFragment extends Fragment implements TextWatcher,
this.followingUser = followingUser; this.followingUser = followingUser;
} }
private void displayLocationSettingsRequest() { private void displayLocationSettingsRequest(final boolean showWarning) {
if (getView() == null || getActivity() == null) return; if (getView() == null || getActivity() == null) return;
LocationRequest mLocationRequest = LocationRequest.create() LocationRequest mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
...@@ -1050,7 +1058,7 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -1050,7 +1058,7 @@ public class MapFragment extends Fragment implements TextWatcher,
result.getLocationSettingsStates().isGpsUsable() && result.getLocationSettingsStates().isGpsUsable() &&
result.getLocationSettingsStates().isLocationPresent() && result.getLocationSettingsStates().isLocationPresent() &&
result.getLocationSettingsStates().isLocationUsable()) { result.getLocationSettingsStates().isLocationUsable()) {
setupGPS(); setupGPS(showWarning);
} }
} catch (ApiException ex) { } catch (ApiException ex) {
switch (ex.getStatusCode()) { switch (ex.getStatusCode()) {
...@@ -1060,7 +1068,7 @@ public class MapFragment extends Fragment implements TextWatcher, ...@@ -1060,7 +1068,7 @@ public class MapFragment extends Fragment implements TextWatcher,
(ResolvableApiException) ex; (ResolvableApiException) ex;
resolvableApiException resolvableApiException
.startResolutionForResult(getActivity(), 87); .startResolutionForResult(getActivity(), 87);
setupGPS(); setupGPS(showWarning);
} catch (IntentSender.SendIntentException e) { } catch (IntentSender.SendIntentException e) {
} }
break; break;
......
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