forked from organicmaps/organicmaps
[android] Fixed WiFi location provider crash on Honeycomb and above
This commit is contained in:
parent
b508730fec
commit
cee22581d8
1 changed files with 65 additions and 42 deletions
|
@ -3,8 +3,8 @@ package com.mapswithme.maps.location;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
@ -16,6 +16,7 @@ import android.content.IntentFilter;
|
|||
import android.location.Location;
|
||||
import android.net.wifi.ScanResult;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
public class WifiLocation extends BroadcastReceiver
|
||||
{
|
||||
|
@ -25,6 +26,7 @@ public class WifiLocation extends BroadcastReceiver
|
|||
{
|
||||
public void onWifiLocationUpdated(Location l);
|
||||
}
|
||||
|
||||
// @TODO support multiple listeners
|
||||
private Listener m_observer = null;
|
||||
|
||||
|
@ -63,7 +65,7 @@ public class WifiLocation extends BroadcastReceiver
|
|||
c.unregisterReceiver(this);
|
||||
|
||||
// Prepare JSON request with BSSIDs
|
||||
StringBuilder json = new StringBuilder("{\"version\":\"1.1.0\"");
|
||||
final StringBuilder json = new StringBuilder("{\"version\":\"1.1.0\"");
|
||||
|
||||
boolean wifiHeaderAdded = false;
|
||||
List<ScanResult> results = m_wifi.getScanResults();
|
||||
|
@ -92,51 +94,72 @@ public class WifiLocation extends BroadcastReceiver
|
|||
}
|
||||
json.append("}");
|
||||
|
||||
// Result for Listener
|
||||
Location location = null;
|
||||
|
||||
// Send http POST to google location service
|
||||
URL url;
|
||||
OutputStreamWriter wr = null;
|
||||
BufferedReader rd = null;
|
||||
try
|
||||
// From Honeycomb, networking calls should be always executed at non-UI
|
||||
// thread
|
||||
new AsyncTask<String, Void, Boolean>()
|
||||
{
|
||||
url = new URL(MWM_GEOLOCATION_SERVER);
|
||||
URLConnection conn = url.openConnection();
|
||||
conn.setDoOutput(true);
|
||||
wr = new OutputStreamWriter(conn.getOutputStream());
|
||||
wr.write(json.toString());
|
||||
wr.flush();
|
||||
// Get the response
|
||||
rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
||||
String line = null;
|
||||
String response = "";
|
||||
while ((line = rd.readLine()) != null) {
|
||||
response += line;
|
||||
// Result for Listener
|
||||
private Location m_location = null;
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result)
|
||||
{
|
||||
// Notify event should be called on UI thread
|
||||
if (m_observer != null && this.m_location != null)
|
||||
m_observer.onWifiLocationUpdated(this.m_location);
|
||||
m_wifi = null;
|
||||
}
|
||||
|
||||
final JSONObject jRoot = new JSONObject(response);
|
||||
final JSONObject jLocation = jRoot.getJSONObject("location");
|
||||
final double lat = jLocation.getDouble("latitude");
|
||||
final double lon = jLocation.getDouble("longitude");
|
||||
final double acc = jLocation.getDouble("accuracy");
|
||||
@Override
|
||||
protected Boolean doInBackground(String... params)
|
||||
{
|
||||
// Send http POST to google location service
|
||||
HttpURLConnection conn = null;
|
||||
try
|
||||
{
|
||||
final URL url = new URL(MWM_GEOLOCATION_SERVER);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setDoOutput(true);
|
||||
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
|
||||
wr.write(json.toString());
|
||||
wr.flush();
|
||||
// Get the response
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
||||
String line = null;
|
||||
String response = "";
|
||||
while ((line = rd.readLine()) != null)
|
||||
response += line;
|
||||
|
||||
location = new Location("wifiscanner");
|
||||
location.setAccuracy((float)acc);
|
||||
location.setLatitude(lat);
|
||||
location.setLongitude(lon);
|
||||
location.setTime(java.lang.System.currentTimeMillis());
|
||||
final JSONObject jRoot = new JSONObject(response);
|
||||
final JSONObject jLocation = jRoot.getJSONObject("location");
|
||||
final double lat = jLocation.getDouble("latitude");
|
||||
final double lon = jLocation.getDouble("longitude");
|
||||
final double acc = jLocation.getDouble("accuracy");
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
m_location = new Location("wifiscanner");
|
||||
m_location.setAccuracy((float) acc);
|
||||
m_location.setLatitude(lat);
|
||||
m_location.setLongitude(lon);
|
||||
m_location.setTime(java.lang.System.currentTimeMillis());
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (conn != null)
|
||||
conn.disconnect();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}.execute(json.toString());
|
||||
|
||||
if (m_observer != null && location != null)
|
||||
m_observer.onWifiLocationUpdated(location);
|
||||
m_wifi = null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue