1 package com.android.systemui.plugins 2 3 import android.os.Bundle 4 import androidx.annotation.VisibleForTesting 5 6 class WeatherData 7 private constructor( 8 val description: String, 9 val state: WeatherStateIcon, 10 val useCelsius: Boolean, 11 val temperature: Int, 12 ) { 13 companion object { 14 private const val TAG = "WeatherData" 15 @VisibleForTesting const val DESCRIPTION_KEY = "description" 16 @VisibleForTesting const val STATE_KEY = "state" 17 @VisibleForTesting const val USE_CELSIUS_KEY = "use_celsius" 18 @VisibleForTesting const val TEMPERATURE_KEY = "temperature" 19 private const val INVALID_WEATHER_ICON_STATE = -1 20 fromBundlenull21 fun fromBundle(extras: Bundle): WeatherData? { 22 val description = extras.getString(DESCRIPTION_KEY) 23 val state = 24 WeatherStateIcon.fromInt(extras.getInt(STATE_KEY, INVALID_WEATHER_ICON_STATE)) 25 val temperature = readIntFromBundle(extras, TEMPERATURE_KEY) 26 return if ( 27 description == null || 28 state == null || 29 !extras.containsKey(USE_CELSIUS_KEY) || 30 temperature == null 31 ) 32 null 33 else 34 WeatherData( 35 description = description, 36 state = state, 37 useCelsius = extras.getBoolean(USE_CELSIUS_KEY), 38 temperature = temperature 39 ) 40 } 41 readIntFromBundlenull42 private fun readIntFromBundle(extras: Bundle, key: String): Int? = 43 try { 44 extras.getString(key).toInt() 45 } catch (e: Exception) { 46 null 47 } 48 } 49 50 enum class WeatherStateIcon(val id: Int) { 51 UNKNOWN_ICON(0), 52 53 // Clear, day & night. 54 SUNNY(1), 55 CLEAR_NIGHT(2), 56 57 // Mostly clear, day & night. 58 MOSTLY_SUNNY(3), 59 MOSTLY_CLEAR_NIGHT(4), 60 61 // Partly cloudy, day & night. 62 PARTLY_CLOUDY(5), 63 PARTLY_CLOUDY_NIGHT(6), 64 65 // Mostly cloudy, day & night. 66 MOSTLY_CLOUDY_DAY(7), 67 MOSTLY_CLOUDY_NIGHT(8), 68 CLOUDY(9), 69 HAZE_FOG_DUST_SMOKE(10), 70 DRIZZLE(11), 71 HEAVY_RAIN(12), 72 SHOWERS_RAIN(13), 73 74 // Scattered showers, day & night. 75 SCATTERED_SHOWERS_DAY(14), 76 SCATTERED_SHOWERS_NIGHT(15), 77 78 // Isolated scattered thunderstorms, day & night. 79 ISOLATED_SCATTERED_TSTORMS_DAY(16), 80 ISOLATED_SCATTERED_TSTORMS_NIGHT(17), 81 STRONG_TSTORMS(18), 82 BLIZZARD(19), 83 BLOWING_SNOW(20), 84 FLURRIES(21), 85 HEAVY_SNOW(22), 86 87 // Scattered snow showers, day & night. 88 SCATTERED_SNOW_SHOWERS_DAY(23), 89 SCATTERED_SNOW_SHOWERS_NIGHT(24), 90 SNOW_SHOWERS_SNOW(25), 91 MIXED_RAIN_HAIL_RAIN_SLEET(26), 92 SLEET_HAIL(27), 93 TORNADO(28), 94 TROPICAL_STORM_HURRICANE(29), 95 WINDY_BREEZY(30), 96 WINTRY_MIX_RAIN_SNOW(31); 97 98 companion object { <lambda>null99 fun fromInt(value: Int) = values().firstOrNull { it.id == value } 100 } 101 } 102 toStringnull103 override fun toString(): String { 104 val unit = if (useCelsius) "C" else "F" 105 return "$state (\"$description\") $temperature°$unit" 106 } 107 } 108