Flash-ZA

Untitled

Feb 14th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package ca.jeandrepetzer.lab4;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.pm.PackageManager;
  8. import android.hardware.camera2.CameraAccessException;
  9. import android.hardware.camera2.CameraManager;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.widget.CompoundButton;
  13. import android.widget.TextView;
  14. import android.widget.ToggleButton;
  15. //AlertDialog for showing error if flash is not available
  16. import android.app.AlertDialog;
  17.  
  18. import java.util.HashMap;
  19. import java.util.StringTokenizer;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23. private ToggleButton toggleButton;
  24. private CameraManager mCameraManager;
  25. private String mCameraId;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31.  
  32. boolean isFlashAvailable = getApplicationContext().getPackageManager()
  33. .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  34.  
  35. if (!isFlashAvailable) {
  36. showNoFlashError();
  37. }
  38.  
  39. // Getting the camera manager and camera id of the phone
  40. mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
  41. try {
  42. mCameraId = mCameraManager.getCameraIdList()[0];
  43. } catch (CameraAccessException e) {
  44. e.printStackTrace();
  45. }
  46.  
  47. toggleButton = findViewById(R.id.toggleButton);
  48. toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  49. @Override
  50. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  51. //called when the button status is changed
  52. switchFlashLight(isChecked);
  53. }
  54. });
  55.  
  56. // Results in 3 objects in memory - inefficient (Use StringBuffer instead)
  57. // Android garbage collector will remove the unused variables in memory periodically
  58. //String cat = "c";
  59. //cat = cat + "a";
  60. //cat = cat + "t";
  61.  
  62. StringBuffer sb = new StringBuffer();
  63. sb.append("B");
  64. sb.append("u");
  65. sb.append("i");
  66. sb.append("l");
  67. sb.append("d");
  68. sb.append("e");
  69. sb.append("r");
  70. sb.append(".com");
  71. Log.d("itas123", sb.toString());
  72.  
  73. // To break a string into individual words by a delimeter
  74. String line = "The cat ate my hat";
  75. String words[] = line.split(" "); // split using space as the delimeter
  76.  
  77. // This while loop should go through the individual words (e.g. first word will be "this")
  78. // for (int i=0; i<words.length; i++) {
  79. // Log.d("itas123","Here is the next word: " + words[i]);
  80. // }
  81. for (String word : words) {
  82. Log.d("itas123", "Here is the next word: " + word);
  83. }
  84.  
  85. // Hash <ap
  86. //HashMap<String, String> wordMap;
  87. //wordMap = new HashMap<String, String>();
  88.  
  89. // We put key/value pairs into this map (or maybe call it dictionary)
  90. //wordMap.put("evil","cat");
  91. //wordMap.put("war","party");
  92. //wordMap.put("good","dogs");
  93.  
  94. // Retrieve a string from a hashMap - This is way more efficient than the OG way of
  95. // looping through the entire array to find evil's value
  96. // String myCat = wordMap.get("evil");
  97.  
  98. String paragraph1 = "We're taking action against evil people. Because this great nation of many religions understands, our war is not against Islam, or against faith practiced by the Muslim people. Our war is a war against evil. This is clearly a case of good versus evil, and make no mistake about it - good will prevail.\n";
  99. //String paragraph1Words[] = paragraph1.split("[ ,]+");
  100. StringBuffer modifiedParagraph = new StringBuffer();
  101.  
  102. HashMap<String, String> wordMap;
  103. wordMap = new HashMap<String, String>();
  104.  
  105. wordMap.put("evil", "cat");
  106. wordMap.put("war", "party");
  107. wordMap.put("good", "dogs");
  108. wordMap.put("Islam", "animals");
  109. wordMap.put("Muslim", "furry");
  110.  
  111. StringTokenizer tokens = new StringTokenizer(paragraph1," .,",true);
  112. while (tokens.hasMoreTokens()) {
  113. String wordKey = tokens.nextToken();
  114. String matchedWord = wordMap.get(wordKey);
  115. if (matchedWord != null) {
  116. modifiedParagraph.append(matchedWord);
  117. } else {
  118. modifiedParagraph.append(wordKey);
  119. }
  120. }
  121.  
  122. //for (String paragraph1Word : paragraph1Words) {
  123. // wordMap.put(paragraph1Word, paragraph1Word);
  124. //}
  125.  
  126.  
  127.  
  128. //StringBuffer modifiedParagraph = new StringBuffer();
  129.  
  130. //for (String paragraph1Word : paragraph1Words) {
  131. // String newParagraphWord = wordMap.get(paragraph1Word) + " ";
  132. // modifiedParagraph.append(newParagraphWord);
  133. //}
  134.  
  135. Log.d("itas123", modifiedParagraph.toString());
  136.  
  137. TextView fillBox = findViewById(R.id.fillBox);
  138. fillBox.setText(modifiedParagraph.toString());
  139.  
  140. }
  141.  
  142. public void showNoFlashError() {
  143. AlertDialog alert = new AlertDialog.Builder(this)
  144. .create();
  145. alert.setTitle("Oops!");
  146. alert.setMessage("Flash not available in this device...");
  147. alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
  148. public void onClick(DialogInterface dialog, int which) {
  149. finish();
  150. }
  151. });
  152. alert.show();
  153. }
  154.  
  155. public void switchFlashLight(boolean status) {
  156. try {
  157. mCameraManager.setTorchMode(mCameraId, status);
  158. } catch (CameraAccessException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment