Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8f59470 authored by Luis Vidal's avatar Luis Vidal Committed by Adnan Begovic
Browse files

SystemUI: Update cached weather data when temperature unit changes

Register an observer to keep track of the temperature unit
selected in settings to update the cached data accordingly

TICKET: CYNGNOS-2694

Change-Id: I92adec20835bcd6d6476d793564300611bcba4a1
(cherry picked from commit c241b7e0)
parent a27bbd6b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ import java.text.NumberFormat;

import cyanogenmod.app.StatusBarPanelCustomTile;
import cyanogenmod.providers.CMSettings;
import cyanogenmod.weather.util.WeatherUtils;
import org.cyanogenmod.internal.logging.CMMetricsLogger;

/**
@@ -515,12 +516,12 @@ public class StatusBarHeaderView extends RelativeLayout implements View.OnClickL

    @Override
    public void onWeatherChanged(WeatherController.WeatherInfo info) {
        if (info.temp == null || info.condition == null) {
        if (Double.isNaN(info.temp) || info.condition == null) {
            mWeatherLine1.setText(null);
        } else {
            mWeatherLine1.setText(mContext.getString(
                    R.string.status_bar_expanded_header_weather_format,
                    info.temp,
                    WeatherUtils.formatTemperature(info.temp, info.tempUnit),
                    info.condition));
        }
        mWeatherLine2.setText(info.city);
+2 −1
Original line number Diff line number Diff line
@@ -25,8 +25,9 @@ public interface WeatherController {
        void onWeatherChanged(WeatherInfo temp);
    }
    public static class WeatherInfo {
        public String temp = null;
        public double temp = Double.NaN;
        public String city = null;
        public String condition = null;
        public int tempUnit;
    }
}
+63 −8
Original line number Diff line number Diff line
@@ -21,9 +21,12 @@ import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import cyanogenmod.providers.CMSettings;
import cyanogenmod.providers.WeatherContract;
import cyanogenmod.weather.CMWeatherManager;
import cyanogenmod.weather.util.WeatherUtils;

import java.util.ArrayList;
@@ -32,6 +35,8 @@ import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_CITY;
import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_CONDITION;
import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_TEMPERATURE;
import static cyanogenmod.providers.WeatherContract.WeatherColumns.CURRENT_TEMPERATURE_UNIT;
import static cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit.CELSIUS;
import static cyanogenmod.providers.WeatherContract.WeatherColumns.TempUnit.FAHRENHEIT;

public class WeatherControllerImpl implements WeatherController {

@@ -39,6 +44,8 @@ public class WeatherControllerImpl implements WeatherController {
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    private WeatherContentObserver mWeatherContentObserver;
    private Handler mHandler;
    private int mWeatherUnit;
    private Uri mWeatherTempetarureUri;

    public static final ComponentName COMPONENT_WEATHER_FORECAST = new ComponentName(
            "com.cyanogenmod.lockclock", "com.cyanogenmod.lockclock.weather.ForecastActivity");
@@ -61,9 +68,13 @@ public class WeatherControllerImpl implements WeatherController {
                mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        mHandler = new Handler();
        mWeatherContentObserver = new WeatherContentObserver(mHandler);
        mWeatherTempetarureUri
                = CMSettings.Global.getUriFor(CMSettings.Global.WEATHER_TEMPERATURE_UNIT);
        mContext.getContentResolver().registerContentObserver(
                WeatherContract.WeatherColumns.CURRENT_WEATHER_URI,
                true, mWeatherContentObserver);
                WeatherContract.WeatherColumns.CURRENT_WEATHER_URI,true, mWeatherContentObserver);
        mContext.getContentResolver().registerContentObserver(mWeatherTempetarureUri, true,
                mWeatherContentObserver);
        queryWeatherTempUnit();
        queryWeather();
    }

@@ -97,7 +108,16 @@ public class WeatherControllerImpl implements WeatherController {
        } else {
            try {
                c.moveToFirst();
                mCachedInfo.temp = WeatherUtils.formatTemperature(c.getDouble(0), c.getInt(1));
                double temp = c.getDouble(0);
                int reportedUnit = c.getInt(1);
                if (reportedUnit == CELSIUS && mWeatherUnit == FAHRENHEIT) {
                    temp = WeatherUtils.celsiusToFahrenheit(temp);
                } else if (reportedUnit == FAHRENHEIT && mWeatherUnit == CELSIUS) {
                    temp = WeatherUtils.fahrenheitToCelsius(temp);
                }

                mCachedInfo.temp = temp;
                mCachedInfo.tempUnit = mWeatherUnit;
                mCachedInfo.city = c.getString(2);
                mCachedInfo.condition = c.getString(3);
            } finally {
@@ -112,18 +132,53 @@ public class WeatherControllerImpl implements WeatherController {
        }
    }

    private final class WeatherContentObserver extends ContentObserver {
    private class WeatherContentObserver extends ContentObserver {

        public WeatherContentObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if (DEBUG) Log.d(TAG, "Received onChange notification");
        public void onChange(boolean selfChange, Uri uri) {
            if (uri != null) {
                if (uri.compareTo(WeatherContract.WeatherColumns.CURRENT_WEATHER_URI) == 0) {
                    queryWeather();
                    fireCallback();
                } else if (uri.compareTo(mWeatherTempetarureUri) == 0) {
                    queryWeatherTempUnit();
                    fixCachedWeatherInfo();
                    fireCallback();
                } else {
                    super.onChange(selfChange, uri);
                }
            }
        }

        @Override
        public void onChange(boolean selfChange) {
            onChange(selfChange, null);
        }
    }

    private void queryWeatherTempUnit() {
        try {
            mWeatherUnit = CMSettings.Global.getInt(mContext.getContentResolver(),
                    CMSettings.Global.WEATHER_TEMPERATURE_UNIT);
        } catch (CMSettings.CMSettingNotFoundException e) {
            //CMSettingsProvider should have taken care of setting a default value for this setting
            //so how is that we ended up here?? We need to set a valid temp unit anyway to keep
            //this going
            mWeatherUnit = WeatherContract.WeatherColumns.TempUnit.CELSIUS;
        }
    }

    private void fixCachedWeatherInfo() {
        if (mCachedInfo.tempUnit == CELSIUS && mWeatherUnit == FAHRENHEIT) {
            mCachedInfo.temp = WeatherUtils.celsiusToFahrenheit(mCachedInfo.temp);
            mCachedInfo.tempUnit = FAHRENHEIT;
        } else if (mCachedInfo.tempUnit == FAHRENHEIT && mWeatherUnit == CELSIUS) {
            mCachedInfo.temp = WeatherUtils.fahrenheitToCelsius(mCachedInfo.temp);
            mCachedInfo.tempUnit = CELSIUS;
        }
    }
}