Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ca.jeandrepetzer.lab4;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.pm.PackageManager;
- import android.hardware.camera2.CameraAccessException;
- import android.hardware.camera2.CameraManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.CompoundButton;
- import android.widget.TextView;
- import android.widget.ToggleButton;
- //AlertDialog for showing error if flash is not available
- import android.app.AlertDialog;
- import java.util.HashMap;
- import java.util.StringTokenizer;
- public class MainActivity extends AppCompatActivity {
- private ToggleButton toggleButton;
- private CameraManager mCameraManager;
- private String mCameraId;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- boolean isFlashAvailable = getApplicationContext().getPackageManager()
- .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
- if (!isFlashAvailable) {
- showNoFlashError();
- }
- // Getting the camera manager and camera id of the phone
- mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
- try {
- mCameraId = mCameraManager.getCameraIdList()[0];
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- toggleButton = findViewById(R.id.toggleButton);
- toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- //called when the button status is changed
- switchFlashLight(isChecked);
- }
- });
- // Results in 3 objects in memory - inefficient (Use StringBuffer instead)
- // Android garbage collector will remove the unused variables in memory periodically
- //String cat = "c";
- //cat = cat + "a";
- //cat = cat + "t";
- StringBuffer sb = new StringBuffer();
- sb.append("B");
- sb.append("u");
- sb.append("i");
- sb.append("l");
- sb.append("d");
- sb.append("e");
- sb.append("r");
- sb.append(".com");
- Log.d("itas123", sb.toString());
- // To break a string into individual words by a delimeter
- String line = "The cat ate my hat";
- String words[] = line.split(" "); // split using space as the delimeter
- // This while loop should go through the individual words (e.g. first word will be "this")
- // for (int i=0; i<words.length; i++) {
- // Log.d("itas123","Here is the next word: " + words[i]);
- // }
- for (String word : words) {
- Log.d("itas123", "Here is the next word: " + word);
- }
- // Hash <ap
- //HashMap<String, String> wordMap;
- //wordMap = new HashMap<String, String>();
- // We put key/value pairs into this map (or maybe call it dictionary)
- //wordMap.put("evil","cat");
- //wordMap.put("war","party");
- //wordMap.put("good","dogs");
- // Retrieve a string from a hashMap - This is way more efficient than the OG way of
- // looping through the entire array to find evil's value
- // String myCat = wordMap.get("evil");
- 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";
- //String paragraph1Words[] = paragraph1.split("[ ,]+");
- StringBuffer modifiedParagraph = new StringBuffer();
- HashMap<String, String> wordMap;
- wordMap = new HashMap<String, String>();
- wordMap.put("evil", "cat");
- wordMap.put("war", "party");
- wordMap.put("good", "dogs");
- wordMap.put("Islam", "animals");
- wordMap.put("Muslim", "furry");
- StringTokenizer tokens = new StringTokenizer(paragraph1," .,",true);
- while (tokens.hasMoreTokens()) {
- String wordKey = tokens.nextToken();
- String matchedWord = wordMap.get(wordKey);
- if (matchedWord != null) {
- modifiedParagraph.append(matchedWord);
- } else {
- modifiedParagraph.append(wordKey);
- }
- }
- //for (String paragraph1Word : paragraph1Words) {
- // wordMap.put(paragraph1Word, paragraph1Word);
- //}
- //StringBuffer modifiedParagraph = new StringBuffer();
- //for (String paragraph1Word : paragraph1Words) {
- // String newParagraphWord = wordMap.get(paragraph1Word) + " ";
- // modifiedParagraph.append(newParagraphWord);
- //}
- Log.d("itas123", modifiedParagraph.toString());
- TextView fillBox = findViewById(R.id.fillBox);
- fillBox.setText(modifiedParagraph.toString());
- }
- public void showNoFlashError() {
- AlertDialog alert = new AlertDialog.Builder(this)
- .create();
- alert.setTitle("Oops!");
- alert.setMessage("Flash not available in this device...");
- alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- });
- alert.show();
- }
- public void switchFlashLight(boolean status) {
- try {
- mCameraManager.setTorchMode(mCameraId, status);
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment