Change zxing call method using contract

Clean imports
This commit is contained in:
jacques 2022-01-30 22:20:31 +01:00
parent 062d3f960b
commit 5cc0483487

View file

@ -21,15 +21,15 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.google.zxing.client.android.Intents; import com.journeyapps.barcodescanner.ScanContract;
import com.google.zxing.integration.android.IntentIntegrator; import com.journeyapps.barcodescanner.ScanOptions;
import com.google.zxing.integration.android.IntentResult;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -212,16 +212,24 @@ public class DrugListActivity extends AppCompatActivity {
super.onResume(); super.onResume();
} }
/** scanNow /** Register the launcher and result handler
* * ActivityResultLauncher
* @param view
* call ZXing Library to scan a new QR/EAN code
*/ */
public void scanNow(View view) { private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(),
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan(); result -> {
if(result.getContents() == null) {
Toast.makeText(DrugListActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(DrugListActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});
// Launch scan
public void onButtonClick(View view) {
barcodeLauncher.launch(new ScanOptions());
} }
@Override /* @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) { if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) {
// This is important, otherwise the result will not be passed to the fragment // This is important, otherwise the result will not be passed to the fragment
@ -295,7 +303,7 @@ public class DrugListActivity extends AppCompatActivity {
askToAddInDB(scannedDrug); askToAddInDB(scannedDrug);
} }
} }
} }*/
/** /**
* show keyboardInput dialog * show keyboardInput dialog