PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Sky/Sky/src/org/jsharkey/sky/ForecastUtils.java

http://android-sky.googlecode.com/
Java | 119 lines | 58 code | 11 blank | 50 comment | 19 complexity | 758d4078f593fd2ada73704247e4f596 MD5 | raw file
  1/*
  2 * Copyright (C) 2009 Jeff Sharkey, http://jsharkey.org/
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package org.jsharkey.sky;
 18
 19import java.util.regex.Pattern;
 20
 21import org.jsharkey.sky.ForecastProvider.AppWidgetsColumns;
 22import org.jsharkey.sky.ForecastProvider.ForecastsColumns;
 23
 24import android.content.res.Resources;
 25import android.text.format.Time;
 26
 27/**
 28 * Various forecast utilities.
 29 */
 30public class ForecastUtils {
 31    /**
 32     * Time when we consider daytime to begin. We keep this early to make sure
 33     * that our 6AM widget update will change icons correctly.
 34     */
 35    private static final int DAYTIME_BEGIN_HOUR = 5;
 36
 37    /**
 38     * Time when we consider daytime to end. We keep this early to make sure
 39     * that our 6PM widget update will change icons correctly.
 40     */
 41    private static final int DAYTIME_END_HOUR = 16;
 42
 43    private static final Pattern sIconAlert = Pattern.compile("(alert|advisory|warning|watch)", Pattern.CASE_INSENSITIVE);
 44    private static final Pattern sIconStorm = Pattern.compile("(thunder|tstms)", Pattern.CASE_INSENSITIVE);
 45    private static final Pattern sIconSnow = Pattern.compile("(snow|ice|frost|flurries)", Pattern.CASE_INSENSITIVE);
 46    private static final Pattern sIconShower = Pattern.compile("(rain)", Pattern.CASE_INSENSITIVE);
 47    private static final Pattern sIconScatter = Pattern.compile("(shower|drizzle)", Pattern.CASE_INSENSITIVE);
 48    private static final Pattern sIconClear = Pattern.compile("(sunny|breezy|clear)", Pattern.CASE_INSENSITIVE);
 49    private static final Pattern sIconClouds = Pattern.compile("(cloud|fog)", Pattern.CASE_INSENSITIVE);
 50
 51    /**
 52     * Select an icon to describe the given {@link ForecastsColumns#CONDITIONS}
 53     * string. Uses a descending importance scale that matches keywords against
 54     * the described conditions.
 55     * 
 56     * @param daytime If true, return daylight-specific icons when available,
 57     *            otherwise assume night icons.
 58     */
 59    public static int getIconForForecast(String conditions, boolean daytime) {
 60        int icon = 0;
 61        if (sIconAlert.matcher(conditions).find()) {
 62            icon = R.drawable.weather_severe_alert;
 63        } else if (sIconStorm.matcher(conditions).find()) {
 64            icon = R.drawable.weather_storm;
 65        } else if (sIconSnow.matcher(conditions).find()) {
 66            icon = R.drawable.weather_snow;
 67        } else if (sIconShower.matcher(conditions).find()) {
 68            icon = R.drawable.weather_showers;
 69        } else if (sIconScatter.matcher(conditions).find()) {
 70            icon = R.drawable.weather_showers_scattered;
 71        } else if (sIconClear.matcher(conditions).find()) {
 72            icon = daytime ? R.drawable.weather_clear : R.drawable.weather_clear_night;
 73        } else if (sIconClouds.matcher(conditions).find()) {
 74            icon = daytime ? R.drawable.weather_few_clouds : R.drawable.weather_few_clouds_night;
 75        }
 76        return icon;
 77    }
 78
 79    /**
 80     * Correctly format the given temperature for user display.
 81     * 
 82     * @param temp Temperature to format, in degrees Fahrenheit.
 83     * @param units Target units to convert to before displaying, usually
 84     *            something like {@link AppWidgetsColumns#UNITS_FAHRENHEIT}.
 85     */
 86    public static String formatTemp(Resources res, int temp, int units) {
 87        if (units == AppWidgetsColumns.UNITS_FAHRENHEIT) {
 88            return res.getString(R.string.temperature_f, temp);
 89        } else if (units == AppWidgetsColumns.UNITS_CELSIUS) {
 90            // Convert to Celsius before display
 91            temp = ((temp - 32) * 5) / 9;
 92            return res.getString(R.string.temperature_c, temp);
 93        }
 94        return null;
 95    }
 96
 97    /**
 98     * Get the timestamp of the last midnight, in a base similar to
 99     * {@link System#currentTimeMillis()}.
100     */
101    public static long getLastMidnight() {
102        Time time = new Time();
103        time.setToNow();
104        time.hour = 0;
105        time.minute = 0;
106        time.second = 0;
107        return time.toMillis(false);
108    }
109
110    /**
111     * Calcuate if it's currently "daytime" by our internal definition. Used to
112     * decide which icons to show when updating widgets.
113     */
114    public static boolean isDaytime() {
115        Time time = new Time();
116        time.setToNow();
117        return (time.hour >= DAYTIME_BEGIN_HOUR && time.hour <= DAYTIME_END_HOUR);
118    }
119}