@Override public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.sample_widget); // Load image for all appWidgetIds. Picasso picasso = PicassoProvider.get(); picasso.load(Data.URLS[new Random().nextInt(Data.URLS.length)]) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .transform(new GrayscaleTransformation(picasso)) // .into(updateViews, R.id.image, appWidgetIds); } }
@Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.sample_list_detail_item, parent, false); holder = new ViewHolder(); holder.image = view.findViewById(R.id.photo); holder.text = view.findViewById(R.id.url); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Get the image URL for the current position. String url = getItem(position); holder.text.setText(url); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() .load(url) .placeholder(R.drawable.placeholder) .error(R.drawable.error) .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size) .centerInside() .tag(context) .into(holder.image); return view; }
@Test public void invalidErrorImage() { try { new RequestCreator().error(0); fail("Resource ID of zero should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().error(null); fail("Null drawable should throw exception."); } catch (IllegalArgumentException ignored) { } try { new RequestCreator().error(1).error(new ColorDrawable(0)); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } try { new RequestCreator().error(new ColorDrawable(0)).error(1); fail("Two placeholders should throw exception."); } catch (IllegalStateException ignored) { } }
@Test public void intoRemoteViewsWidgetWithErrorDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).error(new ColorDrawable(0)) .into(mockRemoteViews(), 0, new int[] { 1, 2, 3 }); fail("Calling into() with error drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Test public void intoRemoteViewsNotificationWithErrorDrawableThrows() { try { new RequestCreator(picasso, URI_1, 0).error(new ColorDrawable(0)) .into(mockRemoteViews(), 0, 0, mockNotification()); fail("Calling into() with error drawable should throw exception"); } catch (IllegalArgumentException ignored) { } }
@Override public View getView(int position, View convertView, ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if (view == null) { view = new SquaredImageView(context); view.setScaleType(CENTER_CROP); } // Get the image URL for the current position. String url = getItem(position); // Trigger the download of the URL asynchronously into the image view. PicassoProvider.get() // .load(url) // .placeholder(R.drawable.placeholder) // .error(R.drawable.error) // .fit() // .tag(context) // .into(view); return view; }