Exemple #1
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));
   }
 }
Exemple #2
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);
  }
Exemple #3
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);
        }
      }
    }
  }