display user location on iitcm (see #179)

This commit is contained in:
Philipp Schaefer
2013-04-28 22:46:18 +02:00
parent 1048ce28eb
commit 28233b8118
7 changed files with 164 additions and 4 deletions

View File

@ -4,6 +4,9 @@ import java.io.IOException;
import com.cradle.iitc_mobile.R;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
@ -29,6 +32,9 @@ public class IITC_Mobile extends Activity {
private boolean desktop = false;
private OnSharedPreferenceChangeListener listener;
private String intel_url = "https://www.ingress.com/intel";
private boolean user_loc = false;
private LocationManager loc_mngr = null;
private LocationListener loc_listener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -45,11 +51,38 @@ public class IITC_Mobile extends Activity {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("pref_force_desktop"))
desktop = sharedPreferences.getBoolean("pref_force_desktop", false);
if (key.equals("pref_user_loc")) {
user_loc = sharedPreferences.getBoolean("pref_user_loc", false);
}
IITC_Mobile.this.loadUrl(intel_url);
}
};
sharedPref.registerOnSharedPreferenceChangeListener(listener);
// Acquire a reference to the system Location Manager
loc_mngr = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
loc_listener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
drawMarker(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
user_loc = sharedPref.getBoolean("pref_user_loc", false);
if (user_loc == true) {
// Register the listener with the Location Manager to receive location updates
loc_mngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, loc_listener);
loc_mngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc_listener);
}
// load new iitc web view with ingress intel page
Intent intent = getIntent();
String action = intent.getAction();
@ -78,6 +111,12 @@ public class IITC_Mobile extends Activity {
Log.d("iitcm", "resuming...setting reset idleTimer");
iitc_view.loadUrl("javascript: window.idleTime = 0");
iitc_view.loadUrl("javascript: window.renderUpdateStatus()");
if (user_loc == true) {
// Register the listener with the Location Manager to receive location updates
loc_mngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, loc_listener);
loc_mngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc_listener);
}
}
@Override
@ -107,6 +146,10 @@ public class IITC_Mobile extends Activity {
}
}
Log.d("iitcm", "stopping iitcm");
if (user_loc == true)
loc_mngr.removeUpdates(loc_listener);
super.onStop();
}
@ -185,6 +228,7 @@ public class IITC_Mobile extends Activity {
}
}
// vp=f enables desktop mode...vp=m is the defaul mobile view
private String addUrlParam(String url) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
this.desktop = sharedPref.getBoolean("pref_force_desktop", false);
@ -195,6 +239,8 @@ public class IITC_Mobile extends Activity {
return (url + "?vp=m");
}
// inject the iitc-script and load the intel url
// plugins are injected onPageFinished
public void loadUrl(String url) {
url = addUrlParam(url);
Log.d("iitcm", "injecting js...");
@ -202,4 +248,16 @@ public class IITC_Mobile extends Activity {
Log.d("iitcm", "loading url: " + url);
iitc_view.loadUrl(url);
}
}
// update the user location marker on the map
public void drawMarker(Location loc) {
Log.d("iitcm", "update location..." + loc.toString());
// throw away all positions with accuracy > 100 meters
// should avoid gps glitches
if (loc.getAccuracy() < 100) {
iitc_view.loadUrl("javascript: " +
"window.plugin.userLocation.updateLocation( " +
loc.getLatitude() + ", " + loc.getLongitude() + ");");
}
}
}