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

Commit 812d8b08 authored by Luis Vidal's avatar Luis Vidal Committed by Meninblack007
Browse files

Add Weather Content Provider [5/5]

Use the Weather Content Provider in the cmsdk to pull the
weather data.

However, SystemUI will rely on LockClock to force weather
updates via the new Weather Service, which in turn will
send a broadcast when new weather data is available.

Change-Id: I3c65ea5f4cc297a7944fcdef33f496cdf2d68d0a

SystemUI: Expose weather text colors

Weather text colors in the expanded status bar are hardcoded to
white. Expose these via cm_colors so that themes can color them
however they'd like.

A martini. Shaken, not stirred.

Change-Id: I5e5a738c8b97f556562114a66d102fcb924a0493

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
parent 05c7a65c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -17,4 +17,9 @@
<resources>
    <!-- Status bar battery level text color -->
    <color name="status_bar_battery_level_text_color">#ffffff</color>

    <!-- Expanded Status Bar Weather Text Colors -->
    <color name="status_bar_temperature_text_color">#FFFFFFFF</color>
    <color name="status_bar_temperature_location_text_color">#FFFFFFFF</color>

</resources>
+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;
    }
}
+88 −30
Original line number Diff line number Diff line
@@ -16,43 +16,49 @@

package com.android.systemui.statusbar.policy;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
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;

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 {

    private static final String TAG = WeatherController.class.getSimpleName();
    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");
    public static final String ACTION_UPDATE_FINISHED
            = "com.cyanogenmod.lockclock.action.WEATHER_UPDATE_FINISHED";
    public static final String EXTRA_UPDATE_CANCELLED = "update_cancelled";
    public static final String ACTION_FORCE_WEATHER_UPDATE
            = "com.cyanogenmod.lockclock.action.FORCE_WEATHER_UPDATE";
    public static final Uri CURRENT_WEATHER_URI
            = Uri.parse("content://com.cyanogenmod.lockclock.weather.provider/weather/current");
    public static final String[] WEATHER_PROJECTION = new String[]{
            "temperature",
            "city",
            "condition"
    private static final String[] WEATHER_PROJECTION = new String[]{
            CURRENT_TEMPERATURE,
            CURRENT_TEMPERATURE_UNIT,
            CURRENT_CITY,
            CURRENT_CONDITION
    };

    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
    private final Receiver mReceiver = new Receiver();
    private final Context mContext;

    private WeatherInfo mCachedInfo = new WeatherInfo();
@@ -60,10 +66,16 @@ public class WeatherControllerImpl implements WeatherController {
    public WeatherControllerImpl(Context context) {
        mContext = context;
                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);
        mContext.getContentResolver().registerContentObserver(mWeatherTempetarureUri, true,
                mWeatherContentObserver);
        queryWeatherTempUnit();
        queryWeather();
        final IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_UPDATE_FINISHED);
        mContext.registerReceiver(mReceiver, filter);
    }

    public void addCallback(Callback callback) {
@@ -85,17 +97,29 @@ public class WeatherControllerImpl implements WeatherController {
    }

    private void queryWeather() {
        Cursor c = mContext.getContentResolver().query(CURRENT_WEATHER_URI, WEATHER_PROJECTION,
        Cursor c = mContext.getContentResolver().query(
                WeatherContract.WeatherColumns.CURRENT_WEATHER_URI, WEATHER_PROJECTION,
                null, null, null);
        if (c == null) {
            if(DEBUG) Log.e(TAG, "cursor was null for temperature, forcing weather update");
            //LockClock keeps track of the user settings (temp unit, search by geo location/city)
            //so we delegate the responsibility of handling a weather update to LockClock
            mContext.sendBroadcast(new Intent(ACTION_FORCE_WEATHER_UPDATE));
        } else {
            try {
                c.moveToFirst();
                mCachedInfo.temp = c.getString(0);
                mCachedInfo.city = c.getString(1);
                mCachedInfo.condition = c.getString(2);
                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 {
                c.close();
            }
@@ -108,19 +132,53 @@ public class WeatherControllerImpl implements WeatherController {
        }
    }

    private final class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Log.d(TAG, "onReceive " + intent.getAction());
            if (intent.hasExtra(EXTRA_UPDATE_CANCELLED)) {
                if (intent.getBooleanExtra(EXTRA_UPDATE_CANCELLED, false)) {
                    // no update
                    return;
                }
    private class WeatherContentObserver extends ContentObserver {

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

        @Override
        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;
        }
    }
}