Back to project page Simplest-Flashlight-App.
The source code is released under:
Apache License
If you think the Android project Simplest-Flashlight-App 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.andrewq.simpleflashlight; //from ww w . j av a2 s.c o m import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.os.Vibrator; import android.preference.PreferenceManager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { private boolean isLightOn = false; private Camera camera; private ToggleButton button; public Vibrator v; RelativeLayout mainLayout, flashlightLayout, strobeLayout; @Override public void onCreate(Bundle savedInstanceState) { Log.d("MainActivity.java", "onCreate() was called"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); button = (ToggleButton) findViewById(R.id.buttonFlashlight); flashlightLayout = (RelativeLayout) findViewById(R.id.flashlightLayout); strobeLayout = (RelativeLayout) findViewById(R.id.strobeLayout); mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); final Context context = this; PackageManager pm = context.getPackageManager(); if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { v.vibrate(100); new AlertDialog.Builder(this) .setTitle("Sorry") .setMessage( "It appears that your device is incompatible with this app. Sorry for the inconvenience.") .setNeutralButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }).show(); return; } if (camera == null) { camera = Camera.open(); } final Parameters p = camera.getParameters(); // Use the OnSwipeTouchListener class to define gestures and their // actions // TODO: the `flashlightLayout` and `strobeLayout` OnTouchListeners must // match flashlightLayout.setOnTouchListener(new OnSwipeTouchListener() { public void onSwipeTop() { } public void onSwipeRight() { strobeLayout.setVisibility(View.VISIBLE); flashlightLayout.setVisibility(View.GONE); } public void onSwipeLeft() { } public void onSwipeBottom() { } }); // Use the OnSwipeTouchListener class to define gestures and their // actions // TODO: the `flashlightLayout` and `strobeLayout` OnTouchListeners must // match strobeLayout.setOnTouchListener(new OnSwipeTouchListener() { public void onSwipeTop() { } public void onSwipeRight() { strobeLayout.setVisibility(View.GONE); flashlightLayout.setVisibility(View.VISIBLE); } public void onSwipeLeft() { } public void onSwipeBottom() { } }); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (isLightOn) { Toast.makeText(context, "Light off!", Toast.LENGTH_SHORT) .show(); v.vibrate(40); p.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(p); camera.stopPreview(); isLightOn = false; } else { Toast.makeText(context, "Light on!", Toast.LENGTH_SHORT) .show(); v.vibrate(40); p.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(p); camera.startPreview(); isLightOn = true; } } }); strobeSetup(); } @Override public void onWindowFocusChanged(boolean hasFocus) { TextView strobeTv1; SeekBar skbr1; SharedPreferences getPrefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); boolean box = getPrefs.getBoolean("pref_brightness", true); if (box == true) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 100 / 100.0f; getWindow().setAttributes(lp); } else { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = -1; getWindow().setAttributes(lp); } boolean box2 = getPrefs.getBoolean("pref_strobe_speed", false); if (box2 == true) { strobeTv1 = (TextView) findViewById(R.id.strobe_tv2); skbr1 = (SeekBar) findViewById(R.id.strobe_adjust); strobeTv1.setVisibility(View.VISIBLE); skbr1.setVisibility(View.VISIBLE); } else { strobeTv1 = (TextView) findViewById(R.id.strobe_tv2); skbr1 = (SeekBar) findViewById(R.id.strobe_adjust); strobeTv1.setVisibility(View.GONE); skbr1.setVisibility(View.GONE); } super.onWindowFocusChanged(hasFocus); } @Override protected void onPause() { Log.d("MainActivity.java", "onPause() was called"); super.onPause(); if (camera != null) { camera.release(); camera = null; } } @Override protected void onStop() { Log.d("MainActivity.java", "onStop() was called"); super.onStop(); if (camera != null) { camera.release(); } } protected void onDestroy() { Log.d("MainActivity.java", "onDestroy() was called"); super.onDestroy(); if (camera != null) { camera.release(); camera = null; } } @Override public void onResume() { Log.d("MainActivity.java", "onResume() was called"); super.onResume(); if (camera == null) { camera = Camera.open(); } } /** Menu inflater Method */ @Override public boolean onCreateOptionsMenu(Menu menu) { Log.d("MainActivity.java", "onCreateOptionsMenu() was called"); getMenuInflater().inflate(R.menu.menu, menu); return true; } /** Called when the user clicks a menu button */ public boolean onOptionsItemSelected(MenuItem item) { Log.d("MainActivity.java", "onOptionsItemSelected() was called"); super.onOptionsItemSelected(item); switch (item.getItemId()) { case R.id.about: aboutMenuItem(); break; case R.id.settings: Intent intent = new Intent(getApplicationContext(), Settings.class); startActivity(intent); break; } return super.onOptionsItemSelected(item); } private void strobeSetup() { @SuppressWarnings("unused") SeekBar seekbar1; seekbar1 = (SeekBar) findViewById(R.id.strobe_adjust); //need help with rest of code... } private void aboutMenuItem() { new AlertDialog.Builder(this) .setTitle("About the Developer") .setMessage( "Welcome to the simplest flashlight app on Google Play!" + "\n" + "This application toggles your LED light on your smart phone so that you can use it as a flashlight. " + "I am a high school student learning the ropes of building elegant and simple Android applications. " + "Please leave a rating on the Play Store and check out some of my other applications too!" + "\n" + "If you would like to donate to my cause, all donations are accepted but not expected. All proceeds go towards new applications. " + "I hope this application suits all your flashlight needs. " + "\n" + "Thank you!") .setNeutralButton("Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // leave blank } }).show(); } }