Exemple #1
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);
      }
    }
  }
  public boolean execute(String action, JSONArray data, final CallbackContext callbackContext) {
    Boolean result = false;

    if (ACTION_START.equalsIgnoreCase(action)) {
      if (geotrigger.isEnabled()) {
        callbackContext.error("Service has already been started");
      } else if (!geotrigger.isConfigured()) {
        callbackContext.error("Call configure before calling start");
      } else {

        this.geotrigger.start();
        result = true;
        callbackContext.success("start succeed");
      }
    } else if (ACTION_STOP.equalsIgnoreCase(action)) {
      if (!geotrigger.isEnabled()) {
        callbackContext.error("The service hasn't started yet");
      } else {
        this.geotrigger.stop();
        result = true;
        callbackContext.success("stop succeed");
      }
    } else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
      if (geotrigger.isEnabled()) {
        Log.v(Config.TAG, "has enabled, then disable all the previous geofences");
        geotrigger.stop();
      }

      TripPlan tripPlanToConfigure = null;
      try {
        TripPlanParser parser = new TripPlanParser(data.toString());
        tripPlanToConfigure = parser.getTripplan();

      } catch (Exception e) {
        e.printStackTrace();
        callbackContext.error("Errors occur when parsing tripplan data");
      }

      if (tripPlanToConfigure != null) {
        this.geotrigger.configure(tripPlanToConfigure);
        result = true;
        callbackContext.success("configure succeed");
      }
    } else if (ACTION_RECONFIGURE.equalsIgnoreCase(action)) {

      TripPlan tripPlanToConfigure = null;
      try {
        TripPlanParser parser = new TripPlanParser(data.toString());
        tripPlanToConfigure = parser.getTripplan();

      } catch (Exception e) {
        e.printStackTrace();
        callbackContext.error("Errors occur when parsing tripplan data");
      }

      if (tripPlanToConfigure != null) {
        this.geotrigger.reconfigure(tripPlanToConfigure);
        result = true;
        callbackContext.success("configure succeed");
      }
    } else if (ACTION_ADDPLACE.equalsIgnoreCase(action)) {
      if (!this.geotrigger.isConfigured()) {
        callbackContext.error("The service is not configured yet");
      } else {
        Place place = null;
        try {
          PlaceParser parser = new PlaceParser(data.toString());
          place = parser.getPlace();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (place == null) {
          callbackContext.error("Errors occur when parsing tripplan data");
        } else {
          boolean isAdded = this.geotrigger.addPlace(place);
          if (isAdded) {
            result = true;
            callbackContext.success("adding place succeed");
          } else {
            callbackContext.error("failed to add place");
          }
        }
      }

    } else if (ACTION_DELETEPLACE.equalsIgnoreCase(action)) {
      if (!this.geotrigger.isConfigured()) {
        callbackContext.error("The service is not configured yet");
      } else {
        String placeUuid = null;
        try {
          PlaceUuidParser parser = new PlaceUuidParser(data.toString());
          placeUuid = parser.getPlaceUuid();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (placeUuid == null) {
          callbackContext.error("Errors occur when parsing place uuid");
        } else {
          boolean isDeleted = this.geotrigger.deletePlace(placeUuid);
          if (isDeleted) {
            result = true;
            callbackContext.success("deleting place succeed");
          } else {
            callbackContext.error("failed to delete place");
          }
        }
      }
    } else if (ACTION_ENABLEPLACE.equalsIgnoreCase(action)) {
      if (!this.geotrigger.isConfigured()) {
        callbackContext.error("The service is not configured yet");
      } else {
        String placeUuid = null;
        try {
          PlaceUuidParser parser = new PlaceUuidParser(data.toString());
          placeUuid = parser.getPlaceUuid();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (placeUuid == null) {
          callbackContext.error("Errors occur when parsing place uuid");
        } else {
          boolean isEnabled = this.geotrigger.enablePlace(placeUuid);
          if (isEnabled) {
            result = true;
            callbackContext.success("enable place succeed");
          } else {
            callbackContext.error("failed to enable place");
          }
        }
      }
    } else if (ACTION_DISABLEPLACE.equalsIgnoreCase(action)) {
      if (!this.geotrigger.isConfigured()) {
        callbackContext.error("The service is not configured yet");
      } else {
        String placeUuid = null;
        try {
          PlaceUuidParser parser = new PlaceUuidParser(data.toString());
          placeUuid = parser.getPlaceUuid();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (placeUuid == null) {
          callbackContext.error("Errors occur when parsing place uuid");
        } else {
          boolean isEnabled = this.geotrigger.disablePlace(placeUuid);
          if (isEnabled) {
            result = true;
            callbackContext.success("disable place succeed");
          } else {
            callbackContext.error("failed to disable place");
          }
        }
      }
    } else if (ACTION_GETCURRENTLOCATION.equalsIgnoreCase(action)) {
      final LocationManager locationManager =
          (LocationManager) this.cordova.getActivity().getSystemService(Context.LOCATION_SERVICE);
      Criteria criteria = new Criteria();
      criteria.setAltitudeRequired(false);
      criteria.setBearingRequired(false);
      criteria.setSpeedRequired(true);
      criteria.setCostAllowed(true);
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
      criteria.setPowerRequirement(Criteria.POWER_HIGH);

      String bestProvider = locationManager.getBestProvider(criteria, true);
      if (bestProvider != LocationManager.PASSIVE_PROVIDER) {
        locationManager.requestLocationUpdates(
            bestProvider,
            0,
            0,
            new LocationListener() {

              @Override
              public void onLocationChanged(Location location) {
                JSONObject object = new JSONObject();
                try {
                  object.put("lat", location.getLatitude());
                  object.put("lng", location.getLongitude());
                } catch (JSONException e) {
                  e.printStackTrace();
                }
                callbackContext.success(object);
                locationManager.removeUpdates(this);
              }

              @Override
              public void onProviderDisabled(String arg0) {}

              @Override
              public void onProviderEnabled(String arg0) {}

              @Override
              public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
            });
      }
    } else if (ACTION_SETONNOTIFICATIONCLICKEDCALLBACK.equalsIgnoreCase(action)) {
      String callbackId = callbackContext.getCallbackId();
      this.callbackIds.put(ACTION_SETONNOTIFICATIONCLICKEDCALLBACK, callbackId);
      Log.d(Config.TAG, "notificaton callback is set");
      return true;
    } else if (ACTION_MOCK_START.equalsIgnoreCase(action)) {
      if (this.geoFaker != null && this.geoFaker.isStarted()) {
        callbackContext.error("geofaker has started");
      } else {
        this.geoFaker = new Geofaker(this.cordova.getActivity());
        this.geoFaker.start();
        result = true;
        callbackContext.success("mocking started");
      }
    } else if (ACTION_MOCK_STOP.equalsIgnoreCase(action)) {
      if (this.geoFaker != null && this.geoFaker.isStarted()) {
        this.geoFaker.stop();
        result = true;
        callbackContext.success("mocking stopped");
      } else {
        callbackContext.error("geofaker has already stopped");
      }
    } else if (ACTION_MOCK.equalsIgnoreCase(action)) {
      Intent intent = new Intent();
      intent.setAction(Geofaker.INTENT_MOCK_GPS_PROVIDER);
      intent.putExtra(Geofaker.MOCKED_JSON_COORDINATES, data.toString());
      activity.sendBroadcast(intent);
      result = true;
      callbackContext.success("mocked position: " + data.toString());
    }

    return result;
  }