From d82c7a4e5949b2d465bf071dd87de1e220f8a5ad Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Sat, 5 Feb 2022 19:16:18 +0100 Subject: [PATCH] Test CustomScannerContract --- .../foucry/pilldroid/CustomScanContract.java | 21 +++++++++++++++++++ .../foucry/pilldroid/DrugListActivity.java | 15 +++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/net/foucry/pilldroid/CustomScanContract.java diff --git a/app/src/main/java/net/foucry/pilldroid/CustomScanContract.java b/app/src/main/java/net/foucry/pilldroid/CustomScanContract.java new file mode 100644 index 0000000..1b58d13 --- /dev/null +++ b/app/src/main/java/net/foucry/pilldroid/CustomScanContract.java @@ -0,0 +1,21 @@ +package net.foucry.pilldroid; + +import android.content.Context; +import android.content.Intent; + +import androidx.activity.result.contract.ActivityResultContract; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +public class CustomScanContract extends ActivityResultContract { + @NonNull + @Override + public Intent createIntent(@NonNull Context context, ScanOptions input { + return input createScanIntent(context); + } + + @Override + public ScanIntentResult parseResult(int resultCode, @Nullable Intent intent) { + return ScanIntentResult.parseActivityResult(resultCode, intent) + } +} diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index 81ddada..315e7b1 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -205,7 +205,7 @@ public class DrugListActivity extends AppCompatActivity { public void onPause() { super.onPause(); - Log.d(TAG, "onPause really!"); + Log.d(TAG, "onPause"); AlarmReceiver.scheduleAlarm(this); } @@ -216,8 +216,15 @@ public class DrugListActivity extends AppCompatActivity { /** Register the launcher and result handler * ActivityResultLauncher */ - private final ActivityResultLauncher barcodeLauncher = registerForActivityResult(new ScanContract(), - result -> { + private final ActivityResultLauncher barcodeLauncher = registerForActivityResult(new CustomScanContract.StartActivityForResult(), + new ActivityResultCallback() { + @Override + public void onActivityResult(ActivityResult result) { + if(result.getResultCode() = Activity.RESULT_OK) { + Intent data = result.getData(); + Log.d(TAG, "return data ==" + data); + //doSomeOperations(); + /*result -> { if(result.getContents() == null) { Intent originalIntent = result.getOriginalIntent(); if (originalIntent == null) { @@ -237,7 +244,7 @@ public class DrugListActivity extends AppCompatActivity { Log.d(TAG, "Scanned"); Toast.makeText(DrugListActivity.this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show(); - } + }*/ }); // Launch scan