mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Add confirm dialog for invitation qr codes
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package org.thoughtcrime.securesms.qr;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.journeyapps.barcodescanner.BarcodeResult;
|
||||
import com.journeyapps.barcodescanner.CaptureManager;
|
||||
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
|
||||
|
||||
public class CustomCaptureManager extends CaptureManager {
|
||||
|
||||
private OnResultInterceptor interceptor;
|
||||
|
||||
public CustomCaptureManager(Activity activity, DecoratedBarcodeView barcodeView) {
|
||||
super(activity, barcodeView);
|
||||
}
|
||||
|
||||
public void setResultInterceptor(OnResultInterceptor interceptor) {
|
||||
this.interceptor = interceptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void returnResult(BarcodeResult rawResult) {
|
||||
if (interceptor != null) {
|
||||
interceptor.onResult(rawResult, () -> {
|
||||
super.returnResult(rawResult);
|
||||
});
|
||||
} else {
|
||||
super.returnResult(rawResult);
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnResultInterceptor {
|
||||
void onResult(BarcodeResult result, Runnable finishCallback);
|
||||
}
|
||||
}
|
||||
@@ -10,8 +10,11 @@ import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.journeyapps.barcodescanner.CaptureManager;
|
||||
import com.b44t.messenger.DcContext;
|
||||
import com.b44t.messenger.DcLot;
|
||||
import com.journeyapps.barcodescanner.CompoundBarcodeView;
|
||||
|
||||
import org.thoughtcrime.securesms.BaseActionBarActivity;
|
||||
@@ -26,10 +29,12 @@ public class RegistrationQrActivity extends BaseActionBarActivity {
|
||||
public static final String ADD_AS_SECOND_DEVICE_EXTRA = "add_as_second_device";
|
||||
public static final String QRDATA_EXTRA = "qrdata";
|
||||
|
||||
private CaptureManager capture;
|
||||
private CustomCaptureManager capture;
|
||||
|
||||
private CompoundBarcodeView barcodeScannerView;
|
||||
|
||||
private DcContext dcContext;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -66,6 +71,8 @@ public class RegistrationQrActivity extends BaseActionBarActivity {
|
||||
.onAnyResult(this::handleQrScanWithPermissions)
|
||||
.onAnyDenied(this::handleQrScanWithDeniedPermission)
|
||||
.execute();
|
||||
|
||||
dcContext = DcHelper.getContext(this);
|
||||
}
|
||||
|
||||
private void handleQrScanWithPermissions() {
|
||||
@@ -89,23 +96,61 @@ public class RegistrationQrActivity extends BaseActionBarActivity {
|
||||
DcHelper.openHelp(this, "#multiclient");
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_paste) {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(QRDATA_EXTRA, Util.getTextFromClipboard(this));
|
||||
setResult(Activity.RESULT_OK, intent);
|
||||
finish();
|
||||
String rawQr = Util.getTextFromClipboard(this);
|
||||
|
||||
Runnable okCallback = () -> {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(QRDATA_EXTRA, rawQr);
|
||||
setResult(Activity.RESULT_OK, intent);
|
||||
finish();
|
||||
};
|
||||
|
||||
showConfirmDialog(rawQr, okCallback);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
private void showConfirmDialog(String rawQr, Runnable okCallback) {
|
||||
DcLot qrParsed = dcContext.checkQr(rawQr);
|
||||
|
||||
String dialogMsg = "";
|
||||
if (qrParsed.getState() == DcContext.DC_QR_ASK_VERIFYCONTACT) {
|
||||
String name = dcContext.getContact(qrParsed.getId()).getDisplayName();
|
||||
dialogMsg = getString(R.string.instant_onboarding_confirm_contact, name);
|
||||
} else if (qrParsed.getState() == DcContext.DC_QR_ASK_VERIFYGROUP) {
|
||||
String groupName = qrParsed.getText1();
|
||||
dialogMsg = getString(R.string.instant_onboarding_confirm_group, groupName);
|
||||
}
|
||||
|
||||
AlertDialog confirmDialog = new AlertDialog.Builder(this)
|
||||
.setMessage(dialogMsg)
|
||||
.setPositiveButton("OK", (dialog, which) -> {
|
||||
okCallback.run();
|
||||
})
|
||||
.setNegativeButton("Cancel", null)
|
||||
.show();
|
||||
|
||||
confirmDialog.getButton(AlertDialog.BUTTON_POSITIVE)
|
||||
.setTextColor(ContextCompat.getColor(this, R.color.core_blue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
||||
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
private void init(CompoundBarcodeView barcodeScannerView, Intent intent, Bundle savedInstanceState) {
|
||||
capture = new CaptureManager(this, barcodeScannerView);
|
||||
capture = new CustomCaptureManager(this, barcodeScannerView);
|
||||
|
||||
capture.setResultInterceptor((result, finishCallback) -> {
|
||||
String rawQr = result.getText();
|
||||
|
||||
showConfirmDialog(rawQr, finishCallback);
|
||||
});
|
||||
|
||||
capture.initializeFromIntent(intent, savedInstanceState);
|
||||
capture.decode();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user