SnapdragonCamera: Fix the memory leak on view.

Keep using modules instead of creating.
Don't remove the view without cleaning it.

Change-Id: I260a660cac3d770f7d02dfdc1881e15490da66a
diff --git a/res/layout/camera.xml b/res/layout/camera.xml
index 9a3a01a..781ff9c 100644
--- a/res/layout/camera.xml
+++ b/res/layout/camera.xml
@@ -13,9 +13,23 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<com.android.camera.ui.CameraRootView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/camera_app_root"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/camera_root_frame"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
-</com.android.camera.ui.CameraRootView>
+    <com.android.camera.ui.CameraRootView
+        android:id="@+id/camera_photo_root"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" >
+    </com.android.camera.ui.CameraRootView>
+    <com.android.camera.ui.CameraRootView
+        android:id="@+id/camera_video_root"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" >
+    </com.android.camera.ui.CameraRootView>
+    <com.android.camera.ui.CameraRootView
+        android:id="@+id/camera_pano_root"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" >
+    </com.android.camera.ui.CameraRootView>
+</FrameLayout>
diff --git a/src/com/android/camera/CameraActivity.java b/src/com/android/camera/CameraActivity.java
index d898371..4175bad 100644
--- a/src/com/android/camera/CameraActivity.java
+++ b/src/com/android/camera/CameraActivity.java
@@ -152,8 +152,14 @@
     private PlaceholderManager mPlaceholderManager;
     private int mCurrentModuleIndex;
     private CameraModule mCurrentModule;
+    private PhotoModule mPhotoModule;
+    private VideoModule mVideoModule;
+    private WideAnglePanoramaModule mPanoModule;
     private FrameLayout mAboveFilmstripControlLayout;
-    private View mCameraModuleRootView;
+    private FrameLayout mCameraRootFrame;
+    private View mCameraPhotoModuleRootView;
+    private View mCameraVideoModuleRootView;
+    private View mCameraPanoModuleRootView;
     private FilmStripView mFilmStripView;
     private ProgressBar mBottomProgress;
     private View mPanoStitchingPanel;
@@ -1084,7 +1090,10 @@
         mPlaceholderManager.addTaskListener(mPlaceholderListener);
         LayoutInflater inflater = getLayoutInflater();
         View rootLayout = inflater.inflate(R.layout.camera, null, false);
