PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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. package org.jsharkey.sky;
  17. import java.util.regex.Pattern;
  18. import org.jsharkey.sky.ForecastProvider.AppWidgetsColumns;
  19. import org.jsharkey.sky.ForecastProvider.ForecastsColumns;
  20. import android.content.res.Resources;
  21. import android.text.format.Time;
  22. /**
  23. * Various forecast utilities.
  24. */
  25. public class ForecastUtils {
  26. /**
  27. * Time when we consider daytime to begin. We keep this early to make sure
  28. * that our 6AM widget update will change icons correctly.
  29. */
  30. private static final int DAYTIME_BEGIN_HOUR = 5;
  31. /**
  32. * Time when we consider daytime to end. We keep this early to make sure
  33. * that our 6PM widget update will change icons correctly.
  34. */
  35. private static final int DAYTIME_END_HOUR = 16;
  36. private static final Pattern sIconAlert = Pattern.compile("(alert|advisory|warning|watch)", Pattern.CASE_INSENSITIVE);
  37. private static final Pattern sIconStorm = Pattern.compile("(thunder|tstms)", Pattern.CASE_INSENSITIVE);
  38. private static final Pattern sIconSnow = Pattern.compile("(snow|ice|frost|flurries)", Pattern.CASE_INSENSITIVE);
  39. private static final Pattern sIconShower = Pattern.compile("(rain)", Pattern.CASE_INSENSITIVE);
  40. private static final Pattern sIconScatter = Pattern.compile("(shower|drizzle)", Pattern.CASE_INSENSITIVE);
  41. private static final Pattern sIconClear = Pattern.compile("(sunny|breezy|clear)", Pattern.CASE_INSENSITIVE);
  42. private static final Pattern sIconClouds = Pattern.compile("(cloud|fog)", Pattern.CASE_INSENSITIVE);
  43. /**
  44. * Select an icon to describe the given {@link ForecastsColumns#CONDITIONS}
  45. * string. Uses a descending importance scale that matches keywords against
  46. * the described conditions.
  47. *
  48. * @param daytime If true, return daylight-specific icons when available,
  49. * otherwise assume night icons.
  50. */
  51. public static int getIconForForecast(String conditions, boolean daytime) {
  52. int icon = 0;
  53. if (sIconAlert.matcher(conditions).find()) {
  54. icon = R.drawable.weather_severe_alert;
  55. } else if (sIconStorm.matcher(conditions).find()) {
  56. icon = R.drawable.weather_storm;
  57. } else if (sIconSnow.matcher(conditions).find()) {
  58. icon = R.drawable.weather_snow;
  59. } else if (sIconShower.matcher(conditions).find()) {
  60. icon = R.drawable.weather_showers;
  61. } else if (sIconScatter.matcher(conditions).find()) {
  62. icon = R.drawable.weather_showers_scattered;
  63. } else if (sIconClear.matcher(conditions).find()) {
  64. icon = daytime ? R.drawable.weather_clear : R.drawable.weather_clear_night;
  65. } else if (sIconClouds.matcher(conditions).find()) {
  66. icon = daytime ? R.drawable.weather_few_clouds : R.drawable.weather_few_clouds_night;
  67. }
  68. return icon;
  69. }
  70. /**
  71. * Correctly format the given temperature for user display.
  72. *
  73. * @param temp Temperature to format, in degrees Fahrenheit.
  74. * @param units Target units to convert to before displaying, usually
  75. * something like {@link AppWidgetsColumns#UNITS_FAHRENHEIT}.
  76. */
  77. public static String formatTemp(Resources res, int temp, int units) {
  78. if (units == AppWidgetsColumns.UNITS_FAHRENHEIT) {
  79. return res.getString(R.string.temperature_f, temp);
  80. } else if (units == AppWidgetsColumns.UNITS_CELSIUS) {
  81. // Convert to Celsius before display
  82. temp = ((temp - 32) * 5) / 9;
  83. return res.getString(R.string.temperature_c, temp);
  84. }
  85. return null;
  86. }
  87. /**
  88. * Get the timestamp of the last midnight, in a base similar to
  89. * {@link System#currentTimeMillis()}.
  90. */
  91. public static long getLastMidnight() {
  92. Time time = new Time();
  93. time.setToNow();
  94. time.hour = 0;
  95. time.minute = 0;
  96. time.second = 0;
  97. return time.toMillis(false);
  98. }
  99. /**
  100. * Calcuate if it's currently "daytime" by our internal definition. Used to
  101. * decide which icons to show when updating widgets.
  102. */
  103. public static boolean isDaytime() {
  104. Time time = new Time();
  105. time.setToNow();
  106. return (time.hour >= DAYTIME_BEGIN_HOUR && time.hour <= DAYTIME_END_HOUR);
  107. }
  108. }