Skip to content

Commit

Permalink
Show CAN info message
Browse files Browse the repository at this point in the history
  • Loading branch information
martenrebane committed Oct 8, 2024
1 parent 4f2e302 commit 5f1ee4d
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -131,6 +129,17 @@ public void setCan(String can) {
Timber.log(Log.ERROR, "Unable to save CAN");
}

public void setShowCanMessage(boolean showCanMessage) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(resources.getString(R.string.nfc_show_can_message), showCanMessage);
editor.apply();
}

public boolean getShowCanMessage() {
return preferences.getBoolean(resources.getString(R.string.nfc_show_can_message),
true);
}

public boolean getIsRoleAskingEnabled() {
return preferences.getBoolean(resources.getString(R.string.main_settings_ask_role_and_address_key),
false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import java.nio.charset.StandardCharsets;

import ee.ria.DigiDoc.R;
import ee.ria.DigiDoc.android.Activity;
import ee.ria.DigiDoc.android.ApplicationApp;
import ee.ria.DigiDoc.android.Constants;
import ee.ria.DigiDoc.android.accessibility.AccessibilityUtils;
import ee.ria.DigiDoc.android.signature.update.SignatureAddView;
import ee.ria.DigiDoc.android.signature.update.SignatureUpdateViewModel;
import ee.ria.DigiDoc.android.utils.ErrorMessageUtil;
import ee.ria.DigiDoc.android.utils.navigator.Navigator;
import ee.ria.DigiDoc.android.utils.widget.NotificationDialog;
import ee.ria.DigiDoc.common.PinConstants;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.PublishSubject;
Expand All @@ -59,6 +61,8 @@ public class NFCView extends LinearLayout implements SignatureAddView<NFCRequest
private final TextInputLayout pinLayout;
private final MaterialTextView pinLabel;

private final NotificationDialog nfcCanNotificationDialog;

private AccessibilityManager.TouchExplorationStateChangeListener accessibilityTouchExplorationStateChangeListener;

public NFCView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Expand All @@ -78,6 +82,9 @@ public NFCView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
pinLayout = findViewById(R.id.signatureUpdateNFCPIN2Layout);
pinLabel = findViewById(R.id.signatureUpdateNFCPIN2Label);

nfcCanNotificationDialog = new NotificationDialog(navigator.activity(),
R.string.signature_update_nfc_can_info, R.id.nfcCanNotificationDialog);

handleNFCSupportLayout();

if (AccessibilityUtils.isTalkBackEnabled()) {
Expand Down Expand Up @@ -113,6 +120,25 @@ public boolean positiveButtonEnabled() {
return canText != null && isCANLengthValid(canText.toString()) && pinText != null && isPinLengthValid(pinText.toString());
}

@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);

if (navigator.activity() instanceof Activity) {
boolean shouldShowCanMessage = ((Activity) navigator.activity())
.getSettingsDataStore()
.getShowCanMessage();

if (shouldShowCanMessage && visibility == VISIBLE && isNFCSupported()) {
postDelayed(nfcCanNotificationDialog::show, 1000);
} else {
nfcCanNotificationDialog.dismiss();
}
}

handleNFCSupportLayout();
}

@Override
public void reset(SignatureUpdateViewModel viewModel) {
canView.setText(viewModel.can());
Expand All @@ -124,6 +150,7 @@ public void reset(SignatureUpdateViewModel viewModel) {
message.clearFocus();
canView.clearFocus();
pinView.clearFocus();
nfcCanNotificationDialog.dismiss();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import ee.ria.DigiDoc.android.utils.navigator.ContentView;
import ee.ria.DigiDoc.android.utils.rxbinding.app.RxDialog;
import ee.ria.DigiDoc.sign.utils.UrlMessage;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.PublishSubject;
import io.reactivex.rxjava3.subjects.Subject;
Expand Down Expand Up @@ -48,7 +47,7 @@ public ConfirmationDialog(@NonNull Context context, @StringRes int message, int
setMessage(Html.fromHtml(UrlMessage.withURLAndQuestion(
getContext(),
message,
R.string.main_diagnostics_restart_message_read_more,
R.string.read_more_message,
R.string.main_diagnostics_restart_message_restart_now
), Html.FROM_HTML_MODE_LEGACY));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package ee.ria.DigiDoc.android.utils.widget;

import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import ee.ria.DigiDoc.R;
import ee.ria.DigiDoc.android.Activity;
import ee.ria.DigiDoc.android.accessibility.AccessibilityUtils;
import ee.ria.DigiDoc.android.utils.SecureUtil;
import ee.ria.DigiDoc.android.utils.navigator.ContentView;
import ee.ria.DigiDoc.sign.utils.UrlMessage;

public final class NotificationDialog extends AlertDialog implements ContentView,
DialogInterface.OnClickListener {

private final Context context;
private final int action;
private CheckBox dontShowAgainCheckbox;

public NotificationDialog(@NonNull Context context, @StringRes int message, int action) {
super(context);
SecureUtil.markAsSecure(context, getWindow());

this.context = context;

if (action == R.id.nfcCanNotificationDialog) {
int layoutPadding = 50;
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(layoutPadding, layoutPadding, layoutPadding, layoutPadding);

Spanned urlMessage = Html.fromHtml(UrlMessage.withURL(
context,
message,
R.string.read_more_message
), Html.FROM_HTML_MODE_LEGACY);

TextView messageView = new TextView(context);
messageView.setTextAppearance(R.style.MaterialTypography_Dense_Body1);
messageView.setLayoutParams(new ViewGroup.LayoutParams(
CoordinatorLayout.LayoutParams.WRAP_CONTENT,
CoordinatorLayout.LayoutParams.WRAP_CONTENT)
);
messageView.setText(urlMessage);
messageView.setPadding(15, 0, 15, 25);
messageView.setGravity(Gravity.CENTER);

messageView.setMovementMethod(LinkMovementMethod.getInstance());
messageView.setClickable(true);

messageView.setContentDescription(urlMessage);

if (AccessibilityUtils.isTalkBackEnabled()) {
messageView.setOnClickListener(view -> {
String messageWithUrl = context.getString(R.string.signature_update_nfc_can_info);
String url = UrlMessage.extractLink(messageWithUrl);
if (!url.isEmpty()) {
Intent uriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(context, uriIntent, null);
}
});
} else {
messageView.setOnClickListener(null);
}

layout.addView(messageView);

dontShowAgainCheckbox = new AppCompatCheckBox(context);
dontShowAgainCheckbox.setId(android.R.id.checkbox);
dontShowAgainCheckbox.setText(R.string.dont_show_again_message);
layout.addView(dontShowAgainCheckbox);

Button centerButton = new Button(context);
centerButton.setText(context.getString(android.R.string.ok));
centerButton.setTextColor(Color.WHITE);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);

buttonLayoutParams.setMargins(15, 50, 15, 25);
buttonLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
centerButton.setLayoutParams(buttonLayoutParams);

centerButton.setOnClickListener(v -> onClick(this, DialogInterface.BUTTON_POSITIVE));

layout.addView(centerButton);

setView(layout);
}
else {
setMessage(context.getString(message));
setButton(BUTTON_POSITIVE, context.getString(android.R.string.ok), this);
}

this.action = action;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (action == R.id.nfcCanNotificationDialog) {
if (context instanceof Activity) {
Activity activity = (Activity) context;

boolean isChecked = dontShowAgainCheckbox.isChecked();
activity.getSettingsDataStore().setShowCanMessage(!isChecked);
}
}
dismiss();
}

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
String confirmationDialogDescription = getContext().getResources().getString(R.string.confirmation_dialog);
event.getText().add(confirmationDialogDescription + ",");
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
}
5 changes: 4 additions & 1 deletion app/src/main/res/values-et/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@
<string name="main_diagnostics_logging_switch">Aktiveeri ühekordne logifaili genereerimine</string>
<string name="main_diagnostics_save_log">Salvesta logifail</string>
<string name="main_diagnostics_restart_message">Logimise aktiveerimiseks taaskäivita RIA DigiDoc. https://www.id.ee/artikkel/logifaili-genereerimine-digidoc4-kliendis/</string>
<string name="main_diagnostics_restart_message_read_more">Loe täpsemalt siit</string>
<string name="main_diagnostics_restart_message_restart_now">Taaskäivita rakendus?</string>

<string name="read_more_message">Loe täpsemalt siit</string>
<string name="dont_show_again_message">Ära rohkem näita</string>

<!-- Signature Home -->
<string name="signature_home_create_text">Allkirjastamiseks või kontrollimiseks vali fail</string>
<string name="signature_home_create_text_accessibility">Vali fail allkirjastamiseks või kontrollimiseks</string>
Expand Down Expand Up @@ -310,6 +312,7 @@
<string name="signature_update_nfc_technical_error">Midagi läks valesti. Palun proovi uuesti</string>
<string name="signature_update_nfc_hold">Hoia ID-kaarti telefoni lähedal</string>
<string name="signature_update_nfc_detected">ID-kaardiga kontaktivaba ühenduse loomine</string>
<string name="signature_update_nfc_can_info">NFC allkirjastamisel kasutatakse CAN numbrit. Mitmekordsel CANi valesti sisestamisel võib NFC lukustuda. https://www.id.ee/artikkel/digiallkirjastamine-ria-digidoc-mobiilirakenduses/</string>

<!-- Signature Details -->
<string name="signature_error_details_title">Hoiatused</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@
<string name="main_diagnostics_logging_switch">Активировать одноразовое создание лог-файла</string>
<string name="main_diagnostics_save_log">Сохранить лог-файл</string>
<string name="main_diagnostics_restart_message">Перезапустите RIA DigiDoc, чтобы активировать ведение журнала. https://www.id.ee/ru/artikkel/generirovanie-log-fajla-v-kliente-digidoc4/</string>
<string name="main_diagnostics_restart_message_read_more">Подробнее читайте здесь</string>
<string name="main_diagnostics_restart_message_restart_now">Перезапустить приложение?</string>

<string name="read_more_message">Подробнее читайте здесь</string>
<string name="dont_show_again_message">Больше не показывать</string>

<!-- Signature Home -->
<string name="signature_home_create_text">Выберите файл для подписания или проверки</string>
<string name="signature_home_create_text_accessibility">Выберите файл для подписания или проверки</string>
Expand Down Expand Up @@ -312,6 +314,7 @@
<string name="signature_update_nfc_technical_error">Что-то пошло не так. Пожалуйста, попробуйте еще раз</string>
<string name="signature_update_nfc_hold">Удерживайте ID-карту рядом с телефоном</string>
<string name="signature_update_nfc_detected">Создание бесконтактного соединения с ID-картой</string>
<string name="signature_update_nfc_can_info">При подписании через NFC используется номер CAN. NFC может заблокироваться, если CAN введен неправильно несколько раз. https://www.id.ee/ru/artikkel/czifrovoe-podpisanie-v-mobilnom-prilozhenii-ria-digidoc/</string>

<!-- Signature Details -->
<string name="signature_error_details_title">Предупреждения</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,7 @@
<string name="sha_256">SHA-256</string>
<string name="sha_1">SHA-1</string>

<!-- NFC -->
<string name="nfc_show_can_message">nfcShowCanMessage</string>

</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,7 @@
<!-- SiVa Confirmation Dialog -->
<item type="id" name="sivaConfirmationDialog" />

<!-- Notification Dialog -->
<item type="id" name="nfcCanNotificationDialog" />

</resources>
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@
<string name="main_diagnostics_logging_switch">Enable one-time log generation</string>
<string name="main_diagnostics_save_log">Save log</string>
<string name="main_diagnostics_restart_message">Restart RIA DigiDoc to activate logging. https://www.id.ee/en/article/log-file-generation-in-digidoc4-client/</string>
<string name="main_diagnostics_restart_message_read_more">Read more here</string>
<string name="main_diagnostics_restart_message_restart_now">Restart now?</string>

<string name="read_more_message">Read more here</string>
<string name="dont_show_again_message">Don\'t show again</string>

<!-- Signature Home -->
<string name="signature_home_create_text">Choose a file to sign or validate</string>
<string name="signature_home_create_text_accessibility">Choose a file to sign or validate</string>
Expand Down Expand Up @@ -305,11 +307,12 @@
<string name="signature_update_nfc_message">Enter your ID-card access number (CAN) and PIN2 code to sign with ID-card</string>
<string name="signature_update_nfc_can">ID-card access number (CAN)</string>
<string name="signature_update_nfc_pin2">PIN2 code</string>
<string name="signature_update_nfc_adapter_missing">This device does not support contactless connection or NFC is turned off </string>
<string name="signature_update_nfc_adapter_missing">This device does not support contactless connection or NFC is turned off</string>
<string name="signature_update_nfc_tag_lost">NFC connection lost</string>
<string name="signature_update_nfc_technical_error">Technical error</string>
<string name="signature_update_nfc_hold">Hold your phone near the ID-card</string>
<string name="signature_update_nfc_detected">Authenticating with ID-card</string>
<string name="signature_update_nfc_can_info">NFC signing uses the CAN number. NFC may lock up if CAN is entered incorrectly multiple times. https://www.id.ee/en/article/digital-signing-in-ria-digidoc-mobile-application/</string>

<!-- Signature Details -->
<string name="signature_error_details_title">Warnings</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static String withURL(Context context, @StringRes int messageTranslation,
}

public static String withURLAndQuestion(Context context, @StringRes int messageTranslation,
@StringRes int urlMessageTranslation,
@StringRes int urlMessageTranslation,
@StringRes int continueQuestion) {
String message = getTextFromTranslation(context, messageTranslation);
Matcher urlMatcher = Patterns.WEB_URL.matcher(message);
Expand All @@ -44,7 +44,7 @@ private static String getTextFromTranslation(Context context, int textId) {
return resources.getString(textId);
}

private static String extractLink(String text) {
public static String extractLink(String text) {
Matcher m = Patterns.WEB_URL.matcher(text);
while (m.find()) {
return m.group();
Expand Down

0 comments on commit 5f1ee4d

Please sign in to comment.