Gallery2: add loading progress dialog for FilterShowActivity

Add a progress dialog to limit access to dual cam filters until after
DDM is successfully loaded. Dual camera filter category will be
hidden by default until DDM is loaded and verified.

Conflicts:
        res/layout/filtershow_activity.xml
        res/layout/filtershow_main_panel.xml
        src/com/android/gallery3d/filtershow/category/MainPanel.java

CRs-Fixed: 929338
Change-Id: I9602c3fb31da37856b00b029d4e6c8d1e8ae3d71
diff --git a/res/layout/filtershow_activity.xml b/res/layout/filtershow_activity.xml
index 1340bc4..d89f6b2 100644
--- a/res/layout/filtershow_activity.xml
+++ b/res/layout/filtershow_activity.xml
@@ -21,26 +21,11 @@
     android:layout_height="match_parent"
     android:background="#212121" >
 
-    <LinearLayout
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:orientation="vertical" >
-
         <FrameLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1" >
 
-            <ProgressBar
-                android:id="@+id/loading"
-                style="@android:style/Widget.Holo.ProgressBar.Large"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_gravity="center"
-                android:background="@null"
-                android:indeterminate="true"
-                android:indeterminateOnly="true" />
-
             <RelativeLayout
                 android:id="@+id/imageContainer"
                 android:layout_width="match_parent"
@@ -90,7 +75,6 @@
                     android:layout_weight="1" />
             </com.android.gallery3d.filtershow.CenteredLinearLayout>
         </FrameLayout>
-    </LinearLayout>
 
     <com.android.gallery3d.filtershow.category.CategorySelected
         android:id="@+id/categorySelectedIndicator"
@@ -98,4 +82,4 @@
         android:layout_height="@dimen/category_panel_icon_size"
         android:visibility="gone" />
 
-</FrameLayout>
\ No newline at end of file
+</FrameLayout>
diff --git a/res/layout/filtershow_main_panel.xml b/res/layout/filtershow_main_panel.xml
index 7a0ead8..d0e6246 100644
--- a/res/layout/filtershow_main_panel.xml
+++ b/res/layout/filtershow_main_panel.xml
@@ -108,6 +108,7 @@
                     android:background="@android:color/transparent"
                     android:padding="2dip"
                     android:scaleType="centerInside"
+                    android:visibility="gone"
                     android:src="@drawable/dualcamera"/>
         </LinearLayout>
 
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
index 4ff3e2f..1fcb9a7 100644
--- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java
+++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
@@ -29,6 +29,7 @@
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.DialogInterface.OnCancelListener;
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.content.pm.ActivityInfo;
@@ -115,6 +116,7 @@
 import com.android.gallery3d.filtershow.provider.SharedImageProvider;
 import com.android.gallery3d.filtershow.state.StateAdapter;
 import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine;
+import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine.DdmStatus;
 import com.android.gallery3d.filtershow.tools.SaveImage;
 import com.android.gallery3d.filtershow.tools.XmpPresets;
 import com.android.gallery3d.filtershow.tools.XmpPresets.XMresults;
@@ -194,7 +196,6 @@
     private Menu mMenu;
     private DialogInterface mCurrentDialog = null;
     private PopupMenu mCurrentMenu = null;
-    private boolean mLoadingVisible = true;
     private boolean mReleaseDualCamOnDestory = true;
     private FrameLayout mCategoryFragment;
     private View mEffectsContainer;
@@ -205,6 +206,8 @@
     boolean isOrientationChanged;
     private boolean isComingFromEditorScreen;
 
+    private ProgressDialog mLoadingDialog;
+
     public ProcessingService getProcessingService() {
         return mBoundService;
     }
@@ -759,6 +762,13 @@
         mShowingTinyPlanet = false;
         mLoadBitmapTask = new LoadBitmapTask();
         mLoadBitmapTask.execute(uri);
+
+        if(DualCameraNativeEngine.getInstance().isLibLoaded()) {
+            LoadMpoDataTask mpoLoad = new LoadMpoDataTask();
+            mpoLoad.execute();
+        } else {
+            MasterImage.getImage().setDepthMapLoadingStatus(DdmStatus.DDM_FAILED);
+        }
     }
 
     private void fillBorders() {
@@ -978,10 +988,6 @@
                 mBoundService.setHighresPreviewScaleFactor(highResPreviewScale);
             }
 
