public class ClientRawCache {
    private static SparseArray<ClientRaw> cache = new SparseArray<ClientRaw>();

    private ClientRawCache() {
    }

    public static void update(int appWidgetId, ClientRaw clientRaw) {
        cache.put(appWidgetId, clientRaw);
    }

    public static ClientRaw fetch(int appWidgetId) {
        return cache.get(appWidgetId);
    }

    public static void remove(int appWidget) {
        cache.remove(appWidget);
    }

    public static boolean isCached(int appWidget) {
        return fetch(appWidget) != null;
    }
} public enum WeatherItem {
    APPARENT_TEMPERATURE("Apparent Temperature"),
    AVERAGE_WIND("Average Wind"),
    BLANK("Blank"),
    DAILY_RAINFALL("Daily Rainfall"),
    DEW_POINT("Dew Point"),
    FORECAST("Forecast"),
    GUST("Gust"),
    HEAT_INDEX("Heat Index"),
    HUMIDEX("Humidex"),
    HUMIDITY("Humidity"),
    INDOOR_CONDITIONS("Indoor Conditions"),
    RAINFALL_RATE("Rainfall Rate"),
    SOLAR("Solar"),
    SURFACE_PRESSURE("Surface Pressure"),
    UV_INDEX("UV Index"),
    WIND_CHILL("Wind Chill");

    private String description;

    WeatherItem(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public class Preferences {
    private SharedPreferences preferences;
    private Editor editor;

    private static final String CONFIGURED = "configured";
    private static final String CLIENT_RAW_URL = "client-raw-url";
    private static final String STATION_NAME = "station_name";
    private static final String WEATHER_ITEM_1 = "weather-item-1";
    private static final String WEATHER_ITEM_2 = "weather-item-2";
    private static final String WEATHER_ITEM_3 = "weather-item-3";
    private static final String WEATHER_ITEM_4 = "weather-item-4";
    private static final String WEATHER_ITEM_5 = "weather-item-5";
    private static final String TEMPERATURE_UNIT = "temperature-unit";
    private static final String PRESSURE_UNIT = "pressure-unit";
    private static final String WIND_SPEED_UNIT = "wind-speed-unit";
    private static final String WIND_DIRECTION_UNIT = "wind-direction-unit";
    private static final String RAINFALL_UNIT = "rainfall-unit";
    private static final String DATE_FORMAT = "date-format";
    private static final String TIME_FORMAT = "time-format";
    private static final String TRANSPARENT = "transparent";
    private static final String CONNECTION_TIMEOUT = "connection-timeout";
    private static final String READ_TIMEOUT = "read-timeout";

    public Preferences(Context context) {
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public boolean isConfigured(int appWidgetId) {
        return Boolean.valueOf(getPreference(CONFIGURED, appWidgetId, Boolean.FALSE.toString()));
    }

    public void setConfigured(int appWidgetId, boolean value) {
        setPreference(CONFIGURED, appWidgetId, Boolean.toString(value));
    }

    public String getClientRawUrl(int appWidgetId) {
        return getPreference(CLIENT_RAW_URL, appWidgetId, null);
    }

    public void setClientRawUrl(int appWidgetId, String value) {
        setPreference(CLIENT_RAW_URL, appWidgetId, value);
    }

    public String getStationName(int appWidgetId) {
        return getPreference(STATION_NAME, appWidgetId, null);
    }

    public void setStationName(int appWidgetId, String value) {
        setPreference(STATION_NAME, appWidgetId, value);
    }

    public void removeStationName(int appWidgetId) {
        removePreference(STATION_NAME, appWidgetId);
    }

    public WeatherItem getWeatherItem1(int appWidgetId) {
        return WeatherItem.valueOf(getPreference(WEATHER_ITEM_1, appWidgetId, WeatherItem.SURFACE_PRESSURE.name()));
    }

    public void setWeatherItem1(int appWidgetId, WeatherItem value) {
        setPreference(WEATHER_ITEM_1, appWidgetId, value.name());
    }

    public WeatherItem getWeatherItem2(int appWidgetId) {
        return WeatherItem.valueOf(getPreference(WEATHER_ITEM_2, appWidgetId, WeatherItem.HUMIDITY.name()));
    }

    public void setWeatherItem2(int appWidgetId, WeatherItem value) {
        setPreference(WEATHER_ITEM_2, appWidgetId, value.name());
    }

    public WeatherItem getWeatherItem3(int appWidgetId) {
        return WeatherItem.valueOf(getPreference(WEATHER_ITEM_3, appWidgetId, WeatherItem.DEW_POINT.name()));
    }

    public void setWeatherItem3(int appWidgetId, WeatherItem value) {
        setPreference(WEATHER_ITEM_3, appWidgetId, value.name());
    }

    public WeatherItem getWeatherItem4(int appWidgetId) {
        return WeatherItem.valueOf(getPreference(WEATHER_ITEM_4, appWidgetId, WeatherItem.AVERAGE_WIND.name()));
    }

    public void setWeatherItem4(int appWidgetId, WeatherItem value) {
        setPreference(WEATHER_ITEM_4, appWidgetId, value.name());
    }

    public WeatherItem getWeatherItem5(int appWidgetId) {
        return WeatherItem.valueOf(getPreference(WEATHER_ITEM_5, appWidgetId, WeatherItem.DAILY_RAINFALL.name()));
    }

    public void setWeatherItem5(int appWidgetId, WeatherItem value) {
        setPreference(WEATHER_ITEM_5, appWidgetId, value.name());
    }

    public TemperatureUnit getTemperatureUnit(int appWidgetId) {
        return TemperatureUnit.valueOf(getPreference(TEMPERATURE_UNIT, appWidgetId, TemperatureUnit.CELSIUS.name()));
    }

    public void setTemperatureUnit(int appWidgetId, TemperatureUnit value) {
        setPreference(TEMPERATURE_UNIT, appWidgetId, value.name());
    }

    public PressureUnit getPressureUnit(int appWidgetId) {
        return PressureUnit.valueOf(getPreference(PRESSURE_UNIT, appWidgetId, PressureUnit.HECTOPASCALS.name()));
    }

    public void setPressureUnit(int appWidgetId, PressureUnit value) {
        setPreference(PRESSURE_UNIT, appWidgetId, value.name());
    }

    public WindSpeedUnit getWindSpeedUnit(int appWidgetId) {
        return WindSpeedUnit.valueOf(getPreference(WIND_SPEED_UNIT, appWidgetId, WindSpeedUnit.KILOMETRES_PER_HOUR.name()));
    }

    public void setWindSpeedUnit(int appWidgetId, WindSpeedUnit value) {
        setPreference(WIND_SPEED_UNIT, appWidgetId, value.name());
    }

    public WindDirectionUnit getWindDirectionUnit(int appWidgetId) {
        return WindDirectionUnit.valueOf(getPreference(WIND_DIRECTION_UNIT, appWidgetId, WindDirectionUnit.CARDINAL_DIRECTION.name()));
    }

    public void setWindDirectionUnit(int appWidgetId, WindDirectionUnit value) {
        setPreference(WIND_DIRECTION_UNIT, appWidgetId, value.name());
    }

    public RainfallUnit getRainfallUnit(int appWidgetId) {
        return RainfallUnit.valueOf(getPreference(RAINFALL_UNIT, appWidgetId, RainfallUnit.MILLIMETRES.name()));
    }

    public void setRainfallUnit(int appWidgetId, RainfallUnit value) {
        setPreference(RAINFALL_UNIT, appWidgetId, value.name());
    }

    public DateFormat getDateFormat(int appWidgetId) {
        String dateFormatStr = getPreference(DATE_FORMAT, appWidgetId, null);
        DateFormat dateFormat = null;

        if (dateFormatStr != null) {
            dateFormat = DateFormat.valueOf(dateFormatStr);
        }

        return dateFormat;
    }

    public void setDateFormat(int appWidgetId, DateFormat value) {
        setPreference(DATE_FORMAT, appWidgetId, value.name());
    }

    public void removeDateFormat(int appWidgetId) {
        removePreference(DATE_FORMAT, appWidgetId);
    }

    public TimeFormat getTimeFormat(int appWidgetId) {
        return TimeFormat.valueOf(getPreference(TIME_FORMAT, appWidgetId, TimeFormat.HOUR_24.name()));
    }

    public void setTimeFormat(int appWidgetId, TimeFormat value) {
        setPreference(TIME_FORMAT, appWidgetId, value.name());
    }

    public boolean isTransparent(int appWidgetId) {
        return Boolean.valueOf(getPreference(TRANSPARENT, appWidgetId, Boolean.FALSE.toString()));
    }

    public void setTransparent(int appWidgetId, boolean value) {
        setPreference(TRANSPARENT, appWidgetId, Boolean.toString(value));
    }

    public Timeout getConnectionTimeout(int appWidgetId) {
        return Timeout.valueOf(getPreference(CONNECTION_TIMEOUT, appWidgetId, Timeout.TEN_SECONDS.name()));
    }

    public void setConnectionTimeout(int appWidgetId, Timeout value) {
        setPreference(CONNECTION_TIMEOUT, appWidgetId, value.name());
    }

    public Timeout getReadTimeout(int appWidgetId) {
        return Timeout.valueOf(getPreference(READ_TIMEOUT, appWidgetId, Timeout.TEN_SECONDS.name()));
    }

    public void setReadTimeout(int appWidgetId, Timeout value) {
        setPreference(READ_TIMEOUT, appWidgetId, value.name());
    }

    public void removePreferences(int appWidgetId) {
        removePreference(CONFIGURED, appWidgetId);
        removePreference(CLIENT_RAW_URL, appWidgetId);
        removePreference(STATION_NAME, appWidgetId);
        removePreference(WEATHER_ITEM_1, appWidgetId);
        removePreference(WEATHER_ITEM_2, appWidgetId);
        removePreference(WEATHER_ITEM_3, appWidgetId);
        removePreference(WEATHER_ITEM_4, appWidgetId);
        removePreference(WEATHER_ITEM_5, appWidgetId);
        removePreference(TEMPERATURE_UNIT, appWidgetId);
        removePreference(PRESSURE_UNIT, appWidgetId);
        removePreference(WIND_SPEED_UNIT, appWidgetId);
        removePreference(WIND_DIRECTION_UNIT, appWidgetId);
        removePreference(RAINFALL_UNIT, appWidgetId);
        removePreference(DATE_FORMAT, appWidgetId);
        removePreference(TIME_FORMAT, appWidgetId);
        removePreference(TRANSPARENT, appWidgetId);
        removePreference(CONNECTION_TIMEOUT, appWidgetId);
        removePreference(READ_TIMEOUT, appWidgetId);
    }

    public void commit() {
        if (editor != null) {
            editor.commit();
            editor = null;
        }
    }

    private String getPreference(String key, int appWidgetId, String defaultValue) {
        return preferences.getString(getKeyForAppWidgetId(key, appWidgetId), defaultValue);
    }

    @SuppressLint("CommitPrefEdits")
    private void setPreference(String key, int appWidgetId, String value) {
        if (editor == null) {
            editor = preferences.edit();
        }

        editor.putString(getKeyForAppWidgetId(key, appWidgetId), value);
    }

    @SuppressLint("CommitPrefEdits")
    private void removePreference(String key, int appWidgetId) {
        if (editor == null) {
            editor = preferences.edit();
        }

        editor.remove(getKeyForAppWidgetId(key, appWidgetId));
    }

    private String getKeyForAppWidgetId(String key, int appWidgetId) {
        return key + "-" + appWidgetId;
    }
} public enum DateFormat {
    YYYY_MM_DD("YYYY/MM/DD"),
    DD_MM_YYYY("DD/MM/YYYY"),
    MM_DD_YYYY("MM/DD/YYYY");

    private String description;

    DateFormat(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum TimeFormat {
    HOUR_24("24 Hour"),
    HOUR_12("12 Hour");

    private String description;

    TimeFormat(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum PressureUnit {
    HECTOPASCALS("Hectopascals (hPa)"),
    INCHES_OF_MERCURY("Inches of Mercury (inHg)"),
    KILIOPASCALS("Kilopascals (kPa)"),
    MILLIBARS("Millibars (mb)"),
    MILLIMETRES_OF_MERCURY("Millimetres of Mercury (mmHg)");

    private String description;

    PressureUnit(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum RainfallUnit {
    INCHES("Inches (in)"),
    MILLIMETRES("Millimetres (mm)");

    private String description;

    RainfallUnit(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum TemperatureUnit {
    CELSIUS("Celsius (°C)"),
    FAHRENHEIT("Fahrenheit (°F)");

    private String description;

    TemperatureUnit(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum WindDirectionUnit {
    CARDINAL_DIRECTION("Cardinal Direction"),
    COMPASS_DEGREES("Compass Degrees");

    private String description;

    WindDirectionUnit(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum WindSpeedUnit {
    BEAUFORT_SCALE("Beaufort Scale (Bft)"),
    KILOMETRES_PER_HOUR("Kilometres per Hour (km/h)"),
    KNOTS("Knots (kts)"),
    METRES_PER_SECOND("Metres per Second (m/s)"),
    MILES_PER_HOUR("Miles per Hour (mph)");

    private String description;

    WindSpeedUnit(String description) {
        this.description = description;
    }

    public String toString() {
        return description;
    }
} public enum Timeout {
    NONE("None", 0),
    FIVE_SECONDS("5 Seconds", 5000),
    TEN_SECONDS("10 Seconds", 10000),
    THIRTY_SECONDS("30 Seconds", 30000),
    ONE_MINUTE("1 Minute", 60000);

    private String description;
    private int timeoutMsecs;

    Timeout(String description, int timeoutMsecs) {
        this.description = description;
        this.timeoutMsecs = timeoutMsecs;
    }

    public int getTimeoutMsecs() {
        return timeoutMsecs;
    }

    public String toString() {
        return description;
    }
} public static final String FETCH_FRESH_CLIENT_RAW = "com.waynedgrant.cirrus.fetchFreshClientRaw";
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.waynedgrant.cirrus.clientraw.ClientRawCache;
import com.waynedgrant.cirrus.measures.WeatherItem;
import com.waynedgrant.cirrus.preferences.Preferences;
import com.waynedgrant.cirrus.presentation.formatters.DateFormat;
import com.waynedgrant.cirrus.presentation.formatters.TimeFormat;
import com.waynedgrant.cirrus.units.PressureUnit;
import com.waynedgrant.cirrus.units.RainfallUnit;
import com.waynedgrant.cirrus.units.TemperatureUnit;
import com.waynedgrant.cirrus.units.WindDirectionUnit;
import com.waynedgrant.cirrus.units.WindSpeedUnit;
import com.waynedgrant.cirrus.update.Timeout;
import java.text.MessageFormat;
import static android.R.layout.simple_spinner_dropdown_item;
import static android.R.layout.simple_spinner_item;
import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID;
import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS;
import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
import static android.content.DialogInterface.BUTTON_NEUTRAL;
import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.waynedgrant.cirrus.R.drawable.ic_launcher;
import static com.waynedgrant.cirrus.R.id.clientRawUrlEditText;
import static com.waynedgrant.cirrus.R.id.connectionTimeoutSpinner;
import static com.waynedgrant.cirrus.R.id.dateFormatSpinner;
import static com.waynedgrant.cirrus.R.id.dateFormatTextView;
import static com.waynedgrant.cirrus.R.id.displayDateCheckBox;
import static com.waynedgrant.cirrus.R.id.overrideStationNameCheckBox;
import static com.waynedgrant.cirrus.R.id.pressureUnitSpinner;
import static com.waynedgrant.cirrus.R.id.rainfallUnitSpinner;
import static com.waynedgrant.cirrus.R.id.readTimeoutSpinner;
import static com.waynedgrant.cirrus.R.id.stationNameEditText;
import static com.waynedgrant.cirrus.R.id.temperatureUnitSpinner;
import static com.waynedgrant.cirrus.R.id.timeFormatSpinner;
import static com.waynedgrant.cirrus.R.id.transparentCheckBox;
import static com.waynedgrant.cirrus.R.id.weatherItem1Spinner;
import static com.waynedgrant.cirrus.R.id.weatherItem2Spinner;
import static com.waynedgrant.cirrus.R.id.weatherItem3Spinner;
import static com.waynedgrant.cirrus.R.id.weatherItem4Spinner;
import static com.waynedgrant.cirrus.R.id.weatherItem5Spinner;
import static com.waynedgrant.cirrus.R.id.windDirectionUnitSpinner;
import static com.waynedgrant.cirrus.R.id.windSpeedUnitSpinner;
import static com.waynedgrant.cirrus.R.layout.widget_config_layout;
import static com.waynedgrant.cirrus.R.string.about_message;
import static com.waynedgrant.cirrus.R.string.about_title;
import static com.waynedgrant.cirrus.R.string.app_name;
import static com.waynedgrant.cirrus.R.string.clientRawUrlProtocolNotSupported_message;
import static com.waynedgrant.cirrus.R.string.clientRawUrlRequired_message;
import static com.waynedgrant.cirrus.R.string.stationNameRequired_message;
import static com.waynedgrant.cirrus.WidgetProvider.FETCH_FRESH_CLIENT_RAW;
/* Copyright 2017 Wayne D Grant (www.waynedgrant.com)
   Licensed under the MIT License */

package com.waynedgrant.cirrus;





public class WidgetConfigActivity extends Activity {
    private static final String TAG = "WidgetConfigActivity";

    private int appWidgetId;
    private Intent resultValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate()");

        super.onCreate(savedInstanceState);

        setContentView(widget_config_layout);

        Intent intent = getIntent();
        appWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID);

        resultValue = new Intent();
        resultValue.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);

        setResult(RESULT_CANCELED, resultValue);

        if (appWidgetId == INVALID_APPWIDGET_ID) {
            finish();
        }

        populateActivity();
    }

    private void populateActivity() {
        Preferences preferences = new Preferences(getApplicationContext());

        String clientRawUrl = preferences.getClientRawUrl(appWidgetId);

        EditText editTextUrl = (EditText) findViewById(clientRawUrlEditText);
        editTextUrl.setText(clientRawUrl);

        String stationName = preferences.getStationName(appWidgetId);

        if (stationName != null) {
            CheckBox overrideStationNameCheckBoxControl = (CheckBox) findViewById(overrideStationNameCheckBox);
            overrideStationNameCheckBoxControl.setChecked(true);

            EditText stationNameEditTextControl = (EditText) findViewById(stationNameEditText);
            stationNameEditTextControl.setText(stationName);
            stationNameEditTextControl.setVisibility(VISIBLE);
        }

        WeatherItem[] weatherItems = WeatherItem.values();

        populateSpinner(weatherItem1Spinner, weatherItems, preferences.getWeatherItem1(appWidgetId));
        populateSpinner(weatherItem2Spinner, weatherItems, preferences.getWeatherItem2(appWidgetId));
        populateSpinner(weatherItem3Spinner, weatherItems, preferences.getWeatherItem3(appWidgetId));
        populateSpinner(weatherItem4Spinner, weatherItems, preferences.getWeatherItem4(appWidgetId));
        populateSpinner(weatherItem5Spinner, weatherItems, preferences.getWeatherItem5(appWidgetId));

        populateSpinner(temperatureUnitSpinner, TemperatureUnit.values(), preferences.getTemperatureUnit(appWidgetId));
        populateSpinner(pressureUnitSpinner, PressureUnit.values(), preferences.getPressureUnit(appWidgetId));
        populateSpinner(windSpeedUnitSpinner, WindSpeedUnit.values(), preferences.getWindSpeedUnit(appWidgetId));
        populateSpinner(windDirectionUnitSpinner, WindDirectionUnit.values(), preferences.getWindDirectionUnit(appWidgetId));
        populateSpinner(rainfallUnitSpinner, RainfallUnit.values(), preferences.getRainfallUnit(appWidgetId));

        populateSpinner(dateFormatSpinner, DateFormat.values(), preferences.getDateFormat(appWidgetId));

        if (preferences.getDateFormat(appWidgetId) != null) {
            CheckBox displayDateCheckBoxControl = (CheckBox) findViewById(displayDateCheckBox);
            displayDateCheckBoxControl.setChecked(true);

            TextView dateFormatTextViewControl = (TextView) findViewById(dateFormatTextView);
            dateFormatTextViewControl.setVisibility(VISIBLE);

            Spinner dateFormatSpinnerControl = (Spinner) findViewById(dateFormatSpinner);
            dateFormatSpinnerControl.setVisibility(VISIBLE);
        }

        populateSpinner(timeFormatSpinner, TimeFormat.values(), preferences.getTimeFormat(appWidgetId));

        populateSpinner(connectionTimeoutSpinner, Timeout.values(), preferences.getConnectionTimeout(appWidgetId));
        populateSpinner(readTimeoutSpinner, Timeout.values(), preferences.getReadTimeout(appWidgetId));

        boolean transparent = preferences.isTransparent(appWidgetId);

        CheckBox transparentCheckBoxControl = (CheckBox) findViewById(transparentCheckBox);
        transparentCheckBoxControl.setChecked(transparent);
    }

    private <T> void populateSpinner(int spinnerId, T[] items, T selectedItem) {
        ArrayAdapter<T> arrayAdapter = new ArrayAdapter<T>(this, simple_spinner_item, items);

        Spinner spinner = (Spinner) findViewById(spinnerId);
        arrayAdapter.setDropDownViewResource(simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setSelection(arrayAdapter.getPosition(selectedItem));
    }

    public void onClickOverrideStationName(View view) {
        Log.d(TAG, "onClickOverrideStationName()");

        CheckBox overrideStationNameCheckBox = (CheckBox) view;
        EditText stationNameEditTextControl = (EditText) findViewById(stationNameEditText);

        if (overrideStationNameCheckBox.isChecked()) {
            stationNameEditTextControl.setVisibility(VISIBLE);
        } else {
            stationNameEditTextControl.setVisibility(GONE);
        }
    }

    public void onClickDisplayDate(View view) {
        Log.d(TAG, "onClickDisplayDate()");

        CheckBox displayDateCheckBoxControl = (CheckBox) view;
        TextView dateFormatTextViewControl = (TextView) findViewById(dateFormatTextView);
        Spinner dateFormatSpinnerControl = (Spinner) findViewById(dateFormatSpinner);

        if (displayDateCheckBoxControl.isChecked()) {
            dateFormatTextViewControl.setVisibility(VISIBLE);
            dateFormatSpinnerControl.setVisibility(VISIBLE);
        } else {
            dateFormatTextViewControl.setVisibility(GONE);
            dateFormatSpinnerControl.setVisibility(GONE);
        }
    }

    public void onClickOk(View view) {
        Log.d(TAG, "onClickOk()");

        Context context = getApplicationContext();

        if (!isClientRawUrlPopulated()) {
            displayWarningDialog(context.getString(clientRawUrlRequired_message));
        } else if (!isClientRawUrlProtocolSupported()) {
            displayWarningDialog(context.getString(clientRawUrlProtocolNotSupported_message));
        } else if (isStationNameOverridden() && !isStationNamePopulated()) {
            displayWarningDialog(context.getString(stationNameRequired_message));
        } else {
            Preferences preferences = new Preferences(getApplicationContext());
            String oldClientRawUrl = preferences.getClientRawUrl(appWidgetId);

            updatePreferences(context);

            String newClientRawUrl = preferences.getClientRawUrl(appWidgetId);

            setResult(RESULT_OK, resultValue);

            Intent updateServiceIntent = new Intent(context, UpdateWidgetService.class);
            updateServiceIntent.putExtra(EXTRA_APPWIDGET_IDS, new int[]{appWidgetId});

            boolean fetchFreshClientRaw = !newClientRawUrl.equals(oldClientRawUrl) || !ClientRawCache.isCached(appWidgetId);
            updateServiceIntent.putExtra(FETCH_FRESH_CLIENT_RAW, fetchFreshClientRaw);