143 changes: 30 additions & 113 deletions src/dk/network42/osmfocus/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
Expand All @@ -56,42 +57,35 @@
import android.view.WindowManager;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class MainActivity extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener, GpsStatus.Listener,
SensorEventListener {

static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
static final int PREFERENCE_REQUEST = 9001;
static final int INVALIDATE_VIEW = 1000;
static final int POLL_NOTIFICATIONS = 1001;

private static final int LOCATION_INTERVAL = 1000; //ms
private static final float LOCATION_DISTANCE = 1f; //meters

public static final String userAgent = "OSMfocus";

private static final String TAG = "OsmFocusActivity";
public static final String PREFS_NAME = "OSMFocusPrefsFile";

LocationManager mLocationManager;
LocationClient mLocationClient;
LocationRequest mLocationRequest;

SensorManager sensorManager;
private Sensor sensorAccelerometer;
private Sensor sensorMagneticField;
private Sensor sensorMagneticField;
private float[] valuesAccelerometer;
private float[] valuesMagneticField;
private float[] matrixR;
private float[] matrixI;
private float[] matrixValues;
private MapView mapView;
double mPanLon, mPanLat;
private OsmServer mOsmServer = new OsmServer(null, "Editor");
private OsmServer mOsmServer = new OsmServer(null, userAgent);
private GestureDetectorCompat mGestureDetector;
private ScaleGestureDetector mScaleGestureDetector;
private boolean mScaleInProgress = false;
Expand Down Expand Up @@ -196,30 +190,14 @@ public void handleMessage(Message inputMessage) {
Location loc = getMostRecentKnownLocation();
if (loc != null)
this.onLocationChanged(loc);

mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000/*ms*/);
mLocationRequest.setFastestInterval(500/*ms*/);

//locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
// startActivity(intent);
//}
//Criteria criteria = new Criteria();
//mG.mLocProvider = locationManager.getBestProvider(criteria, false);
//mG.mLocProvider = locationManager.GPS_PROVIDER;
//Location location = locationManager.getLastKnownLocation(mG.mLocProvider);
//if (location != null) {
// Toast toast = Toast.makeText(getApplicationContext(), "Location provider "+mG.mLocProvider, Toast.LENGTH_SHORT);
// toast.show();
// //onLocationChanged(location);
//}

//Location location = new Location("FIXME");
//Location location = mLocationClient.getLastLocation();
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
} else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, this);
}

if (mG.mUseCompass) {
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Expand All @@ -232,15 +210,6 @@ public void handleMessage(Message inputMessage) {
matrixValues = new float[3];
}

// Initialize the location fields
//if (location != null) {
// Log.d(TAG, "Provider " + mG.mLocProvider + " has been selected.");
// onLocationChanged(location);
//} else {
//latituteField.setText("Location not available");
//longitudeField.setText("Location not available");
//}

//registerForContextMenu(mapView);

registerComponentCallbacks(new ComponentCallbacks2() {
Expand Down Expand Up @@ -277,18 +246,13 @@ public Object onRetainNonConfigurationInstance() {
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}

/*
* Called when the Activity is no longer visible.
*/
@Override
protected void onStop() {
if (mLocationClient.isConnected()) {
mLocationClient.removeLocationUpdates(this);
}
mLocationClient.disconnect();
super.onStop();
}

Expand All @@ -315,39 +279,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST:
/*
* If the result code is Activity.RESULT_OK, try
* to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK:
break;
}
case PREFERENCE_REQUEST:
mG.update(getBaseContext());
mapView.postInvalidate();
break;
}
}

private boolean servicesConnected() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == resultCode) {
Log.d("Location Updates", "Google Play services is available.");
return true;
} else {
//int errorCode = GooglePlayServicesUtil.getErrorCode();
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
if (errorDialog != null) {
errorDialog.show();
} else {
showOkDialog(this, "Something went wrong with Google Play Services");
}
return false;
}
}

public static void showOkDialog(Context context, String txt)
{
Expand All @@ -358,42 +295,7 @@ public static void showOkDialog(Context context, String txt)
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void onConnected(Bundle dataBundle) {
Log.d(TAG, "onConnected");
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}

@Override
public void onDisconnected() {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
//showErrorDialog(connectionResult.getErrorCode());
}
}

@Override
public boolean onTouchEvent(MotionEvent event){
boolean h = mScaleGestureDetector.onTouchEvent(event);
Expand Down Expand Up @@ -611,6 +513,21 @@ public void onLocationChanged(Location location) {
}
}

@Override
public void onProviderDisabled(String provider) {
//Log.e(TAG, "onProviderDisabled: " + provider);
}

@Override
public void onProviderEnabled(String provider) {
//Log.e(TAG, "onProviderEnabled: " + provider);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
//Log.e(TAG, "onStatusChanged: " + provider);
}

public void onGpsStatusChanged(int event) {
boolean statchg = false;
switch (event) {
Expand Down
4 changes: 2 additions & 2 deletions src/dk/network42/osmfocus/OsmServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class OsmServer {
static private final int SERVER_CONNECT_TIMEOUT_MS = 30*1000;
static private final int API_TIMEOUT_MS = 10*1000;
static final int API_MAX_DOWNLOAD_DEGREES = (int) 1E7/4;
static private final String DEFAULT_API_URL = "http://api.openstreetmap.org/api/"+API_VERSION+"/";
static private final String DEFAULT_API_URL = "https://api.openstreetmap.org/api/"+API_VERSION+"/";

private final String mApiUrl;
private final String mAgent;
Expand All @@ -44,7 +44,7 @@ public OsmServer(String apiUrl, String agent) {
// top is the latitude of the top (northernmost) side of the bounding box.
//
// Example:
// http://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145
// https://api.openstreetmap.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145
//
// Error codes
// HTTP status code 400 (Bad Request)
Expand Down
1 change: 1 addition & 0 deletions src/dk/network42/osmfocus/OsmTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private Bitmap dlFromUrl(String tileurl) {
//Log.d(TAG, "Loading tile "+tileurl);
URL url = new URL(tileurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", MainActivity.userAgent);
conn.setDoInput(true);
//Log.d(TAG, "Connect for tile download");
conn.connect();
Expand Down
4 changes: 2 additions & 2 deletions src/dk/network42/osmfocus/OsmTileLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void handleMessage(Message inputMessage) {

static String urlFromType(int type) {
if (type==MapLayer.MAPTYPE_OSM) {
return "http://a.tile.openstreetmap.org";
return "https://a.tile.openstreetmap.org";
} else if (type==MapLayer.MAPTYPE_OSMCYCLEMAP) {
return "http://a.tile.opencyclemap.org/cycle";
return "http://a.tile.opencyclemap.org/cycle";
}
return "";
}
Expand Down