package com.bleros.zaltman.miniFlashLight;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Camera;
import android.util.Log;
import android.view.View;
import android.widget.ToggleButton;
import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;

public class MiniFlashLight extends SmallApplication
{
    private static final String TAG = MiniFlashLight.class.getSimpleName();
    private Camera mCamera;
    private Configuration mConfig;

    @Override
    public void onCreate()
    {
        Log.d(TAG, "onCreate()");
        super.onCreate();

        mConfig = new Configuration(getResources().getConfiguration());
        setContentView(R.layout.main);
        setTitle(R.string.app_name);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        SmallAppWindow.Attributes attr = getWindow().getAttributes();
        attr.flags &= ~SmallAppWindow.Attributes.FLAG_RESIZABLE;
        attr.flags |= SmallAppWindow.Attributes.FLAG_NO_TITLEBAR;
        getWindow().setAttributes(attr);

        final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        toggleButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (toggleButton.isChecked())
                {
                    flashLightOn();
                }
                else
                {
                    flashLightOff();
                }
            }
        });

        toggleButton.setChecked(flashLightOn());
    }

    @Override
    public void onStart()
    {
        Log.d(TAG, "onStart()");
        super.onStart();
    }

    @Override
    public void onStop()
    {
        Log.d(TAG, "onStop()");
        super.onStop();
    }

    @Override
    public void onDestroy()
    {
        Log.d(TAG, "onDestroy()");
        super.onDestroy();
        flashLightOff();
    }

    public boolean flashLightOn()
    {
        Log.d(TAG, "flashLightOn()");
        try
        {
            if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
            {
                mCamera = Camera.open();
                Camera.Parameters p = mCamera.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(p);
                mCamera.startPreview();
                return true;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    public void flashLightOff()
    {
        Log.d(TAG, "flashLightOff()");
        try
        {
            if ((mCamera != null) && getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
            {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean onSmallAppConfigurationChanged(Configuration newConfig)
    {
        Log.d(TAG, "onSmallAppConfigurationChanged()");
        int diff = newConfig.diff(mConfig);
        mConfig = new Configuration(getResources().getConfiguration());

        // Avoid application from restarting when orientation changed
        if ((diff & ActivityInfo.CONFIG_ORIENTATION) != 0)
        {
            Log.d(TAG, "onSmallAppConfigurationChanged() - change of orientation detected...");
            return true;
        }

        return super.onSmallAppConfigurationChanged(newConfig);
    }
}

@@ -647,32 +647,77 @@
 nCreate()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -730,24 +730,34 @@
 Create()%22);%0A
+        %7D%0A
         supe
@@ -1837,32 +1837,77 @@
 onStart()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -1919,24 +1919,34 @@
 nStart()%22);%0A
+        %7D%0A
         supe
@@ -2002,32 +2002,77 @@
  onStop()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -2083,24 +2083,34 @@
 onStop()%22);%0A
+        %7D%0A
         supe
@@ -2168,32 +2168,77 @@
 Destroy()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -2252,24 +2252,34 @@
 estroy()%22);%0A
+        %7D%0A
         supe
@@ -2357,32 +2357,77 @@
 LightOn()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -2444,24 +2444,34 @@
 ightOn()%22);%0A
+        %7D%0A
         try%0A
@@ -3032,32 +3032,77 @@
 ightOff()%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -3120,24 +3120,34 @@
 ghtOff()%22);%0A
+        %7D%0A
         try%0A
@@ -3589,32 +3589,77 @@
 ewConfig)%0A    %7B%0A
+        if (BuildConfig.DEBUG)%0A        %7B%0A    
         Log.d(TA
@@ -3694,24 +3694,34 @@
 hanged()%22);%0A
+        %7D%0A
         int 
@@ -3956,32 +3956,85 @@
 != 0)%0A        %7B%0A
+            if (BuildConfig.DEBUG)%0A            %7B%0A    
             Log.
@@ -4109,24 +4109,38 @@
 ected...%22);%0A
+            %7D%0A
             

Disable logging in release builds.