Back to project page Flashlight.
The source code is released under:
MIT License
If you think the Android project Flashlight listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.omihaz.flashlight; /*from w ww. j av a 2 s. c o m*/ import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.hardware.Camera; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.TextView; import java.io.IOException; public class MainActivity extends Activity implements SurfaceHolder.Callback { private static final String TAG = "Flashlight"; private BroadcastReceiver mReceiver; private TextView mStatusView; private GestureDetector mGestureDetector; private Camera mCamera; private boolean mEnabled; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetector = new GestureDetector(this, new DoubleClickGestureListener()); mGestureDetector.setIsLongpressEnabled(false); mReceiver = new KeyGuardReceiver(this); registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT)); createSurface(); createStatusView(); } @SuppressWarnings("deprecation") private void createSurface() { SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface); SurfaceHolder holder = surfaceView.getHolder(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } holder.addCallback(this); } private void createStatusView() { mStatusView = (TextView) findViewById(R.id.status); mStatusView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (mGestureDetector != null) { mGestureDetector.onTouchEvent(event); } return true; } }); mStatusView.post(new Runnable() { @Override public void run() { mCamera = Camera.open(); } }); updateViewState(); } @Override protected void onDestroy() { if (mReceiver != null) { unregisterReceiver(mReceiver); mReceiver = null; } if (mCamera != null) { mCamera.release(); mCamera = null; } super.onDestroy(); } @Override protected void onStop() { super.onStop(); updateLightState(false); } private void resumeLight() { if (mCamera != null) { updateLightState(false); updateLightState(); } } private void updateLightState() { updateLightState(mEnabled); } private void updateLightState(boolean enabled) { if (mCamera == null) { Log.w(TAG, "Cannot change light state, because camera not found!"); return; } Camera.Parameters params = mCamera.getParameters(); params.setFlashMode( enabled ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF ); mCamera.setParameters(params); updateViewState(); } private void updateViewState() { mStatusView.setBackgroundColor(getBackgroundStatusColor()); mStatusView.setTextColor(getTextStatusColor()); mStatusView.setText(mEnabled ? R.string.turn_off : R.string.turn_on); mStatusView.setKeepScreenOn(mEnabled); } private int getBackgroundStatusColor() { return mEnabled ? Color.WHITE : Color.BLACK; } private int getTextStatusColor() { return mEnabled ? Color.BLACK : Color.WHITE; } @Override public void surfaceCreated(SurfaceHolder holder) { if (mCamera != null) { try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); resumeLight(); } catch (IOException e) { Log.e(TAG, "Cannot set preview mode to camera", e); } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mCamera != null) { mCamera.stopPreview(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } private final class DoubleClickGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onSingleTapConfirmed(MotionEvent e) { return updateState(true); } @Override public boolean onDoubleTap(MotionEvent e) { return updateState(!mEnabled); } private boolean updateState(boolean enable) { if (mEnabled != enable) { mEnabled = enable; updateLightState(); return true; } return false; } } private static class KeyGuardReceiver extends BroadcastReceiver { private final MainActivity mActivity; private KeyGuardReceiver(MainActivity activity) { mActivity = activity; } @Override public void onReceive(Context context, Intent intent) { if (mActivity == null || context == null || intent == null) { return; } if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) { mActivity.resumeLight(); } } } }