--- /dev/null
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.ui;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+
+import androidx.activity.result.ActivityResultCaller;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContract;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+public class QR {
+ private static final String SCAN_APP_NAME = "com.google.zxing.client.android.SCAN";
+ public static ActivityResultLauncher<Void> registerLauncher(ActivityResultCaller activity,
+ QRScanResultReceiver resultReceiver) {
+ return activity.registerForActivityResult(new ActivityResultContract<Void, String>() {
+ @NonNull
+ @Override
+ public Intent createIntent(@NonNull Context context, Void input) {
+ final Intent intent = new Intent(SCAN_APP_NAME);
+ intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
+ return intent;
+ }
+ @Override
+ public String parseResult(int resultCode, @Nullable Intent intent) {
+ if (resultCode == Activity.RESULT_CANCELED || intent == null)
+ return null;
+ return intent.getStringExtra("SCAN_RESULT");
+ }
+ }, resultReceiver::onQRScanResult);
+ }
+ public interface QRScanResultReceiver {
+ void onQRScanResult(String scanned);
+ }
+}
--- /dev/null
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.ui;
+
+import android.content.Context;
+
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+import androidx.lifecycle.MutableLiveData;
+
+public abstract class QRScanAbleFragment extends Fragment {
+ private static final MutableLiveData<Integer> qrScanTrigger = new MutableLiveData<>();
+ protected final ActivityResultLauncher<Void> scanQrLauncher = QR.registerLauncher(this, this::onQrScanned);
+ public static void triggerQRScan() {
+ qrScanTrigger.setValue(1);
+ }
+ protected abstract void onQrScanned(String text);
+ @Override
+ public void onAttach(@NonNull Context context) {
+ super.onAttach(context);
+ qrScanTrigger.observe(this, ignored -> {
+ scanQrLauncher.launch(null);
+ });
+ }
+}
package net.ktnx.mobileledger.ui.new_transaction;
-import android.app.Activity;
import android.content.Context;
-import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.renderscript.RSInvalidStateException;
import android.view.ViewGroup;
import android.widget.ProgressBar;
-import androidx.activity.result.ActivityResultLauncher;
-import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import net.ktnx.mobileledger.model.LedgerTransaction;
import net.ktnx.mobileledger.model.LedgerTransactionAccount;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.QRScanAbleFragment;
import net.ktnx.mobileledger.utils.Logger;
import net.ktnx.mobileledger.utils.Misc;
import net.ktnx.mobileledger.utils.SimpleDate;
// TODO: offer to undo account remove-on-swipe
-public class NewTransactionFragment extends Fragment {
+public class NewTransactionFragment extends QRScanAbleFragment {
private NewTransactionItemsAdapter listAdapter;
private NewTransactionModel viewModel;
- final ActivityResultLauncher<Void> scanQrLauncher =
- registerForActivityResult(new ActivityResultContract<Void, String>() {
- @NonNull
- @Override
- public Intent createIntent(@NonNull Context context, Void input) {
- final Intent intent = new Intent("com.google.zxing.client.android.SCAN");
- intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
- return intent;
- }
- @Override
- public String parseResult(int resultCode, @Nullable Intent intent) {
- if (resultCode == Activity.RESULT_CANCELED)
- return null;
- return intent.getStringExtra("SCAN_RESULT");
- }
- }, this::onQrScanned);
private FloatingActionButton fab;
private OnNewTransactionFragmentInteractionListener mListener;
private MobileLedgerProfile mProfile;
// Required empty public constructor
setHasOptionsMenu(true);
}
- private void onQrScanned(String text) {
+ protected void onQrScanned(String text) {
Logger.debug("qr", String.format("Got QR scan result [%s]", text));
Pattern p =
Pattern.compile("^(\\d+)\\*(\\d+)\\*(\\d+)-(\\d+)-(\\d+)\\*([:\\d]+)\\*([\\d.]+)$");