<commit_before>package com.hufi.taxmanreader.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.hufi.taxmanreader.GroomApplication;
import com.hufi.taxmanreader.GroomService;
import com.hufi.taxmanreader.R;
import com.hufi.taxmanreader.model.Ticket;
import com.hufi.taxmanreader.utils.GroomScannerView;
import com.hufi.taxmanreader.utils.TaxmanUtils;

import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;

import me.dm7.barcodescanner.zxing.ZXingScannerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ScannerFragment extends Fragment implements ZXingScannerView.ResultHandler {
    //  private ZXingScannerView mScannerView;

    private GroomScannerView groomScannerView;

    private String cameraIDUsed;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        groomScannerView = new GroomScannerView(getActivity());
        groomScannerView.setAutoFocus(true);
       /* mScannerView = new ZXingScannerView(getActivity());
        mScannerView.setAutoFocus(true);*/

        setupFormats();
        if (!setUpBackCamera()) cameraIDUsed = "0";
        //return mScannerView;
        return groomScannerView;
    }

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setHasOptionsMenu(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        groomScannerView.setResultHandler(this);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.scanner));
        groomScannerView.startCamera(Integer.valueOf(cameraIDUsed));
    }

    @Override
    public void onPause() {
        super.onPause();
        groomScannerView.stopCamera();
    }

    public void setupFormats() {
        List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();
        formats.add(BarcodeFormat.QR_CODE);

        if (groomScannerView != null) {
            groomScannerView.setFormats(formats);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.turn_flash:
                if (groomScannerView.getFlash()) {
                    groomScannerView.setFlash(false);
                    if (!groomScannerView.getFlash()) item.setIcon(R.drawable.ic_action_flash);
                } else {
                    groomScannerView.setFlash(true);
                    if (groomScannerView.getFlash()) item.setIcon(R.drawable.ic_action_flash_light);
                }
                break;
            case R.id.swap_camera:
                changeCamera();
                groomScannerView.stopCamera();
                groomScannerView.startCamera(Integer.valueOf(cameraIDUsed));
                break;
            case R.id.by_id:
                launchDialog();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void handleResult(Result result) {
        try {
            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decode(getString(R.string.public_key), Base64.DEFAULT));
            KeyFactory keyFactory = KeyFactory.getInstance("ECDSA");
            PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
            JsonWebSignature jws = new JsonWebSignature();
            jws.setCompactSerialization(result.getText());
            jws.setKey(publicKey);

            JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                    .setVerificationKey(publicKey)
                    .build();

            JwtClaims jwtClaims = jwtConsumer.processToClaims(result.getText());
            launchResult(jwtClaims.getRawJson());
        } catch (Exception ex) {
            launchResult("");
        }
    }


    private void launchDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setPositiveButton(R.string.scanner, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Dialog f = (Dialog) dialog;
                EditText ticketID = (EditText) f.findViewById(R.id.dialog_ticket_id);

                if(TaxmanUtils.userConnected()){
                    GroomApplication.service.getTicket(Integer.valueOf(ticketID.getText().toString())).enqueue(new Callback<Ticket>() {
                        @Override
                        public void onResponse(Call<Ticket> call, Response<Ticket> response) {
                            launchManual(response.body());
                        }

                        @Override
                        public void onFailure(Call<Ticket> call, Throwable t) {
                            Toast.makeText(GroomApplication.getContext(), getString(R.string.service_failure), Toast.LENGTH_SHORT).show();<commit_msg>Error handling for not found ticket IDs<commit_after>package com.hufi.taxmanreader.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.hufi.taxmanreader.GroomApplication;
import com.hufi.taxmanreader.GroomService;
import com.hufi.taxmanreader.R;
import com.hufi.taxmanreader.model.Ticket;
import com.hufi.taxmanreader.utils.GroomScannerView;
import com.hufi.taxmanreader.utils.TaxmanUtils;

import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.jwt.consumer.JwtConsumer;
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
import org.jose4j.lang.JoseException;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;

import me.dm7.barcodescanner.zxing.ZXingScannerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ScannerFragment extends Fragment implements ZXingScannerView.ResultHandler {
    //  private ZXingScannerView mScannerView;

    private GroomScannerView groomScannerView;

    private String cameraIDUsed;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        groomScannerView = new GroomScannerView(getActivity());
        groomScannerView.setAutoFocus(true);
       /* mScannerView = new ZXingScannerView(getActivity());
        mScannerView.setAutoFocus(true);*/

        setupFormats();
        if (!setUpBackCamera()) cameraIDUsed = "0";
        //return mScannerView;
        return groomScannerView;
    }

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setHasOptionsMenu(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        groomScannerView.setResultHandler(this);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.scanner));
        groomScannerView.startCamera(Integer.valueOf(cameraIDUsed));
    }

    @Override
    public void onPause() {
        super.onPause();
        groomScannerView.stopCamera();
    }

    public void setupFormats() {
        List<BarcodeFormat> formats = new ArrayList<BarcodeFormat>();
        formats.add(BarcodeFormat.QR_CODE);

        if (groomScannerView != null) {
            groomScannerView.setFormats(formats);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.turn_flash:
                if (groomScannerView.getFlash()) {
                    groomScannerView.setFlash(false);
                    if (!groomScannerView.getFlash()) item.setIcon(R.drawable.ic_action_flash);
                } else {
                    groomScannerView.setFlash(true);
                    if (groomScannerView.getFlash()) item.setIcon(R.drawable.ic_action_flash_light);
                }
                break;
            case R.id.swap_camera:
                changeCamera();
                groomScannerView.stopCamera();
                groomScannerView.startCamera(Integer.valueOf(cameraIDUsed));
                break;
            case R.id.by_id:
                launchDialog();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void handleResult(Result result) {
        try {
            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decode(getString(R.string.public_key), Base64.DEFAULT));
            KeyFactory keyFactory = KeyFactory.getInstance("ECDSA");
            PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
            JsonWebSignature jws = new JsonWebSignature();
            jws.setCompactSerialization(result.getText());
            jws.setKey(publicKey);

            JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                    .setVerificationKey(publicKey)
                    .build();

            JwtClaims jwtClaims = jwtConsumer.processToClaims(result.getText());
            launchResult(jwtClaims.getRawJson());
        } catch (Exception ex) {
            launchResult("");
        }
    }


    private void launchDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setPositiveButton(R.string.scanner, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Dialog f = (Dialog) dialog;
                EditText ticketID = (EditText) f.findViewById(R.id.dialog_ticket_id);

                if(TaxmanUtils.userConnected()){
                    GroomApplication.service.getTicket(Integer.valueOf(ticketID.getText().toString())).enqueue(new Callback<Ticket>() {
                        @Override
                        public void onResponse(Call<Ticket> call, Response<Ticket> response) {
                            if (response.code() == 200) {
                                launchManual(response.body());
                            }
                            else {
                                if (response.code() == 404) {
                                    Toast.makeText(GroomApplication.getContext(), "Ticket not found", Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    Toast.makeText(GroomApplication.getContext(), "Unexpected status code from server", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<Ticket> call, Throwable t) {
                            Toast.makeText(GroomApplication.getContext(), getString(R.string.service_failure), Toast.LENGTH_SHORT).show();
{'fraction_non_alphanumeric': 0.09461586901763225, 'fraction_numerical': 0.004722921914357683, 'mean_word_length': 2.5598206780610817, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793859', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3465, 'n_words': 589}