package example.org.fooapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;


public class CustomizeMemeActivity extends ActionBarActivity {

    private static final String DRAWABLE_ID_EXTRA_NAME = "drawable.id";
    private int mDrawableId;

    public static Intent createIntentForDrawable(Context context, int drawableId) {
        return new Intent(context, CustomizeMemeActivity.class)
                .putExtra(DRAWABLE_ID_EXTRA_NAME, drawableId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customize_meme);
        Intent intent = getIntent();

        if (!intent.hasExtra(DRAWABLE_ID_EXTRA_NAME)) {
            Toast.makeText(this, "No image to share :-(", Toast.LENGTH_SHORT).show();
            finish(); // close this activity
        }

        mDrawableId = intent.getIntExtra(DRAWABLE_ID_EXTRA_NAME, 0);
        ((ImageView) findViewById(R.id.meme_image)).setImageResource(mDrawableId);

        findViewById(R.id.share_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("CustomizeMemeActivity", "share button tapped");
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mDrawableId);
                shareBitmap(bitmap);
            }
        });
    }

    /**
     * Saves the bitmap to disk and launches a share intent pointing to it
     */
    private void shareBitmap(Bitmap bitmap) {
        try {
            File path = uniquePublicallyAccessibleFile(".jpg");
            saveBitmapToFile(bitmap, Bitmap.CompressFormat.JPEG, path);
            Intent shareIntent = new Intent()
                    .setAction(Intent.ACTION_SEND)
                    .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(path))
                    .setType("image/*");
            startActivity(Intent.createChooser(shareIntent, "Share meme"));
        } catch (IOException e) {
            Log.e("CustomizeMemeActivity", "Error saving image", e);
            Toast.makeText(this, "Error saving image", Toast.LENGTH_SHORT).show();
        }
    }

    private File uniquePublicallyAccessibleFile(String extension) {
        File externalDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        // Unique filename
        return new File(externalDir, UUID.randomUUID().toString() + extension);
    }

    private void saveBitmapToFile(Bitmap bitmap, Bitmap.CompressFormat format, File file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);

        boolean exceptionThrown = true;
        try {
            bitmap.compress(format, 90, fos);
            exceptionThrown = false;
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // Swallow exception in finally block if we're already throwing an exception
                if (!exceptionThrown) {
                    throw e;
                }
            }
        }
    }
}

@@ -156,16 +156,140 @@
 actory;%0A
+import android.graphics.Canvas;%0Aimport android.graphics.Color;%0Aimport android.graphics.Paint;%0Aimport android.graphics.Rect;%0A
 import a
@@ -457,24 +457,56 @@
 .view.View;%0A
+import android.widget.EditText;%0A
 import andro
@@ -1605,24 +1605,108 @@
 awableId);%0A%0A
+        final EditText captionInput = (EditText) findViewById(R.id.caption_input);%0A%0A
         find
@@ -1938,25 +1938,34 @@
      Bitmap 
-b
+immutableB
 itmap = Bitm
@@ -2015,24 +2015,195 @@
 rawableId);%0A
+                Bitmap mutableBitmap = immutableBitmap.copy(immutableBitmap.getConfig(), true);%0A                addText(mutableBitmap, captionInput.getText().toString());%0A
             
@@ -2218,17 +2218,24 @@
 eBitmap(
-b
+mutableB
 itmap);%0A
@@ -2263,24 +2263,699 @@
  %7D);%0A    %7D%0A%0A
+    /**%0A     * Super-simple implementation for adding text to image.%0A     */%0A    private void addText(Bitmap bitmap, String text) %7B%0A        Canvas canvas = new Canvas(bitmap);%0A        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);%0A        paint.setTextSize(24 * bitmap.getDensity());%0A        paint.setColor(Color.WHITE);%0A%0A        Rect bounds = new Rect();%0A        paint.getTextBounds(text, 0, text.length(), bounds);%0A%0A        // center horizontally%0A        int x = (bitmap.getWidth() - bounds.width()) / 2;%0A%0A        // 12 dps above bottom%0A        int y = (bitmap.getHeight() - bounds.height() - 12 * bitmap.getDensity());%0A%0A        canvas.drawText(text, x, y, paint);%0A    %7D%0A%0A
     /**%0A    

add text FTW