package com.example.picasso;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RemoteViews;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.Random;

final class PicassoSampleAdapter extends BaseAdapter {

  private static final int NOTIFICATION_ID = 666;

  enum Sample {
    GRID_VIEW("Image Grid View", SampleGridViewActivity.class),
    GALLERY("Load from Gallery", SampleGalleryActivity.class),
    CONTACTS("Contact Photos", SampleContactsActivity.class),
    LIST_DETAIL("List / Detail View", SampleListDetailActivity.class),
    SHOW_NOTIFICATION("Sample Notification", null) {
      @Override public void launch(Activity activity) {
        RemoteViews remoteViews =
            new RemoteViews(activity.getPackageName(), R.layout.notification_view);

        Intent intent = new Intent(activity, SampleGridViewActivity.class);

        NotificationCompat.Builder builder =
            new NotificationCompat.Builder(activity).setSmallIcon(R.drawable.icon)
                .setContentIntent(PendingIntent.getActivity(activity, -1, intent, 0))
                .setContent(remoteViews);

        Notification notification = builder.getNotification();

        NotificationManager notificationManager =
            (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notification);

        // Now load an image for this notification.
        Picasso.get() //
            .load(Data.URLS[new Random().nextInt(Data.URLS.length)]) //
            .resizeDimen(R.dimen.notification_icon_width_height,
                R.dimen.notification_icon_width_height) //
            .into(remoteViews, R.id.photo, NOTIFICATION_ID, notification);
      }
    };

    private final Class<? extends Activity> activityClass;
    private final String name;

    Sample(String name, Class<? extends Activity> activityClass) {
      this.activityClass = activityClass;
      this.name = name;
    }

    public void launch(Activity activity) {
      activity.startActivity(new Intent(activity, activityClass));
      activity.finish();
    }
  }

  private final LayoutInflater inflater;

  PicassoSampleAdapter(Context context) {
    inflater = LayoutInflater.from(context);
  }

  @Override public int getCount() {
    return Sample.values().length;
  }

  @Override public Sample getItem(int position) {
    return Sample.values()[position];
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    TextView view = (TextView) convertView;
    if (view == null) {
      view = (TextView) inflater.inflate(R.layout.picasso_sample_activity_item, parent, false);
    }

    view.setText(getItem(position).name);

    return view;
  }
}

@@ -85,16 +85,56 @@
 cation;%0A
+import android.app.NotificationChannel;%0A
 import a
@@ -254,32 +254,57 @@
 content.Intent;%0A
+import android.os.Build;%0A
 import android.s
@@ -700,16 +700,73 @@
 D = 666;
+%0A  private static final String CHANNEL_ID = %22channel-id%22;
 %0A%0A  enum
@@ -1364,30 +1364,21 @@
 tion
-Compat.Builder builder
+ notification
  =%0A 
@@ -1427,17 +1427,46 @@
 activity
-)
+, CHANNEL_ID)%0A                
 .setSmal
@@ -1614,18 +1614,16 @@
 teViews)
-;%0A
 %0A       
@@ -1627,59 +1627,101 @@
     
-Notification notification = builder.getNotification
+        .setAutoCancel(true)%0A                .setChannelId(CHANNEL_ID)%0A                .build
 ();%0A
@@ -1862,16 +1862,337 @@
 RVICE);%0A
+%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.O) %7B%0A          NotificationChannel channel =%0A              new NotificationChannel(CHANNEL_ID, %22Picasso Notification Channel%22,%0A                  NotificationManager.IMPORTANCE_DEFAULT);%0A          notificationManager.createNotificationChannel(channel);%0A        %7D%0A%0A
         

Fix Notifications to use channels on O+