Android Open Source - umbrella.alert Check Weather Service






From Project

Back to project page umbrella.alert.

License

The source code is released under:

GNU General Public License

If you think the Android project umbrella.alert listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package macbury.umbrella.service;
/*  w w w. j  a  v a  2 s . c  om*/
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import macbury.umbrella.UmbrellaApplication;
import macbury.umbrella.managers.IntentsManager;
import macbury.umbrella.model.Forecast;
import macbury.umbrella.providers.ForecastProvider;
import macbury.umbrella.providers.ForecastProviderError;
import macbury.umbrella.providers.ForecastProviderListener;

public class CheckWeatherService extends Service implements ForecastProviderListener {
  private static final String TAG           = "CheckWeatherService";
  private static final String WAKE_LOCK_TAG = "de.macbury.WAKE_LOCK";
  private ForecastProvider forecastProvider;
  private Forecast currentForecast;
  private UmbrellaApplication app;
  private PowerManager powerManager;
  private PowerManager.WakeLock wakeLock;

  public enum SyncStatus {
    Started,
    Working,
    Success,
    Error
  }

  public CheckWeatherService() {
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Creating service...");
    this.powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    this.wakeLock     = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
    this.wakeLock.acquire();

    this.app          = (UmbrellaApplication)getApplication();
    forecastProvider  = new ForecastProvider(this);
    forecastProvider.setListener(this);
    setStatus(SyncStatus.Started);
  }

  private void setStatus(SyncStatus status) {
    sendBroadcast(app.intents.syncBroadcast(status));
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    UmbrellaApplication app = (UmbrellaApplication)getApplication();

    Forecast storedForecast = app.store.getForecast();

    boolean forceRefresh = intent != null && intent.getBooleanExtra(IntentsManager.EXTRA_FORCE_REFRESH, false);

    if (storedForecast == null || storedForecast.isNotFresh()) {
      app.store.setUmbrellaNotificationDismissed(false);
    }

    //app.notifications.syncWeatherNotification(); //TODO: remove only for debug

    if (storedForecast == null || storedForecast.isNotFresh() || forceRefresh) {
      Log.i(TAG, "Starting command" );
      forecastProvider.fetch();
    } else if(!forecastProvider.isRunning()) {
      Log.i(TAG, "Fresh date skipping");
      setStatus(SyncStatus.Success);
      stopSelf();
    } else {
      Log.i(TAG, "Already running...");
    }
    return super.onStartCommand(intent, flags, startId);
  }

  @Override
  public void onForecastComplete(ForecastProvider forecastProvider) {
    stopSelf();
  }

  @Override
  public void onForecastError(ForecastProvider forecastProvider, ForecastProviderError error) {
    // show notification error here?
    setStatus(SyncStatus.Error);
  }

  @Override
  public void onForecastSuccess(ForecastProvider forecastProvider, Forecast currentForecast) {
    this.currentForecast = currentForecast;
    setStatus(SyncStatus.Success);
    showNotificationIfNeeded();
  }

  private void showNotificationIfNeeded() {
    app.store.put(currentForecast);

    if (currentForecast.takeUmbrella() && !app.store.isUmbrellaNotificationDismissed()) {
      app.notifications.showTakeUmbrella();
    } else {
      app.notifications.hideTakeUmbrella();
    }
  }

  @Override
  public void onDestroy() {
    Log.i(TAG, "Stopping service...");
    wakeLock.release();
    super.onDestroy();
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}




Java Source Code List

macbury.umbrella.ApplicationTest.java
macbury.umbrella.ForecastTest.java
macbury.umbrella.UmbrellaApplication.java
macbury.umbrella.activity.ForecastActivity.java
macbury.umbrella.activity.SettingsActivity.java
macbury.umbrella.cards.AboutCard.java
macbury.umbrella.cards.CityCard.java
macbury.umbrella.cards.PrecipitationCard.java
macbury.umbrella.factories.WeatherForecastFactory.java
macbury.umbrella.fragments.ForecastFragment.java
macbury.umbrella.fragments.LoadingFragment.java
macbury.umbrella.fragments.SettingsFragment.java
macbury.umbrella.managers.AlarmsManager.java
macbury.umbrella.managers.IntentsManager.java
macbury.umbrella.managers.NotificationsManager.java
macbury.umbrella.managers.ServicesManager.java
macbury.umbrella.managers.StoreManager.java
macbury.umbrella.model.Forecast.java
macbury.umbrella.model.RainData.java
macbury.umbrella.providers.ForecastProviderError.java
macbury.umbrella.providers.ForecastProviderListener.java
macbury.umbrella.providers.ForecastProvider.java
macbury.umbrella.receiver.CheckWeatherReceiver.java
macbury.umbrella.receiver.DissmisedTakeUmbrellaNotificationReceiver.java
macbury.umbrella.receiver.SyncStatusBroadcastReceiver.java
macbury.umbrella.service.CheckWeatherService.java