// Path: ply-android-common/src/main/java/com/productlayer/android/common/util/GTINUtil.java
// public class GTINUtil {
// 
//     /**
//      * Extracts a GTIN from a GS1-128 (Code 128) formatted bar code.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted GTIN or null if none found
//      */
//     public static String extractFromCode128(String code) {
//         if (code.length() < 16) {
//             return null;
//         }
//         if (code.startsWith("01") || code.startsWith("02")) {
//             return code.substring(2, 16);
//         }
//         return null;
//     }
// 
//     /**
//      * Extracts a GTIN from a GS1 DataMatrix formatted bar code.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted GTIN or null if none found
//      */
//     public static String extractFromDataMatrix(String code) {
//         if (code.length() < 16) {
//             return null;
//         }
//         if (code.startsWith("01")) {
//             return code.substring(2, 16);
//         }
//         return null;
//     }
// 
//     /**
//      * Converts a UPC-E code to UPC-A.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted UPC-A code or null on any error
//      */
//     public static String extractFromUPCE(String code) {
//         int len = code.length();
//         String trimmed;
//         if (len == 6) {
//             trimmed = code;
//         } else if (len == 7) {
//             trimmed = code.substring(0, len - 1);
//         } else if (len == 8) {
//             trimmed = code.substring(1, len - 1);
//         } else {
//             return null;
//         }
//         char c1 = trimmed.charAt(0);
//         char c2 = trimmed.charAt(1);
//         char c3 = trimmed.charAt(2);
//         char c4 = trimmed.charAt(3);
//         char c5 = trimmed.charAt(4);
//         char c6 = trimmed.charAt(5);
//         String manufacturer;
//         String item;
//         switch (c6) {
//             case '0':
//             case '1':
//             case '2':
//                 manufacturer = c1 + c2 + c6 + "00";
//                 item = "00" + c3 + c4 + c5;
//                 break;
//             case '3':
//                 manufacturer = c1 + c2 + c3 + "00";
//                 item = "000" + c4 + c5;
//                 break;
//             case '4':
//                 manufacturer = c1 + c2 + c3 + c4 + "0";
//                 item = "0000" + c5;
//                 break;
//             default:
//                 manufacturer = c1 + c2 + c3 + c4 + c5 + "";
//                 item = "0000" + c6;
//         }
//         String newCode = "0" + manufacturer + item;
//         return newCode + GTINValidator.calcChecksum(newCode.toCharArray(), newCode.length());
//     }
// 
// }


import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.productlayer.android.common.R;
import com.productlayer.android.common.util.GTINUtil;
import com.productlayer.core.error.PLYHttpException;
import com.productlayer.core.logic.ProductLogic;
import com.productlayer.core.utils.GTINValidator;
import java.util.Arrays;
import java.util.List;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
        super.onCreate(state);
        scannerView = new ZXingScannerView(this);
        scannerView.setFormats(supportedFormats);
        scannerView.setAutoFocus(true);
        setContentView(scannerView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        scannerView.setResultHandler(this);
        scannerView.startCamera();
        //scannerView.setFlash(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        scannerView.stopCamera();
    }

    // ACTIVITY LIFECYCLE - END //

    @Override
    public void handleResult(Result result) {
        BarcodeFormat format = result.getBarcodeFormat();
        String barcode = result.getText();
        Log.i(getClass().getSimpleName(), "Scanned barcode from " + format + " with raw value " + barcode);
        String gtin;
        if (format == BarcodeFormat.CODE_128) {
// Path: ply-android-common/src/main/java/com/productlayer/android/common/util/GTINUtil.java
// public class GTINUtil {
// 
//     /**
//      * Extracts a GTIN from a GS1-128 (Code 128) formatted bar code.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted GTIN or null if none found
//      */
//     public static String extractFromCode128(String code) {
//         if (code.length() < 16) {
//             return null;
//         }
//         if (code.startsWith("01") || code.startsWith("02")) {
//             return code.substring(2, 16);
//         }
//         return null;
//     }
// 
//     /**
//      * Extracts a GTIN from a GS1 DataMatrix formatted bar code.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted GTIN or null if none found
//      */
//     public static String extractFromDataMatrix(String code) {
//         if (code.length() < 16) {
//             return null;
//         }
//         if (code.startsWith("01")) {
//             return code.substring(2, 16);
//         }
//         return null;
//     }
// 
//     /**
//      * Converts a UPC-E code to UPC-A.
//      *
//      * @param code
//      *         the raw bar code value
//      * @return the extracted UPC-A code or null on any error
//      */
//     public static String extractFromUPCE(String code) {
//         int len = code.length();
//         String trimmed;
//         if (len == 6) {
//             trimmed = code;
//         } else if (len == 7) {
//             trimmed = code.substring(0, len - 1);
//         } else if (len == 8) {
//             trimmed = code.substring(1, len - 1);
//         } else {
//             return null;
//         }
//         char c1 = trimmed.charAt(0);
//         char c2 = trimmed.charAt(1);
//         char c3 = trimmed.charAt(2);
//         char c4 = trimmed.charAt(3);
//         char c5 = trimmed.charAt(4);
//         char c6 = trimmed.charAt(5);
//         String manufacturer;
//         String item;
//         switch (c6) {
//             case '0':
//             case '1':
//             case '2':
//                 manufacturer = c1 + c2 + c6 + "00";
//                 item = "00" + c3 + c4 + c5;
//                 break;
//             case '3':
//                 manufacturer = c1 + c2 + c3 + "00";
//                 item = "000" + c4 + c5;
//                 break;
//             case '4':
//                 manufacturer = c1 + c2 + c3 + c4 + "0";
//                 item = "0000" + c5;
//                 break;
//             default:
//                 manufacturer = c1 + c2 + c3 + c4 + c5 + "";
//                 item = "0000" + c6;
//         }
//         String newCode = "0" + manufacturer + item;
//         return newCode + GTINValidator.calcChecksum(newCode.toCharArray(), newCode.length());
//     }
// 
// }


// Path: ply-android-common/src/main/java/com/productlayer/android/common/activity/ScannerActivity.java
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.productlayer.android.common.R;
import com.productlayer.android.common.util.GTINUtil;
import com.productlayer.core.error.PLYHttpException;
import com.productlayer.core.logic.ProductLogic;
import com.productlayer.core.utils.GTINValidator;
import java.util.Arrays;
import java.util.List;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

        super.onCreate(state);
        scannerView = new ZXingScannerView(this);
        scannerView.setFormats(supportedFormats);
        scannerView.setAutoFocus(true);
        setContentView(scannerView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        scannerView.setResultHandler(this);
        scannerView.startCamera();
        //scannerView.setFlash(true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        scannerView.stopCamera();
    }

    // ACTIVITY LIFECYCLE - END //

    @Override
    public void handleResult(Result result) {
        BarcodeFormat format = result.getBarcodeFormat();
        String barcode = result.getText();
        Log.i(getClass().getSimpleName(), "Scanned barcode from " + format + " with raw value " + barcode);
        String gtin;
        if (format == BarcodeFormat.CODE_128) {
            gtin = GTINUtil.extractFromCode128(barcode);