package com.swijaya.galaxytorch;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class GalaxyTorchActivity extends Activity implements View.OnClickListener {

    private final String TAG = "GalaxyTorchActivity";

    private CameraDevice mCameraDevice;
    private FrameLayout mPreviewLayout; // should be hidden

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mCameraDevice = new CameraDevice();
        mPreviewLayout = (FrameLayout) findViewById(R.id.camera_preview);

        Button button = (Button) findViewById(R.id.pressbutton);
        button.setOnClickListener(this);
    }

    public void onClick(View v) {
        boolean isTorchOn = mCameraDevice.isFlashlightOn();
        Log.v(TAG, "Current torch state: " + (isTorchOn ? "on" : "off"));

        if (!isTorchOn) {
            // we're toggling the torch ON
            SurfaceView preview = mCameraDevice.acquireCamera(this);
            mPreviewLayout.addView(preview);
        }

        if (!mCameraDevice.toggleCameraLED(!isTorchOn)) {
            Log.e(TAG, "Cannot toggle camera LED");
        }
        Log.v(TAG, "Current torch state should be " +
                (mCameraDevice.isFlashlightOn() ? "on" : "off"));
    }

}

@@ -471,16 +471,56 @@
 e hidden
+%0A    private SurfaceView mCameraPreview;
 %0A%0A    /*
@@ -1205,21 +1205,61 @@
     
-SurfaceView p
+assert (mCameraPreview == null);%0A            mCameraP
 revi
@@ -1337,28 +1337,311 @@
 iew(
-preview);%0A        %7D%0A
+mCameraPreview);%0A        %7D else %7B%0A            // we're toggling the torch OFF%0A            assert (mCameraPreview != null);%0A            mPreviewLayout.removeView(mCameraPreview);%0A            mCameraPreview = null;%0A        %7D%0A%0A        // toggling the torch OFF should automatically release camera resources
 %0A   
@@ -1757,16 +1757,17 @@
       %7D%0A
+%0A
         

Manage life cycle of camera preview.