示例#1
0
  // LocationListener impl
  @Override
  public void onLocationChanged(Location location) {
    String topic = topic(location);
    if (!topic.equals(currentTopic)) {
      Log.d(LOG_TAG, "new grid location: " + topic);
      if (sendInfectionMessage(true)) {
        currentTopic = topic;
        subscribeToCurrentTopic();
      }
    }

    int thisRegion = calcRegionCode(location);
    if (thisRegion != region || bioHazards.isEmpty()) {
      Log.d(
          LOG_TAG,
          "new region or no biohazards in region " + thisRegion + " - generating biohazards");
      region = thisRegion;
      createBiohazards(location);
      EventBus.getDefault().post(new MapEvent(MapEvent.Type.REGION_UPDATED, region, bioHazards));
    }

    for (Biohazard b : bioHazards) {
      if (b.getLocation().distanceTo(location) < BIOHAZARD_INFECTION_RADIUS) {
        Virus v = VirusFactory.fromBiohazard(b.getSeed(), b.getId());
        if (gameDatabase.findVirus(v.getId()) == null) {
          Log.d(LOG_TAG, "infection by approaching biohazard " + b.getId());
          gameDatabase.addVisitedBiohazard(b.getId());
          addVirus(v);
        }
      }
    }
  }
示例#2
0
 private void addVirus(Virus v) {
   if (v != null && gameDatabase.findVirus(v.getId()) == null) {
     Log.d(LOG_TAG, "infected with virus: " + v.getId());
     gameDatabase.addVirus(v);
     vibrator.vibrate(300);
     EventBus.getDefault().post(new GameEvent(GameEvent.Type.NEW_VIRUS, v.getId(), 1));
   }
 }
示例#3
0
  @Override
  public void onCreate() {
    super.onCreate();

    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    gameDatabase = new GameDatabase(this);

    // init game DB
    if (gameDatabase.getViruses().isEmpty()) {
      Virus v = VirusFactory.createMutation(null);
      Log.d(LOG_TAG, "new virus: " + v.getId());
      gameDatabase.addVirus(v);
      EventBus.getDefault().post(new GameEvent(GameEvent.Type.NEW_VIRUS, v.getId(), 0));
    }
    clientId = gameDatabase.getSetting("clientId", null);
    if (clientId == null) {
      clientId = UUID.randomUUID().toString();
      gameDatabase.putSetting("clientId", clientId);
      Log.i(LOG_TAG, "persisted new clientId: " + clientId);
    }

    // MQTT
    mqttClient = new MqttAndroidClient(this, BROKER_URI, clientId);
    mqttClient.setCallback(this);

    // Location
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
      locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER,
          MIN_LOCATION_UPDATE_INTERVAL_MS,
          MIN_LOCATION_UPDATE_DISTANCE_M,
          this);
    else
      locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER,
          MIN_LOCATION_UPDATE_INTERVAL_MS,
          MIN_LOCATION_UPDATE_DISTANCE_M,
          this);

    findBestLastLocation();

    try {
      MqttConnectOptions options = new MqttConnectOptions();
      options.setKeepAliveInterval(0); // no keepalive pings
      mqttClient.connect(options, null, this);
      Log.d(LOG_TAG, "connected to MQTT broker as " + clientId);
    } catch (MqttException e) {
      Log.e(LOG_TAG, "could not connect to MQTT broker at " + BROKER_URI);
    }

    EventBus.getDefault().register(this);
  }
示例#4
0
  private boolean sendInfectionMessage(boolean entering) {
    if (mqttClient != null && mqttClient.isConnected()) {
      try {
        JSONArray a = gameDatabase.findAllVirusDataAsJson();
        JSONObject o = new JSONObject();
        if (entering) o.put("entering", currentTopic);
        o.put("sender", clientId);
        o.put("viruses", a);
        String payload = o.toString();
        Log.d(LOG_TAG, "publishing to " + currentTopic + ": " + payload);
        IMqttDeliveryToken token =
            mqttClient.publish(currentTopic, new MqttMessage(payload.getBytes()));
        token.setActionCallback(
            new IMqttActionListener() {
              @Override
              public void onSuccess(IMqttToken asyncActionToken) {
                sendEnterMessage = false;
              }

              @Override
              public void onFailure(IMqttToken asyncActionToken, Throwable exception) {}
            });
        return true;
      } catch (MqttException e) {
        //
      } catch (JSONException e) {
        Log.e(LOG_TAG, "JSONException", e);
      }
    }
    return false;
  }
 @Override
 public void run() {
   savePending = false;
   database.saveLater(game);
 }
 public void onPause() {
   handler.removeCallbacks(this);
   database.save(game);
 }