Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. ImageButton btnSwitch;
  4. private Camera camera;
  5. private boolean isFlashOn, hasFlash;
  6. private MediaPlayer mp;
  7. private Camera.Parameters params;
  8.  
  9. protected void onCreate(Bundle b) {
  10. super.onCreate(b);
  11. setContentView(R.layout.activity_main);
  12. // flash switch button
  13. btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
  14. // First check if device is supporting flashlight or not
  15. hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  16. if (!hasFlash) {
  17. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  18. builder.setTitle("Error")
  19. .setMessage("Sorry, your device doesn't support flash light!")
  20. .setCancelable(false)
  21. .setNegativeButton
  22. ("Close", new DialogInterface.OnClickListener() {
  23. public void onClick(DialogInterface dialog, int id) {
  24. finish();
  25. }
  26. }
  27. );
  28. AlertDialog alert = builder.create();
  29. alert.show();
  30. return;
  31. }
  32. // get the camera
  33. getCamera();
  34. // displaying button image
  35. toggleButtonImage();
  36. // Switch button click event to toggle flash on/off
  37. btnSwitch.setOnClickListener
  38. (new View.OnClickListener() {
  39. public void onClick(View v) {
  40. if (isFlashOn) {
  41. // turn off flash
  42. turnOffFlash();
  43. } else {
  44. // turn on flash
  45. turnOnFlash();
  46. }
  47. }
  48. }
  49. );
  50. }
  51.  
  52. protected void getCamera() {
  53. if (camera == null) {
  54. try {
  55. camera = Camera.open();
  56. params = camera.getParameters();
  57. } catch (RuntimeException e) {
  58. Log.e("Failed to Open. Error: ", e.getMessage());
  59. }
  60. }
  61. }
  62.  
  63. protected void turnOnFlash() {
  64. if (!isFlashOn) {
  65. if (camera == null || params == null) {
  66. return;
  67. }
  68. // play sound
  69. playSound();
  70. params = camera.getParameters();
  71. params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
  72. camera.setParameters(params);
  73. camera.startPreview();
  74. isFlashOn = true;
  75. // changing button/switch image
  76. toggleButtonImage();
  77. }
  78. }
  79.  
  80. protected void turnOffFlash() {
  81. if (isFlashOn) {
  82. if (camera == null || params == null) {
  83. return;
  84. }
  85. // play sound
  86. playSound();
  87. params = camera.getParameters();
  88. params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
  89. camera.setParameters(params);
  90. camera.stopPreview();
  91. isFlashOn = false;
  92. // changing button/switch image
  93. toggleButtonImage();
  94. }
  95. }
  96.  
  97. protected void playSound() {
  98. if (isFlashOn) {
  99. mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
  100. } else {
  101. mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
  102. }
  103. mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  104. public void onCompletion(MediaPlayer mp) {
  105. // TODO Auto-generated method stub
  106. mp.release();
  107. }
  108. });
  109. mp.start();
  110. }
  111.  
  112. protected void toggleButtonImage() {
  113. if (isFlashOn) {
  114. btnSwitch.setImageResource(R.drawable.btn_switch_on);
  115. } else {
  116. btnSwitch.setImageResource(R.drawable.btn_switch_off);
  117. }
  118. }
  119.  
  120. protected void onDestroy() {
  121. super.onDestroy();
  122. }
  123.  
  124. protected void onPause() {
  125. super.onPause();
  126. // on pause turn off the flash
  127. turnOffFlash();
  128. }
  129.  
  130. protected void onRestart() {
  131. super.onRestart();
  132. }
  133.  
  134. protected void onResume() {
  135. super.onResume();
  136. // on resume turn on the flash
  137. if (hasFlash)
  138. turnOnFlash();
  139. }
  140.  
  141. protected void onStart() {
  142. super.onStart();
  143. // on starting the app get the camera params
  144. getCamera();
  145.  
  146. }
  147.  
  148. protected void onStop() {
  149. super.onStop();
  150. // on stop release the camera
  151. if (camera != null) {
  152. camera.release();
  153. camera = null;
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement