LocationLayout.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package com.ivalentin.margolariak;
  2. import java.util.Locale;
  3. import com.google.android.gms.maps.CameraUpdateFactory;
  4. import com.google.android.gms.maps.GoogleMap;
  5. import com.google.android.gms.maps.MapView;
  6. import com.google.android.gms.maps.MapsInitializer;
  7. import com.google.android.gms.maps.OnMapReadyCallback;
  8. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  9. import com.google.android.gms.maps.model.LatLng;
  10. import com.google.android.gms.maps.model.MarkerOptions;
  11. import android.content.Context;
  12. import android.content.SharedPreferences;
  13. import android.content.pm.PackageManager;
  14. import android.location.Criteria;
  15. import android.location.Location;
  16. import android.location.LocationListener;
  17. import android.location.LocationManager;
  18. import android.os.Bundle;
  19. import android.app.Fragment;
  20. import android.support.v4.app.ActivityCompat;
  21. import android.util.Log;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.TextView;
  26. /**
  27. * Section that shows a map with the location of Gasteizko Margolariak.
  28. * If no recent report location, it will explain that and will show the
  29. * next scheduled activity.
  30. *
  31. * @author Iñigo Valentin
  32. *
  33. * @see Fragment
  34. * @see OnMapReadyCallback
  35. *
  36. */
  37. public class LocationLayout extends Fragment implements OnMapReadyCallback, LocationListener {
  38. //The map view
  39. private MapView mapView;
  40. private MarkerOptions moGm;
  41. private LocationManager locationManager;
  42. //Locations for the user and Gasteizko Margolariak
  43. private LatLng gmLocation;
  44. //The main View
  45. private View v;
  46. /**
  47. * Run when the fragment is inflated.
  48. *
  49. * @param inflater A LayoutInflater to manage views
  50. * @param container The container View
  51. * @param savedInstanceState Bundle containing the state
  52. *
  53. * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
  54. */
  55. @Override
  56. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
  57. //Get the view
  58. v = inflater.inflate(R.layout.fragment_layout_location, container, false);
  59. //Set up
  60. locationManager = (LocationManager) v.getContext().getSystemService(Context.LOCATION_SERVICE);
  61. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)){
  62. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
  63. }
  64. //Set the title
  65. ((MainActivity) getActivity()).setSectionTitle(v.getContext().getString(R.string.menu_location));
  66. mapView = (MapView) v.findViewById(R.id.mapview);
  67. mapView.onCreate(savedInstanceState);
  68. // Gets to GoogleMap from the MapView and does initialization stuff
  69. mapView.getMapAsync(this);
  70. //Return the view
  71. return v;
  72. }
  73. /**
  74. * Called when the fragment is bought back to the foreground.
  75. * Ensures that the map is displayed then, and activates the location manager.
  76. *
  77. * @see android.app.Fragment#onResume()
  78. */
  79. @Override
  80. public void onResume() {
  81. if (mapView != null) {
  82. mapView.onResume();
  83. }
  84. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)){
  85. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
  86. }
  87. super.onResume();
  88. }
  89. /**
  90. * Called when the fragment is paused.
  91. * Stops the location manager
  92. * @see android.app.Fragment#onPause()
  93. */
  94. @Override
  95. public void onPause(){
  96. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  97. locationManager.removeUpdates(this);
  98. }
  99. super.onPause();
  100. }
  101. /**
  102. * Called when the fragment is destroyed.
  103. * Ensures that the map is destroyed then.
  104. *
  105. * @see android.support.v4.app.Fragment#onDestroy()
  106. */
  107. @Override
  108. public void onDestroy() {
  109. super.onDestroy();
  110. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  111. locationManager.removeUpdates(this);
  112. }
  113. if (mapView != null){
  114. mapView.onResume();
  115. mapView.onDestroy();
  116. }
  117. }
  118. /**
  119. * Called in a situation of low memory.
  120. * It let's the mapView handle the situation.
  121. *
  122. * @see android.support.v4.app.Fragment#onLowMemory()
  123. */
  124. @Override
  125. public void onLowMemory() {
  126. super.onLowMemory();
  127. if (mapView != null){
  128. mapView.onResume();
  129. mapView.onLowMemory();
  130. }
  131. }
  132. /**
  133. * Called when the map is ready to be used.
  134. * Initializes it and sets the Gasteizko Margolariak marker.
  135. *
  136. * @param googleMap the map
  137. *
  138. * @see com.google.android.gms.maps.OnMapReadyCallback#onMapReady(com.google.android.gms.maps.GoogleMap)
  139. */
  140. @Override
  141. public void onMapReady(GoogleMap googleMap) {
  142. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  143. googleMap.setMyLocationEnabled(true);
  144. }
  145. googleMap.getUiSettings().setMyLocationButtonEnabled(false);
  146. // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
  147. try {
  148. MapsInitializer.initialize(this.getActivity());
  149. } catch (Exception e) {
  150. Log.e("Error initializing maps", e.toString());
  151. }
  152. //Set GM marker
  153. SharedPreferences preferences = v.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  154. Double lat = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LATITUDE, 0));
  155. Double lon = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LONGITUDE, 0));
  156. gmLocation = new LatLng(lat, lon);
  157. moGm = new MarkerOptions();
  158. moGm.title(v.getContext().getString(R.string.app_name));
  159. moGm.position(gmLocation);
  160. moGm.icon(BitmapDescriptorFactory.fromResource(R.drawable.pinpoint_gm));
  161. googleMap.addMarker(moGm);
  162. googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 14.0f));
  163. //Start device location requests
  164. if (!(ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  165. locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
  166. onLocationChanged(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
  167. }
  168. }
  169. private void calCulateDistance(Location userLocation){
  170. TextView text = (TextView) v.findViewById(R.id.tv_location_distance);
  171. try {
  172. Double distance = Distance.calculateDistance(userLocation.getLatitude(), userLocation.getLongitude(), gmLocation.latitude, gmLocation.longitude);
  173. if (distance <= 2) {
  174. distance = 1000 * distance;
  175. text.setText(String.format(getString(R.string.home_section_location_text_short), distance.intValue(), (int) (0.012 * distance.intValue())));
  176. } else {
  177. text.setText(String.format(getString(R.string.home_section_location_text_long), String.format(Locale.US, "%.02f", distance)));
  178. }
  179. }
  180. catch(Exception ex){
  181. Log.e("Distance error", ex.toString());
  182. text.setText("");
  183. }
  184. }
  185. /**
  186. * Called when the user location changes.
  187. * Recalculates the list of around events and calls updateLocation() to
  188. * update the distance in the location section.
  189. *
  190. * @param location The new location
  191. *
  192. * @see android.location.LocationListener#onLocationChanged(android.location.Location)
  193. */
  194. @Override
  195. public void onLocationChanged(Location location) {
  196. //TODO: Read again the GM Location.
  197. SharedPreferences preferences = v.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  198. if (!preferences.getString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION).equals(GM.DEFAULT_PREF_GM_LOCATION)) {
  199. Double lat = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LATITUDE, 0));
  200. Double lon = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LONGITUDE, 0));
  201. gmLocation = new LatLng(lat, lon);
  202. calCulateDistance(location);
  203. if (moGm != null){
  204. moGm.visible(true);
  205. }
  206. }
  207. }
  208. /**
  209. * Called when the location provider changes it's state.
  210. * Recalculates the list of events in the around section.
  211. *
  212. * @param provider The name of the provider
  213. * @param status Status code of the provider
  214. * @param extras Extras passed
  215. *
  216. * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
  217. */
  218. @Override
  219. public void onStatusChanged(String provider, int status, Bundle extras) {
  220. //populateAround();
  221. }
  222. /**
  223. * Called when a location provider is enabled.
  224. * Recalculates the list of events.
  225. *
  226. * @param provider The name of the provider
  227. *
  228. * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
  229. */
  230. @Override
  231. public void onProviderEnabled(String provider) {
  232. //populateAround();
  233. }
  234. /**
  235. * Called when a location provider is disabled.
  236. * Recalculates the list of events.
  237. *
  238. * @param provider The name of the provider
  239. *
  240. * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
  241. */
  242. @Override
  243. public void onProviderDisabled(String provider) {
  244. //populateAround();
  245. }
  246. }