diff --git a/.idea/misc.xml b/.idea/misc.xml index 86bb94acb2..514c99b327 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,10 +7,10 @@ diff --git a/app/src/main/java/com/tangem/presentation/dialog/WaitSecurityDelayDialog.java b/app/src/main/java/com/tangem/presentation/dialog/WaitSecurityDelayDialog.java index 36bff1bebe..9df6222f12 100644 --- a/app/src/main/java/com/tangem/presentation/dialog/WaitSecurityDelayDialog.java +++ b/app/src/main/java/com/tangem/presentation/dialog/WaitSecurityDelayDialog.java @@ -15,6 +15,7 @@ import com.tangem.wallet.R; import java.util.Timer; import java.util.TimerTask; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; @@ -22,17 +23,22 @@ import androidx.fragment.app.DialogFragment; * Created by dvol on 06.03.2018. */ public class WaitSecurityDelayDialog extends DialogFragment { - ProgressBar progressBar; - int msTimeout = 60000, msProgress = 0; - Timer timer; + public static final String TAG = WaitSecurityDelayDialog.class.getSimpleName(); + + private ProgressBar progressBar; + private int msTimeout = 60000, msProgress = 0; + private Timer timer; + + private static Timer timerToShowDelayDialog = null; + private static WaitSecurityDelayDialog instance = null; + + private final static int minRemainingDelayToShowDialog = 1000; + private final static int delayBeforeShowDialog = 5000; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - LayoutInflater inflater = getActivity().getLayoutInflater(); - // Inflate and set the layout for the dialog - // Pass null as the parent view because its going in the dialog layout View v = inflater.inflate(R.layout.dialog_wait_pin2, null); progressBar = v.findViewById(R.id.progressBar); @@ -43,131 +49,102 @@ public class WaitSecurityDelayDialog extends DialogFragment { timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { - progressBar.post(new Runnable() { - @Override - public void run() { - int progress = WaitSecurityDelayDialog.this.progressBar.getProgress(); - if (progress < WaitSecurityDelayDialog.this.progressBar.getMax()) { - WaitSecurityDelayDialog.this.progressBar.setProgress(progress + 1000); - } + progressBar.post(() -> { + int progress = WaitSecurityDelayDialog.this.progressBar.getProgress(); + if (progress < WaitSecurityDelayDialog.this.progressBar.getMax()) { + WaitSecurityDelayDialog.this.progressBar.setProgress(progress + 1000); } }); } }, 1000, 1000); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.tangem_logo_small_new) - .setTitle("Security delay") + .setTitle(R.string.security_delay) .setView(v) .setCancelable(false) .create(); } @Override - public void onCancel(DialogInterface dialog) { + public void onCancel(@NonNull DialogInterface dialog) { super.onCancel(dialog); } - public void setup(int msTimeout, int msProgress) { - this.msTimeout = msTimeout; - this.msProgress = msProgress; - } - - public void setRemainingTimeout(final int msec) { - progressBar.post(new Runnable() { - @Override - public void run() { - int progress = WaitSecurityDelayDialog.this.progressBar.getProgress(); - if (timer != null) { - // we get delay latency from card for first time - don't change progress by timer, only by card answer - progressBar.setMax(progress + msec); - timer.cancel(); - timer = null; - } else { - int newProgress = progressBar.getMax() - msec; - if (newProgress > progress) { - progressBar.setProgress(newProgress); - } else { - progressBar.setMax(progress + msec); - } - } - } - }); - } - - static Timer timerToShowDelayDialog = null; - static WaitSecurityDelayDialog instance = null; - -// public static WaitSecurityDelayDialog getInstance() { -// if (instance == null) { -// instance = new WaitSecurityDelayDialog(); -// } -// return instance; -// } - - private final static int MinRemainingDelayToShowDialog = 1000; - private final static int DelayBeforeShowDialog = 5000; - public static void onReadBeforeRequest(final AppCompatActivity activity, final int timeout) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (timerToShowDelayDialog != null || timeout < DelayBeforeShowDialog + MinRemainingDelayToShowDialog) - return; - timerToShowDelayDialog = new Timer(); - timerToShowDelayDialog.schedule(new TimerTask() { - @Override - public void run() { - if (WaitSecurityDelayDialog.instance != null) return; - instance = new WaitSecurityDelayDialog(); - instance.setup(timeout, DelayBeforeShowDialog); - instance.setCancelable(false); - instance.show(activity.getSupportFragmentManager(), "WaitSecurityDelayDialog"); - } - }, DelayBeforeShowDialog); - } + activity.runOnUiThread(() -> { + if (timerToShowDelayDialog != null || timeout < delayBeforeShowDialog + minRemainingDelayToShowDialog) + return; + timerToShowDelayDialog = new Timer(); + timerToShowDelayDialog.schedule(new TimerTask() { + @Override + public void run() { + if (WaitSecurityDelayDialog.instance != null) return; + instance = new WaitSecurityDelayDialog(); + instance.setup(timeout, delayBeforeShowDialog); + instance.setCancelable(false); + instance.show(activity.getSupportFragmentManager(), TAG); + } + }, delayBeforeShowDialog); }); } public static void onReadAfterRequest(final Activity activity) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (timerToShowDelayDialog == null) return; - timerToShowDelayDialog.cancel(); - timerToShowDelayDialog = null; - } + activity.runOnUiThread(() -> { + if (timerToShowDelayDialog == null) return; + timerToShowDelayDialog.cancel(); + timerToShowDelayDialog = null; }); } public static void onReadWait(final AppCompatActivity activity, final int msec) { - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (timerToShowDelayDialog != null) { - timerToShowDelayDialog.cancel(); - timerToShowDelayDialog = null; - } + activity.runOnUiThread(() -> { + if (timerToShowDelayDialog != null) { + timerToShowDelayDialog.cancel(); + timerToShowDelayDialog = null; + } - if (msec == 0) { - if (instance != null) { - //TODO remove onNext java.lang.IllegalStateException: Fragment WaitSecurityDelayDialog{ba2c429 (2368a785-523e-4c3d-b46a-402d2c4f102b)} not associated with a fragment manager. - instance.dismiss(); - instance = null; - } - return; + if (msec == 0) { + if (instance != null && instance.isAdded()) { + instance.dismiss(); + instance = null; } - if (instance == null) { - if (msec > MinRemainingDelayToShowDialog) { - instance = new WaitSecurityDelayDialog(); - // 1000ms - card delay notification interval - instance.setup(msec + 1000, 1000); - instance.setCancelable(false); - instance.show(activity.getSupportFragmentManager(), "WaitSecurityDelayDialog"); - } - } else { - instance.setRemainingTimeout(msec); + return; + } + + if (instance == null) { + if (msec > minRemainingDelayToShowDialog) { + instance = new WaitSecurityDelayDialog(); + // 1000ms - card delay notification interval + instance.setup(msec + 1000, 1000); + instance.setCancelable(false); + instance.show(activity.getSupportFragmentManager(), TAG); } + } else + instance.setRemainingTimeout(msec); + }); + } + + private void setup(int msTimeout, int msProgress) { + this.msTimeout = msTimeout; + this.msProgress = msProgress; + } + + private void setRemainingTimeout(final int msec) { + progressBar.post(() -> { + int progress = WaitSecurityDelayDialog.this.progressBar.getProgress(); + if (timer != null) { + // we get delay latency from card for first time - don't change progress by timer, only by card answer + progressBar.setMax(progress + msec); + timer.cancel(); + timer = null; + } else { + int newProgress = progressBar.getMax() - msec; + if (newProgress > progress) + progressBar.setProgress(newProgress); + else + progressBar.setMax(progress + msec); } }); } -} + +} \ No newline at end of file