Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!WIP! Refactor map fragment #85

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
public class HomeFragment extends Fragment implements View.OnClickListener {

// TODO Should this be defined in the Singleton GlobalState, since these are global constants?
public final static String[] EXTRA_OPTIONS = { "com.wikijourney.wikijourney.MAX_POI",
"com.wikijourney.wikijourney.RANGE",
"com.wikijourney.wikijourney.PLACE",
"com.wikijourney.wikijourney.METHOD" };
public final static class MapOption {
public final static String MAX_POI = "com.wikijourney.wikijourney.MAX_POI";
public final static String RANGE = "com.wikijourney.wikijourney.RANGE";
public final static String PLACE = "com.wikijourney.wikijourney.PLACE";
public final static String METHOD = "com.wikijourney.wikijourney.METHOD";
}

public final static int METHOD_AROUND = 0;
public final static int METHOD_PLACE = 1;

private LocationManager locationManager;


public HomeFragment() {
// Required empty public constructor
}
Expand Down Expand Up @@ -125,34 +127,34 @@ private void goMap(View pView, int method) {
EditText maxPOIInput = (EditText)pView.findViewById(R.id.input_maxPOI);
try {
int maxPOI = Integer.parseInt(maxPOIInput.getText().toString());
args.putInt(EXTRA_OPTIONS[0], maxPOI);
args.putInt(MapOption.MAX_POI, maxPOI);
} catch (NumberFormatException e) {
args.putInt(EXTRA_OPTIONS[0], res.getInteger(R.integer.default_maxPOI)); //TODO : Let the user fix this default value thanks to Options Menu
args.putInt(MapOption.MAX_POI, res.getInteger(R.integer.default_maxPOI)); //TODO : Let the user fix this default value thanks to Options Menu
}

//We find the range value
EditText rangeInput = (EditText)pView.findViewById(R.id.input_range);
try {
double range = Double.parseDouble(rangeInput.getText().toString());
args.putDouble(EXTRA_OPTIONS[1], range);
args.putDouble(MapOption.RANGE, range);
} catch (NumberFormatException e) {
args.putDouble(EXTRA_OPTIONS[1], res.getInteger(R.integer.default_range)); //TODO : Let the user fix this default value thanks to Options Menu
args.putDouble(MapOption.RANGE, res.getInteger(R.integer.default_range)); //TODO : Let the user fix this default value thanks to Options Menu
}

//If mode is around a place, we get the place
if(method == METHOD_PLACE) {
EditText placeInput = (EditText) pView.findViewById(R.id.input_place);
try {
String place = placeInput.getText().toString();
args.putString(EXTRA_OPTIONS[2], place);
args.putString(MapOption.PLACE, place);
} catch (NumberFormatException e) {
args.putString(EXTRA_OPTIONS[2], "");
args.putString(MapOption.PLACE, "");
}
}
else
args.putString(EXTRA_OPTIONS[2], "");
args.putString(MapOption.PLACE, "");

args.putInt(EXTRA_OPTIONS[3], method);
args.putInt(MapOption.METHOD, method);

//We hide the keyboard
Utils.hideKeyboard(getActivity(), getActivity().getCurrentFocus());
Expand Down
103 changes: 33 additions & 70 deletions app/src/main/java/com/wikijourney/wikijourney/views/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public class MapFragment extends Fragment {
private Snackbar locatingSnackbar;
private Snackbar downloadSnackbar;


public MapFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -99,34 +94,24 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// We get the Bundle values
Bundle args = getArguments();

try {
paramMaxPoi = args.getInt(HomeFragment.EXTRA_OPTIONS[0]);
} catch (Exception e) {
paramMaxPoi = getResources().getInteger(R.integer.default_maxPOI);
}
try {
paramRange = args.getDouble(HomeFragment.EXTRA_OPTIONS[1]);
} catch (Exception e) {
paramRange = getResources().getInteger(R.integer.default_range);
}
try {
paramPlace = args.getString(HomeFragment.EXTRA_OPTIONS[2]);
} catch (Exception e) {
paramPlace = "null"; // Place value
}
try {
paramMethod = args.getInt(HomeFragment.EXTRA_OPTIONS[3]);
} catch (Exception e) {
paramMethod = HomeFragment.METHOD_AROUND;
}
// Catch each parameters given by the Intent
paramMaxPoi = args.getInt(HomeFragment.MapOption.MAX_POI,
getResources().getInteger(R.integer.default_maxPOI));

if (paramMethod == HomeFragment.METHOD_AROUND) {
locateUser();
paramRange = args.getDouble(HomeFragment.MapOption.RANGE,
getResources().getInteger(R.integer.default_range));

} else if(paramMethod == HomeFragment.METHOD_PLACE) {
drawMap(paramPlace);
}
paramPlace = args.getString(HomeFragment.MapOption.PLACE,
"null");

paramMethod = args.getInt(HomeFragment.MapOption.METHOD,
HomeFragment.METHOD_AROUND);

if (paramMethod == HomeFragment.METHOD_PLACE) {
drawPOIOnMap(paramPlace);
} else {
locateUser();
}
return view;
}

Expand All @@ -139,7 +124,6 @@ private void locateUser() {

// Acquire a reference to the system Location Manager
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// Define a listener that responds to location updates
locationListener = new LocationListener() {
Expand All @@ -148,14 +132,10 @@ public void onLocationChanged(Location location) {
if (locatingSnackbar != null) {
locatingSnackbar.dismiss();
}
// TODO Temporary fix
// This stop the location updates, so the map doesn't always refresh
// locationManager.removeUpdates(locationListener);
isUserLocatedOnce = true;
drawCurrentLocation(location);
if (!isUserLocatedOnce) {
isUserLocatedOnce = true;
drawMap();
}
drawPOIOnMap();
}

public void onStatusChanged(String provider, int status, Bundle extras) {
Expand All @@ -169,8 +149,7 @@ public void onProviderDisabled(String provider) {
};

// Register the listener with the Location Manager to receive location updates
// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
}

@Override
Expand Down Expand Up @@ -199,7 +178,7 @@ private void drawCurrentLocation(Location location) {

// This starts the map at the desired point
userLocation = new GeoPoint(location);
if (!isUserLocatedOnce) {
if (isUserLocatedOnce) {
mapController.setCenter(userLocation);
}

Expand All @@ -208,9 +187,6 @@ private void drawCurrentLocation(Location location) {
userLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(userLocationMarker);

// And we have to use this to refresh the map
// map.invalidate();

// We can change some properties of the marker (don't forget to refresh the map !!)
userLocationMarker.setInfoWindow(new CustomInfoWindow(map));
Drawable icon = null;
Expand All @@ -226,12 +202,7 @@ private void drawCurrentLocation(Location location) {
}
}

private void drawMap() {
// We get the POI around the user with WikiJourney API
String url;
url = gs.API_URL + "long=" + userLocation.getLongitude() + "&lat=" + userLocation.getLatitude()
+ "&maxPOI=" + paramMaxPoi + "&range=" + paramRange + "&lg=" + language;

private void downloadPOIOnMap(String url, int method) {
// Check if the Internet is up
final ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
Expand All @@ -245,14 +216,23 @@ private void drawMap() {
downloadSnackbar = Snackbar.make(getActivity().findViewById(R.id.fragment_container), R.string.snackbar_downloading, Snackbar.LENGTH_INDEFINITE);
downloadSnackbar.show();
}
new DownloadWjApi(url, HomeFragment.METHOD_AROUND, context, mapFragment).invoke(false);
new DownloadWjApi(url, method, context, mapFragment).invoke(false);

} else {
UI.openPopUp(mapFragment.getActivity(), getResources().getString(R.string.error_activate_internet_title), getResources().getString(R.string.error_activate_internet));
}
}
private void drawMap(String paramPlace) {

private void drawPOIOnMap() {
// We get the POI around the user with WikiJourney API
String url = GlobalState.API_URL + "long=" + userLocation.getLongitude() + "&lat=" + userLocation.getLatitude()
+ "&maxPOI=" + paramMaxPoi + "&range=" + paramRange + "&lg=" + language;

downloadPOIOnMap(url, HomeFragment.METHOD_AROUND);
}

private void drawPOIOnMap(String paramPlace) {
// We get the POI around given place with WikiJourney API
String url;
String encodedPlace = "";
try { // https://stackoverflow.com/a/10786112/3641865
Expand All @@ -263,24 +243,7 @@ private void drawMap(String paramPlace) {
url = gs.API_URL + "place=" + encodedPlace + "&maxPOI=" + paramMaxPoi + "&range="
+ paramRange + "&lg=" + language;

// Check if the Internet is up
final ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
// These are needed for the download library and the methods called later
final Context context = this.getActivity();
final MapFragment mapFragment = this;

if (networkInfo != null && networkInfo.isConnected()) {
// Show a Snackbar while we wait for WikiJourney server, so the user doesn't think the app crashed
if (getActivity().findViewById(R.id.fragment_container) != null) {
downloadSnackbar = Snackbar.make(getActivity().findViewById(R.id.fragment_container), R.string.snackbar_downloading, Snackbar.LENGTH_INDEFINITE);
downloadSnackbar.show();
}
new DownloadWjApi(url, HomeFragment.METHOD_PLACE, context, mapFragment).invoke(false);

} else {
UI.openPopUp(mapFragment.getActivity(), getResources().getString(R.string.error_activate_internet_title), getResources().getString(R.string.error_activate_internet));
}
downloadPOIOnMap(url, HomeFragment.METHOD_PLACE);
}

private class DownloadWjApi {
Expand All @@ -306,7 +269,7 @@ public void invoke(boolean useSelfSignedSSL) {
client.setSSLSocketFactory(sf);
}
catch (Exception e) {
// Empty catch, what should we put here?
Log.e("DownloadWjApi", e.toString());
}
}
client.setTimeout(30_000); // Set timeout to 30s, the server may be slow...
Expand Down