Add a Muzei extension, changing the wallpaper with the weather

Requires installation of Muzei: https://play.google.com/store/apps/details?id=net.nurik.roman.muzei
This commit is contained in:
Dan Galpin
2015-05-25 06:03:14 -07:00
parent 52efa534a2
commit 089e1a7cc7
7 changed files with 144 additions and 1 deletions

View File

@@ -29,4 +29,5 @@ dependencies {
compile 'com.android.support:design:22.2.0' compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0' compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.google.android.gms:play-services-gcm:7.0.0' compile 'com.google.android.gms:play-services-gcm:7.0.0'
compile 'com.google.android.apps.muzei:muzei-api:2.0'
} }

View File

@@ -110,6 +110,17 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<!-- Muzei Extension -->
<service android:name=".muzei.WeatherMuzeiSource"
android:icon="@drawable/ic_muzei"
android:label="@string/app_name"
android:description="@string/muzei_description" >
<intent-filter>
<action android:name="com.google.android.apps.muzei.api.MuzeiArtSource" />
</intent-filter>
<meta-data android:name="color" android:value="@color/primary" />
</service>
<!-- Today Widget --> <!-- Today Widget -->
<receiver <receiver
android:name=".widget.TodayWidgetProvider" android:name=".widget.TodayWidgetProvider"
@@ -123,6 +134,7 @@
</receiver> </receiver>
<service android:name=".widget.TodayWidgetIntentService" /> <service android:name=".widget.TodayWidgetIntentService" />
<!-- Detail Widget -->
<receiver <receiver
android:name=".widget.DetailWidgetProvider" android:name=".widget.DetailWidgetProvider"
android:label="@string/title_widget_detail" android:label="@string/title_widget_detail"

View File

@@ -339,7 +339,7 @@ public class Utility {
stringId = R.string.condition_2xx; stringId = R.string.condition_2xx;
} else if (weatherId >= 300 && weatherId <= 321) { } else if (weatherId >= 300 && weatherId <= 321) {
stringId = R.string.condition_3xx; stringId = R.string.condition_3xx;
} else switch(weatherId) { } else switch (weatherId) {
case 500: case 500:
stringId = R.string.condition_500; stringId = R.string.condition_500;
break; break;
@@ -502,6 +502,42 @@ public class Utility {
return context.getString(stringId); return context.getString(stringId);
} }
/*
* Helper method to provide the correct image according to the weather condition id returned
* by the OpenWeatherMap call.
*
* @param weatherId from OpenWeatherMap API response
* @return A string URL to an appropriate image or null if no mapping is found
*/
public static String getImageUrlForWeatherCondition(int weatherId) {
// Based on weather code data found at:
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
if (weatherId >= 200 && weatherId <= 232) {
return "http://upload.wikimedia.org/wikipedia/commons/2/28/Thunderstorm_in_Annemasse,_France.jpg";
} else if (weatherId >= 300 && weatherId <= 321) {
return "http://upload.wikimedia.org/wikipedia/commons/a/a0/Rain_on_leaf_504605006.jpg";
} else if (weatherId >= 500 && weatherId <= 504) {
return "http://upload.wikimedia.org/wikipedia/commons/6/6c/Rain-on-Thassos.jpg";
} else if (weatherId == 511) {
return "http://upload.wikimedia.org/wikipedia/commons/b/b8/Fresh_snow.JPG";
} else if (weatherId >= 520 && weatherId <= 531) {
return "http://upload.wikimedia.org/wikipedia/commons/6/6c/Rain-on-Thassos.jpg";
} else if (weatherId >= 600 && weatherId <= 622) {
return "http://upload.wikimedia.org/wikipedia/commons/b/b8/Fresh_snow.JPG";
} else if (weatherId >= 701 && weatherId <= 761) {
return "http://upload.wikimedia.org/wikipedia/commons/e/e6/Westminster_fog_-_London_-_UK.jpg";
} else if (weatherId == 761 || weatherId == 781) {
return "http://upload.wikimedia.org/wikipedia/commons/d/dc/Raised_dust_ahead_of_a_severe_thunderstorm_1.jpg";
} else if (weatherId == 800) {
return "http://upload.wikimedia.org/wikipedia/commons/7/7e/A_few_trees_and_the_sun_(6009964513).jpg";
} else if (weatherId == 801) {
return "http://upload.wikimedia.org/wikipedia/commons/e/e7/Cloudy_Blue_Sky_(5031259890).jpg";
} else if (weatherId >= 802 && weatherId <= 804) {
return "http://upload.wikimedia.org/wikipedia/commons/5/54/Cloudy_hills_in_Elis,_Greece_2.jpg";
}
return null;
}
/** /**
* Returns true if the network is available or about to become available. * Returns true if the network is available or about to become available.
* *

View File

@@ -0,0 +1,79 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.sunshine.app.muzei;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.Utility;
import com.example.android.sunshine.app.data.WeatherContract;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
import com.google.android.apps.muzei.api.Artwork;
import com.google.android.apps.muzei.api.MuzeiArtSource;
/**
* Muzei source that changes your background based on the current weather conditions
*/
public class WeatherMuzeiSource extends MuzeiArtSource {
private static final String[] FORECAST_COLUMNS = new String[]{
WeatherContract.WeatherEntry.COLUMN_WEATHER_ID,
WeatherContract.WeatherEntry.COLUMN_SHORT_DESC
};
// these indices must match the projection
private static final int INDEX_WEATHER_ID = 0;
private static final int INDEX_SHORT_DESC = 1;
public WeatherMuzeiSource() {
super("WeatherMuzeiSource");
}
@Override
protected void onHandleIntent(Intent intent) {
super.onHandleIntent(intent);
boolean dataUpdated = intent != null &&
SunshineSyncAdapter.ACTION_DATA_UPDATED.equals(intent.getAction());
if (dataUpdated && isEnabled()) {
onUpdate(UPDATE_REASON_OTHER);
}
}
@Override
protected void onUpdate(int reason) {
String location = Utility.getPreferredLocation(this);
Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(
location, System.currentTimeMillis());
Cursor cursor = getContentResolver().query(weatherForLocationUri, FORECAST_COLUMNS, null,
null, WeatherContract.WeatherEntry.COLUMN_DATE + " ASC");
if (cursor.moveToFirst()) {
int weatherId = cursor.getInt(INDEX_WEATHER_ID);
String desc = cursor.getString(INDEX_SHORT_DESC);
String imageUrl = Utility.getImageUrlForWeatherCondition(weatherId);
// Only publish a new wallpaper if we have a valid image
if (imageUrl != null) {
publishArtwork(new Artwork.Builder()
.imageUri(Uri.parse(imageUrl))
.title(desc)
.byline(location)
.viewIntent(new Intent(this, MainActivity.class))
.build());
}
}
cursor.close();
}
}

