Beispiel #1
0
    @Override
    public void onExit(Place place, long time, long duration) {
      //			Activity activity = BackgroundGeofencingPlugin.this.cordova.getActivity();
      //			NotificationManager mNotificationManager = (NotificationManager)
      // activity.getSystemService(Context.NOTIFICATION_SERVICE);
      //
      //			Intent main = new Intent(activity, CordovaApp.class);
      //            main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      //
      //            PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, main,
      // PendingIntent.FLAG_UPDATE_CURRENT);
      //            Notification.Builder builder = new Notification.Builder(activity);
      //            builder.setContentTitle("farewell:"+place.getPlaceName());
      //            builder.setContentText("exit:" + place.getPlaceName());
      //            builder.setSmallIcon(android.R.drawable.ic_menu_mylocation);
      //            builder.setContentIntent(pendingIntent);
      //            builder.setAutoCancel(true);
      //
      //            mNotificationManager.notify((int) System.currentTimeMillis(), builder.build());

      Toast.makeText(activity, "exit place:" + place.getPlaceName(), Toast.LENGTH_LONG);

      Log.v(
          Config.TAG,
          "exit place :" + place.getPlaceName() + "	at time:" + time + "	for:" + duration);
    }
Beispiel #2
0
  private static PendingIntent getPendingIntentByPlace(
      Context context, Place place, Intent intent, boolean toCreate) {
    if (place == null || intent == null) {
      return null;
    }
    //
    //		Intent intent = new Intent(INTENT_ACTION_GEOTRIGGER);
    //		intent.addCategory(INTENT_CATEGORY_GEOTRIGGER);
    Uri.Builder builder = new Uri.Builder();
    builder.scheme(INTENT_DATA_SCHEME_GEOTRIGGER);
    builder.authority(INTENT_HOST_GEOTRIGGER);
    builder.appendQueryParameter("placeUuid", place.getUuid());
    intent.setData(builder.build());

    intent.putExtra(INTENT_EXTRA_FIELD_PLACE, place.toJSONObject().toString());

    int flag = PendingIntent.FLAG_UPDATE_CURRENT;

    if (!toCreate) {
      flag = PendingIntent.FLAG_NO_CREATE;
    }

    PendingIntent stationaryRegionPI = PendingIntent.getBroadcast(context, 0, intent, flag);

    return stationaryRegionPI;
  }
Beispiel #3
0
 public boolean addPlace(Place place) {
   if (place != null && this.tripplan != null) {
     if (tripplan.getPlaceByUuid(place.getUuid()) != null) {
       return false;
     } else {
       if (place.isSubscribed()) {
         this.registerPlace(place);
       }
       tripplan.addPlace(place);
     }
   }
   return false;
 }
Beispiel #4
0
 public boolean deletePlace(String placeUuid) {
   if (placeUuid != null && this.tripplan != null) {
     Place place = null;
     if ((place = tripplan.removePlace(placeUuid)) == null) {
       return false;
     } else {
       if (place.isSubscribed()) {
         this.unregisterPlace(place);
       }
       return true;
     }
   }
   return false;
 }
Beispiel #5
0
 public boolean disablePlace(String placeUuid) {
   if (placeUuid != null && this.tripplan != null) {
     Place place = tripplan.getPlaceByUuid(placeUuid);
     if (place == null) {
       return false;
     } else {
       if (place.isSubscribed()) {
         place.setSubscribed(false);
         this.unregisterPlace(place);
       }
       return true;
     }
   }
   return false;
 }
Beispiel #6
0
  public void stop() {
    if (!this.isEnabled) {
      Log.v(Config.TAG, "has not started");
      return;
    }

    if (tripplan != null) {
      List<Place> places = this.tripplan.getPlaces();
      for (Place place : places) {
        if (place.isSubscribed()) {
          this.unregisterPlace(place);
        }
      }
    }

    this.isEnabled = false;
  }
Beispiel #7
0
  public void start() {
    if (this.isEnabled) {
      Log.v(Config.TAG, "has started");
      return;
    }

    if (this.tripplan != null) {
      // Here be the execution of the stationary region monitor
      List<Place> places = this.tripplan.getPlaces();
      for (Place place : places) {
        if (place.isSubscribed()) {
          registerPlace(place);
        }
      }
    }
    this.isEnabled = true;
  }
Beispiel #8
0
  private void onPlaceTriggered(Place place, boolean entering) {

    geotriggerListener = new GeotriggerListener(context);
    if (entering) {
      Log.d(Config.TAG, "- ENTER");
      place.timeStamp();

      if (geotriggerListener != null) {
        geotriggerListener.onEnter(place, place.getTimeStamp());
      }

      // to just enable the notifications once, delete the place from tripplan.
      Geotrigger geotrigger = new Geotrigger(context);
      if (geotrigger != null) {
        boolean isDeleted = geotrigger.deletePlace(place.getUuid());
        if (isDeleted) {
          geotrigger.onPause();
        }
      }
    } else if (!entering) {
      Log.d(Config.TAG, "- EXIT");
      long enterTime = place.getTimeStamp();
      place.timeStamp();
      long duration = place.getTimeStamp() - enterTime;
      if (geotriggerListener != null) {
        geotriggerListener.onExit(place, place.getTimeStamp(), duration);
      }
    }
  }
