Skip to content

Commit

Permalink
Fix some location related add activity bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal-test committed Nov 6, 2011
1 parent 3f947c3 commit 093afb6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/dev-instructions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ e) Click ok to exit.

6.
a) If your project still has errors, try highlighting the project root and pressing F5 to refresh. If that doesn't work, try right clicking the root and go to 'Android Tools->Fix Project Properties'
b) Should be good to go now.
b) Should be good to go now.
2 changes: 1 addition & 1 deletion taedium/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="me.taedium.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7"/>
<uses-sdk android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
Expand Down
3 changes: 1 addition & 2 deletions taedium/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Application level -->
<string name="add">Add</string>
Expand Down Expand Up @@ -70,7 +69,7 @@
<string name="tvAddSpecifyAddress">Enter Address</string>
<string name="tvAddAutoTag">Do any of these tags apply?</string>
<string name="tvAddManualTag">You can also add your own tags</string>
<string name="tvAddManualTagNoAuto">Sorry, we couldn\'t find any tags that match your activiy.</string>
<string name="tvAddManualTagNoAuto">Sorry, we couldn\'t find any tags that match your activity.</string>
<string name="tvNumPeople">Number of People</string>
<string name="tvChildSafe">Suitable for Children</string>
<string name="tvDuration">Duration</string>
Expand Down
34 changes: 25 additions & 9 deletions taedium/src/me/taedium/android/ApplicationGlobals.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

//Singleton class for holding globals
//Apparently the recommended way for passing around big lists of objects
//between activities is via globals, hence the existence of this class.
public class ApplicationGlobals {
private static final String MODULE = "GLOBALS";

//TODO: used age instead of pass KIDFRIENDLY/ADULTSONLY
public enum RecParamType {
Expand Down Expand Up @@ -66,14 +68,12 @@ public void startLocationListener(Context context) {
mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

// TODO when adding an activity may want to use GPS instead of network...or speed up the GPS!
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 100, mLocationListener);
}
else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, mLocationListener);
}
else {
String provider = getLocationProvider();
if (provider == null) {
Toast.makeText(context, "No Location providers enabled.", Toast.LENGTH_LONG).show();
} else {
Log.i(MODULE, "Initializing location listener with: " + provider);
mLocationManager.requestLocationUpdates(provider, 0, 100, mLocationListener);
}
}

Expand Down Expand Up @@ -118,8 +118,24 @@ public static int getCurrentTimeInSeconds() {
return seconds;
}

public Location getCurrentLocation() {
return mLocationListener.getCurrentLocation();
public Location getCurrentLocation() {
Location l = mLocationListener.getCurrentLocation();
if (l == null) {
String provider = getLocationProvider();
if (provider == null) return null;
return mLocationManager.getLastKnownLocation(provider);
}
return l;
}

public String getLocationProvider() {
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
return LocationManager.NETWORK_PROVIDER;
}
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return LocationManager.GPS_PROVIDER;
}
return null;
}

public boolean locationIsEnabled(Activity currentActivity) {
Expand Down
28 changes: 20 additions & 8 deletions taedium/src/me/taedium/android/add/AddLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.List;

import me.taedium.android.ApplicationGlobals;
import me.taedium.android.R;
import android.app.Dialog;
import android.content.Intent;
import android.location.Address;
Expand All @@ -17,13 +19,10 @@
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import me.taedium.android.ApplicationGlobals;
import me.taedium.android.R;
import me.taedium.android.add.AddTags;
import android.widget.Toast;

public class AddLocation extends WizardActivity {
private static final String module = "ADD_LOCATION"; //used for log messages
private static final String MODULE = "ADD_LOCATION"; //used for log messages
private static final int MAP_ADD_ACTIVITY = 61;
private static final int DIALOG_ENTER_ADDRESS = 303;

Expand All @@ -47,12 +46,16 @@ protected void onCreate(Bundle savedInstanceState) {
bMyLocation.setEnabled(false);
bMyLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(MODULE, "Getting current location");
// Find current GPS location
Location location = ApplicationGlobals.getInstance().getCurrentLocation();
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i(MODULE, "Location received, latitude: " + latitude + ", longitude: " + longitude);
updateLatLongDisplay();
} else {
Toast.makeText(AddLocation.this, "Couldn't determine current location", Toast.LENGTH_SHORT).show();
}
}
});
Expand Down Expand Up @@ -81,6 +84,7 @@ public void onClick(View v) {
bMyLocation.setEnabled(true);
bUseMap.setEnabled(true);
bSpecifyAddress.setEnabled(true);
ApplicationGlobals.getInstance().startLocationListener(AddLocation.this);
updateLatLongDisplay();
}
});
Expand All @@ -90,6 +94,7 @@ public void onClick(View v) {
bMyLocation.setEnabled(false);
bUseMap.setEnabled(false);
bSpecifyAddress.setEnabled(false);
ApplicationGlobals.getInstance().stopLocationListener();
updateLatLongDisplay();
}
});
Expand Down Expand Up @@ -168,7 +173,7 @@ private void getLocation(String address) {
}
}
catch (IOException e) {
Log.e(module,e.getStackTrace().toString());
Log.e(MODULE,e.getStackTrace().toString());
}
}

Expand All @@ -179,6 +184,8 @@ private void updateLatLongDisplay() {
if (rbLocationEnabled.isChecked()) {
if (!hasValue) {
bNext.setEnabled(false);
} else {
bNext.setEnabled(true);
}
tvLatDisplay.setVisibility(View.VISIBLE);
tvLongDisplay.setVisibility(View.VISIBLE);
Expand All @@ -193,16 +200,21 @@ private void updateLatLongDisplay() {
protected void fillData() {
data.putDouble("lat", latitude);
data.putDouble("long", longitude);
data.putBoolean("location", rbLocationEnabled.isChecked());
}

@Override
protected void restoreData() {
// Restore previously saved data
if (data.containsKey("long") && data.containsKey("lat") && (data.getDouble("long") != 0 || data.getDouble("lat") != 0)) {
rbLocationEnabled.setChecked(true);
latitude = data.getDouble("lat");
longitude = data.getDouble("long");
updateLatLongDisplay();
}
if (data.containsKey("location") && data.getBoolean("location")) {
rbLocationEnabled.setChecked(true);
} else {
rbLocationDisabled.setChecked(true);
}
updateLatLongDisplay();
}
}

0 comments on commit 093afb6

Please sign in to comment.