-            if(DualCameraNativeEngine.getInstance().isLibLoaded()) {
-                LoadMpoDataTask mpoLoad = new LoadMpoDataTask();
-                mpoLoad.execute();
-            }
             MasterImage.getImage().warnListeners();
         }
     }
@@ -994,31 +1000,47 @@
 
         @Override
         protected void onPostExecute(Boolean result) {
-            MasterImage.getImage().warnListeners();
-
+            MasterImage.getImage().setDepthMapLoadingStatus(result?DdmStatus.DDM_LOADED:DdmStatus.DDM_FAILED);
             Fragment currentPanel = getSupportFragmentManager().findFragmentByTag(MainPanel.FRAGMENT_TAG);
             if (currentPanel instanceof MainPanel) {
                 MainPanel mainPanel = (MainPanel) currentPanel;
-                mainPanel.enableDualCameraButton(result);
+                mainPanel.updateDualCameraButton();
             }
         }
     }
 
     public boolean isLoadingVisible() {
-        return mLoadingVisible;
+        if(mLoadingDialog != null) {
+            return mLoadingDialog.isShowing();
+        }
+
+        return false;
     }
 
     public void startLoadingIndicator() {
-        final View loading = findViewById(R.id.loading);
-        mLoadingVisible = true;
-        loading.setVisibility(View.VISIBLE);
+        if(mLoadingDialog == null) {
+            mLoadingDialog = new ProgressDialog(this);
+            mLoadingDialog.setMessage(getString(R.string.loading_image));
+            mLoadingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
+            mLoadingDialog.setIndeterminate(true);
+            mLoadingDialog.setCancelable(true);
+            mLoadingDialog.setCanceledOnTouchOutside(false);
+            mLoadingDialog.setOnCancelListener(new OnCancelListener() {
+                @Override
+                public void onCancel(DialogInterface dialog) {
+                    finish();
+                }
+            });
+        }
+
+        mLoadingDialog.show();
     }
 
     public void stopLoadingIndicator() {
-        final View loading = findViewById(R.id.loading);
-        loading.setVisibility(View.GONE);
-        mLoadingVisible = false;
-    }
+        if(mLoadingDialog != null && mLoadingDialog.isShowing()) {
+            mLoadingDialog.dismiss();
+        }
+     }
 
     private class LoadBitmapTask extends AsyncTask<Uri, Boolean, Boolean> {
         int mBitmapSize;
diff --git a/src/com/android/gallery3d/filtershow/category/MainPanel.java b/src/com/android/gallery3d/filtershow/category/MainPanel.java
index e7d686e..e10dd51 100644
--- a/src/com/android/gallery3d/filtershow/category/MainPanel.java
+++ b/src/com/android/gallery3d/filtershow/category/MainPanel.java
@@ -34,6 +34,7 @@
 import com.android.gallery3d.filtershow.imageshow.MasterImage;
 import com.android.gallery3d.filtershow.state.StatePanel;
 import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine;
+import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine.DdmStatus;
 
 public class MainPanel extends Fragment {
 
@@ -57,9 +58,9 @@
     public static final int BORDERS = 1;
     public static final int GEOMETRY = 2;
     public static final int FILTERS = 3;
-    public static final int DUALCAM = 4;
-    public static final int VERSIONS = 5;
-    public static final int MAKEUP = 6;
+    public static final int MAKEUP = 4;
+    public static final int DUALCAM = 5;
+    public static final int VERSIONS = 6;
 
     private int mCurrentSelected = -1;
     private int mPreviousToggleVersions = -1;
@@ -179,7 +180,8 @@
                 showPanel(DUALCAM);
             }
         });
-        enableDualCameraButton(DualCameraNativeEngine.getInstance().isLibLoaded());
+
+        updateDualCameraButton();
 
         FilterShowActivity activity = (FilterShowActivity) getActivity();
         //showImageStatePanel(activity.isShowingImageStatePanel());
