Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
Updates location handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gstarnberger committed Jan 1, 2013
1 parent 0c3c21e commit fe96dce
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 65 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="name.starnberger.guenther.android.cbw" android:versionCode="19"
android:versionName="1.2.2" android:installLocation="auto">
package="name.starnberger.guenther.android.cbw" android:versionCode="20"
android:versionName="1.2.3" android:installLocation="auto">
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET" />
Expand Down
4 changes: 3 additions & 1 deletion src/name/starnberger/guenther/android/cbw/CBWFeedParser.java
Expand Up @@ -17,7 +17,9 @@ static ArrayList<Station> parse(InputStream istream) {
Element station = root.getChild("station");
station.setEndElementListener(new EndElementListener() {
public void end() {
stations.add((Station) currentStation.copy());
Station copiedStation = currentStation.copy();
copiedStation.updateLocation();
stations.add(copiedStation);
currentStation.clear();
}
});
Expand Down
37 changes: 8 additions & 29 deletions src/name/starnberger/guenther/android/cbw/ListStations.java
Expand Up @@ -183,23 +183,22 @@ public void run() {
Thread getLocationThread = new Thread(null, new Runnable() {
@Override
public void run() {
Location loc = getLocation();
updateCurLocation();

try {
viewStationsThread.join();

lastUpdate = System.currentTimeMillis();

if (loc == null) {
if (curLocation == null) {
SortOrder previousSortOrder = sortOrder;
sortOrder = SortOrder.ALPHABETICALLY;
runOnUiThread(updateUI);

if(previousSortOrder == SortOrder.BY_DISTANCE) {
UIshowErr(getString(R.string.error_no_gps));
}
}
if (loc != null) {
} else {
runOnUiThread(updateUI);
}
} catch (InterruptedException e) {
Expand All @@ -215,27 +214,6 @@ public void run() {
getString(R.string.progress_body), true);
}

// The station updater is a test class required to display the splash screen
// private class StationUpdater extends AsyncTask<String, Void, Object> {
// protected Void doInBackground(String... args) {
//
// // This is where you would do all the work of downloading your data
//
// updateStations();
//
// return "replace this with your data object";
// }
//
// protected void onPostExecute(Object result) {
// // Pass the result data back to the main activity
// ListStations.this.data = result;
//
// if (MyActivity.this.pd != null) {
// MyActivity.this.pd.dismiss();
// }
// }
// }

private void getStations() {
HttpClient httpclient;
HttpGet httpget;
Expand Down Expand Up @@ -282,9 +260,8 @@ private void getStations() {
}
}

private Location getLocation() {
private void updateCurLocation() {
this.curLocation = locationHelper.getLocation();
return curLocation;
}

private void UIshowErr(final String errMsg) {
Expand Down Expand Up @@ -336,8 +313,10 @@ public void run() {
Collections.sort(m_stations, stationComparator);

m_adapter.clear();
for (int i = 0; i < m_stations.size(); i++)
m_adapter.add(m_stations.get(i));
m_adapter.setCurLocation(curLocation);
for (Station station : m_stations) {
m_adapter.add(station);
}
}
m_ProgressDialog.dismiss();
hideSplash();
Expand Down
24 changes: 10 additions & 14 deletions src/name/starnberger/guenther/android/cbw/LocationHelper.java
Expand Up @@ -5,7 +5,6 @@
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
Expand Down Expand Up @@ -65,17 +64,6 @@ public synchronized Location getLocation() {
}
}

public Location getCachedLocation() {
// FIXME: Should we call updateProvider here?

if(this.provider == null) {
return null;
} else if (cachedLocation == null) {
cachedLocation = mgr.getLastKnownLocation(provider);
}
return cachedLocation;
}

@Override
public void onLocationChanged(Location location) {
callBackLocation = location;
Expand Down Expand Up @@ -104,13 +92,21 @@ public void onStatusChanged(String provider, int status, Bundle extras) {

// Must be called by containing class
public void onPause() {
mgr.removeUpdates(this);
if (waitReq != null) {
waitReq.release();
}
}

private void updateProvider() {
this.provider = mgr.getBestProvider(new Criteria(), true);
String[] providerPriority = {LocationManager.NETWORK_PROVIDER, LocationManager.GPS_PROVIDER, LocationManager.PASSIVE_PROVIDER};

for (String provider : providerPriority) {
if (mgr.isProviderEnabled(provider)) {
this.provider = provider;
return;
}
}

this.provider = null;
}
}
31 changes: 18 additions & 13 deletions src/name/starnberger/guenther/android/cbw/Station.java
Expand Up @@ -88,27 +88,32 @@ public Station copy() {
station.setBikesAvailable(bikesAvailable);
station.setBoxesAvailable(boxesAvailable);
station.setLocation(location);
station.setActive(this.isActive());

Location location = new Location(LOCATION_PROVIDER);

try {
location.setLatitude(Double.parseDouble(latitude));
location.setLongitude(Double.parseDouble(longitude));
} catch (NumberFormatException e) {
}

station.setLocation(location);

station.setLatitude(latitude);
station.setLongitude(longitude);
station.setActive(active);
return station;
}

public void clear() {
this.setStationName(null);
this.setStationDescription(null);
this.setBikesAvailable(null);
this.setBoxesAvailable(null);
this.setLocation(null);
this.setActive(false);
this.setLatitude(null);
this.setLongitude(null);
}

public void updateLocation() {
Location location = new Location(LOCATION_PROVIDER);

try {
location.setLatitude(Double.parseDouble(latitude));
location.setLongitude(Double.parseDouble(longitude));
} catch (NumberFormatException e) {
}

this.location = location;
}
}
15 changes: 9 additions & 6 deletions src/name/starnberger/guenther/android/cbw/StationAdapter.java
Expand Up @@ -19,6 +19,7 @@
class StationAdapter extends ArrayAdapter<Station> {
private ListStations listStations;
private ArrayList<Station> items;
private Location curLocation = null;

public StationAdapter(ListStations listStations, int textViewResourceId,
ArrayList<Station> items) {
Expand Down Expand Up @@ -89,15 +90,13 @@ public View getView(int position, View convertView, ViewGroup parent) {
TextView distance_num = (TextView) v.findViewById(R.id.distance_num);
TextView distance_km = (TextView) v.findViewById(R.id.distance_km);

Location loc = listStations.getLocationHelper().getCachedLocation();

float dist = 0;
if(loc != null) {
dist = loc.distanceTo(o.getLocation()) / 1000;
if(curLocation != null) {
dist = curLocation.distanceTo(o.getLocation()) / 1000;
}

if (distance_num != null) {
if(loc == null) {
if(curLocation == null) {
distance.setVisibility(View.INVISIBLE);
distance_num.setVisibility(View.INVISIBLE);
distance_km.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -126,7 +125,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}

if(active_val && (loc == null)) {
if(active_val && (curLocation == null)) {
distance_and_active_row.setVisibility(View.GONE);
} else {
distance_and_active_row.setVisibility(View.VISIBLE);
Expand All @@ -135,4 +134,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
return v;
}

public void setCurLocation(Location curLocation) {
this.curLocation = curLocation;
}
}

0 comments on commit fe96dce

Please sign in to comment.