Changing callbacks to EventBus.

master
Tomasz Półgrabia 2016-10-08 17:12:45 +02:00
parent 6b2c8b247e
commit b89125b1ac
7 changed files with 293 additions and 84 deletions

View File

@ -5,9 +5,12 @@ import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Toast; import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.panoramiobindings.callback.ProviderStatusCallback; import pl.tpolgrabia.panoramiobindings.callback.ProviderStatusCallback;
import pl.tpolgrabia.urbanexplorer.events.LocationChangedEventBuilder;
import pl.tpolgrabia.urbanexplorer.events.ProviderStatusChangedEventBuilder;
import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback; import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils; import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
@ -36,6 +39,10 @@ public class StandardLocationListener implements LocationListener {
} }
Toast.makeText(ctx, "Location changed " + location, Toast.LENGTH_LONG).show(); Toast.makeText(ctx, "Location changed " + location, Toast.LENGTH_LONG).show();
LocationUtils.updateLastLocationUPdate(ctx); LocationUtils.updateLastLocationUPdate(ctx);
EventBus.getDefault().post(
new LocationChangedEventBuilder()
.setLocation(location)
.build());
} }
@Override @Override
@ -51,6 +58,12 @@ public class StandardLocationListener implements LocationListener {
for (ProviderStatusCallback callback : providerStatusCallbacks){ for (ProviderStatusCallback callback : providerStatusCallbacks){
callback.callback(provider, true); callback.callback(provider, true);
} }
EventBus.getDefault().post(
new ProviderStatusChangedEventBuilder()
.setProvider(provider)
.setEnabled(true)
.build()
);
} }
@Override @Override
@ -60,6 +73,13 @@ public class StandardLocationListener implements LocationListener {
for (ProviderStatusCallback callback : providerStatusCallbacks){ for (ProviderStatusCallback callback : providerStatusCallbacks){
callback.callback(provider, false); callback.callback(provider, false);
} }
EventBus.getDefault().post(
new ProviderStatusChangedEventBuilder()
.setProvider(provider)
.setEnabled(false)
.build()
);
} }
public void addCallback(StandardLocationListenerCallback callback) { public void addCallback(StandardLocationListenerCallback callback) {

View File

@ -0,0 +1,54 @@
package pl.tpolgrabia.urbanexplorer.events;
import android.location.Location;
/**
* Created by tpolgrabia on 08.10.16.
*/
public class LocationChangedEvent {
private Location location;
private Object source;
private Long time;
public LocationChangedEvent() {
}
public LocationChangedEvent(Location location, Object source, Long time) {
this.location = location;
this.source = source;
this.time = time;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
@Override
public String toString() {
return "LocationChangedEvent{" +
"location=" + location +
", source=" + source +
", time=" + time +
'}';
}
}

View File

@ -0,0 +1,28 @@
package pl.tpolgrabia.urbanexplorer.events;
import android.location.Location;
public class LocationChangedEventBuilder {
private Location location;
private Object source;
private Long time;
public LocationChangedEventBuilder setLocation(Location location) {
this.location = location;
return this;
}
public LocationChangedEventBuilder setSource(Object source) {
this.source = source;
return this;
}
public LocationChangedEventBuilder setTime(Long time) {
this.time = time;
return this;
}
public LocationChangedEvent build() {
return new LocationChangedEvent(location, source, time);
}
}

View File

@ -0,0 +1,53 @@
package pl.tpolgrabia.urbanexplorer.events;
/**
* Created by tpolgrabia on 08.10.16.
*/
public class ProviderStatusChangedEvent {
private boolean enabled;
private Object source;
private Long time;
private String provider;
public ProviderStatusChangedEvent() {
}
public ProviderStatusChangedEvent(boolean enabled, Object source, Long time, String provider) {
this.enabled = enabled;
this.source = source;
this.time = time;
this.provider = provider;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
}

View File

@ -0,0 +1,32 @@
package pl.tpolgrabia.urbanexplorer.events;
public class ProviderStatusChangedEventBuilder {
private boolean enabled;
private Object source;
private Long time;
private String provider;
public ProviderStatusChangedEventBuilder setEnabled(boolean enabled) {
this.enabled = enabled;
return this;
}
public ProviderStatusChangedEventBuilder setSource(Object source) {
this.source = source;
return this;
}
public ProviderStatusChangedEventBuilder setTime(Long time) {
this.time = time;
return this;
}
public ProviderStatusChangedEventBuilder setProvider(String provider) {
this.provider = provider;
return this;
}
public ProviderStatusChangedEvent build() {
return new ProviderStatusChangedEvent(enabled, source, time, provider);
}
}

View File

@ -1,16 +1,13 @@
package pl.tpolgrabia.urbanexplorer.fragments; package pl.tpolgrabia.urbanexplorer.fragments;
import android.content.Intent;
import android.location.Location; import android.location.Location;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -22,11 +19,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.googleutils.callback.LocationGeoCoderCallback; import pl.tpolgrabia.googleutils.callback.LocationGeoCoderCallback;
import pl.tpolgrabia.googleutils.constants.GooglePlacesConstants; import pl.tpolgrabia.googleutils.constants.GooglePlacesConstants;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhoto;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult; import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import pl.tpolgrabia.googleutils.utils.GeocoderUtils; import pl.tpolgrabia.googleutils.utils.GeocoderUtils;
import pl.tpolgrabia.googleutils.utils.PlacesUtils; import pl.tpolgrabia.googleutils.utils.PlacesUtils;
import pl.tpolgrabia.panoramiobindings.callback.ProviderStatusCallback;
import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
@ -34,9 +29,10 @@ import pl.tpolgrabia.urbanexplorer.adapters.PlacesAdapter;
import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesRequest; import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesRequest;
import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesResponse; import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesResponse;
import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesState; import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesState;
import pl.tpolgrabia.urbanexplorer.events.LocationChangedEvent;
import pl.tpolgrabia.urbanexplorer.handlers.GooglePlacesLongClickItemHandler;
import pl.tpolgrabia.urbanexplorer.handlers.GooglePlacesScrollListener; import pl.tpolgrabia.urbanexplorer.handlers.GooglePlacesScrollListener;
import pl.tpolgrabia.urbanexplorer.worker.GooglePlacesWorker; import pl.tpolgrabia.urbanexplorer.worker.GooglePlacesWorker;
import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent; import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent;
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils; import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils; import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils;
@ -45,7 +41,6 @@ import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -64,7 +59,6 @@ public class PlacesFragment extends Fragment {
private Semaphore semaphore = new Semaphore(1); private Semaphore semaphore = new Semaphore(1);
private boolean noMoreResults = false; private boolean noMoreResults = false;
private final Pattern pattern = Pattern.compile(".*href=\"(.*)\".*");
public PlacesFragment() { public PlacesFragment() {
// Required empty public constructor // Required empty public constructor
@ -84,34 +78,7 @@ public class PlacesFragment extends Fragment {
final View inflatedView = inflater.inflate(R.layout.fragment_places, container, false); final View inflatedView = inflater.inflate(R.layout.fragment_places, container, false);
final ListView placesWidget = (ListView) inflatedView.findViewById(R.id.google_places); final ListView placesWidget = (ListView) inflatedView.findViewById(R.id.google_places);
placesWidget.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { placesWidget.setOnItemLongClickListener(new GooglePlacesLongClickItemHandler(this, placesWidget));
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
GooglePlaceResult item = (GooglePlaceResult) placesWidget.getAdapter().getItem(position);
if (item.getPhotos() != null && !item.getPhotos().isEmpty()) {
GooglePlacePhoto alink = item.getPhotos().get(0);
lg.debug("Photo link: {}", alink);
final List<String> htmlAttributions = alink.getHtmlAttributions();
lg.debug("Html attributions: {}", htmlAttributions);
if (htmlAttributions != null && !htmlAttributions.isEmpty()) {
String attribute = htmlAttributions.get(0);
lg.debug("Attribute {}", attribute);
Matcher matcher = pattern.matcher(attribute);
boolean found = matcher.find();
if (found) {
String link = matcher.group(1);
lg.debug("Link: {}", link);
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
getActivity().startActivity(intent);
} else {
lg.warn("Not expected link url html attribute expression {}", attribute);
}
}
}
return false;
}
});
placesWidget.setOnScrollListener(new GooglePlacesScrollListener(this)); placesWidget.setOnScrollListener(new GooglePlacesScrollListener(this));
return inflatedView; return inflatedView;
@ -123,54 +90,6 @@ public class PlacesFragment extends Fragment {
placesUtils = new PlacesUtils(getActivity(), AppConstants.GOOGLE_API_KEY); placesUtils = new PlacesUtils(getActivity(), AppConstants.GOOGLE_API_KEY);
MainActivity mainActivity = (MainActivity) getActivity(); MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.getLocationCallback()
.addCallback(new StandardLocationListenerCallback() {
@Override
public void callback(Location location) {
lg.debug("Location changed: {}", location);
if (location == null) {
return;
}
Toast.makeText(getActivity(),
String.format("Location changed: %.3f,%.3f",
location.getLatitude(), location.getLongitude()),
Toast.LENGTH_SHORT).show();
places = null;
nextPageToken = null;
noMoreResults = false;
pageId = 0L;
fetchNearbyPlacesAndPresent(location);
}
});
mainActivity.getLocationCallback()
.addProviderCallback(new ProviderStatusCallback() {
@Override
public void callback(String provider, boolean enabled) {
lg.debug("Provider {} has changed the status to {}", provider, enabled);
if (!enabled) {
return;
}
if (getActivity() == null) {
return;
}
Location location = LocationUtils.getLastKnownLocation(getActivity());
if (location == null) {
return;
}
fetchNearbyPlacesAndPresent(location);
}
});
geocoderUtils = new GeocoderUtils(getActivity(), AppConstants.GOOGLE_API_KEY); geocoderUtils = new GeocoderUtils(getActivity(), AppConstants.GOOGLE_API_KEY);
BufferedReader br = null; BufferedReader br = null;
@ -209,6 +128,48 @@ public class PlacesFragment extends Fragment {
} }
@Subscribe
public void handleProviderStatusChanged(String provider, boolean enabled) {
lg.debug("Provider {} has changed the status to {}", provider, enabled);
if (!enabled) {
return;
}
if (getActivity() == null) {
return;
}
Location location = LocationUtils.getLastKnownLocation(getActivity());
if (location == null) {
return;
}
fetchNearbyPlacesAndPresent(location);
}
@Subscribe
public void handleLocationChanged(LocationChangedEvent event) {
Location location = event.getLocation();
lg.debug("Location changed: {}", location);
if (location == null) {
return;
}
Toast.makeText(getActivity(),
String.format("Location changed: %.3f,%.3f",
location.getLatitude(), location.getLongitude()),
Toast.LENGTH_SHORT).show();
places = null;
nextPageToken = null;
noMoreResults = false;
pageId = 0L;
fetchNearbyPlacesAndPresent(location);
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@ -373,4 +334,5 @@ public class PlacesFragment extends Fragment {
noMoreResults = false; noMoreResults = false;
fetchNearbyPlacesAndPresent(LocationUtils.getLastKnownLocation(getActivity())); fetchNearbyPlacesAndPresent(LocationUtils.getLastKnownLocation(getActivity()));
} }
} }

View File

@ -0,0 +1,60 @@
package pl.tpolgrabia.urbanexplorer.handlers;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhoto;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import pl.tpolgrabia.urbanexplorer.fragments.PlacesFragment;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by tpolgrabia on 08.10.16.
*/
public class GooglePlacesLongClickItemHandler implements AdapterView.OnItemLongClickListener {
private Logger lg = LoggerFactory.getLogger(GooglePlacesLongClickItemHandler.class);
private final Pattern GOOGLE_PLACES_URL_PATTERN = Pattern.compile(".*href=\"(.*)\".*");
private PlacesFragment placesFragment;
private final ListView placesWidget;
public GooglePlacesLongClickItemHandler(PlacesFragment placesFragment, ListView placesWidget) {
this.placesFragment = placesFragment;
this.placesWidget = placesWidget;
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
GooglePlaceResult item = (GooglePlaceResult) placesWidget.getAdapter().getItem(position);
if (item.getPhotos() != null && !item.getPhotos().isEmpty()) {
GooglePlacePhoto alink = item.getPhotos().get(0);
lg.debug("Photo link: {}", alink);
final List<String> htmlAttributions = alink.getHtmlAttributions();
lg.debug("Html attributions: {}", htmlAttributions);
if (htmlAttributions != null && !htmlAttributions.isEmpty()) {
String attribute = htmlAttributions.get(0);
lg.debug("Attribute {}", attribute);
Matcher matcher = GOOGLE_PLACES_URL_PATTERN.matcher(attribute);
boolean found = matcher.find();
if (found) {
String link = matcher.group(1);
lg.debug("Link: {}", link);
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
placesFragment.getActivity().startActivity(intent);
} else {
lg.warn("Not expected link url html attribute expression {}", attribute);
}
}
}
return true;
}
}