@@ -425,8 +427,10 @@
         transaction.commit();
     }
 
-    public void enableDualCameraButton(boolean enable) {
+    public void updateDualCameraButton() {
         if(dualCamButton != null) {
+            DdmStatus status = MasterImage.getImage().getDepthMapLoadingStatus();
+            boolean enable = (status == DdmStatus.DDM_LOADED);
             dualCamButton.setVisibility(enable?View.VISIBLE:View.GONE);
             TextView tvDualCam = (TextView) mEffectsTextContainer
                     .findViewById(R.id.tvDualCam);
diff --git a/src/com/android/gallery3d/filtershow/imageshow/ImageShow.java b/src/com/android/gallery3d/filtershow/imageshow/ImageShow.java
index 261d43f..58bddce 100644
--- a/src/com/android/gallery3d/filtershow/imageshow/ImageShow.java
+++ b/src/com/android/gallery3d/filtershow/imageshow/ImageShow.java
@@ -272,14 +272,12 @@
         MasterImage img = MasterImage.getImage();
         // Hide the loading indicator as needed
         if (mActivity.isLoadingVisible() && getFilteredImage() != null) {
-            if ((img.getLoadedPreset() == null)
-                    || (img.getLoadedPreset() != null
-                    && img.getLoadedPreset().equals(img.getCurrentPreset()))) {
-                mActivity.stopLoadingIndicator();
-            } else if (img.getLoadedPreset() != null) {
+            if(img.getLoadedPreset() != null
+                    && !img.getLoadedPreset().equals(img.getCurrentPreset())) {
                 return;
+            } else if (img.isDepthMapLoadingDone()) {
+                mActivity.stopLoadingIndicator();
             }
-            mActivity.stopLoadingIndicator();
         }
 
         Bitmap fusionUnderlay = MasterImage.getImage().getFusionUnderlay();
diff --git a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java
index 9dc7d78..9493ed7 100644
--- a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java
+++ b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java
@@ -16,8 +16,6 @@
 
 package com.android.gallery3d.filtershow.imageshow;
 
-import java.io.File;
-import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Vector;
 
@@ -54,6 +52,7 @@
 import com.android.gallery3d.filtershow.pipeline.SharedPreset;
 import com.android.gallery3d.filtershow.state.StateAdapter;
 import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine;
+import com.android.gallery3d.filtershow.tools.DualCameraNativeEngine.DdmStatus;
 import com.android.gallery3d.mpo.MpoParser;
 
 public class MasterImage implements RenderingRequestCaller {
@@ -98,6 +97,7 @@
     private Bitmap mFusionUnderlay = null;
     private Rect mImageBounds = null;
     private Rect mFusionBounds = null;
+    private DualCameraNativeEngine.DdmStatus mDepthMapLoadingStatus = DdmStatus.DDM_IDLE;
 
     private ValueAnimator mAnimator = null;
     private float mMaskScale = 1;
@@ -878,58 +878,49 @@
 
             // check for pre-generated dm file
             String mpoFilepath = ImageLoader.getLocalPathFromUri(getActivity(), getUri());
-            String depthFilepath = MpoParser.getDepthmapFilepath(mpoFilepath);
-            File depthFile = new File(depthFilepath);
-            if(depthFile.exists()) {
-                // TODO: read from depth map file and init DDM
-                //                ByteBuffer depthMap = MpoParser.readDepthMapFile(depthFilepath);
-                //
-                //                DualCameraNativeEngine.getInstance().loadDepthMap(
-                //                        mPrimaryMpoImg, primaryWidth, primaryHeight, primaryStride,
-                //                        depthMap, depthMapWidth, depthMapHeight, depthMapStride);
+            // read auxiliary image and generate depth map.
+            Bitmap auxiliaryBm = BitmapFactory.decodeByteArray(auxiliaryMpoData, 0, auxiliaryMpoData.length);
+            auxiliaryMpoData = null;
 
-            } else {
-                // read auxiliary image and generate depth map.
-                Bitmap auxiliaryBm = BitmapFactory.decodeByteArray(auxiliaryMpoData, 0, auxiliaryMpoData.length);
-                auxiliaryMpoData = null;
-
-                if(auxiliaryBm == null) {
-                    primaryBm.recycle();
-                    primaryBm = null;
-                    return false;
-                }
-
-                DualCameraNativeEngine.getInstance().initDepthMap(
-                        primaryBm, auxiliaryBm, mpoFilepath,
-                        DualCameraNativeEngine.getInstance().getCalibFilepath(mActivity));
-
+            if(auxiliaryBm == null) {
                 primaryBm.recycle();
                 primaryBm = null;
-                auxiliaryBm.recycle();
-                auxiliaryBm = null;
+                return false;
+            }
 
-                Point size = new Point();
-                boolean result = DualCameraNativeEngine.getInstance().getDepthMapSize(size);
-                if(result) {
-                    Log.v(LOGTAG, "get depthmapsize returned true. size: " + size.x + "x" + size.y);
+            DualCameraNativeEngine.getInstance().initDepthMap(
+                    primaryBm, auxiliaryBm, mpoFilepath,
+                    DualCameraNativeEngine.getInstance().getCalibFilepath(mActivity));
 
+            primaryBm.recycle();
+            primaryBm = null;
+            auxiliaryBm.recycle();
+            auxiliaryBm = null;
+
+            Point size = new Point();
+            boolean result = DualCameraNativeEngine.getInstance().getDepthMapSize(size);
+            if(result) {
+                Log.v(LOGTAG, "get depthmapsize returned true. size: " + size.x + "x" + size.y);
+
+                if(size.x == 0 || size.y == 0) {
+                    Log.v(LOGTAG, "invalid ddm size: " + size.x + "x" + size.y);
+                    loaded = false;
+                } else {
                     Bitmap depthMap = Bitmap.createBitmap(size.x, size.y, Config.ALPHA_8);
                     if(DualCameraNativeEngine.getInstance().getDepthMap(depthMap)) {
                         loaded = true;
-
-                        // TODO: write and read depth map.
-                        // MpoParser.writeDepthMapFile(depthFilepath, depthMap);
                     } else {
                         Log.w(LOGTAG, "get depthmap returned false");
                     }
 
                     depthMap.recycle();
                     depthMap = null;
-                } else {
-                    Log.w(LOGTAG, "get depthmapsize returned false");
                 }
+            } else {
+                Log.w(LOGTAG, "get depthmapsize returned false");
             }
         }
+
         return loaded;
     }
 
@@ -957,4 +948,17 @@
     public Rect getImageBounds() {
         return mImageBounds;
     }
+
+    public boolean isDepthMapLoadingDone() {
+        return (mDepthMapLoadingStatus == DdmStatus.DDM_LOADED ||
+                mDepthMapLoadingStatus == DdmStatus.DDM_FAILED);
+    }
+
+    public DdmStatus getDepthMapLoadingStatus() {
+        return mDepthMapLoadingStatus;
+    }
+
+    public void setDepthMapLoadingStatus(DdmStatus status) {
+        mDepthMapLoadingStatus = status;
+    }
 }
diff --git a/src/com/android/gallery3d/filtershow/tools/DualCameraNativeEngine.java b/src/com/android/gallery3d/filtershow/tools/DualCameraNativeEngine.java
index e5f13b4..5a8581f 100644
--- a/src/com/android/gallery3d/filtershow/tools/DualCameraNativeEngine.java
+++ b/src/com/android/gallery3d/filtershow/tools/DualCameraNativeEngine.java
@@ -30,7 +30,6 @@
 package com.android.gallery3d.filtershow.tools;
 
 import java.io.File;
-import java.nio.ByteBuffer;
 
 import android.content.Context;
 import android.graphics.Bitmap;
@@ -50,6 +49,14 @@
         }
     }
 
+    // Status of Depth Map loading for current image
+    public static enum DdmStatus {
+        DDM_IDLE,
+        DDM_LOADING,
+        DDM_LOADED,
+        DDM_FAILED
+    };
+
     public static final String DEPTH_MAP_EXT = "dm";
     private static final String CALIBRATION_FILENAME = "ddm_calib_file.dat";
     private static boolean mLibLoaded;