Merge "Added "discard unsaved changes" behavior for exiting." into gb-ub-photos-bryce
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
index 409f1e3..90ef74e 100644
--- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java
+++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
@@ -18,10 +18,12 @@
 
 import android.app.ActionBar;
 import android.app.Activity;
+import android.app.AlertDialog;
 import android.app.ProgressDialog;
 import android.app.WallpaperManager;
 import android.content.ContentValues;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.content.res.Resources;
@@ -972,7 +974,30 @@
     @Override
     public void onBackPressed() {
         if (mPanelController.onBackPressed()) {
-            saveImage();
+            if (detectSpecialExitCases()) {
+                saveImage();
+            } else if(!mImageShow.hasModifications()) {
+                done();
+            } else {
+                AlertDialog.Builder builder = new AlertDialog.Builder(this);
+                builder.setMessage(R.string.unsaved).setTitle(R.string.save_before_exit);
+                builder.setPositiveButton(R.string.save_and_exit, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        saveImage();
+                    }
+                });
+                builder.setNeutralButton(R.string.exit, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        done();
+                    }
+                });
+                builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                    }
+                });
+
+                AlertDialog dialog = builder.show();
+            }
         }
     }
 
@@ -1026,27 +1051,7 @@
     private boolean mOutputted = false;
 
     public void saveImage() {
-        if (mCropExtras != null) {
-            if (mCropExtras.getExtraOutput() != null) {
-                mSaveToExtraUri = true;
-                mOutputted = true;
-            }
-            if (mCropExtras.getSetAsWallpaper()) {
-                mSaveAsWallpaper = true;
-                mOutputted = true;
-            }
-            if (mCropExtras.getReturnData()) {
-
-                mReturnAsExtra = true;
-                mOutputted = true;
-            }
-
-            if (mOutputted) {
-                mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true);
-                showSavingProgress(null);
-                mImageShow.returnFilteredResult(this);
-            }
-        }
+        handleSpecialExitCases();
         if (!mOutputted) {
             if (mImageShow.hasModifications()) {
                 // Get the name of the album, to which the image will be saved
@@ -1061,6 +1066,33 @@
         }
     }
 
+    public boolean detectSpecialExitCases() {
+        return mCropExtras != null && (mCropExtras.getExtraOutput() != null
+                || mCropExtras.getSetAsWallpaper() || mCropExtras.getReturnData());
+    }
+
+    public void handleSpecialExitCases() {
+        if (mCropExtras != null) {
+            if (mCropExtras.getExtraOutput() != null) {
+                mSaveToExtraUri = true;
+                mOutputted = true;
+            }
+            if (mCropExtras.getSetAsWallpaper()) {
+                mSaveAsWallpaper = true;
+                mOutputted = true;
+            }
+            if (mCropExtras.getReturnData()) {
+                mReturnAsExtra = true;
+                mOutputted = true;
+            }
+            if (mOutputted) {
+                mImageShow.getImagePreset().mGeoData.setUseCropExtrasFlag(true);
+                showSavingProgress(null);
+                mImageShow.returnFilteredResult(this);
+            }
+        }
+    }
+
     public void onFilteredResult(Bitmap filtered) {
         Intent intent = new Intent();
         intent.putExtra(CropExtras.KEY_CROPPED_RECT, mImageShow.getImageCropBounds());