<commit_before>package com.example.scan.scan;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.util.SparseArray;
import android.widget.ImageView;
import android.content.Context;
import android.widget.Toast;

import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.Frame.Builder;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;

public class homepage extends AppCompatActivity {
    private static final int
            REQUEST_IMAGE_CAPTURE = 1, PICK_IMAGE = 100;
    public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;
    private static final String LOG_TAG = "testOCR.java";
    private static final String TAG = "testOCR.java";

    public String textFileName = "";

    Button btnscan;
    Button btnPicLib;
    Button btnDocList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        Intent intent = getIntent();
        String s = intent.getStringExtra("username");
        s = s.trim();

        if (s.contains(" "))
            s = s.substring(0, s.indexOf(" ")); // trim to first name

        s = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
        TextView user = (TextView) findViewById(R.id.showuser);
        user.setText("Hello, " + s + "!");

        // Scan button invokes Camera
        btnscan = (Button) findViewById(R.id.scanbutton);
        btnscan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
            }
        });

        btnDocList = (Button) findViewById(R.id.doclibrarybutton);
        btnDocList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startListView = new Intent(homepage.this, ListFilesActivity.class);
                startActivity(startListView);
            }
        });

        // Photo Library button helps select a photo from Picture Library and use it
        btnPicLib = (Button) findViewById(R.id.photolibrarybutton);
        btnPicLib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openGallery();
            }
        });
    }

    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (BuildConfig.DEBUG && resultCode != RESULT_OK)
            throw new RuntimeException("Result NOT ok for some reason");

        Bitmap imageBitmap = null;

        // IF PHOTO IS TAKEN USING CAMERA
        if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) {
            //Get our saved file into a bitmap object:
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            imageBitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
        }
        // IF PICTURE IS TAKEN FROM PHOTO LIBRARY
        if (requestCode == PICK_IMAGE) {
            Uri imageUri = data.getData();

            //PRIMARY PHOTO LIBRARY METHOD
            try {
                imageBitmap = decodeUri(imageUri);
            } catch (FileNotFoundException e) { // this should never happen
                e.printStackTrace();
                return;
            }

            //BACKUP METHOD
            /**
             try {
             imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
             } catch (IOException e) {
             e.printStackTrace();
             }
             */
        }

        if (BuildConfig.DEBUG && imageBitmap == null)
            throw new RuntimeException("how is this null?");

        // PREPROCESSING (has been removed temporarily due to dysfunctionality.
        // imageBitmap = PreProcessing.doStuff(imageBitmap);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Type text file name:");

        // Set up the input
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        // Set up the buttons
        final Bitmap finalImageBitmap = imageBitmap;
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // OCR stuff
                // move this outside if you uncomment the textview
                String answer = "";
                Context context = getApplicationContext();
                TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
                Frame frame = new Frame.Builder().setBitmap(finalImageBitmap).build();
                SparseArray<TextBlock> textBlocks = ocrFrame.detect(frame);
                for (int i = 0; i < textBlocks.size(); i++) // probably should use stringbuilder
                    answer += textBlocks.get(textBlocks.keyAt(i)).getValue();

                // save plz
                textFileName = input.getText().toString();
                if (textFileName.equals("")) {
                    // random values
                    textFileName = "sCan-" + (int) (Math.random() * 100000);
                }

                try {
                    OutputStreamWriter out = new OutputStreamWriter(openFileOutput(textFileName + ".txt", 0));
                    out.write(answer);
                    out.close();
                    Toast.makeText(getApplicationContext(), "Saved text to " + textFileName + ".txt", Toast.LENGTH_SHORT).show();
                } catch (Throwable t) {
                    Toast.makeText(getApplicationContext(), "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
                    if (BuildConfig.DEBUG)
                        t.printStackTrace();
                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();

        // OCR
        // uncomment this code to just show the text without saving it
        /*if (false) {Intent intent = new Intent(this, DisplayText.class);
        intent.putExtra("text", answer);
        startActivity(intent);}*/

        // uncomment this code to just show what was captured
        /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);<commit_msg>fix crash bug if back button pressed<commit_after>package com.example.scan.scan;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.util.SparseArray;
import android.widget.ImageView;
import android.content.Context;
import android.widget.Toast;

import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.Frame.Builder;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;

public class homepage extends AppCompatActivity {
    private static final int
            PICK_IMAGE = 100, CAPTURE_IMAGE = 1777;

    public String textFileName = "";

    Button btnscan;
    Button btnPicLib;
    Button btnDocList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        Intent intent = getIntent();
        String s = intent.getStringExtra("username");
        s = s.trim();

        if (s.contains(" "))
            s = s.substring(0, s.indexOf(" ")); // trim to first name

        s = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
        TextView user = (TextView) findViewById(R.id.showuser);
        user.setText("Hello, " + s + "!");

        // Scan button invokes Camera
        btnscan = (Button) findViewById(R.id.scanbutton);
        btnscan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

        btnDocList = (Button) findViewById(R.id.doclibrarybutton);
        btnDocList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startListView = new Intent(homepage.this, ListFilesActivity.class);
                startActivity(startListView);
            }
        });

        // Photo Library button helps select a photo from Picture Library and use it
        btnPicLib = (Button) findViewById(R.id.photolibrarybutton);
        btnPicLib.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openGallery();
            }
        });
    }

    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            if (BuildConfig.DEBUG)
                Toast.makeText(getApplicationContext(), "Canceled.", Toast.LENGTH_SHORT).show();
            return;
        }

        Bitmap imageBitmap = null;

        // IF PHOTO IS TAKEN USING CAMERA
        if (requestCode == CAPTURE_IMAGE) {
            //Get our saved file into a bitmap object:
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            imageBitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
        }
        // IF PICTURE IS TAKEN FROM PHOTO LIBRARY
        if (requestCode == PICK_IMAGE) {
            Uri imageUri = data.getData();

            // primary method
            try {
                imageBitmap = decodeUri(imageUri);
            } catch (FileNotFoundException e) { // this should never happen
                e.printStackTrace();
                return;
            }

            //BACKUP METHOD
            /*try {
                imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }

        if (BuildConfig.DEBUG && imageBitmap == null)
            throw new RuntimeException("how is this null?");

        // PREPROCESSING (has been removed temporarily due to dysfunctionality.
        // imageBitmap = PreProcessing.doStuff(imageBitmap);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Type text file name:");

        // Set up the input
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        // Set up the buttons
        final Bitmap finalImageBitmap = imageBitmap;
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // OCR stuff
                // move this outside if you uncomment the textview
                String answer = "";
                Context context = getApplicationContext();
                TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build();
                Frame frame = new Frame.Builder().setBitmap(finalImageBitmap).build();
                SparseArray<TextBlock> textBlocks = ocrFrame.detect(frame);
                for (int i = 0; i < textBlocks.size(); i++) // probably should use stringbuilder
                    answer += textBlocks.get(textBlocks.keyAt(i)).getValue();

                // save plz
                textFileName = input.getText().toString();
                if (textFileName.equals("")) {
                    // random values
                    textFileName = "sCan-" + (int) (Math.random() * 100000);
                }

                try {
                    OutputStreamWriter out = new OutputStreamWriter(openFileOutput(textFileName + ".txt", 0));
                    out.write(answer);
                    out.close();
                    Toast.makeText(getApplicationContext(), "Saved text to " + textFileName + ".txt", Toast.LENGTH_SHORT).show();
                } catch (Throwable t) {
                    Toast.makeText(getApplicationContext(), "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
                    if (BuildConfig.DEBUG)
                        throw new RuntimeException(t);
                }
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();

        // OCR
        // uncomment this code to just show the text without saving it
        /*if (false) {Intent intent = new Intent(this, DisplayText.class);
        intent.putExtra("text", answer);
        startActivity(intent);}*/

        // uncomment this code to just show what was captured
        /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
{'fraction_non_alphanumeric': 0.10009296560272699, 'fraction_numerical': 0.0039045553145336228, 'mean_word_length': 2.2984464431725264, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6125753', 'n_tokens_mistral': 4295, 'n_tokens_neox': 4001, 'n_words': 984}