Skip to content
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
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.example.android.shushme"
minSdkVersion 16
Expand All @@ -26,7 +26,7 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.google.android.gms:play-services-places:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.android.gms:play-services-places:11.0.0'
compile 'com.google.android.gms:play-services-location:11.0.0'
testCompile 'junit:junit:4.12'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
</intent-filter>
</activity>

<receiver android:name=".GeofenceBroadcastReciver"/>

<provider
android:name="com.example.android.shushme.provider.PlaceContentProvider"
android:authorities="com.example.android.shushme"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.android.shushme;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
* Created by Erfan on 29/12/2017.
*/

public class GeofenceBroadcastReciver extends BroadcastReceiver
{
public static final String TAG = GeofenceBroadcastReciver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "onRecive called");
}
}
117 changes: 117 additions & 0 deletions app/src/main/java/com/example/android/shushme/Geofencing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.example.android.shushme;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Erfan on 29/12/2017.
*/

public class Geofencing implements ResultCallback<Status>
{
private static final String TAG = Geofencing.class.getSimpleName();
private List<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;
private GoogleApiClient mGoogleApiClient;
private Context mContext;
private int GEOFENCE_RADIUS = 10;
private Long GEOFENCE_TIMEOUT = Geofence.NEVER_EXPIRE;

public Geofencing(Context context, GoogleApiClient googleApiClient)
{
this.mContext = context;
this.mGoogleApiClient = googleApiClient;
mGeofencePendingIntent = null;
mGeofenceList = new ArrayList<>();
}

public void registerAllGeofences()
{
if(mGoogleApiClient == null || !mGoogleApiClient.isConnected() ||
mGeofenceList == null || mGeofenceList.size() == 0)
return;
try
{
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()).setResultCallback(this);
}
catch (SecurityException securityException)
{
Log.e(TAG, securityException.getMessage());
}
}


public void unregisterAllGeofences()
{
if(mGoogleApiClient == null || !mGoogleApiClient.isConnected())
return;
try
{
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient,
getGeofencePendingIntent()).setResultCallback(this);
}
catch (SecurityException securityException)
{
Log.e(TAG, securityException.getMessage());
}
}

public void updateGeofenceList(PlaceBuffer places)
{
mGeofenceList = new ArrayList<>();
if(places == null || places.getCount() == 0)
return;
for(Place place : places)
{
String placeUid = place.getId();
double placeLat = place.getLatLng().latitude;
double placeLong = place.getLatLng().longitude;
Geofence geofence = new Geofence.Builder()
.setRequestId(placeUid)
.setExpirationDuration(GEOFENCE_TIMEOUT)
.setCircularRegion(placeLat, placeLong, GEOFENCE_RADIUS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
mGeofenceList.add(geofence);

}
}
private GeofencingRequest getGeofencingRequest()
{
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent()
{
if(mGeofencePendingIntent != null)
return mGeofencePendingIntent;
Intent intent = new Intent(mContext, GeofenceBroadcastReciver.class);
mGeofencePendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}

@Override
public void onResult(@NonNull Status status)
{
Log.e(TAG, String.format("Error adding/removing geofence : %s", status.getStatus().toString()));
}
}
35 changes: 33 additions & 2 deletions app/src/main/java/com/example/android/shushme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
Expand All @@ -31,6 +32,8 @@
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.example.android.shushme.provider.PlaceContract;
Expand Down Expand Up @@ -64,7 +67,8 @@ public class MainActivity extends AppCompatActivity implements
private PlaceListAdapter mAdapter;
private RecyclerView mRecyclerView;
private GoogleApiClient mClient;

private Geofencing mGeofencing;
private boolean mIsEnabled;
/**
* Called when the activity is starting
*
Expand All @@ -80,6 +84,28 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new PlaceListAdapter(this, null);
mRecyclerView.setAdapter(mAdapter);
Switch onOffSwitch = (Switch)findViewById(R.id.enable_switch);
mIsEnabled = getPreferences(MODE_PRIVATE).getBoolean(getString(R.string.setting_enabled), false);
onOffSwitch.setChecked(mIsEnabled);
onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putBoolean(getString(R.string.setting_enabled), isChecked);
mIsEnabled = isChecked;
editor.commit();
if(isChecked)
{
mGeofencing.registerAllGeofences();
}
else
{
mGeofencing.unregisterAllGeofences();
}
}
});

// TODO (9) Create a boolean SharedPreference to store the state of the "Enable Geofences" switch
// and initialize the switch based on the value of that SharedPreference
Expand All @@ -97,7 +123,7 @@ protected void onCreate(Bundle savedInstanceState) {
.addApi(Places.GEO_DATA_API)
.enableAutoManage(this, this)
.build();

mGeofencing = new Geofencing(this, mClient);
// TODO (1) Create a Geofencing class with a Context and GoogleApiClient constructor that
// initializes a private member ArrayList of Geofences called mGeofenceList

Expand Down Expand Up @@ -177,6 +203,11 @@ public void refreshPlacesData() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
mAdapter.swapPlaces(places);
mGeofencing.updateGeofenceList(places);
if(mIsEnabled)
{
mGeofencing.registerAllGeofences();
}
// TODO (11) Call updateGeofenceList and registerAllGeofences if mIsEnabled is true
}
});
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:3.0.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Fri Dec 29 20:05:33 IRST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip