Guest User

Untitled

a guest
Sep 20th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. RequestUserPermission requestUserPermission = new RequestUserPermission(this);
  6. requestUserPermission.verifyStoragePermissions();
  7. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  8. createSoundPoolWithBuilder();
  9. } else {
  10. createSoundPoolWithConstructor();
  11. }
  12. soundPool.setOnLoadCompleteListener(this);
  13. sound = soundPool.load(this, R.raw.click, 1);
  14. mySwitch = (Switch) findViewById(R.id.my_switch);
  15. mySwitch.setChecked(false);
  16. mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  17. @Override
  18. public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
  19. if (isChecked) {
  20. setFlashLigthOn();
  21. } else {
  22. setFlashLightOff();
  23. }
  24. }
  25. });
  26.  
  27. boolean isCameraFlash = getApplicationContext().getPackageManager()
  28. .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  29. if (!isCameraFlash) {
  30. showCameraAlert();
  31. } else {
  32. camera = Camera.open();
  33. }
  34. }
  35.  
  36. private void showCameraAlert() {
  37. new AlertDialog.Builder(this)
  38. .setTitle(R.string.error_title)
  39. .setMessage(R.string.error_text)
  40. .setPositiveButton(R.string.exit_message, new DialogInterface.OnClickListener() {
  41.  
  42. @Override
  43. public void onClick(DialogInterface dialog, int which) {
  44. finish();
  45. }
  46. })
  47. .setIcon(android.R.drawable.ic_dialog_alert)
  48. .show();
  49. }
  50.  
  51. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  52. protected void createSoundPoolWithBuilder() {
  53. AudioAttributes attributes = new AudioAttributes.Builder()
  54. .setUsage(AudioAttributes.USAGE_GAME)
  55. .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
  56. .build();
  57. soundPool = new SoundPool.Builder().setAudioAttributes(attributes).setMaxStreams(1).build();
  58. }
  59.  
  60. @SuppressWarnings("deprecation")
  61. protected void createSoundPoolWithConstructor() {
  62. soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
  63. }
  64.  
  65. private void setFlashLigthOn() {
  66. soundPool.play(sound, 1, 1, 0, 0, 1);
  67. new Thread(new Runnable() {
  68. @Override
  69. public void run() {
  70. if (camera != null) {
  71. parameters = camera.getParameters();
  72. if (parameters != null) {
  73. List supportedFlashModes = parameters.getSupportedFlashModes();
  74. if (supportedFlashModes.contains(Parameters.FLASH_MODE_TORCH)) {
  75. parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
  76. // } else if (supportedFlashModes.contains(Parameters.FLASH_MODE_ON)) {
  77. // parameters.setFlashMode(Parameters.FLASH_MODE_ON);
  78. } else camera = null;
  79. if (camera != null) {
  80. try {
  81. camera.setParameters(parameters);
  82. camera.startPreview();
  83. } catch (RuntimeException f) {
  84.  
  85. }
  86. try {
  87. camera.setPreviewTexture(new SurfaceTexture(0));
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }).start();
  96. }
  97.  
  98. private void setFlashLightOff() {
  99. soundPool.play(sound, 1, 1, 0, 0, 1);
  100. new Thread(new Runnable() {
  101. @Override
  102. public void run() {
  103. if (camera != null) {
  104. parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
  105. camera.setParameters(parameters);
  106. camera.stopPreview();
  107. }
  108. }
  109. }).start();
  110. }
  111.  
  112. private void releaseCamera() {
  113. if (camera != null) {
  114. camera.release();
  115. camera = null;
  116. }
  117. }
  118.  
  119. @Override
  120. protected void onStop() {
  121. super.onStop();
  122. // releaseCamera();
  123. }
  124.  
  125. @Override
  126. protected void onPause() {
  127. super.onPause();
  128. //releaseCamera();
  129. // mySwitch.setChecked(false);
  130. }
  131.  
  132. @Override
  133. protected void onResume() {
  134. super.onResume();
  135. if (camera == null) {
  136. camera = Camera.open();
  137. } else {
  138. setFlashLigthOn();
  139. }
  140. // mySwitch.setChecked(false);
  141. }
  142.  
  143. @Override
  144. protected void onStart() {
  145. super.onStart();
  146. camera = Camera.open();
  147.  
  148. }
  149.  
  150. @Override
  151. public void onLoadComplete(SoundPool soundPool, int i, int i1) {
  152.  
  153. }
Add Comment
Please, Sign In to add comment