Skip to content

Commit a7734fe

Browse files
author
Paul Ruiz
committed
cleaning up code
1 parent b996392 commit a7734fe

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

GeoFencing/geofencing/src/main/java/com/ptrprograms/geofencing/Activity/MainActivity.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
import android.app.Activity;
44
import android.app.PendingIntent;
5-
import android.content.BroadcastReceiver;
6-
import android.content.Context;
75
import android.content.Intent;
8-
import android.content.IntentFilter;
96
import android.location.Location;
107
import android.os.Bundle;
11-
import android.support.v4.content.LocalBroadcastManager;
128
import android.widget.CompoundButton;
139
import android.widget.ToggleButton;
1410

@@ -23,21 +19,19 @@
2319

2420
import java.util.ArrayList;
2521

26-
2722
public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
2823
GooglePlayServicesClient.OnConnectionFailedListener,
2924
LocationClient.OnAddGeofencesResultListener,
3025
LocationClient.OnRemoveGeofencesResultListener {
3126

32-
private static String FENCE_ID = "com.ptrprograms.geofence";
27+
private final static String FENCE_ID = "com.ptrprograms.geofence";
28+
private final int RADIUS = 100;
3329

3430
private ToggleButton mToggleButton;
3531
private Geofence mGeofence;
3632
private LocationClient mLocationClient;
3733
private Intent mIntent;
3834
private PendingIntent mPendingIntent;
39-
private GeofenceSampleReceiver mBroadcastReceiver;
40-
IntentFilter mIntentFilter = new IntentFilter();
4135

4236
@Override
4337
protected void onCreate(Bundle savedInstanceState) {
@@ -50,12 +44,10 @@ protected void onCreate(Bundle savedInstanceState) {
5044
mIntent = new Intent( this, GeofencingService.class );
5145
mPendingIntent = PendingIntent.getService( this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT );
5246

53-
mBroadcastReceiver = new GeofenceSampleReceiver();
54-
5547
mToggleButton = (ToggleButton) findViewById( R.id.geofencing_button );
5648
mToggleButton.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
5749
@Override
58-
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
50+
public void onCheckedChanged( CompoundButton compoundButton, boolean b ) {
5951
if( b ) {
6052
startGeofence();
6153
} else {
@@ -81,18 +73,12 @@ private void verifyPlayServices() {
8173

8274
private void startGeofence() {
8375
Location location = mLocationClient.getLastLocation();
84-
int radius = 100;
8576

8677
Geofence.Builder builder = new Geofence.Builder();
87-
mGeofence = builder
88-
.setRequestId(FENCE_ID)
89-
.setCircularRegion(
90-
location.getLatitude(),
91-
location.getLongitude(),
92-
radius)
93-
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
94-
| Geofence.GEOFENCE_TRANSITION_EXIT)
95-
.setExpirationDuration(Geofence.NEVER_EXPIRE)
78+
mGeofence = builder.setRequestId( FENCE_ID )
79+
.setCircularRegion( location.getLatitude(), location.getLongitude(), RADIUS )
80+
.setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT )
81+
.setExpirationDuration( Geofence.NEVER_EXPIRE )
9682
.build();
9783

9884
ArrayList<Geofence> geofences = new ArrayList<Geofence>();
@@ -110,7 +96,6 @@ protected void onResume() {
11096
if ( !mLocationClient.isConnected() && !mLocationClient.isConnecting() ) {
11197
mLocationClient.connect();
11298
}
113-
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter);
11499
}
115100

116101
@Override
@@ -119,13 +104,6 @@ protected void onPause() {
119104
mLocationClient.disconnect();
120105
}
121106

122-
public class GeofenceSampleReceiver extends BroadcastReceiver {
123-
@Override
124-
public void onReceive(Context context, Intent intent) {
125-
startService( mIntent );
126-
}
127-
}
128-
129107
@Override
130108
public void onConnected(Bundle bundle) {
131109

@@ -140,7 +118,6 @@ public void onDisconnected() {
140118
public void onAddGeofencesResult(int status, String[] geofenceIds ) {
141119
if( status == LocationStatusCodes.SUCCESS ) {
142120
Intent intent = new Intent( mIntent );
143-
intent.setAction(GeofencingService.ACTION_INIT);
144121
startService( intent );
145122
}
146123
}
@@ -153,9 +130,8 @@ public void onConnectionFailed(ConnectionResult connectionResult) {
153130
@Override
154131
public void onRemoveGeofencesByRequestIdsResult(int status, String[] strings) {
155132
if( status == LocationStatusCodes.SUCCESS ) {
133+
stopService( mIntent );
156134
}
157-
158-
stopService( mIntent );
159135
}
160136

161137
@Override
@@ -164,4 +140,5 @@ public void onRemoveGeofencesByPendingIntentResult(int status, PendingIntent pen
164140
stopService( mIntent );
165141
}
166142
}
143+
167144
}

GeoFencing/geofencing/src/main/java/com/ptrprograms/geofencing/Service/GeofencingService.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
public class GeofencingService extends IntentService {
1818

19-
public static String ACTION_INIT = "com.ptrprograms.geofencing.init";
20-
2119
private NotificationManager mNotificationManager;
2220

2321
public GeofencingService(String name) {
@@ -29,15 +27,15 @@ public GeofencingService() {
2927
}
3028

3129
@Override
32-
public IBinder onBind(Intent intent) {
30+
public IBinder onBind( Intent intent ) {
3331
return null;
3432
}
3533

3634
@Override
37-
protected void onHandleIntent(Intent intent) {
35+
protected void onHandleIntent( Intent intent ) {
3836
NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
3937
builder.setSmallIcon( R.drawable.ic_launcher );
40-
builder.setDefaults(Notification.DEFAULT_ALL );
38+
builder.setDefaults( Notification.DEFAULT_ALL );
4139
builder.setOngoing( true );
4240

4341
int transitionType = LocationClient.getGeofenceTransition( intent );

0 commit comments

Comments
 (0)