Beispiel #9
0
 private void unregisterPlace(Place place) {
   Intent intent = new Intent(INTENT_ACTION_GEOTRIGGER_STATIONARY_PROXIMITY_ALERT);
   intent.addCategory(INTENT_CATEGORY_GEOTRIGGER);
   PendingIntent pendingIntent = getPendingIntentByPlace(context, place, intent, false);
   if (pendingIntent != null) {
     this.locationManager.removeProximityAlert(pendingIntent);
     Log.v(Config.TAG, place.getPlaceName() + " removed");
   }
 }
Beispiel #10
0
 private void registerPlace(Place place) {
   Geofence geofence = place.getGeofence();
   if (geofence != null) {
     Intent intent = new Intent(INTENT_ACTION_GEOTRIGGER_STATIONARY_PROXIMITY_ALERT);
     intent.addCategory(INTENT_CATEGORY_GEOTRIGGER);
     PendingIntent stationaryRegionPI = getPendingIntentByPlace(context, place, intent, true);
     locationManager.addProximityAlert(
         geofence.getLatitude(),
         geofence.getLongitude(),
         geofence.getRadius(),
         (long) -1,
         stationaryRegionPI);
   }
 }
Beispiel #11
0
    @Override
    public void onEnter(Place place, long time) {
      //			Activity activity = BackgroundGeofencingPlugin.this.cordova.getActivity();
      NotificationManager mNotificationManager =
          (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
      JSONObject offerData = new JSONObject();
      try {
        offerData.put("offerUuid", place.getOfferUuid());
        offerData.put("placeName", place.getPlaceName());
      } catch (JSONException e) {
        e.printStackTrace();
      }

      Intent intent = new Intent(activity, CordovaApp.class);
      //			intent.addCategory(INTENT_CATEGORY_OFFER_NOTIFICATION);
      Uri.Builder uriBuilder = new Uri.Builder();
      uriBuilder.scheme(INTENT_DATA_SCHEME_OFFER_NOTIFICATION);
      uriBuilder.authority(INTENT_HOST_OFFER_NOTIFICATION);
      uriBuilder.appendQueryParameter("placeUuid", place.getUuid());
      intent.setData(uriBuilder.build());
      intent.putExtra(INTENT_EXTRA_KEY_NOTIFICATION_OFFER_DATA, offerData.toString());
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      PendingIntent pendingIntent =
          PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

      Notification.Builder builder = new Notification.Builder(activity);
      // builder.setContentTitle(place.getPlaceName()+" has a new offer"); //By Abdullah
      builder.setContentTitle(place.getPlaceName()); // By Abdullah
      // builder.setContentText("Click here to view the offer");		//By Abdullah
      builder.setContentText(place.getShortDesc()); // By Abdullah
      builder.setSmallIcon(R.drawable.icon);
      builder.setContentIntent(pendingIntent);
      builder.setAutoCancel(true);
      builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

      mNotificationManager.notify((int) System.currentTimeMillis(), builder.build());

      Log.v(Config.TAG, "enter into place :" + place.getPlaceName() + "	at time:" + time);

      // unregister place in apigee
      Intent tripPlanSyncIntent = new Intent(activity, TripPlanSyncService.class);
      tripPlanSyncIntent.putExtra(TripPlanSyncService.PLACE_ID_TO_UNSUBSRIBE, place.getUuid());
      activity.startService(tripPlanSyncIntent);
    }
Beispiel #12
0
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i(Config.TAG, "stationaryRegionReceiver");

    this.context = context;

    String action = intent.getAction();
    if (action.equals(INTENT_ACTION_GEOTRIGGER_STATIONARY_PROXIMITY_ALERT)) {

      //	            Location location =
      // (Location)intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
      Boolean entering = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);

      String placeData = intent.getStringExtra(INTENT_EXTRA_FIELD_PLACE);
      if (placeData == null) {
        return;
      }

      if (entering) {
        Place place = null;
        try {
          PlaceParser placeParser = new PlaceParser(placeData);
          place = placeParser.getPlace();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (place == null) {
          Log.v(Config.TAG, "place is null");
          return;
        }

        //	            if(location.getAccuracy() <= ACCURACY) {
        //
        //
        //	            	this.onPlaceTriggered(place, true);
        //	            }else {
        LocationManager locationManager =
            (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        Intent sanityCheckIntent = new Intent(INTENT_ACTION_GEOTRIGGER_SANITY_CHECK);
        sanityCheckIntent.addCategory(INTENT_CATEGORY_GEOTRIGGER);
        locationManager.requestSingleUpdate(
            criteria, getPendingIntentByPlace(context, place, sanityCheckIntent, true));
        //	            }
      }

    } else if (action.equals(INTENT_ACTION_GEOTRIGGER_SANITY_CHECK)) {

      String key = LocationManager.KEY_LOCATION_CHANGED;
      Location location = (Location) intent.getExtras().get(key);

      String placeData = intent.getStringExtra(INTENT_EXTRA_FIELD_PLACE);
      if (placeData == null) {
        return;
      }

      Place place = null;
      try {
        PlaceParser placeParser = new PlaceParser(placeData);
        place = placeParser.getPlace();
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (place == null) {
        Log.v(Config.TAG, "place is null");
        return;
      }

      if (place.isLocatedIn(location)) {
        this.onPlaceTriggered(place, true);
      }
    }
  }