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