Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. public class GoogleMapFragment extends BaseFragment implements OnMapReadyCallback,
  2. View.OnClickListener, MapContract.View, GoogleMap.OnMyLocationButtonClickListener,
  3. GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
  4. ResultCallback, OnCompleteListener<LocationSettingsResponse>, SearchView.OnQueryTextListener {
  5.  
  6. private static final String TAG = GoogleMapFragment.class.getSimpleName();
  7.  
  8. MapPresenter presenter;
  9.  
  10. GoogleMap mGoogleMap;
  11. protected LocationRequest mLocationRequest;
  12. protected GoogleApiClient mGoogleApiClient;
  13.  
  14. @Override
  15. protected int getLayout() { return R.layout.fragment_google_map; }
  16.  
  17. @Override
  18. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  19. super.onViewCreated(view, savedInstanceState);
  20.  
  21. getActivity().setTitle("Mapa");
  22.  
  23. // Setting up map
  24. SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
  25. .findFragmentById(R.id.map);
  26.  
  27. if (mapFragment == null) {
  28. FragmentManager fragmentManager = getFragmentManager();
  29. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  30. mapFragment = SupportMapFragment.newInstance();
  31. fragmentTransaction.replace(R.id.map, mapFragment).commit();
  32. }
  33. mapFragment.getMapAsync(this);
  34. }
  35.  
  36. @Override
  37. public void setUpGoogleApiClient() {
  38. mGoogleApiClient = new GoogleApiClient.Builder(getContext())
  39. .addApi(LocationServices.API)
  40. .addConnectionCallbacks(this)
  41. .addOnConnectionFailedListener(this).build();
  42. mGoogleApiClient.connect();
  43. }
  44.  
  45. @Override
  46. public void onMapReady(GoogleMap googleMap) {
  47. mGoogleMap = googleMap;
  48.  
  49. setUpGoogleApiClient();
  50. mLocationRequest = new LocationRequest();
  51. mLocationRequest.setInterval(30 * 1000);
  52. mLocationRequest.setFastestInterval(10 * 1000);
  53. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  54.  
  55. // Initialize Google Play Services
  56. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  57. if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(),
  58. Manifest.permission.ACCESS_FINE_LOCATION)
  59. == PackageManager.PERMISSION_GRANTED) {
  60. // Location Permission already granted
  61. startLocationService();
  62. mGoogleMap.setMyLocationEnabled(true);
  63. } else {
  64. // Request Location Permission
  65. checkLocationPermission();
  66. }
  67. }
  68. else {
  69. startLocationService();
  70. mGoogleMap.setMyLocationEnabled(true);
  71. }
  72. }
  73.  
  74. @Override
  75. public void onConnected(@Nullable Bundle bundle) {
  76. LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
  77. .addLocationRequest(mLocationRequest);
  78. builder.setAlwaysShow(true);
  79.  
  80. Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
  81. task.addOnCompleteListener(this);
  82. }
  83.  
  84. @Override
  85. public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
  86. try {
  87. LocationSettingsResponse response = task.getResult(ApiException.class);
  88. } catch (ApiException exception) {
  89. switch (exception.getStatusCode()) {
  90. case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
  91. try {
  92. // Cast to a resolvable exception.
  93. ResolvableApiException resolvable = (ResolvableApiException) exception;
  94. // Show the dialog by calling startResolutionForResult(),
  95. // and check the result in onActivityResult().
  96. resolvable.startResolutionForResult(getActivity(), 101);
  97. } catch (IntentSender.SendIntentException e) {
  98. // Ignore the error.
  99. } catch (ClassCastException e) {
  100. // Ignore, should be an impossible error.
  101. }
  102. break;
  103. case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
  104. // Location settings are not satisfied. However, we have no way to fix the
  105. // settings so we won't show the dialog.
  106. break;
  107. }
  108. }
  109. }
  110.  
  111. @Override
  112. public void onConnectionSuspended(int i) {
  113. Toast.makeText(getContext(), "onConnectionSuspended", Toast.LENGTH_LONG).show();
  114. }
  115.  
  116. @Override
  117. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  118. Toast.makeText(getContext(), "onConnectionFailed", Toast.LENGTH_LONG).show();
  119. }
  120.  
  121. @Override
  122. public void onResult(@NonNull Result result) {
  123. final Status status = result.getStatus();
  124.  
  125. switch (status.getStatusCode()) {
  126. case LocationSettingsStatusCodes.SUCCESS:
  127. // No need to show the dialog;
  128. break;
  129. case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
  130. // GPS turned off, show the user a dialog
  131. try {
  132. // Show the dialog and check the result
  133. status.startResolutionForResult(getActivity(), REQUEST_CHECK_SETTINGS);
  134. } catch (IntentSender.SendIntentException e) {
  135. // Failed to show dialog
  136. }
  137. break;
  138. case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
  139. // Location settings are unavailable, so not possible to show any dialog now
  140. break;
  141. }
  142. }
  143.  
  144. @Override
  145. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  146. final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
  147. switch (requestCode) {
  148. case 101:
  149. switch (resultCode) {
  150. case Activity.RESULT_OK:
  151. // All required changes were successfully made
  152. Toast.makeText(getActivity(),states.isLocationPresent() + "", Toast.LENGTH_SHORT).show();
  153. break;
  154. case Activity.RESULT_CANCELED:
  155. // The user was asked to change settings, but chose not to
  156. Toast.makeText(getActivity(),"Canceled", Toast.LENGTH_SHORT).show();
  157. break;
  158. default:
  159. break;
  160. }
  161. break;
  162. }
  163. }
  164.  
  165. private void checkLocationPermission() {
  166. if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
  167. != PackageManager.PERMISSION_GRANTED) {
  168. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  169. MY_PERMISSIONS_REQUEST_LOCATION);
  170. } else {
  171. startLocationService();
  172. startTrackService();
  173. }
  174. }
  175.  
  176. private void startTrackService() {
  177. if (getActivity() != null) {
  178. getActivity().startService(new Intent(getContext(), TrackerService.class));
  179. }
  180. }
  181.  
  182. private void startLocationService() {
  183. if (getActivity() != null) {
  184. getActivity().startService(new Intent(getContext(), LocationService.class));
  185. }
  186. }
  187.  
  188. @Override
  189. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  190. @NonNull int[] grantResults) {
  191. switch (requestCode) {
  192. case MY_PERMISSIONS_REQUEST_LOCATION: {
  193. // If request is cancelled, the result arrays are empty
  194. if (grantResults.length > 0
  195. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  196.  
  197. // Permission granted. Do the needed location-related task
  198. if (getActivity() != null && ContextCompat.checkSelfPermission(getActivity(),
  199. Manifest.permission.ACCESS_FINE_LOCATION)
  200. == PackageManager.PERMISSION_GRANTED) {
  201.  
  202. startLocationService();
  203. startTrackService();
  204. }
  205. } else {
  206. // Permission denied, display Toast message
  207. Toast.makeText(getActivity(),
  208. getResources().getText(R.string.localization_permission_denied), Toast.LENGTH_LONG)
  209. .show();
  210. }
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement