Android Open Source - umbrella.alert Forecast






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.model;
//w  ww . j  a  v a 2s  . co m
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by macbury on 26.08.14.
 */
public class Forecast {
  private static final String TAG = "Forecast";
  private ArrayList<RainData> data;
  private Date fromDate;
  private Date toDate;
  private Date lastUpdate;
  private String city;
  private float totalRainVolume;

  public Forecast() {
    data = new ArrayList<RainData>();
  }

  public void parse(JSONObject object) {
    data.clear();
    totalRainVolume = 0;
    fromDate        = null;
    toDate          = null;
    lastUpdate      = new Date();
    try {
      getAllInfo(object);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  private void getAllInfo(JSONObject root) throws JSONException {
    JSONObject cityJson = root.getJSONObject("city");
    this.city           = cityJson.getString("name");

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int currentDay    = calendar.get(Calendar.DAY_OF_YEAR);

    JSONArray listJSON  = root.getJSONArray("list");
    for(int i = 0; i < Math.min(listJSON.length(), 8); i++) {
      RainData rainData      = new RainData();

      JSONObject rawRainData = listJSON.getJSONObject(i);
      rainData.setAt(rawRainData.getInt("dt"));
      calendar.setTime(rainData.getAt());
      
      if (calendar.get(Calendar.DAY_OF_YEAR) == currentDay) {
        if (rawRainData.has("rain")) {
          JSONObject rain = rawRainData.getJSONObject("rain");
          rainData.setVolume((float)rain.getDouble("3h"));
        } else {
          rainData.setVolume(0);
        }

        if (rawRainData.has("main")) {
          JSONObject main = rawRainData.getJSONObject("main");
          rainData.setTemperature((float)main.getDouble("temp"));
          rainData.setHumidity(main.getInt("humidity"));
        } else {
          rainData.setTemperature(0);
          rainData.setHumidity(0);
        }

        totalRainVolume += rainData.getVolume();

        if (fromDate == null || rainData.getAt().before(fromDate)) {
          fromDate = rainData.getAt();
        }

        if (toDate == null || rainData.getAt().after(toDate)) {
          toDate   = rainData.getAt();
        }

        data.add(rainData);
      } else {
        //Log.i(TAG, "Date: "+currentDay+" != "+calendar.get(Calendar.DAY_OF_YEAR));
      }
    }

    calendar.setTime(fromDate);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    fromDate = calendar.getTime();

    calendar.setTime(toDate);

    calendar.set(Calendar.HOUR, calendar.getActualMaximum(Calendar.HOUR));
    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
    toDate = calendar.getTime();

    Log.i(TAG, "From: "+fromDate.toString()+" to "+toDate.toString());
  }

  public float getTemperature() {
    Date currentDate = new Date();

    for(RainData rd : data) {
      if(currentDate.before(rd.getAt())) {
        return rd.getTemperature();
      }
    }
    return 0.0f;
  }

  public boolean takeUmbrella() {
    return totalRainVolume > 0.0;
  }

  public float getTotalRainVolume() {
    return totalRainVolume;
  }

  public Date getToDate() {
    return toDate;
  }

  public Date getFromDate() {
    return fromDate;
  }

  public String getCity() {
    return city;
  }

  public String toJSON() {
    Gson gson = new Gson();
    return gson.toJson(this);
  }

  public boolean isFresh() {
    Date currentDate = new Date();
    return currentDate.after(fromDate) && currentDate.before(toDate);
  }

  public boolean isNotFresh() {
    return !isFresh();
  }

  public Date getLastUpdate() {
    return lastUpdate;
  }

  public ArrayList<RainData> getRainData() {
    return data;
  }


  public void setCityIfEmpty(String currentCity) {
    if (city == null || city.length() <= 1) {
      city = currentCity;
    }
  }
}




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