-        mCameraModuleRootView = rootLayout.findViewById(R.id.camera_app_root);
+        mCameraRootFrame = (FrameLayout)rootLayout.findViewById(R.id.camera_root_frame);
+        mCameraPhotoModuleRootView = rootLayout.findViewById(R.id.camera_photo_root);
+        mCameraVideoModuleRootView = rootLayout.findViewById(R.id.camera_video_root);
+        mCameraPanoModuleRootView = rootLayout.findViewById(R.id.camera_pano_root);
         mPanoStitchingPanel = findViewById(R.id.pano_stitching_progress_panel);
         mBottomProgress = (ProgressBar) findViewById(R.id.pano_stitching_progress_bar);
         mCameraPreviewData = new CameraPreviewData(rootLayout,
@@ -1133,7 +1142,6 @@
 
         mOrientationListener = new MyOrientationEventListener(this);
         setModuleFromIndex(moduleIndex);
-        mCurrentModule.init(this, mCameraModuleRootView);
 
         if (!mSecureCamera) {
             mDataAdapter = mWrappedDataAdapter;
@@ -1452,32 +1460,53 @@
      * index an sets it as mCurrentModule.
      */
     private void setModuleFromIndex(int moduleIndex) {
+        mCameraPhotoModuleRootView.setVisibility(View.GONE);
+        mCameraVideoModuleRootView.setVisibility(View.GONE);
+        mCameraPanoModuleRootView.setVisibility(View.GONE);
+        mCameraRootFrame.removeAllViews();
         mCurrentModuleIndex = moduleIndex;
         switch (moduleIndex) {
             case ModuleSwitcher.VIDEO_MODULE_INDEX:
-                mCurrentModule = new VideoModule();
+                if(mVideoModule == null) {
+                    mVideoModule = new VideoModule();
+                    mVideoModule.init(this, mCameraVideoModuleRootView);
+                }
+                mCurrentModule = mVideoModule;
+                mCameraRootFrame.addView(mCameraVideoModuleRootView);
+                mCameraVideoModuleRootView.setVisibility(View.VISIBLE);
                 break;
 
             case ModuleSwitcher.PHOTO_MODULE_INDEX:
-                mCurrentModule = new PhotoModule();
+                if(mPhotoModule == null) {
+                    mPhotoModule = new PhotoModule();
+                    mPhotoModule.init(this, mCameraPhotoModuleRootView);
+                }
+                mCurrentModule = mPhotoModule;
+                mCameraRootFrame.addView(mCameraPhotoModuleRootView);
+                mCameraPhotoModuleRootView.setVisibility(View.VISIBLE);
                 break;
 
             case ModuleSwitcher.WIDE_ANGLE_PANO_MODULE_INDEX:
-                mCurrentModule = new WideAnglePanoramaModule();
+                if(mPanoModule == null) {
+                    mPanoModule = new WideAnglePanoramaModule();
+                    mPanoModule.init(this, mCameraPanoModuleRootView);
+                }
+                mCurrentModule = mPanoModule;
+                mCameraRootFrame.addView(mCameraPanoModuleRootView);
+                mCameraPanoModuleRootView.setVisibility(View.VISIBLE);
                 break;
 
-            case ModuleSwitcher.LIGHTCYCLE_MODULE_INDEX:
-                mCurrentModule = PhotoSphereHelper.createPanoramaModule();
-                break;
-            case ModuleSwitcher.GCAM_MODULE_INDEX:
-                // Force immediate release of Camera instance
-                CameraHolder.instance().strongRelease();
-                mCurrentModule = GcamHelper.createGcamModule();
-                break;
+            case ModuleSwitcher.LIGHTCYCLE_MODULE_INDEX: //Unused module for now
+            case ModuleSwitcher.GCAM_MODULE_INDEX:  //Unused module for now
             default:
                 // Fall back to photo mode.
-                mCurrentModule = new PhotoModule();
-                mCurrentModuleIndex = ModuleSwitcher.PHOTO_MODULE_INDEX;
+                if(mPhotoModule == null) {
+                    mPhotoModule = new PhotoModule();
+                    mPhotoModule.init(this, mCameraPhotoModuleRootView);
+                }
+                mCurrentModule = mPhotoModule;
+                mCameraRootFrame.addView(mCameraPhotoModuleRootView);
+                mCameraPhotoModuleRootView.setVisibility(View.VISIBLE);
                 break;
         }
     }
@@ -1517,7 +1546,6 @@
     }
 
     private void openModule(CameraModule module) {
-        module.init(this, mCameraModuleRootView);
         module.onResumeBeforeSuper();
         module.onResumeAfterSuper();
     }
@@ -1525,8 +1553,6 @@
     private void closeModule(CameraModule module) {
         module.onPauseBeforeSuper();
         module.onPauseAfterSuper();
-        ((ViewGroup) mCameraModuleRootView).removeAllViews();
-        ((ViewGroup) mCameraModuleRootView).clearDisappearingChildren();
     }
 
     private void performDeletion() {
diff --git a/src/com/android/camera/CameraSettings.java b/src/com/android/camera/CameraSettings.java
index de6fc62..a92abbe 100644
--- a/src/com/android/camera/CameraSettings.java
+++ b/src/com/android/camera/CameraSettings.java
@@ -964,12 +964,12 @@
         editor.apply();
     }
 
-    public static void upgradeGlobalPreferences(SharedPreferences pref) {
-        upgradeOldVersion(pref);
+    public static void upgradeGlobalPreferences(SharedPreferences pref, Context context) {
+        upgradeOldVersion(pref, context);
         upgradeCameraId(pref);
     }
 
-    private static void upgradeOldVersion(SharedPreferences pref) {
+    private static void upgradeOldVersion(SharedPreferences pref,  Context context) {
         int version;
         try {
             version = pref.getInt(KEY_VERSION, 0);
@@ -1079,7 +1079,7 @@
         // we may write the preference to wrong camera later.
         preferences.setLocalId(context, currentCameraId);
 
-        upgradeGlobalPreferences(preferences.getGlobal());
+        upgradeGlobalPreferences(preferences.getGlobal(), context);
         upgradeLocalPreferences(preferences.getLocal());
 
         // Write back the current camera id because parameters are related to
diff --git a/src/com/android/camera/PhotoModule.java b/src/com/android/camera/PhotoModule.java
index 76c6a21..5524fec 100644
--- a/src/com/android/camera/PhotoModule.java
+++ b/src/com/android/camera/PhotoModule.java
@@ -505,7 +505,7 @@
         mActivity = activity;
         mRootView = parent;
         mPreferences = new ComboPreferences(mActivity);
-        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), activity);
         mCameraId = getPreferredCameraId(mPreferences);
 
         mContentResolver = mActivity.getContentResolver();
@@ -2148,6 +2148,11 @@
     @Override
     public void onResumeBeforeSuper() {
         mPaused = false;
+        mPreferences = new ComboPreferences(mActivity);
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), mActivity);
+        mCameraId = getPreferredCameraId(mPreferences);
+        mPreferences.setLocalId(mActivity, mCameraId);
+        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
     }
 
     private void openCamera() {
@@ -2194,6 +2199,7 @@
             Log.v(TAG, "On resume.");
             onResumeTasks();
         }
+        mUI.setSwitcherIndex();
         mHandler.post(new Runnable(){
             @Override
             public void run(){
@@ -2839,6 +2845,7 @@
         if (CameraUtil.isSupported(colorEffect, mParameters.getSupportedColorEffects())) {
             mParameters.setColorEffect(colorEffect);
         }
+
         //Set Saturation
         String saturationStr = mPreferences.getString(
                 CameraSettings.KEY_SATURATION,
diff --git a/src/com/android/camera/PhotoUI.java b/src/com/android/camera/PhotoUI.java
index eb077c3..f2b7a53 100644
--- a/src/com/android/camera/PhotoUI.java
+++ b/src/com/android/camera/PhotoUI.java
@@ -475,6 +475,11 @@
     public void showSwitcher() {
         mSwitcher.setVisibility(View.VISIBLE);
     }
+
+    public void setSwitcherIndex() {
+        mSwitcher.setCurrentIndex(ModuleSwitcher.PHOTO_MODULE_INDEX);
+    }
+
     // called from onResume but only the first time
     public  void initializeFirstTime() {
         // Initialize shutter button.
diff --git a/src/com/android/camera/VideoModule.java b/src/com/android/camera/VideoModule.java
index ee72e6d..22d6929 100644
--- a/src/com/android/camera/VideoModule.java
+++ b/src/com/android/camera/VideoModule.java
@@ -422,7 +422,7 @@
         mActivity = activity;
         mUI = new VideoUI(activity, this, root);
         mPreferences = new ComboPreferences(mActivity);
-        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), activity);
         mCameraId = getPreferredCameraId(mPreferences);
 
         mPreferences.setLocalId(mActivity, mCameraId);
@@ -1009,6 +1009,11 @@
     @Override
     public void onResumeBeforeSuper() {
         mPaused = false;
+        mPreferences = new ComboPreferences(mActivity);
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), mActivity);
+        mCameraId = getPreferredCameraId(mPreferences);
+        mPreferences.setLocalId(mActivity, mCameraId);
+        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
     }
 
     @Override
@@ -1016,6 +1021,7 @@
         mUI.enableShutter(false);
         mZoomValue = 0;
 
+        initializeVideoControl();
         showVideoSnapshotUI(false);
 
         if (!mPreviewing) {
@@ -1034,7 +1040,7 @@
         mUI.initDisplayChangeListener();
         // Initializing it here after the preview is started.
         mUI.initializeZoom(mParameters);
-
+        mUI.setSwitcherIndex();
         keepScreenOnAwhile();
 
         mOrientationManager.resume();
diff --git a/src/com/android/camera/VideoUI.java b/src/com/android/camera/VideoUI.java
index 24ea15d..14d59c4 100644
--- a/src/com/android/camera/VideoUI.java
+++ b/src/com/android/camera/VideoUI.java
@@ -399,6 +399,10 @@
         mSwitcher.setVisibility(View.VISIBLE);
     }
 
+    public void setSwitcherIndex() {
+        mSwitcher.setCurrentIndex(ModuleSwitcher.VIDEO_MODULE_INDEX);
+    }
+
     public boolean collapseCameraControls() {
         boolean ret = false;
         if (mPopup != null) {
diff --git a/src/com/android/camera/WideAnglePanoramaModule.java b/src/com/android/camera/WideAnglePanoramaModule.java
index b612ee9..f4e6925 100644
--- a/src/com/android/camera/WideAnglePanoramaModule.java
+++ b/src/com/android/camera/WideAnglePanoramaModule.java
@@ -203,6 +203,17 @@
         }
     }
 
+    private int getPreferredCameraId(ComboPreferences preferences) {
+        int intentCameraId = CameraUtil.getCameraFacingIntentExtras(mActivity);
+        if (intentCameraId != -1) {
+            // Testing purpose. Launch a specific camera through the intent
+            // extras.
+            return intentCameraId;
+        } else {
+            return CameraSettings.readPreferredCameraId(preferences);
+        }
+    }
+
     @Override
     public void init(CameraActivity activity, View parent) {
         mActivity = activity;
@@ -266,7 +277,8 @@
         mDialogWaitingPreviousString = appRes.getString(R.string.pano_dialog_waiting_previous);
 
         mPreferences = new ComboPreferences(mActivity);
-        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
+        mPreferences.setLocalId(mActivity, getPreferredCameraId(mPreferences));
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), activity);
         mLocationManager = new LocationManager(mActivity, null);
 
         mMainHandler = new Handler() {
@@ -904,6 +916,10 @@
     @Override
     public void onResumeBeforeSuper() {
         mPaused = false;
+        mPreferences = new ComboPreferences(mActivity);
+        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal(), mActivity);
+        mPreferences.setLocalId(mActivity, getPreferredCameraId(mPreferences));
+        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());
     }
 
     @Override
@@ -943,6 +959,7 @@
                 }
             });
         }
+        mUI.setSwitcherIndex();
         keepScreenOnAwhile();
 
         mOrientationManager.resume();
diff --git a/src/com/android/camera/WideAnglePanoramaUI.java b/src/com/android/camera/WideAnglePanoramaUI.java
index 57b414a..1462b66 100644
--- a/src/com/android/camera/WideAnglePanoramaUI.java
+++ b/src/com/android/camera/WideAnglePanoramaUI.java
@@ -155,6 +155,10 @@
         mSwitcher.setVisibility(View.VISIBLE);
     }
 
+    public void setSwitcherIndex() {
+        mSwitcher.setCurrentIndex(ModuleSwitcher.WIDE_ANGLE_PANO_MODULE_INDEX);
+    }
+
     public void setCaptureProgressOnDirectionChangeListener(
             PanoProgressBar.OnDirectionChangeListener listener) {
         mCaptureProgressBar.setOnDirectionChangeListener(listener);