Test CustomScannerContract

This commit is contained in:
Jacques Foucry 2022-02-05 19:16:18 +01:00
parent e33297def4
commit d82c7a4e59
2 changed files with 32 additions and 4 deletions

View file

@ -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<ScanOptions, ScanIntentResult> {
@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)
}
}

View file

@ -205,7 +205,7 @@ public class DrugListActivity extends AppCompatActivity {
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
Log.d(TAG, "onPause really!"); Log.d(TAG, "onPause");
AlarmReceiver.scheduleAlarm(this); AlarmReceiver.scheduleAlarm(this);
} }
@ -216,8 +216,15 @@ public class DrugListActivity extends AppCompatActivity {
/** Register the launcher and result handler /** Register the launcher and result handler
* ActivityResultLauncher * ActivityResultLauncher
*/ */
private final ActivityResultLauncher<ScanOptions> barcodeLauncher = registerForActivityResult(new ScanContract(), private final ActivityResultLauncher<ScanOptions, ScanIntentResult> barcodeLauncher = registerForActivityResult(new CustomScanContract.StartActivityForResult(),
result -> { new ActivityResultCallback<ActivityResult>() {
@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) { if(result.getContents() == null) {
Intent originalIntent = result.getOriginalIntent(); Intent originalIntent = result.getOriginalIntent();
if (originalIntent == null) { if (originalIntent == null) {
@ -237,7 +244,7 @@ public class DrugListActivity extends AppCompatActivity {
Log.d(TAG, "Scanned"); Log.d(TAG, "Scanned");
Toast.makeText(DrugListActivity.this, "Scanned: " + result.getContents(), Toast.makeText(DrugListActivity.this, "Scanned: " + result.getContents(),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} }*/
}); });
// Launch scan // Launch scan