PageRenderTime 55ms CodeModel.GetById 25ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://android-sky.googlecode.com/
Java | 135 lines | 67 code | 13 blank | 55 comment | 21 complexity | 5bca8a8bbd9e141de19e2eb965fdcaba 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 String[] sIconAlert = new String[] { "alert", "advisory", "warning", "watch" };
 44    private static final String[] sIconStorm = new String[] { "thunder", "tstms" };
 45    private static final String[] sIconSnow = new String[] { "snow", "ice", "frost", "flurr", "wintry" };
 46    private static final String[] sIconShower = new String[] { "rain" };
 47    private static final String[] sIconScatter = new String[] { "shower", "drizzle" };
 48    private static final String[] sIconClear = new String[] { "sunny", "breezy", "clear" };
 49    private static final String[] sIconClouds = new String[] { "cloud", "fog" };
 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        conditions = conditions.toLowerCase();
 62        
 63        if (stringContains(conditions, sIconAlert)) {
 64            icon = R.drawable.weather_severe_alert;
 65        } else if (stringContains(conditions, sIconStorm)) {
 66            icon = R.drawable.weather_storm;
 67        } else if (stringContains(conditions, sIconSnow)) {
 68            icon = R.drawable.weather_snow;
 69        } else if (stringContains(conditions, sIconShower)) {
 70            icon = R.drawable.weather_showers;
 71        } else if (stringContains(conditions, sIconScatter)) {
 72            icon = R.drawable.weather_showers_scattered;
 73        } else if (stringContains(conditions, sIconClear)) {
 74            icon = daytime ? R.drawable.weather_clear : R.drawable.weather_clear_night;
 75        } else if (stringContains(conditions, sIconClouds)) {
 76            icon = daytime ? R.drawable.weather_few_clouds : R.drawable.weather_few_clouds_night;
 77        }
 78        return icon;
 79    }
 80
 81    /**
 82     * Search the subject string for the given words using a case-sensitive
 83     * search. This is usually faster than a {@link Pattern} regular expression
 84     * because we don't have JIT.
 85     */
 86    private static boolean stringContains(String subject, String[] searchWords) {
 87        for (String word : searchWords) {
 88            if (subject.contains(word)) {
 89                return true;
 90            }
 91        }
 92        return false;
 93    }
 94
 95    /**
 96     * Correctly format the given temperature for user display.
 97     * 
 98     * @param temp Temperature to format, in degrees Fahrenheit.
 99     * @param units Target units to convert to before displaying, usually
100     *            something like {@link AppWidgetsColumns#UNITS_FAHRENHEIT}.
101     */
102    public static String formatTemp(Resources res, int temp, int units) {
103        if (units == AppWidgetsColumns.UNITS_FAHRENHEIT) {
104            return res.getString(R.string.temperature_f, temp);
105        } else if (units == AppWidgetsColumns.UNITS_CELSIUS) {
106            // Convert to Celsius before display
107            temp = ((temp - 32) * 5) / 9;
108            return res.getString(R.string.temperature_c, temp);
109        }
110        return null;
111    }
112
113    /**
114     * Get the timestamp of the last midnight, in a base similar to
115     * {@link System#currentTimeMillis()}.
116     */
117    public static long getLastMidnight() {
118        Time time = new Time();
119        time.setToNow();
120        time.hour = 0;
121        time.minute = 0;
122        time.second = 0;
123        return time.toMillis(false);
124    }
125
126    /**
127     * Calcuate if it's currently "daytime" by our internal definition. Used to
128     * decide which icons to show when updating widgets.
129     */
130    public static boolean isDaytime() {
131        Time time = new Time();
132        time.setToNow();
133        return (time.hour >= DAYTIME_BEGIN_HOUR && time.hour <= DAYTIME_END_HOUR);
134    }
135}