From d1d49a8db23c5ccf80f710ca27f0b9a63b84d9f9 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 24 Mar 2020 21:00:43 +0100 Subject: [PATCH] improve PairingProgressDialog --- app/src/main/AndroidManifest.xml | 4 +- .../dependencyInjection/FragmentsModule.kt | 2 + .../activities/PairingHelperActivity.kt | 18 +++- .../danaRS/dialogs/PairingProgressDialog.java | 91 +++++++++++-------- 4 files changed, 76 insertions(+), 39 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index caf37d462d..01df007470 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -81,7 +81,9 @@ - + diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt index dbd9cdf424..361521ca10 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/FragmentsModule.kt @@ -33,6 +33,7 @@ import info.nightscout.androidaps.plugins.profile.local.LocalProfileFragment import info.nightscout.androidaps.plugins.profile.ns.NSProfileFragment import info.nightscout.androidaps.plugins.pump.combo.ComboFragment import info.nightscout.androidaps.plugins.pump.danaR.DanaRFragment +import info.nightscout.androidaps.plugins.pump.danaRS.dialogs.PairingProgressDialog import info.nightscout.androidaps.plugins.pump.insight.LocalInsightFragment import info.nightscout.androidaps.plugins.pump.medtronic.MedtronicFragment import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpFragment @@ -83,6 +84,7 @@ abstract class FragmentsModule { @ContributesAndroidInjector abstract fun contributesVirtualPumpFragment(): VirtualPumpFragment + @ContributesAndroidInjector abstract fun contributesPairingProgressDialog(): PairingProgressDialog @ContributesAndroidInjector abstract fun contributesBolusProgressDialog(): BolusProgressDialog @ContributesAndroidInjector abstract fun contributesCalibrationDialog(): CalibrationDialog @ContributesAndroidInjector abstract fun contributesCarbsDialog(): CarbsDialog diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt index 03d31c10a2..5a2ab565ae 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/activities/PairingHelperActivity.kt @@ -1,18 +1,32 @@ package info.nightscout.androidaps.plugins.pump.danaRS.activities import android.annotation.SuppressLint +import android.content.Intent import android.content.pm.ActivityInfo import android.os.Bundle import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.plugins.pump.danaRS.dialogs.PairingProgressDialog class PairingHelperActivity : NoSplashAppCompatActivity() { + var dialog: PairingProgressDialog? = null + @SuppressLint("SourceLockedOrientationActivity") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - PairingProgressDialog() + dialog = PairingProgressDialog() .setHelperActivity(this) - .show(supportFragmentManager, "PairingProgress") + dialog?.show(supportFragmentManager, "PairingProgress") requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } + + override fun onDestroy() { + super.onDestroy() + dialog = null + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + dialog?.resetToNewPairing() + } + } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/dialogs/PairingProgressDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/dialogs/PairingProgressDialog.java index 9518dac337..dcf17dc355 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/dialogs/PairingProgressDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/dialogs/PairingProgressDialog.java @@ -14,19 +14,25 @@ import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; -import androidx.fragment.app.DialogFragment; +import javax.inject.Inject; -import info.nightscout.androidaps.MainApp; +import dagger.android.support.DaggerDialogFragment; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; +import info.nightscout.androidaps.plugins.bus.RxBusWrapper; import info.nightscout.androidaps.plugins.pump.danaRS.activities.PairingHelperActivity; import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess; import info.nightscout.androidaps.utils.FabricPrivacy; +import info.nightscout.androidaps.utils.resources.ResourceHelper; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; -public class PairingProgressDialog extends DialogFragment { +public class PairingProgressDialog extends DaggerDialogFragment { + + @Inject ResourceHelper resourceHelper; + @Inject RxBusWrapper rxBus; + @Inject FabricPrivacy fabricPrivacy; + private CompositeDisposable disposable = new CompositeDisposable(); private TextView statusView; @@ -36,41 +42,21 @@ public class PairingProgressDialog extends DialogFragment { private static boolean pairingEnded = false; - private static Handler sHandler; - private static HandlerThread sHandlerThread; + private static Handler handler; + private static HandlerThread handlerThread; + + private static Runnable runnable; public PairingProgressDialog() { super(); // Required empty public constructor - if (sHandlerThread == null) { - sHandlerThread = new HandlerThread(PairingProgressDialog.class.getSimpleName()); - sHandlerThread.start(); - sHandler = new Handler(sHandlerThread.getLooper()); + if (handlerThread == null) { + handlerThread = new HandlerThread(PairingProgressDialog.class.getSimpleName()); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.danars_pairingprogressdialog, container, false); - - getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); - getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); - setCancelable(false); - getDialog().setCanceledOnTouchOutside(false); - - statusView = view.findViewById(R.id.danars_pairingprogress_status); - progressBar = view.findViewById(R.id.danars_pairingprogress_progressbar); - button = view.findViewById(R.id.ok); - - progressBar.setMax(100); - progressBar.setProgress(0); - statusView.setText(MainApp.gs(R.string.waitingforpairing)); - button.setVisibility(View.GONE); - button.setOnClickListener(v -> dismiss()); - - sHandler.post(() -> { + runnable = () -> { for (int i = 0; i < 20; i++) { if (pairingEnded) { Activity activity = getActivity(); @@ -102,17 +88,36 @@ public class PairingProgressDialog extends DialogFragment { button.setVisibility(View.VISIBLE); }); } - }); + }; + } + + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.danars_pairingprogressdialog, container, false); + + getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); + getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); + setCancelable(false); + getDialog().setCanceledOnTouchOutside(false); + + statusView = view.findViewById(R.id.danars_pairingprogress_status); + progressBar = view.findViewById(R.id.danars_pairingprogress_progressbar); + button = view.findViewById(R.id.ok); + + setViews(); + return view; } @Override public void onResume() { super.onResume(); - disposable.add(RxBus.Companion.getINSTANCE() + disposable.add(rxBus .toObservable(EventDanaRSPairingSuccess.class) .observeOn(Schedulers.io()) - .subscribe(event -> pairingEnded = true, exception -> FabricPrivacy.getInstance().logException(exception)) + .subscribe(event -> pairingEnded = true, fabricPrivacy::logException) ); if (pairingEnded) dismiss(); getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -132,6 +137,20 @@ public class PairingProgressDialog extends DialogFragment { disposable.clear(); } + private void setViews() { + progressBar.setMax(100); + progressBar.setProgress(0); + statusView.setText(resourceHelper.gs(R.string.waitingforpairing)); + button.setVisibility(View.GONE); + button.setOnClickListener(v -> dismiss()); + handler.post(runnable); + } + + public void resetToNewPairing() { + handler.removeCallbacks(runnable); + setViews(); + } + public PairingProgressDialog setHelperActivity(PairingHelperActivity activity) { this.helperActivity = activity; return this;