package com.swijaya.galaxytorch;

import android.app.Activity;
import android.content.res.Configuration;
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
    private SurfaceView mCameraPreview;

    /** 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
            assert (mCameraPreview == null);
            mCameraPreview = mCameraDevice.acquireCamera(this);
            mPreviewLayout.addView(mCameraPreview);
        } else {
            // we're toggling the torch OFF
        }

        // toggling the torch OFF should automatically release camera resources
        assert (mCameraPreview != null);
        if (!mCameraDevice.toggleCameraLED(!isTorchOn)) {
            Log.e(TAG, "Cannot toggle camera LED");
        }

        isTorchOn = mCameraDevice.isFlashlightOn();
        Log.v(TAG, "Current torch state should be " + (isTorchOn ? "on" : "off"));

        if (!isTorchOn) {
            // clean up after toggling OFF: preview surface
            Log.v(TAG, "Cleaning up preview surface");
            mPreviewLayout.removeView(mCameraPreview);
            mCameraPreview = null;
        }
    }

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// as per the manifest configuration, this will be called on
		// orientation change or the virtual keyboard being hidden,
		// which should not even happen at all, due to the activity's
		// orientation being set to 'portrait' mode in the manifest
		super.onConfigurationChanged(newConfig);
	}

}

@@ -60,50 +60,8 @@
 ty;%0A
-import android.content.res.Configuration;%0A
 impo
@@ -82,16 +82,16 @@
 Bundle;%0A
+
 import a
@@ -2048,17 +2048,22 @@
 %0A    %7D%0A%0A
-%09
+    /*
 @Overrid
@@ -2064,17 +2064,20 @@
 verride%0A
-%09
+    
 public v
@@ -2130,18 +2130,24 @@
 nfig) %7B%0A
-%09%09
+        
 // as pe
@@ -2199,18 +2199,24 @@
 lled on%0A
-%09%09
+        
 // orien
@@ -2267,18 +2267,24 @@
 hidden,%0A
-%09%09
+        
 // which
@@ -2337,18 +2337,24 @@
 ivity's%0A
-%09%09
+        
 // orien
@@ -2409,10 +2409,16 @@
 est%0A
-%09%09
+        
 supe
@@ -2458,9 +2458,1800 @@
 g);%0A
-%09
+    %7D*/%0A%0A    /*@Override%0A    protected void onDestroy() %7B%0A        // the entire lifetime ends here%0A        super.onDestroy();%0A        Log.v(TAG, %22onDestroy%22);%0A    %7D*/%0A%0A    @Override%0A    protected void onPause() %7B%0A        // the foreground lifetime ends here (called often)%0A        super.onPause();%0A        Log.v(TAG, %22onPause%22);%0A%0A        // turn off the torch if it is on%0A        // XXX: toggleCameraLED() has noticeable delay! consider%0A        //      alternative approach to concurrently finish onPause%0A        //      while toggling camera torch and releasing resources%0A        boolean isTorchOn = mCameraDevice.isFlashlightOn();%0A        if (isTorchOn) %7B%0A            // toggling the camera LED off also releases resources, which%0A            // contain extra actions that are probably better performed%0A            // elsewhere in the lifecycle model%0A            if (!mCameraDevice.toggleCameraLED(false)) %7B%0A                Log.e(TAG, %22Cannot toggle camera LED%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /*@Override%0A    protected void onResume() %7B%0A        // the foreground lifetime starts here (called often)%0A        super.onResume();%0A        Log.v(TAG, %22onResume%22);%0A    %7D*/%0A%0A    @Override%0A    protected void onStart() %7B%0A        // the visible timeline starts here%0A        super.onStart();%0A        Log.v(TAG, %22onStart%22);%0A%0A        assert (mCameraPreview == null);%0A        assert (!mCameraDevice.isFlashlightOn());%0A    %7D%0A%0A    @Override%0A    protected void onStop() %7B%0A        // the visible timeline ends here%0A        super.onStop();%0A        Log.v(TAG, %22onStop%22);%0A%0A        // clean up after toggling OFF: preview surface%0A        assert (mPreviewLayout != null);%0A        Log.v(TAG, %22Cleaning up preview surface%22);%0A        mPreviewLayout.removeView(mCameraPreview);%0A        mCameraPreview = null;%0A    
 %7D%0A%0A%7D

Implement the main activity's lifecycle callbacks.