From e125b060f31f2a9665e0cf010fbec9098b8c4994 Mon Sep 17 00:00:00 2001 From: Erfan Date: Sat, 30 Dec 2017 00:00:23 +0330 Subject: [PATCH] make all Geofences and use methods in MainActivity. puff --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 2 + .../shushme/GeofenceBroadcastReciver.java | 21 ++++ .../example/android/shushme/Geofencing.java | 117 ++++++++++++++++++ .../example/android/shushme/MainActivity.java | 35 +++++- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 7 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/example/android/shushme/GeofenceBroadcastReciver.java create mode 100644 app/src/main/java/com/example/android/shushme/Geofencing.java diff --git a/app/build.gradle b/app/build.gradle index 4a0fc28f..852ca68f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 @@ -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' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 44d2738d..cafdcd17 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,8 @@ + + +{ + private static final String TAG = Geofencing.class.getSimpleName(); + private List 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())); + } +} diff --git a/app/src/main/java/com/example/android/shushme/MainActivity.java b/app/src/main/java/com/example/android/shushme/MainActivity.java index b3ccdefd..f9a5d431 100644 --- a/app/src/main/java/com/example/android/shushme/MainActivity.java +++ b/app/src/main/java/com/example/android/shushme/MainActivity.java @@ -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; @@ -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; @@ -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 * @@ -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 @@ -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 @@ -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 } }); diff --git a/build.gradle b/build.gradle index 74b2ab0d..c33a638b 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 04e285f3..31056da6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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