package com.stackallocated.imageupload;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class UploadActivity extends Activity {
    private final static String TAG = "UploadActivity";

    void makeToast(String msg) {
        Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<Uri> imageUris = null;
        final Intent intent = getIntent();
        // We have an image to upload, get list of images.
        if (Intent.ACTION_SEND.equals(intent.getAction())) {
            Log.v(TAG, "Got send action");

            Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (imageUri != null) {
                imageUris = new ArrayList<Uri>(Arrays.asList(imageUri));
            } else {
                makeToast("No image provided to upload!");
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
            Log.v(TAG, "Got multiple send action");

            imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        }

        // Sanitize the list of image URIs.
        Iterator<Uri> it = imageUris.iterator();
        while (it.hasNext()) {
            Uri imageUri = it.next();

            if (imageUri == null) {
                Log.d(TAG, "Removed null URI");
                it.remove();
                continue;
            }

            try {
                AssetFileDescriptor desc = getContentResolver().openAssetFileDescriptor(imageUri, "r");
                desc.close();
            } catch (Exception e) {
                Log.d(TAG, "Couldn't open URI '" + imageUri + "'");
                it.remove();
            }
        }

        // Trigger the upload.
        if (imageUris != null && imageUris.size() > 0) {
            if (imageUris.size() > 1) {
                makeToast("Uploading " + imageUris.size() + " images!");
            } else{
                makeToast("Uploading image!");
            }
            for (Uri imageUri : imageUris) {
                Log.d(TAG, "Uploading image '" + imageUri + "'");
            }
        }

        // This activity never needs to show the UI.
        finish();
        return;
    }
}

@@ -578,24 +578,684 @@
 w();%0A    %7D%0A%0A
+    // Removes URIs that are null or not openable.%0A    void sanitizeUris(ArrayList%3CUri%3E uris) %7B%0A        Iterator%3CUri%3E it = uris.iterator();%0A        while (it.hasNext()) %7B%0A            Uri uri = it.next();%0A%0A            if (uri == null) %7B%0A                Log.d(TAG, %22Removed null URI%22);%0A                it.remove();%0A                continue;%0A            %7D%0A%0A            try %7B%0A                AssetFileDescriptor desc = getContentResolver().openAssetFileDescriptor(uri, %22r%22);%0A                desc.close();%0A            %7D catch (Exception e) %7B%0A                Log.d(TAG, %22Couldn't open URI '%22 + uri + %22'%22);%0A                it.remove();%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Overrid
@@ -2168,581 +2168,32 @@
     
-Iterator%3CUri%3E it = imageUris.iterator();%0A        while (it.hasNext()) %7B%0A            Uri imageUri = it.next();%0A%0A            if (imageUri == null) %7B%0A                Log.d(TAG, %22Removed null URI%22);%0A                it.remove();%0A                continue;%0A            %7D%0A%0A            try %7B%0A                AssetFileDescriptor desc = getContentResolver().openAssetFileDescriptor(imageUri, %22r%22);%0A                desc.close();%0A            %7D catch (Exception e) %7B%0A                Log.d(TAG, %22Couldn't open URI '%22 + imageUri + %22'%22);%0A                it.remove();%0A            %7D%0A        %7D
+sanitizeUris(imageUris);
 %0A%0A  

Refactor URI sanitization