Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GoogleMapFragment extends BaseFragment implements OnMapReadyCallback,
- View.OnClickListener, MapContract.View, GoogleMap.OnMyLocationButtonClickListener,
- GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
- ResultCallback, OnCompleteListener<LocationSettingsResponse>, SearchView.OnQueryTextListener {
- private static final String TAG = GoogleMapFragment.class.getSimpleName();
- MapPresenter presenter;
- GoogleMap mGoogleMap;
- protected LocationRequest mLocationRequest;
- protected GoogleApiClient mGoogleApiClient;
- @Override
- protected int getLayout() { return R.layout.fragment_google_map; }
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- getActivity().setTitle("Mapa");
- // Setting up map
- SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
- .findFragmentById(R.id.map);
- if (mapFragment == null) {
- FragmentManager fragmentManager = getFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- mapFragment = SupportMapFragment.newInstance();
- fragmentTransaction.replace(R.id.map, mapFragment).commit();
- }
- mapFragment.getMapAsync(this);
- }
- @Override
- public void setUpGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(getContext())
- .addApi(LocationServices.API)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this).build();
- mGoogleApiClient.connect();
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mGoogleMap = googleMap;
- setUpGoogleApiClient();
- mLocationRequest = new LocationRequest();
- mLocationRequest.setInterval(30 * 1000);
- mLocationRequest.setFastestInterval(10 * 1000);
- mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- // Initialize Google Play Services
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(),
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- // Location Permission already granted
- startLocationService();
- mGoogleMap.setMyLocationEnabled(true);
- } else {
- // Request Location Permission
- checkLocationPermission();
- }
- }
- else {
- startLocationService();
- mGoogleMap.setMyLocationEnabled(true);
- }
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
- .addLocationRequest(mLocationRequest);
- builder.setAlwaysShow(true);
- Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
- task.addOnCompleteListener(this);
- }
- @Override
- public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
- try {
- LocationSettingsResponse response = task.getResult(ApiException.class);
- } catch (ApiException exception) {
- switch (exception.getStatusCode()) {
- case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
- try {
- // Cast to a resolvable exception.
- ResolvableApiException resolvable = (ResolvableApiException) exception;
- // Show the dialog by calling startResolutionForResult(),
- // and check the result in onActivityResult().
- resolvable.startResolutionForResult(getActivity(), 101);
- } catch (IntentSender.SendIntentException e) {
- // Ignore the error.
- } catch (ClassCastException e) {
- // Ignore, should be an impossible 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;
- }
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- Toast.makeText(getContext(), "onConnectionSuspended", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Toast.makeText(getContext(), "onConnectionFailed", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onResult(@NonNull Result result) {
- final Status status = result.getStatus();
- switch (status.getStatusCode()) {
- case LocationSettingsStatusCodes.SUCCESS:
- // No need to show the dialog;
- break;
- case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
- // GPS turned off, show the user a dialog
- try {
- // Show the dialog and check the result
- status.startResolutionForResult(getActivity(), REQUEST_CHECK_SETTINGS);
- } catch (IntentSender.SendIntentException e) {
- // Failed to show dialog
- }
- break;
- case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
- // Location settings are unavailable, so not possible to show any dialog now
- break;
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
- switch (requestCode) {
- case 101:
- switch (resultCode) {
- case Activity.RESULT_OK:
- // All required changes were successfully made
- Toast.makeText(getActivity(),states.isLocationPresent() + "", Toast.LENGTH_SHORT).show();
- break;
- case Activity.RESULT_CANCELED:
- // The user was asked to change settings, but chose not to
- Toast.makeText(getActivity(),"Canceled", Toast.LENGTH_SHORT).show();
- break;
- default:
- break;
- }
- break;
- }
- }
- private void checkLocationPermission() {
- if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- } else {
- startLocationService();
- startTrackService();
- }
- }
- private void startTrackService() {
- if (getActivity() != null) {
- getActivity().startService(new Intent(getContext(), TrackerService.class));
- }
- }
- private void startLocationService() {
- if (getActivity() != null) {
- getActivity().startService(new Intent(getContext(), LocationService.class));
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
- @NonNull 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) {
- // Permission granted. Do the needed location-related task
- if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(),
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- startLocationService();
- startTrackService();
- }
- } else {
- // Permission denied, display Toast message
- Toast.makeText(getActivity(),
- getResources().getText(R.string.localization_permission_denied), Toast.LENGTH_LONG)
- .show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement