package net.ichigotake.pyazing;

import android.app.IntentService;
import android.app.NotificationManager;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public final class CopyToClipboardService extends IntentService {

    private final static String EXTRA_LABEL = "label";

    public static Intent createIntent(Context context, String text, String label) {
        Intent intent = new Intent(context, CopyToClipboardService.class);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.putExtra(EXTRA_LABEL, label);
        return intent;
    }

    public CopyToClipboardService() {
        this(CopyToClipboardService.class.getSimpleName());
    }

    public CopyToClipboardService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            copyToClipboard(
                    intent.getStringExtra(Intent.EXTRA_TEXT),
                    intent.getStringExtra(EXTRA_LABEL)
            );
            Handler mainThread = new Handler(Looper.getMainLooper());
            mainThread.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(
                            getApplicationContext(),
                            R.string.app_copy_to_clipboard_complete,
                            Toast.LENGTH_SHORT
                    ).show();
                }
            });
            cancelNotify();
        }
        stopSelf();
    }

    private void cancelNotify() {
        NotificationManager notificationManager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(UploadMediaService.NOTIFICATION_TAG_COPY_TO_CLIPBOARD, R.string.app_name);
    }

    private void copyToClipboard(String text, String label) {
        ClipData.Item item = new ClipData.Item(text);
        String[] mimeType = new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN};
        ClipData clipData = new ClipData(new ClipDescription(label, mimeType), item);
        ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(clipData);
    }
}

@@ -1060,44 +1060,27 @@
     
-copyToClipboard(%0A                   
+final String text =
  int
@@ -1116,16 +1116,71 @@
 RA_TEXT)
+;%0A            copyToClipboard(%0A                    text
 ,%0A      
@@ -1537,16 +1537,26 @@
         
+getString(
 R.string
@@ -1587,16 +1587,23 @@
 omplete,
+ text),
 %0A       

Fix toast message