Skip to content

Commit 822d52b

Browse files
committed
Integrate Glide to load weather art icons from an external source
1 parent e666cf8 commit 822d52b

11 files changed

Lines changed: 101 additions & 12 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.github.bumptech.glide:glide:3.5.2'
2425
compile 'com.android.support:appcompat-v7:21.0.2'
2526
compile 'com.android.support:support-annotations:22.0.0'
2627
}

app/src/main/java/com/example/android/sunshine/app/DetailFragment.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.widget.ImageView;
3535
import android.widget.TextView;
3636

37+
import com.bumptech.glide.Glide;
3738
import com.example.android.sunshine.app.data.WeatherContract;
3839
import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry;
3940

@@ -184,7 +185,11 @@ public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
184185
int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);
185186

186187
// Use weather art image
187-
mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));
188+
Glide.with(this)
189+
.load(Utility.getArtUrlForWeatherCondition(getActivity(), weatherId))
190+
.error(Utility.getArtResourceForWeatherCondition(weatherId))
191+
.crossFade()
192+
.into(mIconView);
188193

189194
// Read date from cursor and update views for day of week and date
190195
long date = data.getLong(COL_WEATHER_DATE);

app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import android.widget.ImageView;
2525
import android.widget.TextView;
2626

27+
import com.bumptech.glide.Glide;
28+
2729
/**
2830
* {@link ForecastAdapter} exposes a list of weather forecasts
2931
* from a {@link Cursor} to a {@link android.widget.ListView}.
@@ -91,20 +93,27 @@ public void bindView(View view, Context context, Cursor cursor) {
9193

9294
int viewType = getItemViewType(cursor.getPosition());
9395
int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID);
96+
int fallbackIconId;
9497
switch (viewType) {
9598
case VIEW_TYPE_TODAY: {
9699
// Get weather icon
97-
viewHolder.iconView.setImageResource(Utility.getArtResourceForWeatherCondition(
98-
weatherId));
100+
fallbackIconId = Utility.getArtResourceForWeatherCondition(
101+
weatherId);
99102
break;
100103
}
101-
case VIEW_TYPE_FUTURE_DAY: {
104+
default: {
102105
// Get weather icon
103-
viewHolder.iconView.setImageResource(Utility.getIconResourceForWeatherCondition(
104-
weatherId));
106+
fallbackIconId = Utility.getIconResourceForWeatherCondition(
107+
weatherId);
105108
break;
106109
}
107110
}
111+
112+
Glide.with(mContext)
113+
.load(Utility.getArtUrlForWeatherCondition(mContext, weatherId))
114+
.error(fallbackIconId)
115+
.crossFade()
116+
.into(viewHolder.iconView);
108117

109118
// Read date from cursor
110119
long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE);

app/src/main/java/com/example/android/sunshine/app/Utility.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,43 @@ public static int getIconResourceForWeatherCondition(int weatherId) {
216216
return -1;
217217
}
218218

219+
/**
220+
* Helper method to provide the art urls according to the weather condition id returned
221+
* by the OpenWeatherMap call.
222+
*
223+
* @param context Context to use for retrieving the URL format
224+
* @param weatherId from OpenWeatherMap API response
225+
* @return url for the corresponding weather artwork. null if no relation is found.
226+
*/
227+
public static String getArtUrlForWeatherCondition(Context context, int weatherId) {
228+
// Based on weather code data found at:
229+
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
230+
if (weatherId >= 200 && weatherId <= 232) {
231+
return context.getString(R.string.format_art_url, "storm");
232+
} else if (weatherId >= 300 && weatherId <= 321) {
233+
return context.getString(R.string.format_art_url, "light_rain");
234+
} else if (weatherId >= 500 && weatherId <= 504) {
235+
return context.getString(R.string.format_art_url, "rain");
236+
} else if (weatherId == 511) {
237+
return context.getString(R.string.format_art_url, "snow");
238+
} else if (weatherId >= 520 && weatherId <= 531) {
239+
return context.getString(R.string.format_art_url, "rain");
240+
} else if (weatherId >= 600 && weatherId <= 622) {
241+
return context.getString(R.string.format_art_url, "snow");
242+
} else if (weatherId >= 701 && weatherId <= 761) {
243+
return context.getString(R.string.format_art_url, "fog");
244+
} else if (weatherId == 761 || weatherId == 781) {
245+
return context.getString(R.string.format_art_url, "storm");
246+
} else if (weatherId == 800) {
247+
return context.getString(R.string.format_art_url, "clear");
248+
} else if (weatherId == 801) {
249+
return context.getString(R.string.format_art_url, "light_clouds");
250+
} else if (weatherId >= 802 && weatherId <= 804) {
251+
return context.getString(R.string.format_art_url, "clouds");
252+
}
253+
return null;
254+
}
255+
219256
/**
220257
* Helper method to provide the art resource id according to the weather condition id returned
221258
* by the OpenWeatherMap call.

app/src/main/java/com/example/android/sunshine/app/sync/SunshineSyncAdapter.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.accounts.Account;
44
import android.accounts.AccountManager;
5+
import android.annotation.SuppressLint;
56
import android.app.NotificationManager;
67
import android.app.PendingIntent;
78
import android.content.AbstractThreadedSyncAdapter;
@@ -28,6 +29,7 @@
2829
import android.text.format.Time;
2930
import android.util.Log;
3031

32+
import com.bumptech.glide.Glide;
3133
import com.example.android.sunshine.app.MainActivity;
3234
import com.example.android.sunshine.app.R;
3335
import com.example.android.sunshine.app.Utility;
@@ -46,6 +48,7 @@
4648
import java.net.HttpURLConnection;
4749
import java.net.URL;
4850
import java.util.Vector;
51+
import java.util.concurrent.ExecutionException;
4952

5053
public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter {
5154
public final String LOG_TAG = SunshineSyncAdapter.class.getSimpleName();
@@ -377,8 +380,33 @@ private void notifyWeather() {
377380

378381
int iconId = Utility.getIconResourceForWeatherCondition(weatherId);
379382
Resources resources = context.getResources();
380-
Bitmap largeIcon = BitmapFactory.decodeResource(resources,
381-
Utility.getArtResourceForWeatherCondition(weatherId));
383+
int artResourceId = Utility.getArtResourceForWeatherCondition(weatherId);
384+
String artUrl = Utility.getArtUrlForWeatherCondition(context, weatherId);
385+
386+
// On Honeycomb and higher devices, we can retrieve the size of the large icon
387+
// Prior to that, we use a fixed size
388+
@SuppressLint("InlinedApi")
389+
int largeIconWidth = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
390+
? resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width)
391+
: resources.getDimensionPixelSize(R.dimen.notification_large_icon_default);
392+
@SuppressLint("InlinedApi")
393+
int largeIconHeight = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
394+
? resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height)
395+
: resources.getDimensionPixelSize(R.dimen.notification_large_icon_default);
396+
397+
// Retrieve the large icon
398+
Bitmap largeIcon;
399+
try {
400+
largeIcon = Glide.with(context)
401+
.load(artUrl)
402+
.asBitmap()
403+
.error(artResourceId)
404+
.fitCenter()
405+
.into(largeIconWidth, largeIconHeight).get();
406+
} catch (InterruptedException | ExecutionException e) {
407+
Log.e(LOG_TAG, "Error retrieving large icon from " + artUrl, e);
408+
largeIcon = BitmapFactory.decodeResource(resources, artResourceId);
409+
}
382410
String title = context.getString(R.string.app_name);
383411

384412
// Define the text of the forecast.

app/src/main/res/layout/fragment_detail.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
<ImageView
8484
android:id="@+id/detail_icon"
8585
android:layout_width="wrap_content"
86-
android:layout_height="wrap_content" />
86+
android:layout_height="wrap_content"
87+
android:adjustViewBounds="true" />
8788

8889
<TextView
8990
android:id="@+id/detail_forecast_textview"

app/src/main/res/layout/fragment_detail_wide.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@
111111
<ImageView
112112
android:id="@+id/detail_icon"
113113
android:layout_height="wrap_content"
114-
android:layout_width="wrap_content" />
114+
android:layout_width="wrap_content"
115+
android:adjustViewBounds="true" />
115116

116117
<TextView
117118
android:fontFamily="sans-serif-condensed"

app/src/main/res/layout/list_item_forecast.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
android:id="@+id/list_item_icon"
3232
android:layout_gravity="center"
3333
android:layout_width="wrap_content"
34-
android:layout_height="wrap_content"/>
34+
android:layout_height="wrap_content"
35+
android:adjustViewBounds="true"/>
3536

3637
</FrameLayout>
3738

app/src/main/res/layout/list_item_forecast_today.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
android:id="@+id/list_item_icon"
7272
android:layout_width="wrap_content"
7373
android:layout_height="wrap_content"
74-
android:layout_gravity="center_horizontal"/>
74+
android:layout_gravity="center_horizontal"
75+
android:adjustViewBounds="true"/>
7576

7677
<TextView
7778
android:id="@+id/list_item_forecast_textview"

app/src/main/res/values/dimens.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
<!-- Default screen margins, per the Android Design guidelines. -->
1818
<dimen name="activity_horizontal_margin">16dp</dimen>
1919
<dimen name="activity_vertical_margin">16dp</dimen>
20+
21+
<dimen name="notification_large_icon_default">48dp</dimen>
2022
</resources>

0 commit comments

Comments
 (0)