Fix a few monkey issues: 5157713 and 5157712
Add debugging message for 5192628.

Change-Id: I257cabf1a93be256ed84aeaf3c9a871ecc5b1171
diff --git a/src/com/android/gallery3d/ui/MenuExecutor.java b/src/com/android/gallery3d/ui/MenuExecutor.java
index 637c01e..54c4e76 100644
--- a/src/com/android/gallery3d/ui/MenuExecutor.java
+++ b/src/com/android/gallery3d/ui/MenuExecutor.java
@@ -90,11 +90,7 @@
             public void handleMessage(Message message) {
                 switch (message.what) {
                     case MSG_TASK_COMPLETE: {
-                        if (mDialog != null) {
-                            mDialog.dismiss();
-                            mDialog = null;
-                            mTask = null;
-                        }
+                        stopTaskAndDismissDialog();
                         if (message.obj != null) {
                             ProgressListener listener = (ProgressListener) message.obj;
                             listener.onProgressComplete(message.arg1);
@@ -119,7 +115,7 @@
         };
     }
 
-    public void pause() {
+    private void stopTaskAndDismissDialog() {
         if (mTask != null) {
             mTask.cancel();
             mTask.waitDone();
@@ -129,6 +125,10 @@
         }
     }
 
+    public void pause() {
+        stopTaskAndDismissDialog();
+    }
+
     private void onProgressUpdate(int index, ProgressListener listener) {
         mHandler.sendMessage(
                 mHandler.obtainMessage(MSG_TASK_UPDATE, index, 0, listener));
@@ -138,55 +138,6 @@
         mHandler.sendMessage(mHandler.obtainMessage(MSG_TASK_COMPLETE, result, 0, listener));
     }
 
-    private int getShareType(SelectionManager selectionManager) {
-        ArrayList<Path> items = selectionManager.getSelected(false);
-        int type = 0;
-        DataManager dataManager = mActivity.getDataManager();
-        for (Path id : items) {
-            type |= dataManager.getMediaType(id);
-        }
-        return type;
-    }
-
-    private void onShareItemClicked(final SelectionManager selectionManager,
-            final String mimeType, final ComponentName component) {
-        Utils.assertTrue(mDialog == null);
-        final ArrayList<Path> items = selectionManager.getSelected(true);
-        mDialog = showProgressDialog((Activity) mActivity,
-                R.string.loading_image, items.size());
-
-        mTask = mActivity.getThreadPool().submit(new Job<Void>() {
-            @Override
-            public Void run(JobContext jc) {
-                DataManager manager = mActivity.getDataManager();
-                ArrayList<Uri> uris = new ArrayList<Uri>(items.size());
-                int index = 0;
-                for (Path path : items) {
-                    if ((manager.getSupportedOperations(path)
-                            & MediaObject.SUPPORT_SHARE) != 0) {
-                        uris.add(manager.getContentUri(path));
-                    }
-                    onProgressUpdate(++index, null);
-                }
-                if (jc.isCancelled()) return null;
-                Intent intent = new Intent()
-                        .setComponent(component).setType(mimeType);
-                if (uris.isEmpty()) {
-                    return null;
-                } else if (uris.size() == 1) {
-                    intent.setAction(Intent.ACTION_SEND);
-                    intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
-                } else {
-                    intent.setAction(Intent.ACTION_SEND_MULTIPLE);
-                    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
-                }
-                onProgressComplete(EXECUTION_RESULT_SUCCESS, null);
-                mHandler.sendMessage(mHandler.obtainMessage(MSG_DO_SHARE, intent));
-                return null;
-            }
-        }, null);
-    }
-
     private static void setMenuItemVisibility(
             Menu menu, int id, boolean visibility) {
         MenuItem item = menu.findItem(id);
@@ -283,7 +234,7 @@
 
     public void startAction(int action, int title, ProgressListener listener) {
         ArrayList<Path> ids = mSelectionManager.getSelected(false);
-        Utils.assertTrue(mDialog == null);
+        stopTaskAndDismissDialog();
 
         Activity activity = (Activity) mActivity;
         mDialog = showProgressDialog(activity, title, ids.size());
@@ -304,6 +255,9 @@
     private boolean execute(
             DataManager manager, JobContext jc, int cmd, Path path) {
         boolean result = true;
+        Log.v(TAG, "Execute cmd: " + cmd + " for " + path);
+        long startTime = System.currentTimeMillis();
+
         switch (cmd) {
             case R.id.action_confirm_delete:
                 manager.delete(path);
@@ -359,6 +313,8 @@
             default:
                 throw new AssertionError();
         }
+        Log.v(TAG, "It takes " + (System.currentTimeMillis() - startTime) +
+                " ms to execute cmd for " + path);
         return result;
     }
 
@@ -377,20 +333,23 @@
             int index = 0;
             DataManager manager = mActivity.getDataManager();
             int result = EXECUTION_RESULT_SUCCESS;
-            for (Path id : mItems) {
-                if (jc.isCancelled()) {
-                    result = EXECUTION_RESULT_CANCEL;
-                    break;
+            try {
+                for (Path id : mItems) {
+                    if (jc.isCancelled()) {
+                        result = EXECUTION_RESULT_CANCEL;
+                        break;
+                    }
+                    if (!execute(manager, jc, mOperation, id)) {
+                        result = EXECUTION_RESULT_FAIL;
+                    }
+                    onProgressUpdate(index++, mListener);
                 }
-                try {
-                    if (!execute(manager, jc, mOperation, id)) result = EXECUTION_RESULT_FAIL;
-                } catch (Throwable th) {
-                    Log.e(TAG, "failed to execute operation " + mOperation
-                            + " for " + id, th);
-                }
-                onProgressUpdate(index++, mListener);
+            } catch (Throwable th) {
+                Log.e(TAG, "failed to execute operation " + mOperation
+                        + " : " + th);
+            } finally {
+               onProgressComplete(result, mListener);
             }
-            onProgressComplete(result, mListener);
             return null;
         }
     }
diff --git a/src/com/android/gallery3d/ui/TileImageView.java b/src/com/android/gallery3d/ui/TileImageView.java
index 2cecedd..b445c84 100644
--- a/src/com/android/gallery3d/ui/TileImageView.java
+++ b/src/com/android/gallery3d/ui/TileImageView.java
@@ -351,7 +351,7 @@
         if (mIsTextureFreed) {
             layoutTiles(mCenterX, mCenterY, mScale, mRotation);
             mIsTextureFreed = false;
-            updateBackupTexture(mModel.getBackupImage());
+            updateBackupTexture(mModel != null ? mModel.getBackupImage() : null);
         }
     }