Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default radius to avoid crash, add buttons to start and stop service #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
4 changes: 4 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.source=1.6
11 changes: 0 additions & 11 deletions default.properties

This file was deleted.

14 changes: 14 additions & 0 deletions project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-8
25 changes: 25 additions & 0 deletions res/layout/stored_locations.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">

<LinearLayout
android:orientation="horizontal"
android:background="@android:drawable/bottom_bar"
android:paddingLeft="4.0dip"
android:paddingTop="5.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>

<Button
android:id="@+id/start_button"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Start"
android:layout_weight="1.0" />

<Button
android:id="@+id/stop_button"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Stop"
android:layout_weight="1.0" />

</LinearLayout>

<TableLayout android:id="@+id/headerLayout"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:id="@+id/headerRow" android:layout_height="match_parent"
Expand Down
53 changes: 33 additions & 20 deletions src/com/shinetech/android/LocationListenerService.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import android.util.Log;
import android.widget.Toast;

public class LocationListenerService extends Service implements LocationListener {
public class LocationListenerService extends Service implements
LocationListener {

public static final int UPDATE_MESSAGE = 1;
public static final String PROXIMTY_ALERT_INTENT = "com.shinetech.android.PROXIMTY_ALERT";
Expand All @@ -39,17 +40,20 @@ public void onReceive(Context context, Intent intent) {
}

private void handleProximityAlert(Intent intent) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null) {
// We'll just write an empty location
location = new Location("prx");
}
}

