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.content.res.Resources;
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();
    }

    // Removes URIs that are null or not openable.
    void sanitizeUris(ArrayList<Uri> uris) {
        Iterator<Uri> it = uris.iterator();
        while (it.hasNext()) {
            Uri uri = it.next();

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

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

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

        final Resources res = getResources();

        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);
            imageUris = new ArrayList<Uri>(Arrays.asList(imageUri));
        } 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 by discarding null or unopenable ones.
        sanitizeUris(imageUris);

        // Trigger the upload.
        if (imageUris != null) {
            int images = imageUris.size();
            makeToast(res.getQuantityString(R.plurals.uploader_uploading_toast, images, images));

            // Repeated intent creation is so that killing of the upload
            // service doesn't require fancy handling of intents.
            for (Uri imageUri : imageUris) {
                Log.d(TAG, "Enqueuing image '" + imageUri + "'");
                Intent i = new Intent(this, UploadService.class);
                i.putExtra(Intent.EXTRA_STREAM, imageUri);
                startService(i);
            }
        }

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

@@ -1479,19 +1479,35 @@
 Uris = n
-ull
+ew ArrayList%3CUri%3E()
 ;%0A      
@@ -1813,43 +1813,12 @@
 Uris
- = new ArrayList%3CUri%3E(Arrays.asList
+.add
 (ima
@@ -1823,17 +1823,16 @@
 mageUri)
-)
 ;%0A      
@@ -1959,33 +1959,75 @@
 );%0A%0A            
-i
+try %7B%0A                ArrayList%3CUri%3E extraI
 mageUris = inten
@@ -2070,32 +2070,282 @@
 .EXTRA_STREAM);%0A
+                imageUris.addAll(extraImageUris);%0A            %7D catch (Exception e) %7B%0A                // Something broke, don't upload any images.%0A                Log.e(TAG, %22Getting multiple images broke! %22 + e.getLocalizedMessage());%0A            %7D%0A
         %7D%0A%0A     
@@ -2490,45 +2490,8 @@
 ad.%0A
-        if (imageUris != null) %7B%0A    
     
@@ -2533,20 +2533,16 @@
         
-    
 makeToas
@@ -2628,20 +2628,16 @@
         
-    
 // Repea
@@ -2689,20 +2689,16 @@
  upload%0A
-    
         
@@ -2759,20 +2759,16 @@
         
-    
 for (Uri
@@ -2788,28 +2788,24 @@
 mageUris) %7B%0A
-    
             
@@ -2866,20 +2866,16 @@
         
-   
- 
 Intent i
@@ -2928,20 +2928,16 @@
         
-    
 i.putExt
@@ -2983,20 +2983,16 @@
         
- 
-   
 startSer
@@ -3000,30 +3000,16 @@
 ice(i);%0A
-            %7D%0A
         

Refactor ACTION_SEND code a little to deal with null better