id.co.datascrip.dtswarehousesystem.activity.Form_Scan_Scandid.java Source code

Java tutorial

Introduction

Here is the source code for id.co.datascrip.dtswarehousesystem.activity.Form_Scan_Scandid.java

Source

package id.co.datascrip.dtswarehousesystem.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.scandit.barcodepicker.BarcodePicker;
import com.scandit.barcodepicker.OnScanListener;
import com.scandit.barcodepicker.ScanSession;
import com.scandit.barcodepicker.ScanSettings;
import com.scandit.barcodepicker.ScanditLicense;
import com.scandit.recognition.Barcode;

import java.util.Locale;

import id.co.datascrip.dtswarehousesystem.Function;
import id.co.datascrip.dtswarehousesystem.R;
import id.co.datascrip.dtswarehousesystem.custom.Dialog_Input_Manual;
import id.co.datascrip.dtswarehousesystem.font.ShitTextView;

public class Form_Scan_Scandid extends AppCompatActivity implements OnScanListener {

    private static final String API_KEY = "9ToLgTNKw2is3mEcnf99yF0mRyLLdVzRNWxCw0KCM58";

    private BarcodePicker mPicker;
    private boolean mDeniedCameraAccess = false;
    private final static int CAMERA_PERMISSION_REQUEST = 5;

    public static String no = "";
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        bundle = getIntent().getBundleExtra("bundle");
        initScanner();

        RelativeLayout overlayView = mPicker.getOverlayView();
        final FloatingActionButton floatingActionButton = new FloatingActionButton(this);
        floatingActionButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_create_white_18dp));

        ShitTextView tx_title = new ShitTextView(this);
        tx_title.setText(bundle.getString("TitleScan"));
        ShitTextView tx_input_manual = new ShitTextView(this);
        tx_input_manual.setText(bundle.getString("TitleInput").trim().replace(":", ""));

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog_Input_Manual dialog = new Dialog_Input_Manual(Form_Scan_Scandid.this, bundle);
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (!Function.isEmpty(no)) {
                            Intent intent = new Intent();
                            intent.putExtra(getString(R.string.intent_result_scan), no);
                            setResult(RESULT_OK, intent);
                            no = "";
                            finish();
                        }
                    }
                });
            }
        });

        RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(
                AppBarLayout.LayoutParams.WRAP_CONTENT, AppBarLayout.LayoutParams.WRAP_CONTENT);
        rParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rParams.topMargin = 60;
        rParams.rightMargin = 20;

        RelativeLayout.LayoutParams infoParam = new RelativeLayout.LayoutParams(
                AppBarLayout.LayoutParams.WRAP_CONTENT, AppBarLayout.LayoutParams.WRAP_CONTENT);
        infoParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        infoParam.topMargin = 20;
        infoParam.rightMargin = 10;
        tx_input_manual.setTextColor(ContextCompat.getColor(this, R.color.GREEN));
        tx_title.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Felbridge-Light.otf"));

        RelativeLayout.LayoutParams titleParam = new RelativeLayout.LayoutParams(
                AppBarLayout.LayoutParams.MATCH_PARENT, AppBarLayout.LayoutParams.WRAP_CONTENT);
        titleParam.topMargin = 300;
        titleParam.leftMargin = 50;
        tx_title.setTextSize(24);
        tx_title.setTextColor(ContextCompat.getColor(this, R.color.GREEN));
        tx_title.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Felbridge-Bold.otf"));

        overlayView.addView(floatingActionButton, rParams);
        overlayView.addView(tx_input_manual, infoParam);
        overlayView.addView(tx_title, titleParam);
    }

    private ScanSettings getScanSetting() {
        ScanSettings settings = ScanSettings.create();
        if (bundle == null) {
            for (int simbology : Barcode.ALL_SYMBOLOGIES)
                settings.setSymbologyEnabled(simbology, true);
        } else {
            if (bundle.getString("TitleScan").toLowerCase().contains("stbu")
                    || bundle.getString("TitleScan").toLowerCase().contains("do"))
                settings.setSymbologyEnabled(Barcode.SYMBOLOGY_CODE128, true);
            else {
                for (int simbology : Barcode.ALL_SYMBOLOGIES)
                    settings.setSymbologyEnabled(simbology, true);
            }
        }
        return settings;
    }

    private void initScanner() {
        // Set your app key
        ScanditLicense.setAppKey(API_KEY);

        boolean emulatePortraitMode = !BarcodePicker.canRunPortraitPicker();
        if (emulatePortraitMode) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        mPicker = new BarcodePicker(this, getScanSetting());

        // Set the on scan listener to receive barcode scan events.
        mPicker.setOnScanListener(this);
        mPicker.getOverlayView().setTorchEnabled(true);
        mPicker.switchTorchOn(true);
        setContentView(mPicker);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (requestCode == CAMERA_PERMISSION_REQUEST) {
            mDeniedCameraAccess = !(grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED);
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void didScan(ScanSession scanSession) {
        for (Barcode code : scanSession.getNewlyRecognizedCodes()) {
            String data = code.getData();
            String tipe = code.getSymbologyName().toUpperCase(Locale.US);
            Toast.makeText(this, "Data : " + data + "; Tipe : " + tipe, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.putExtra(getString(R.string.intent_result_scan), data);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void grantCameraPermissions() {
        if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            if (!mDeniedCameraAccess) {
                // it's pretty clear for why the camera is required. We don't need to give a detailed  reason.
                this.requestPermissions(new String[] { Manifest.permission.CAMERA }, CAMERA_PERMISSION_REQUEST);
            }
        } else {
            mPicker.startScanning();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // note: onResume will be called repeatedly if camera access is not
        // granted.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            grantCameraPermissions();
        } else {
            mPicker.startScanning();
        }
    }

    @Override
    protected void onPause() {
        mPicker.stopScanning();
        super.onPause();
    }
}