mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-30 22:34:02 +01:00 
			
		
		
		
	Moved out location service manager from nearby activity
This commit is contained in:
		
							parent
							
								
									9e4cc80ceb
								
							
						
					
					
						commit
						2e063a3dcf
					
				
					 7 changed files with 92 additions and 92 deletions
				
			
		|  | @ -1,11 +1,11 @@ | |||
| package fr.free.nrw.commons.nearby; | ||||
| package fr.free.nrw.commons.location; | ||||
| 
 | ||||
| public class LatLng { | ||||
| 
 | ||||
|     public final double latitude; | ||||
|     public final double longitude; | ||||
| 
 | ||||
|     LatLng(double latitude, double longitude) { | ||||
|     public LatLng(double latitude, double longitude) { | ||||
|         if(-180.0D <= longitude && longitude < 180.0D) { | ||||
|             this.longitude = longitude; | ||||
|         } else { | ||||
|  | @ -0,0 +1,76 @@ | |||
| package fr.free.nrw.commons.location; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.location.Criteria; | ||||
| import android.location.Location; | ||||
| import android.location.LocationListener; | ||||
| import android.location.LocationManager; | ||||
| import android.os.Bundle; | ||||
| import android.util.Log; | ||||
| 
 | ||||
| public class LocationServiceManager implements LocationListener { | ||||
|     public static final String TAG = "LocationServiceManager"; | ||||
|     private String provider; | ||||
|     private LocationManager locationManager; | ||||
|     private LatLng mLatestLocation; | ||||
| 
 | ||||
|     public LocationServiceManager(Context context) { | ||||
|         this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); | ||||
|         provider = locationManager.getBestProvider(new Criteria(), true); | ||||
|     } | ||||
| 
 | ||||
|     public LatLng getLatestLocation() { | ||||
|         return mLatestLocation; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Registers a LocationManager to listen for current location | ||||
|      */ | ||||
|     public void registerLocationManager() { | ||||
|         try { | ||||
|             locationManager.requestLocationUpdates(provider, 400, 1, this); | ||||
|             Location location = locationManager.getLastKnownLocation(provider); | ||||
|             //Location works, just need to 'send' GPS coords via emulator extended controls if testing on emulator | ||||
|             Log.d(TAG, "Checking for location..."); | ||||
|             if (location != null) { | ||||
|                 this.onLocationChanged(location); | ||||
|             } | ||||
|         } catch (IllegalArgumentException e) { | ||||
|             Log.e(TAG, "Illegal argument exception", e); | ||||
|         } catch (SecurityException e) { | ||||
|             Log.e(TAG, "Security exception", e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public void unregisterLocationManager() { | ||||
|         try { | ||||
|             locationManager.removeUpdates(this); | ||||
|         } catch (SecurityException e) { | ||||
|             Log.e(TAG, "Security exception", e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onLocationChanged(Location location) { | ||||
|         double currentLatitude = location.getLatitude(); | ||||
|         double currentLongitude = location.getLongitude(); | ||||
|         Log.d(TAG, "Latitude: " + String.valueOf(currentLatitude) + " Longitude: " + String.valueOf(currentLongitude)); | ||||
| 
 | ||||
|         mLatestLocation = new LatLng(currentLatitude, currentLongitude); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onStatusChanged(String provider, int status, Bundle extras) { | ||||
|         Log.d(TAG, provider + "'s status changed to " + status); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onProviderEnabled(String provider) { | ||||
|         Log.d(TAG, "Provider " + provider + " enabled"); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onProviderDisabled(String provider) { | ||||
|         Log.d(TAG, "Provider " + provider + " disabled"); | ||||
|     } | ||||
| } | ||||
|  | @ -1,30 +1,18 @@ | |||
| package fr.free.nrw.commons.nearby; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.location.Criteria; | ||||
| import android.location.Location; | ||||
| import android.location.LocationListener; | ||||
| import android.location.LocationManager; | ||||
| import android.os.Bundle; | ||||
| import android.support.v4.app.FragmentTransaction; | ||||
| import android.util.Log; | ||||
| import android.view.Menu; | ||||
| import android.view.MenuInflater; | ||||
| import android.view.MenuItem; | ||||
| 
 | ||||
| import fr.free.nrw.commons.theme.BaseActivity; | ||||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.location.LocationServiceManager; | ||||
| import fr.free.nrw.commons.theme.BaseActivity; | ||||
| 
 | ||||
| public class NearbyActivity extends BaseActivity { | ||||
| 
 | ||||
|     private MyLocationListener myLocationListener; | ||||
|     private LocationManager locationManager; | ||||
|     private String provider; | ||||
|     private Criteria criteria; | ||||
|     private LatLng mLatestLocation; | ||||
| 
 | ||||
|     private double currentLatitude, currentLongitude; | ||||
|     //private String gpsCoords; | ||||
|     private LocationServiceManager locationManager; | ||||
| 
 | ||||
|     private static final String TAG = NearbyActivity.class.getName(); | ||||
| 
 | ||||
|  | @ -35,7 +23,8 @@ public class NearbyActivity extends BaseActivity { | |||
|         if (getSupportActionBar() != null) { | ||||
|             getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||||
|         } | ||||
|         registerLocationManager(); | ||||
|         locationManager = new LocationServiceManager(this); | ||||
|         locationManager.registerLocationManager(); | ||||
| 
 | ||||
|         // Begin the transaction | ||||
|         FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | ||||
|  | @ -70,75 +59,10 @@ public class NearbyActivity extends BaseActivity { | |||
|         getSupportFragmentManager().beginTransaction() | ||||
|                 .replace(R.id.container, new NearbyListFragment()).commit(); | ||||
|     } | ||||
|     protected LatLng getmLatestLocation() { | ||||
|         return mLatestLocation; | ||||
|     } | ||||
|     /** | ||||
|      * Registers a LocationManager to listen for current location | ||||
|      */ | ||||
|     protected void registerLocationManager() { | ||||
|         locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | ||||
|         criteria = new Criteria(); | ||||
|         provider = locationManager.getBestProvider(criteria, true); | ||||
|         myLocationListener = new MyLocationListener(); | ||||
| 
 | ||||
|         try { | ||||
|             locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener); | ||||
|             Location location = locationManager.getLastKnownLocation(provider); | ||||
|             //Location works, just need to 'send' GPS coords via emulator extended controls if testing on emulator | ||||
|             Log.d(TAG, "Checking for location..."); | ||||
|             if (location != null) { | ||||
|                 myLocationListener.onLocationChanged(location); | ||||
|             } | ||||
|         } catch (IllegalArgumentException e) { | ||||
|             Log.e(TAG, "Illegal argument exception", e); | ||||
|         } catch (SecurityException e) { | ||||
|             Log.e(TAG, "Security exception", e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     protected void unregisterLocationManager() { | ||||
|         try { | ||||
|             locationManager.removeUpdates(myLocationListener); | ||||
|         } catch (SecurityException e) { | ||||
|             Log.e(TAG, "Security exception", e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Listen for user's location when it changes | ||||
|      */ | ||||
|     private class MyLocationListener implements LocationListener { | ||||
| 
 | ||||
|         @Override | ||||
|         public void onLocationChanged(Location location) { | ||||
|             currentLatitude = location.getLatitude(); | ||||
|             currentLongitude = location.getLongitude(); | ||||
|             Log.d(TAG, "Latitude: " + String.valueOf(currentLatitude) + " Longitude: " + String.valueOf(currentLongitude)); | ||||
| 
 | ||||
|             mLatestLocation = new LatLng(currentLatitude, currentLongitude); | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public void onStatusChanged(String provider, int status, Bundle extras) { | ||||
|             Log.d(TAG, provider + "'s status changed to " + status); | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public void onProviderEnabled(String provider) { | ||||
|             Log.d(TAG, "Provider " + provider + " enabled"); | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public void onProviderDisabled(String provider) { | ||||
|             Log.d(TAG, "Provider " + provider + " disabled"); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onDestroy(){ | ||||
|         super.onDestroy(); | ||||
| 
 | ||||
|         unregisterLocationManager(); | ||||
|         locationManager.unregisterLocationManager(); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,6 +1,5 @@ | |||
| package fr.free.nrw.commons.nearby; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.net.Uri; | ||||
| import android.os.AsyncTask; | ||||
|  | @ -10,12 +9,8 @@ import android.util.Log; | |||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.AdapterView; | ||||
| import android.widget.ArrayAdapter; | ||||
| import android.widget.ImageView; | ||||
| import android.widget.ListView; | ||||
| import android.widget.ProgressBar; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import java.util.Collections; | ||||
| import java.util.Comparator; | ||||
|  | @ -25,7 +20,8 @@ import butterknife.BindView; | |||
| import butterknife.ButterKnife; | ||||
| import butterknife.OnItemClick; | ||||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.utils.ResourceUtils; | ||||
| import fr.free.nrw.commons.location.LatLng; | ||||
| import fr.free.nrw.commons.location.LocationServiceManager; | ||||
| 
 | ||||
| import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween; | ||||
| import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; | ||||
|  | @ -69,7 +65,7 @@ public class NearbyListFragment extends ListFragment implements TaskListener { | |||
| 
 | ||||
|         //Check that this is the first time view is created, to avoid double list when screen orientation changed | ||||
|         if(savedInstanceState == null) { | ||||
|             mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation(); | ||||
|             mLatestLocation = new LocationServiceManager(getActivity()).getLatestLocation(); | ||||
|             nearbyAsyncTask = new NearbyAsyncTask(this); | ||||
|             nearbyAsyncTask.execute(); | ||||
|             progressBar.setVisibility(View.VISIBLE); | ||||
|  |  | |||
|  | @ -10,6 +10,8 @@ import java.net.URL; | |||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import fr.free.nrw.commons.location.LatLng; | ||||
| 
 | ||||
| public class NearbyPlaces { | ||||
| 
 | ||||
|     private static final String TAG = NearbyPlaces.class.getName(); | ||||
|  |  | |||
|  | @ -3,6 +3,8 @@ package fr.free.nrw.commons.nearby; | |||
| import android.graphics.Bitmap; | ||||
| import android.net.Uri; | ||||
| 
 | ||||
| import fr.free.nrw.commons.location.LatLng; | ||||
| 
 | ||||
| public class Place { | ||||
| 
 | ||||
|     public String name; | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ package fr.free.nrw.commons.utils; | |||
| 
 | ||||
| import java.text.NumberFormat; | ||||
| 
 | ||||
| import fr.free.nrw.commons.nearby.LatLng; | ||||
| import fr.free.nrw.commons.location.LatLng; | ||||
| 
 | ||||
| public class LengthUtils { | ||||
|     public static String formatDistanceBetween(LatLng point1, LatLng point2) { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 maskara
						maskara