|
2 | 2 |
|
3 | 3 | import android.accounts.Account; |
4 | 4 | import android.accounts.AccountManager; |
| 5 | +import android.annotation.SuppressLint; |
5 | 6 | import android.app.NotificationManager; |
6 | 7 | import android.app.PendingIntent; |
7 | 8 | import android.content.AbstractThreadedSyncAdapter; |
|
28 | 29 | import android.text.format.Time; |
29 | 30 | import android.util.Log; |
30 | 31 |
|
| 32 | +import com.bumptech.glide.Glide; |
31 | 33 | import com.example.android.sunshine.app.MainActivity; |
32 | 34 | import com.example.android.sunshine.app.R; |
33 | 35 | import com.example.android.sunshine.app.Utility; |
|
46 | 48 | import java.net.HttpURLConnection; |
47 | 49 | import java.net.URL; |
48 | 50 | import java.util.Vector; |
| 51 | +import java.util.concurrent.ExecutionException; |
49 | 52 |
|
50 | 53 | public class SunshineSyncAdapter extends AbstractThreadedSyncAdapter { |
51 | 54 | public final String LOG_TAG = SunshineSyncAdapter.class.getSimpleName(); |
@@ -377,8 +380,33 @@ private void notifyWeather() { |
377 | 380 |
|
378 | 381 | int iconId = Utility.getIconResourceForWeatherCondition(weatherId); |
379 | 382 | 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 | + } |
382 | 410 | String title = context.getString(R.string.app_name); |
383 | 411 |
|
384 | 412 | // Define the text of the forecast. |
|
0 commit comments