Initialize the HeadUpDisplay only once in the life time of the activity.

And review the code so that mHeadUpDisplay won't be null when used.

Change-Id: I335a25e64035627c30ec2c6f45c17566f0ea2420
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java
index 0b33728..052d16c 100644
--- a/src/com/android/camera/Camera.java
+++ b/src/com/android/camera/Camera.java
@@ -314,9 +314,7 @@
                     if (!mIsImageCaptureIntent)  {
                         setOrientationIndicator(mLastOrientation);
                     }
-                    if (mGLRootView != null) {
-                        mHeadUpDisplay.setOrientation(mLastOrientation);
-                    }
+                    mHeadUpDisplay.setOrientation(mLastOrientation);
                 }
             }
         };
@@ -358,6 +356,7 @@
         installIntentFilter();
         initializeFocusTone();
         initializeZoom();
+        initializeHeadUpDisplay();
         mFirstTimeInitialized = true;
         changeHeadUpDisplayState();
         addIdleHandler();
@@ -456,7 +455,7 @@
             // Perform zoom only when preview is started and snapshot is not in
             // progress.
             if (mPausing || !isCameraIdle() || !mPreviewing
-                    || mHeadUpDisplay == null || mZoomState != ZOOM_STOPPED) {
+                    || mZoomState != ZOOM_STOPPED) {
                 return false;
             }
 
@@ -970,9 +969,9 @@
         Configuration config = getResources().getConfiguration();
         if (config.orientation == Configuration.ORIENTATION_LANDSCAPE
                 && !mPausing && mFirstTimeInitialized) {
-            if (mGLRootView == null) initializeHeadUpDisplay();
+            if (mGLRootView == null) attachHeadUpDisplay();
         } else if (mGLRootView != null) {
-            finalizeHeadUpDisplay();
+            detachHeadUpDisplay();
         }
     }
 
@@ -996,17 +995,15 @@
     }
 
     private void initializeHeadUpDisplay() {
-        FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
-        mGLRootView = new GLRootView(this);
-        frame.addView(mGLRootView);
-
         mHeadUpDisplay = new CameraHeadUpDisplay(this);
         CameraSettings settings = new CameraSettings(this, mInitialParams);
         mHeadUpDisplay.initialize(this,
                 settings.getPreferenceGroup(R.xml.camera_preferences));
         mHeadUpDisplay.setListener(new MyHeadUpDisplayListener());
-        mHeadUpDisplay.setOrientation(mLastOrientation);
+    }
 
+    private void attachHeadUpDisplay() {
+        mHeadUpDisplay.setOrientation(mLastOrientation);
         if (mParameters.isZoomSupported()) {
             mHeadUpDisplay.setZoomRatios(getZoomRatios());
             mHeadUpDisplay.setZoomIndex(mZoomValue);
@@ -1017,13 +1014,15 @@
                 }
             });
         }
+        FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
+        mGLRootView = new GLRootView(this);
+        mGLRootView.setContentPane(mHeadUpDisplay);
+        frame.addView(mGLRootView);
 
         updateSceneModeInHud();
-
-        mGLRootView.setContentPane(mHeadUpDisplay);
     }
 
-    private void finalizeHeadUpDisplay() {
+    private void detachHeadUpDisplay() {
         mHeadUpDisplay.setGpsHasSignal(false);
         mHeadUpDisplay.collapse();
         ((ViewGroup) mGLRootView.getParent()).removeView(mGLRootView);
diff --git a/src/com/android/camera/VideoCamera.java b/src/com/android/camera/VideoCamera.java
index 0e04e31..958f19e 100644
--- a/src/com/android/camera/VideoCamera.java
+++ b/src/com/android/camera/VideoCamera.java
@@ -346,6 +346,8 @@
         mShutterButton.setOnShutterButtonListener(this);
         mShutterButton.requestFocus();
 
+        initializeHeadUpDisplay();
+
         // Make sure preview is started.
         try {
             startPreviewThread.join();
@@ -367,17 +369,13 @@
         Configuration config = getResources().getConfiguration();
         if (config.orientation == Configuration.ORIENTATION_LANDSCAPE
                 && !mPausing && mGLRootView == null) {
-            initializeHeadUpDisplay();
+            attachHeadUpDisplay();
         } else if (mGLRootView != null) {
-            finalizeHeadUpDisplay();
+            detachHeadUpDisplay();
         }
     }
 
     private void initializeHeadUpDisplay() {
-        FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
-        mGLRootView = new GLRootView(this);
-        frame.addView(mGLRootView);
-
         mHeadUpDisplay = new CamcorderHeadUpDisplay(this);
         CameraSettings settings = new CameraSettings(this, mParameters);
 
@@ -387,11 +385,17 @@
             group = filterPreferenceScreenByIntent(group);
         }
         mHeadUpDisplay.initialize(this, group);
-        mGLRootView.setContentPane(mHeadUpDisplay);
         mHeadUpDisplay.setListener(new MyHeadUpDisplayListener());
     }
 
-    private void finalizeHeadUpDisplay() {
+    private void attachHeadUpDisplay() {
+        FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
+        mGLRootView = new GLRootView(this);
+        frame.addView(mGLRootView);
+        mGLRootView.setContentPane(mHeadUpDisplay);
+    }
+
+    private void detachHeadUpDisplay() {
         mHeadUpDisplay.collapse();
         ((ViewGroup) mGLRootView.getParent()).removeView(mGLRootView);
         mGLRootView = null;