GpsListenerAlarmArbiter.java :  » Location » wakemewhere » com » google » android » apps » wakemewhere » ui » Android Open Source

Android Open Source » Location » wakemewhere 
wakemewhere » com » google » android » apps » wakemewhere » ui » GpsListenerAlarmArbiter.java
package com.google.android.apps.wakemewhere.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.apps.wakemewhere.Alarm;
import com.google.android.apps.wakemewhere.AlarmArbiter;
import com.google.android.apps.wakemewhere.util.Preconditions;

public class GpsListenerAlarmArbiter implements AlarmArbiter {

  private List<Alarm> alarms = new ArrayList<Alarm>();
  private Context context;
  private Intent onFireIntent;

  GpsListenerAlarmArbiter(Context context) {
    Preconditions.nonNull(context);
    this.context = context;
  }

  public void setAlarms(List<Alarm> alarms) {
    alarms.clear();
    alarms.addAll(alarms);
  }

  public void setOnFireIntent(Intent onFireIntent) {
    Preconditions.nonNull(onFireIntent);
    this.onFireIntent = onFireIntent;
  }

  public void start() {
    final LocationManager locationManager = (LocationManager) context
        .getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
        0, new AlarmLocationListener());
  }

  public Alarm checkForTrigger(Location location) {
    for (Alarm alarm : alarms) {
      if (alarm.triggersOn(location)) {
        return alarm;
      }
    }
    return null;
  }

  private class AlarmLocationListener implements LocationListener {
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onProviderDisabled(String provider) {
    }

    public void onLocationChanged(Location location) {
      Alarm triggeredAlarm = checkForTrigger(location);
      if (triggeredAlarm == null) {
        return;
      }

      if (onFireIntent == null) {
        return;
      }
      triggeredAlarm.writeOnto(onFireIntent);
      context.startActivity(onFireIntent);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.