|
| 1 | +package com.clevertap.unity.example; |
| 2 | + |
| 3 | +import static com.clevertap.android.geofence.Logger.DEBUG; |
| 4 | + |
| 5 | +import android.Manifest; |
| 6 | +import android.content.pm.PackageManager; |
| 7 | +import android.location.Location; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import androidx.annotation.NonNull; |
| 12 | +import androidx.core.app.ActivityCompat; |
| 13 | +import androidx.core.content.ContextCompat; |
| 14 | + |
| 15 | +import com.clevertap.android.geofence.CTGeofenceAPI; |
| 16 | +import com.clevertap.android.geofence.CTGeofenceSettings; |
| 17 | +import com.clevertap.android.geofence.interfaces.CTGeofenceEventsListener; |
| 18 | +import com.clevertap.android.geofence.interfaces.CTLocationUpdatesListener; |
| 19 | +import com.clevertap.android.sdk.CleverTapAPI; |
| 20 | +import com.clevertap.unity.CleverTapOverrideActivity; |
| 21 | + |
| 22 | +import org.json.JSONObject; |
| 23 | + |
| 24 | +public class GeofenceExampleActivity extends CleverTapOverrideActivity implements CTGeofenceAPI.OnGeofenceApiInitializedListener, CTGeofenceEventsListener, CTLocationUpdatesListener { |
| 25 | + |
| 26 | + private CTGeofenceAPI geofenceAPI; |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + geofenceAPI = CTGeofenceAPI.getInstance(getApplicationContext()); |
| 32 | + geofenceAPI.setOnGeofenceApiInitializedListener(this); |
| 33 | + geofenceAPI.setCtGeofenceEventsListener(this); |
| 34 | + geofenceAPI.setCtLocationUpdatesListener(this); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void onResume() { |
| 39 | + super.onResume(); |
| 40 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION |
| 41 | + ) == PackageManager.PERMISSION_GRANTED) { |
| 42 | + initGeofenceApi(); |
| 43 | + } else { |
| 44 | + ActivityCompat.requestPermissions( |
| 45 | + this, |
| 46 | + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, |
| 47 | + 1 |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 54 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 55 | + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 56 | + initGeofenceApi(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void initGeofenceApi() { |
| 61 | + CleverTapAPI cleverTapAPI = CleverTapAPI.getDefaultInstance(this); |
| 62 | + CTGeofenceSettings ctGeofenceSettings = new CTGeofenceSettings.Builder() |
| 63 | + .setLogLevel(DEBUG) |
| 64 | + .build(); |
| 65 | + geofenceAPI.init(ctGeofenceSettings, cleverTapAPI); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void OnGeofenceApiInitialized() { |
| 70 | + Toast.makeText(this, "Geofence Api Initialized", Toast.LENGTH_SHORT).show(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onGeofenceEnteredEvent(JSONObject geofenceEnteredEventProperties) { |
| 75 | + Toast.makeText(this, "Geofence Entered", Toast.LENGTH_SHORT).show(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onGeofenceExitedEvent(JSONObject geofenceExitedEventProperties) { |
| 80 | + Toast.makeText(this, "Geofence Exited", Toast.LENGTH_SHORT).show(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onLocationUpdates(Location location) { |
| 85 | + Toast.makeText(this, "Location update " + location, Toast.LENGTH_SHORT).show(); |
| 86 | + } |
| 87 | +} |
0 commit comments