View File

@@ -34,6 +34,7 @@ import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R; import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility; import com.example.android.sunshine.app.Utility;
import com.example.android.sunshine.app.data.WeatherContract; import com.example.android.sunshine.app.data.WeatherContract;
import com.example.android.sunshine.app.muzei.WeatherMuzeiSource;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@@ -341,6 +342,7 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
new String[] {Long.toString(dayTime.setJulianDay(julianStartDay-1))}); new String[] {Long.toString(dayTime.setJulianDay(julianStartDay-1))});
updateWidgets(); updateWidgets();
updateMuzei();
notifyWeather(); notifyWeather();
} }
Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted"); Log.d(LOG_TAG, "Sync Complete. " + cVVector.size() + " Inserted");
@@ -361,6 +363,16 @@ public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
context.sendBroadcast(dataUpdatedIntent); context.sendBroadcast(dataUpdatedIntent);
} }
private void updateMuzei() {
// Muzei is only compatible with Jelly Bean MR1+ devices, so there's no need to update the
// Muzei background on lower API level devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Context context = getContext();
context.startService(new Intent(ACTION_DATA_UPDATED)
.setClass(context, WeatherMuzeiSource.class));
}
}
private void notifyWeather() { private void notifyWeather() {
Context context = getContext(); Context context = getContext();
//checking the last update and notify if it' the first of the day //checking the last update and notify if it' the first of the day

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -140,6 +140,9 @@
<string name="title_widget_today">Sunshine Today</string> <string name="title_widget_today">Sunshine Today</string>
<string name="title_widget_detail">Sunshine Details</string> <string name="title_widget_detail">Sunshine Details</string>
<!-- Strings related to Muzei Source -->
<string name="muzei_description">Today\'s weather</string>
<!-- Empty Weather Database --> <!-- Empty Weather Database -->
<string name="empty_forecast_list">No Weather Information Available</string> <string name="empty_forecast_list">No Weather Information Available</string>
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string> <string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string>