LocationLayout.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. //TODO: Use db
  154. SharedPreferences preferences = v.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  155. Double lat = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LATITUDE, 0));
  156. Double lon = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LONGITUDE, 0));
  157. gmLocation = new LatLng(lat, lon);
  158. moGm = new MarkerOptions();
  159. moGm.title(v.getContext().getString(R.string.app_name));
  160. moGm.position(gmLocation);
  161. moGm.icon(BitmapDescriptorFactory.fromResource(R.drawable.pinpoint_gm));
  162. googleMap.addMarker(moGm);
  163. googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 14.0f));
  164. //Start device location requests
  165. 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)) {
  166. locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
  167. onLocationChanged(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
  168. }
  169. }
  170. private void calCulateDistance(Location userLocation){
  171. TextView text = (TextView) v.findViewById(R.id.tv_location_distance);
  172. try {
  173. Double distance = Distance.calculateDistance(userLocation.getLatitude(), userLocation.getLongitude(), gmLocation.latitude, gmLocation.longitude);
  174. if (distance <= 2) {
  175. distance = 1000 * distance;
  176. text.setText(String.format(getString(R.string.home_section_location_text_short), distance.intValue(), (int) (0.012 * distance.intValue())));
  177. } else {
  178. text.setText(String.format(getString(R.string.home_section_location_text_long), String.format(Locale.US, "%.02f", distance)));
  179. }
  180. }
  181. catch(Exception ex){
  182. Log.e("Distance error", ex.toString());
  183. text.setText("");
  184. }
  185. }
  186. /**
  187. * Called when the user location changes.
  188. * Recalculates the list of around events and calls updateLocation() to
  189. * update the distance in the location section.
  190. *
  191. * @param location The new location
  192. *
  193. * @see android.location.LocationListener#onLocationChanged(android.location.Location)
  194. */
  195. @Override
  196. public void onLocationChanged(Location location) {
  197. //TODO: Read again the GM Location.
  198. SharedPreferences preferences = v.getContext().getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
  199. if (!preferences.getString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION).equals(GM.DEFAULT_PREF_GM_LOCATION)) {
  200. Double lat = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LATITUDE, 0));
  201. Double lon = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LONGITUDE, 0));
  202. gmLocation = new LatLng(lat, lon);
  203. calCulateDistance(location);
  204. if (moGm != null){
  205. moGm.visible(true);
  206. }
  207. }
  208. }
  209. /**
  210. * Called when the location provider changes it's state.
  211. * Recalculates the list of events in the around section.
  212. *
  213. * @param provider The name of the provider
  214. * @param status Status code of the provider
  215. * @param extras Extras passed
  216. *
  217. * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
  218. */
  219. @Override
  220. public void onStatusChanged(String provider, int status, Bundle extras) {
  221. //populateAround();
  222. }
  223. /**
  224. * Called when a location provider is enabled.
  225. * Recalculates the list of events.
  226. *
  227. * @param provider The name of the provider
  228. *
  229. * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
  230. */
  231. @Override
  232. public void onProviderEnabled(String provider) {
  233. //populateAround();
  234. }
  235. /**
  236. * Called when a location provider is disabled.
  237. * Recalculates the list of events.
  238. *
  239. * @param provider The name of the provider
  240. *
  241. * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
  242. */
  243. @Override
  244. public void onProviderDisabled(String provider) {
  245. //populateAround();
  246. }
  247. }