if (intent.hasExtra(LocationManager.KEY_PROXIMITY_ENTERING)){
if (intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, true)==true) {

if (intent.hasExtra(LocationManager.KEY_PROXIMITY_ENTERING)) {
if (intent.getBooleanExtra(
LocationManager.KEY_PROXIMITY_ENTERING, true) == true) {
location.setProvider("prx_enter");
} else {
location.setProvider("prx_exit");
Expand Down Expand Up @@ -122,35 +126,44 @@ public void onStatusChanged(String provider, int i, Bundle b) {
}

private void requestLocationUpdates() {
int sampleDistance = ((LocationMapperApplication) getApplication()).getPreferences().getSampleDistance();
int sampleInterval = ((LocationMapperApplication) getApplication()).getPreferences().getSampleInterval() * 1000 * 60;
Log.i(TAG, "Setting up location updates with sample distance " + sampleDistance + " m and sample interval "
+ sampleInterval + " ms.");
int sampleDistance = ((LocationMapperApplication) getApplication())
.getPreferences().getSampleDistance();
int sampleInterval = ((LocationMapperApplication) getApplication())
.getPreferences().getSampleInterval() * 1000 * 60;
Log.i(TAG, "Setting up location updates with sample distance "
+ sampleDistance + " m and sample interval " + sampleInterval
+ " ms.");

this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this);
List<String> enabledProviders = this.locationManager.getProviders(true);
for (String provider:enabledProviders){
for (String provider : enabledProviders) {
Log.i(TAG, "Requesting location updates from provider " + provider);
this.locationManager.requestLocationUpdates(provider, sampleInterval, sampleDistance, this);
this.locationManager.requestLocationUpdates(provider,
sampleInterval, sampleDistance, this);
}
}

private void addProximityAlert() {
Log.i(TAG, "addProximityAlert()");
int vicinityRadius = ((LocationMapperApplication) getApplication()).getPreferences().getVicinityRadius();
double latitude = ((LocationMapperApplication) getApplication()).getPreferences().getLocation().getLatitude();
double longitude = ((LocationMapperApplication) getApplication()).getPreferences().getLocation().getLongitude();
int vicinityRadius = ((LocationMapperApplication) getApplication())
.getPreferences().getVicinityRadius();
double latitude = ((LocationMapperApplication) getApplication())
.getPreferences().getLocation().getLatitude();
double longitude = ((LocationMapperApplication) getApplication())
.getPreferences().getLocation().getLongitude();

long expiration = -1;

Intent intent = new Intent(PROXIMTY_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Intent intent = new Intent(PROXIMTY_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0,
intent, 0);

locationManager.addProximityAlert(latitude, longitude, vicinityRadius, expiration, proximityIntent);
locationManager.addProximityAlert(latitude, longitude, vicinityRadius,
expiration, proximityIntent);

IntentFilter filter = new IntentFilter(PROXIMTY_ALERT_INTENT);
registerReceiver(this.broadcastReceiver, filter);
IntentFilter filter = new IntentFilter(PROXIMTY_ALERT_INTENT);
registerReceiver(this.broadcastReceiver, filter);
}

}
2 changes: 1 addition & 1 deletion src/com/shinetech/android/Preferences.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void load() {
this.location.setAccuracy(sharedPreferences.getFloat("accuracy", 0f));
this.location.setTime(sharedPreferences.getLong("time", 0l));

this.vicinityRadius = sharedPreferences.getInt("vicinityradius", 0);
this.vicinityRadius = sharedPreferences.getInt("vicinityradius", 100);
this.sampleInterval = sharedPreferences.getInt("sampleinterval", 0);
this.sampleDistance = sharedPreferences.getInt("sampledistance", 0);
}
Expand Down
39 changes: 32 additions & 7 deletions src/com/shinetech/android/ShowStoredLocationActivity.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;

public class ShowStoredLocationActivity extends ListActivity {
public class ShowStoredLocationActivity extends ListActivity implements
OnClickListener {
private static final String TAG = "LocationTrackerActivity";

private LocationDbAdapter dbAdapter;
private SimpleCursorAdapter cursorAdapter;
private Button stopButton;
private Button startButton;

public SimpleCursorAdapter getCursorAdapter() {
return cursorAdapter;
Expand All @@ -44,21 +50,28 @@ public void onCreate(Bundle savedInstanceState) {

dbAdapter = new LocationDbAdapter(this);

ComponentName locationListenerServiceName = new ComponentName(getPackageName(),
LocationListenerService.class.getName());
startService(new Intent().setComponent(locationListenerServiceName));
stopButton = (Button) findViewById(R.id.stop_button);
startButton = (Button) findViewById(R.id.start_button);

stopButton.setOnClickListener(this);
startButton.setOnClickListener(this);

}

@Override
public void onResume() {
Log.i(TAG, "onResume()");
super.onResume();
dbAdapter.open();
String[] from = { LocationDbAdapter.KEY_NAME, LocationDbAdapter.KEY_LATITUDE, LocationDbAdapter.KEY_LONGITUDE,
String[] from = { LocationDbAdapter.KEY_NAME,
LocationDbAdapter.KEY_LATITUDE,
LocationDbAdapter.KEY_LONGITUDE,
LocationDbAdapter.KEY_ACCURACY, LocationDbAdapter.KEY_TIME };
int[] to = { R.id.locationname, R.id.locationlatitude, R.id.locationlongitude, R.id.locationaccuracy,
int[] to = { R.id.locationname, R.id.locationlatitude,
R.id.locationlongitude, R.id.locationaccuracy,
R.id.locationtime };
cursorAdapter = new LocationCursorAdapter(this, R.layout.stored_locations_row_layout,
cursorAdapter = new LocationCursorAdapter(this,
R.layout.stored_locations_row_layout,
dbAdapter.fetchAllLocations(), from, to);
this.setListAdapter(cursorAdapter);
IntentFilter intentFilter = new IntentFilter();
Expand Down Expand Up @@ -93,4 +106,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

@Override
public void onClick(View v) {
ComponentName locationListenerServiceName = new ComponentName(
getPackageName(), LocationListenerService.class.getName());
Intent i = new Intent().setComponent(locationListenerServiceName);

if (v.getId() == R.id.start_button) {
startService(i);
} else if (v.getId() == R.id.stop_button) {
stopService(i);
}
}
}