Fixed refreshing location info.

master
Tomasz Półgrabia 2016-09-03 14:51:44 +02:00
parent c2eca0b662
commit 1fc6b2ad9e
4 changed files with 58 additions and 11 deletions

View File

@ -53,6 +53,11 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
private String locationProvider;
private boolean locationServicesActivated = false;
public StandardLocationListener getLocationCallback() {
return locationCallback;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
@ -81,7 +86,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragments, new HomeFragment())
.replace(R.id.fragments, new HomeFragment())
.commit();
// lLinearLayout locations = (LinearLayout) findViewById(R.id.locations);

View File

@ -5,18 +5,21 @@ import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by tpolgrabia on 28.08.16.
*/
public class StandardLocationListener implements LocationListener {
private static final String CLASS_TAG = StandardLocationListener.class.getSimpleName();
private StandardLocationListenerCallback locationChangedCallback;
private List<StandardLocationListenerCallback> locationChangedCallbacks = new ArrayList<>();
@Override
public void onLocationChanged(Location location) {
Log.i(CLASS_TAG, "Location provider changed: " + location);
if (locationChangedCallback != null) {
locationChangedCallback.callback(location);
for (StandardLocationListenerCallback callback : locationChangedCallbacks) {
callback.callback(location);
}
}
@ -35,7 +38,11 @@ public class StandardLocationListener implements LocationListener {
Log.i(CLASS_TAG, "Provider " + provider + " disabled");
}
public void setLocationChangedCallback(StandardLocationListenerCallback locationChangedCallback) {
this.locationChangedCallback = locationChangedCallback;
public void addCallback(StandardLocationListenerCallback callback) {
locationChangedCallbacks.add(callback);
}
public boolean removeCallback(StandardLocationListenerCallback callback) {
return locationChangedCallbacks.remove(callback);
}
}

View File

@ -21,6 +21,7 @@ import org.json.JSONObject;
import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener;
import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorer.dto.PanoramioImageInfo;
import pl.tpolgrabia.urbanexplorer.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorer.utils.NumberUtils;
@ -49,9 +50,7 @@ public class HomeFragment extends Fragment {
private ImageView nextWidget;
private Long photosCount;
private TextView locationsResultInfo;
private StandardLocationListener locationCallback = new StandardLocationListener();
private LocationManager locationService;
private String locationProvider = null;
public HomeFragment() {
// Required empty public constructor
@ -61,8 +60,14 @@ public class HomeFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
aq = new AQuery(getActivity());
locationProvider = LocationUtils.getDefaultLocation(getActivity());
locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
MainActivity mainActivity = ((MainActivity) getActivity());
mainActivity.getLocationCallback()
.addCallback(new StandardLocationListenerCallback() {
@Override
public void callback(Location location) {
updateLocationInfo();
}
});
}
@ -177,6 +182,7 @@ public class HomeFragment extends Fragment {
}
});
return inflatedView;
}
@ -274,4 +280,18 @@ public class HomeFragment extends Fragment {
return safeParseDouble(radiusyTextView.getText());
}
@Override
public void onResume() {
updateLocationInfo();
}
public void updateLocationInfo() {
TextView locationInfo = (TextView) getView().findViewById(R.id.locationInfo);
locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
if (currLocation != null) {
// update home fragment's location info
locationInfo.setText("Location: " + currLocation.getLatitude() + "," + currLocation.getLongitude());
}
}
}

View File

@ -18,8 +18,10 @@ import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import org.json.JSONException;
import org.json.JSONObject;
import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter;
import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
import pl.tpolgrabia.urbanexplorer.dto.WikiPage;
@ -139,15 +141,28 @@ public class WikiLocationsFragment extends Fragment {
}
});
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() {
@Override
public void callback(Location location) {
updateLocationInfo();
}
});
return inflatedView;
}
@Override
public void onResume() {
super.onResume();
updateLocationInfo();
}
public void updateLocationInfo() {
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
currentLocation.setText("Location: " + location.getLatitude() + "," + location.getLongitude());
if (location != null) {
currentLocation.setText("Location: " + location.getLatitude() + "," + location.getLongitude());
}
}
@Override