Guest User

Untitled

a guest
Mar 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. public class BoundLocationManager extends LiveData<Location> {
  2.  
  3. private static BoundLocationManager instance;
  4. private FusedLocationProviderClient mFusedLocationClient;
  5. private LocationRequest mLocationRequest;
  6.  
  7. public static BoundLocationManager getInstance(Context appContext) {
  8. if (instance == null) {
  9. instance = new BoundLocationManager(appContext);
  10. }
  11. return instance;
  12. }
  13.  
  14. private BoundLocationManager(final Context appContext) {
  15. mFusedLocationClient = LocationServices.getFusedLocationProviderClient(appContext);
  16. createLocationRequest();
  17. mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
  18. @Override
  19. public void onComplete(@NonNull Task<Location> task) {
  20. if(task.isSuccessful() && task.getResult() != null) {
  21. setValue(task.getResult());
  22. } else {
  23. mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
  24. }
  25. }
  26. });
  27.  
  28. }
  29.  
  30. private void createLocationRequest() {
  31. mLocationRequest = new LocationRequest();
  32. mLocationRequest.setInterval(10000);
  33. mLocationRequest.setFastestInterval(5000);
  34. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  35. }
  36.  
  37. LocationCallback mLocationCallback = new LocationCallback() {
  38. @Override
  39. public void onLocationResult(LocationResult locationResult) {
  40. for (Location location : locationResult.getLocations()) {
  41. if (location != null)
  42. setValue(location);
  43. }
  44. }
  45. };
  46.  
  47. @Override
  48. protected void onInactive() {
  49. super.onInactive();
  50. if (mLocationCallback != null)
  51. mFusedLocationClient.removeLocationUpdates(mLocationCallback);
  52. }
  53.  
  54. }
  55.  
  56. private void getLocationUpdates() {
  57. BoundLocationManager.getInstance(getActivity().getApplicationContext()).observe(this, new Observer<Location>() {
  58. @Override
  59. public void onChanged(@Nullable Location location) {
  60. if (location != null) {
  61. getWeather(location.getLatitude(), location.getLongitude());
  62. getAddress(location.getLatitude(), location.getLongitude());
  63. BoundLocationManager.getInstance(getActivity().getApplicationContext()).removeObserver(this);
  64. } else
  65. Toast.makeText(getActivity(), "Location is null", Toast.LENGTH_SHORT).show();
  66. }
  67. });
  68. }
  69.  
  70. @Override
  71. public void onCreate(@Nullable Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. if(ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  74. requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
  75. else {
  76. getLocationUpdates();
  77. }
  78. }
Add Comment
Please, Sign In to add comment