Advertisement
ms_olin

Untitled

May 7th, 2017
1,884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package com.panelic.flashlight;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.pm.PackageManager;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.ImageButton;
  11. import android.hardware.Camera;
  12. import android.hardware.Camera.Parameters;
  13. import android.media.MediaPlayer;
  14. import android.media.MediaPlayer.OnCompletionListener;
  15.  
  16. public class MainActivity extends Activity {
  17.  
  18.     ImageButton btnSwitch;
  19.  
  20.     private Camera camera;
  21.     private boolean isFlashOn;
  22.     private boolean hasFlash;
  23.     Parameters params;
  24.     MediaPlayer mp;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main);
  30.  
  31.         // flash switch button
  32.         btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
  33.  
  34.  
  35.         // First check if device is supporting flashlight or not
  36.         hasFlash = getApplicationContext().getPackageManager()
  37.                 .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  38.  
  39.         if (!hasFlash) {
  40.             // device doesn't support flash
  41.             // Show alert message and close the application
  42.             AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
  43.                     .create();
  44.             alert.setTitle("Error");
  45.             alert.setMessage("Sorry, your device doesn't support flash light!");
  46.             alert.setButton("OK", new DialogInterface.OnClickListener() {
  47.                 public void onClick(DialogInterface dialog, int which) {
  48.                     // closing the application
  49.                     finish();
  50.                 }
  51.             });
  52.             alert.show();
  53.             return;
  54.         }
  55.  
  56.         // get the camera
  57.         getCamera();
  58.  
  59.         // displaying button image
  60.         toggleButtonImage();
  61.  
  62.  
  63.         // Switch button click event to toggle flash on/off
  64.         btnSwitch.setOnClickListener(new View.OnClickListener() {
  65.  
  66.             @Override
  67.             public void onClick(View v) {
  68.                 if (isFlashOn) {
  69.                     // turn off flash
  70.                     turnOffFlash();
  71.                 } else {
  72.                     // turn on flash
  73.                     turnOnFlash();
  74.                 }
  75.             }
  76.         });
  77.     }
  78.  
  79.     // Get the camera
  80.     private void getCamera() {
  81.         if (camera == null) {
  82.             try {
  83.                 camera = Camera.open();
  84.                 params = camera.getParameters();
  85.             } catch (RuntimeException e) {
  86.                 Log.e("Camera Error. Error: ", e.getMessage());
  87.             }
  88.         }
  89.     }
  90.  
  91.     // Turning On flash
  92.     private void turnOnFlash() {
  93.         if (!isFlashOn) {
  94.             if (camera == null || params == null) {
  95.                 return;
  96.             }
  97.             // play sound
  98.             playSound();
  99.  
  100.             params = camera.getParameters();
  101.             params.setFlashMode(Parameters.FLASH_MODE_TORCH);
  102.             camera.setParameters(params);
  103.             camera.startPreview();
  104.             isFlashOn = true;
  105.  
  106.             // changing button/switch image
  107.             toggleButtonImage();
  108.         }
  109.  
  110.     }
  111.  
  112.     // Turning Off flash
  113.     private void turnOffFlash() {
  114.         if (isFlashOn) {
  115.             if (camera == null || params == null) {
  116.                 return;
  117.             }
  118.             // play sound
  119.             playSound();
  120.  
  121.             params = camera.getParameters();
  122.             params.setFlashMode(Parameters.FLASH_MODE_OFF);
  123.             camera.setParameters(params);
  124.             camera.stopPreview();
  125.             isFlashOn = false;
  126.  
  127.             // changing button/switch image
  128.             toggleButtonImage();
  129.         }
  130.     }
  131.  
  132.     // Playing sound
  133.     // will play button toggle sound on flash on / off
  134.     private void playSound(){
  135.         if(isFlashOn){
  136.             mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
  137.         }else{
  138.             mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
  139.         }
  140.         mp.setOnCompletionListener(new OnCompletionListener() {
  141.  
  142.             @Override
  143.             public void onCompletion(MediaPlayer mp) {
  144.                 // TODO Auto-generated method stub
  145.                 mp.release();
  146.             }
  147.         });
  148.         mp.start();
  149.     }
  150.  
  151.     /*
  152.      * Toggle switch button images
  153.      * changing image states to on / off
  154.      * */
  155.     private void toggleButtonImage(){
  156.         if(isFlashOn){
  157.             btnSwitch.setImageResource(R.drawable.btn_switch_on);
  158.         }else{
  159.             btnSwitch.setImageResource(R.drawable.btn_switch_off);
  160.         }
  161.     }
  162.  
  163.     @Override
  164.     protected void onDestroy() {
  165.         super.onDestroy();
  166.     }
  167.  
  168.     @Override
  169.     protected void onPause() {
  170.         super.onPause();
  171.  
  172.         // on pause turn off the flash
  173.         turnOffFlash();
  174.     }
  175.  
  176.     @Override
  177.     protected void onRestart() {
  178.         super.onRestart();
  179.     }
  180.  
  181.     @Override
  182.     protected void onResume() {
  183.         super.onResume();
  184.  
  185.         // on resume turn on the flash
  186.         if(hasFlash)
  187.             turnOnFlash();
  188.     }
  189.  
  190.     @Override
  191.     protected void onStart() {
  192.         super.onStart();
  193.  
  194.         // on starting the app get the camera params
  195.         getCamera();
  196.     }
  197.  
  198.     @Override
  199.     protected void onStop() {
  200.         super.onStop();
  201.  
  202.         // on stop release the camera
  203.         if (camera != null) {
  204.             camera.release();
  205.             camera = null;
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement