public void switchFlashlight(View view) {
   if (getString(R.string.turn_on_flashlight).equals(switch_flashlight.getText())) {
     zxing_barcode_scanner.setTorchOn();
   } else {
     zxing_barcode_scanner.setTorchOff();
   }
 }
 @Override
 public void barcodeResult(BarcodeResult result) {
   if (result != null && result.getBarcodeFormat().equals(BarcodeFormat.QR_CODE)) {
     barcodeScannerView.setStatusText(result.getText());
     etCodePinEntry.setText(result.getText());
     barcodeScannerView.pause();
     submit();
   }
 }
  @AfterViews
  void init() {
    zxing_barcode_scanner.setTorchListener(
        new CompoundBarcodeView.TorchListener() {
          @Override
          public void onTorchOn() {

            switch_flashlight.setText(R.string.turn_off_flashlight);
          }

          @Override
          public void onTorchOff() {
            switch_flashlight.setText(R.string.turn_on_flashlight);
          }
        });

    // if the device does not have flashlight in its camera,
    // then remove the switch flashlight button...
    if (!hasFlash()) {
      switch_flashlight.setVisibility(View.GONE);
    }

    zxing_barcode_scanner = (CompoundBarcodeView) findViewById(R.id.zxing_barcode_scanner);
    captureManager = new CaptureManager(this, zxing_barcode_scanner);
    captureManager.initializeFromIntent(getIntent(), savedInstanceState);
    captureManager.decode();
  }
  /**
   * Creates an instance of the activity by telling barcode scanner to scan using the {@link
   * BarcodeCallback} above and adding an {@link android.view.View.OnClickListener} to the Submit
   * {@link Button}
   *
   * @param savedInstanceState - Saved Instance Bundle
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanning);

    barcodeScannerView = (CompoundBarcodeView) findViewById(R.id.zxing_barcode_scanner);
    barcodeScannerView.decodeContinuous(callback);
    etCodePinEntry = (EditText) findViewById(R.id.etCodePinEntry);

    Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setContentDescription(
        btnSubmit.getResources().getString(R.string.content_description_submits));
    btnSubmit.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            submit();
          }
        });

    ActionBar actionbar = getSupportActionBar();

    Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/ubuntu_l.ttf");
    SpannableString s = new SpannableString("hiTour");
    s.setSpan(new CustomTypefaceSpan("", font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    if (actionbar != null) {
      actionbar.setTitle(s);
    }

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {
      Snackbar.make(
              barcodeScannerView, "Camera permission needed to scan points.", Snackbar.LENGTH_LONG)
          .show();
    }
  }
 /**
  * Deals with certain key presses when using the Barcode Scanner
  *
  * @param keyCode Keys pressed
  * @param event Event
  * @return boolean if keys are pressed
  */
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
 }
 /** Pauses the {@link AppCompatActivity} and pauses the barcode scanner and camera input */
 @Override
 protected void onPause() {
   super.onPause();
   barcodeScannerView.pause();
 }
 /** Resumes the {@link AppCompatActivity} and resumes the Barcode Scanner */
 @Override
 protected void onResume() {
   super.onResume();
   barcodeScannerView.resume();
 }
 /**
  * Clears input received in the {@link EditText} field and the Barcode Scanner's status bar text
  */
 private void clearInput() {
   barcodeScannerView.setStatusText("");
   etCodePinEntry.setText("");
 }
  /**
   * Method that checks if the data input to the {@link EditText} field matches a valid format and
   * that it matches a value stored for a point. Then navigates the use to the point data scanned.
   *
   * <p>Otherwise an error message is shown to the user and the input is cleared ready for the next
   * input.
   */
  private void submit() {

    // Hide keyboard when submit is pressed so Snackbar can be seen
    View view = this.getCurrentFocus();
    if (view != null) {
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    EditText etCodePinEntry = (EditText) findViewById(R.id.etCodePinEntry);
    String result = etCodePinEntry.getText().toString();
    // Check if the point came from being scanned

    // Check if a point or a tour was submitted
    boolean isTour = false;
    if (result.length() > 6 && result.substring(0, 6).equals("POINT-")) {
      // Takes identification part of point id
      result = result.substring(6);
    } else if (result.length() > 2 && result.substring(0, 2).equals("SN")) {
      // Takes identification part of session passphrase
      result = result.substring(2);
      // Sets to identify as a tour
      isTour = true;
    }

    entry = result;

    // Check if user wants to add a session or a point
    if (isTour) {
      // For when the user attempts to add a session
      try {
        // Checks to see if tour session is already on device
        Cursor sessionCursor = FeedActivity.database.getAll(SESSION_TABLE);
        boolean alreadyExists = false;
        for (int i = 0; i < sessionCursor.getCount(); i++) {
          sessionCursor.moveToPosition(i);
          if (result.equals(sessionCursor.getString(sessionCursor.getColumnIndex(PASSPHRASE)))) {
            alreadyExists = true;
          }
        }
        if (alreadyExists) {
          Log.d("FeedActivity", "Tour for " + entry + " already exists!");
          Snackbar.make(
                  barcodeScannerView,
                  "Tour already downloaded on this device.",
                  Snackbar.LENGTH_LONG)
              .show();
          barcodeScannerView.resume();
          clearInput();
        } else {
          // Attempts to download tour if the passphrase is valid
          TourSubmit tourSubmit = new TourSubmit();
          tourSubmit.execute(result);
        }
      } catch (NotInSchemaException e) {
        Log.e("DATABASE_FAIL", Log.getStackTraceString(e));
      }
    } else {
      // Calls FeedActivity#onActivityResult if the point exists.
      // Displays a message otherwise.
      if (pointExistsInTour(result)) {
        Intent data = new Intent();
        data.putExtra("mode", "point");
        data.putExtra(DetailActivity.EXTRA_PIN, result);

        // Replace unlock with a value of 1
        Map<String, String> tourPointColumnsMap = new HashMap<>();
        tourPointColumnsMap.put(UNLOCK, UNLOCK_STATE_UNLOCKED);
        Map<String, String> tourPointPrimaryKeysMap = new HashMap<>();
        Log.i("inScanning", "" + FeedActivity.currentTourId);
        tourPointPrimaryKeysMap.put(TOUR_ID, "" + FeedActivity.currentTourId);
        tourPointPrimaryKeysMap.put(POINT_ID, result);
        try {
          Cursor cursorGetRank =
              FeedActivity.database.getWholeByPrimaryPartial(
                  POINT_TOUR_TABLE, tourPointPrimaryKeysMap);
          cursorGetRank.moveToFirst();
          String pointRank = cursorGetRank.getString(cursorGetRank.getColumnIndex(RANK));
          tourPointColumnsMap.put(RANK, pointRank);
          FeedActivity.database.insert(
              tourPointColumnsMap, tourPointPrimaryKeysMap, POINT_TOUR_TABLE);
        } catch (NotInSchemaException e) {
          Log.e("DATABASE_FAIL", Log.getStackTraceString(e));
        }
        // Notify the feedAdapter that a viewHolder's view has to update.
        ObservableLock observableLock = new ObservableLock();
        observableLock.addObserver(FeedActivity.getCurrentFeedAdapter());
        observableLock.setChange(result, FeedActivity.currentTourId);
        setResult(RESULT_OK, data);
        finish();
      } else {
        Log.d("FeedActivity", "Point for " + entry + " not found!");
        Snackbar.make(
                barcodeScannerView, "Point not found, please try again.", Snackbar.LENGTH_LONG)
            .show();
        barcodeScannerView.resume();
        clearInput();
      }
    }
  }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   return zxing_barcode_scanner.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
 }