<commit_before>package com.bleros.zaltman.miniFlashLight;

import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.view.View;
import android.widget.ToggleButton;
import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;

public class MiniFlashLight extends SmallApplication
{
    private Camera mCamera = null;

    @Override
    public void onCreate()
    {
        super.onCreate();

        setContentView(R.layout.main);
        setTitle(R.string.app_name);

        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();
                }
            }
        });
    }

    @Override
    public void onStart()
    {
        super.onStart();
    }

    @Override
    public void onStop()
    {
        super.onStop();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        flashLightOff();
    }

    public void 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();
                mCamera.unlock();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void flashLightOff()
    {
        try
        {
            if ((mCamera != null) && getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
            {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}<commit_msg>Don't restart on orientation change. Also, start with light on.<commit_after>package com.bleros.zaltman.miniFlashLight;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
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);

        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);
    }
}
{'fraction_non_alphanumeric': 0.10012878300064391, 'fraction_numerical': 0.00016097875080489375, 'mean_word_length': 1.8215258855585832, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519393', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1591, 'n_words': 284}