AndroidAPS/app/src/main/java/info/nightscout/androidaps/setupwizard/SetupWizardActivity.java

235 lines
9.2 KiB
Java
Raw Normal View History

2018-05-20 21:55:53 +02:00
package info.nightscout.androidaps.setupwizard;
2018-04-20 17:27:31 +02:00
2018-04-23 16:03:22 +02:00
import android.content.Intent;
2018-05-22 17:34:09 +02:00
import android.content.pm.PackageManager;
2018-04-20 17:27:31 +02:00
import android.os.Bundle;
import android.view.View;
2018-04-23 14:58:27 +02:00
import android.widget.LinearLayout;
import android.widget.ScrollView;
2018-04-23 14:58:27 +02:00
import android.widget.TextView;
2019-12-22 21:37:26 +01:00
import androidx.annotation.NonNull;
2019-08-08 18:39:56 +02:00
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
2018-04-23 14:58:27 +02:00
import java.util.List;
2018-04-20 17:27:31 +02:00
2019-12-30 00:53:44 +01:00
import javax.inject.Inject;
2018-04-26 15:59:56 +02:00
import info.nightscout.androidaps.MainActivity;
2018-04-20 17:27:31 +02:00
import info.nightscout.androidaps.R;
2019-08-08 18:39:56 +02:00
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.events.EventProfileNeedsUpdate;
2019-08-08 18:39:56 +02:00
import info.nightscout.androidaps.events.EventProfileStoreChanged;
2018-05-09 13:38:17 +02:00
import info.nightscout.androidaps.events.EventPumpStatusChanged;
2019-12-30 00:53:44 +01:00
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
2019-12-30 00:53:44 +01:00
import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin;
2018-05-20 21:55:53 +02:00
import info.nightscout.androidaps.setupwizard.elements.SWItem;
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.AndroidPermission;
2019-10-14 17:23:31 +02:00
import info.nightscout.androidaps.utils.FabricPrivacy;
2019-02-26 20:38:27 +01:00
import info.nightscout.androidaps.utils.LocaleHelper;
import info.nightscout.androidaps.utils.OKDialog;
2019-12-30 00:53:44 +01:00
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.sharedPreferences.SP;
2019-10-14 17:23:31 +02:00
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
2018-04-20 17:27:31 +02:00
2019-08-08 18:39:56 +02:00
public class SetupWizardActivity extends NoSplashAppCompatActivity {
2019-12-30 00:53:44 +01:00
@Inject LocalProfilePlugin localProfilePlugin;
@Inject SWDefinition swDefinition;
@Inject RxBusWrapper rxBus;
@Inject ResourceHelper resourceHelper;
@Inject SP sp;
2019-10-14 17:23:31 +02:00
private CompositeDisposable disposable = new CompositeDisposable();
2018-05-03 16:04:56 +02:00
ScrollView scrollView;
2019-12-30 00:53:44 +01:00
private List<SWScreen> screens;
2018-04-25 14:29:20 +02:00
private int currentWizardPage = 0;
2018-04-23 16:03:22 +02:00
public static final String INTENT_MESSAGE = "WIZZARDPAGE";
2018-04-20 17:27:31 +02:00
@Override
2019-08-08 18:39:56 +02:00
public void onCreate(Bundle savedInstanceState) {
2018-04-20 17:27:31 +02:00
super.onCreate(savedInstanceState);
LocaleHelper.INSTANCE.update(getApplicationContext());
2018-04-20 17:27:31 +02:00
setContentView(R.layout.activity_setupwizard);
2018-04-23 14:58:27 +02:00
2019-12-22 21:37:26 +01:00
scrollView = findViewById(R.id.sw_scrollview);
2019-12-30 00:53:44 +01:00
screens = swDefinition.getScreens();
2018-04-23 16:03:22 +02:00
Intent intent = getIntent();
2018-05-03 20:06:13 +02:00
currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
if (screens.size() > 0 && currentWizardPage < screens.size()) {
SWScreen currentScreen = screens.get(currentWizardPage);
2018-04-23 14:58:27 +02:00
//Set screen name
2019-12-22 21:37:26 +01:00
TextView screenName = findViewById(R.id.sw_content);
2018-04-23 14:58:27 +02:00
screenName.setText(currentScreen.getHeader());
2018-05-09 18:49:42 +02:00
swDefinition.setActivity(this);
2018-04-27 12:30:45 +02:00
//Generate layout first
2018-05-04 10:17:50 +02:00
generateLayout();
2018-05-03 20:06:13 +02:00
updateButtons();
2018-04-23 14:58:27 +02:00
}
2018-05-03 20:06:13 +02:00
}
2018-04-23 14:58:27 +02:00
2018-05-03 20:06:13 +02:00
@Override
public void onPause() {
super.onPause();
2019-10-14 17:23:31 +02:00
disposable.clear();
2018-04-20 17:27:31 +02:00
}
2018-04-30 15:41:40 +02:00
@Override
protected void onResume() {
super.onResume();
2018-05-09 15:10:47 +02:00
swDefinition.setActivity(this);
2019-12-30 00:53:44 +01:00
disposable.add(rxBus
2019-10-14 17:23:31 +02:00
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
2020-01-03 14:30:39 +01:00
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
2019-10-14 17:23:31 +02:00
);
2019-12-30 00:53:44 +01:00
disposable.add(rxBus
2019-10-14 17:23:31 +02:00
.toObservable(EventNSClientStatus.class)
.observeOn(AndroidSchedulers.mainThread())
2020-01-03 14:30:39 +01:00
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
2019-10-14 17:23:31 +02:00
);
2019-12-30 00:53:44 +01:00
disposable.add(rxBus
2019-10-14 23:16:51 +02:00
.toObservable(EventProfileNeedsUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
2020-01-03 14:30:39 +01:00
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
2019-10-14 23:16:51 +02:00
);
2019-12-30 00:53:44 +01:00
disposable.add(rxBus
2019-10-14 23:16:51 +02:00
.toObservable(EventProfileStoreChanged.class)
.observeOn(AndroidSchedulers.mainThread())
2020-01-03 14:30:39 +01:00
.subscribe(event -> updateButtons(), exception -> FabricPrivacy.getInstance().logException(exception))
2019-10-14 23:16:51 +02:00
);
2019-12-30 00:53:44 +01:00
disposable.add(rxBus
2019-10-15 13:31:55 +02:00
.toObservable(EventSWUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (event.getRedraw()) generateLayout();
updateButtons();
2020-01-03 14:30:39 +01:00
}, exception -> FabricPrivacy.getInstance().logException(exception))
2019-10-15 13:31:55 +02:00
);
2018-05-03 20:06:13 +02:00
}
2018-05-04 10:17:50 +02:00
private void generateLayout() {
SWScreen currentScreen = screens.get(currentWizardPage);
LinearLayout layout = SWItem.generateLayout(this.findViewById(R.id.sw_content_fields));
for (int i = 0; i < currentScreen.items.size(); i++) {
SWItem currentItem = currentScreen.items.get(i);
2018-06-26 12:28:53 +02:00
currentItem.generateDialog(layout);
2018-05-04 10:17:50 +02:00
}
2019-10-15 13:31:55 +02:00
scrollView.smoothScrollTo(0, 0);
2018-05-04 10:17:50 +02:00
}
2018-05-03 20:06:13 +02:00
private void updateButtons() {
2018-05-09 18:49:42 +02:00
runOnUiThread(() -> {
SWScreen currentScreen = screens.get(currentWizardPage);
if (currentScreen.validator == null || currentScreen.validator.isValid() || currentScreen.skippable) {
if (currentWizardPage == nextPage()) {
findViewById(R.id.finish_button).setVisibility(View.VISIBLE);
findViewById(R.id.next_button).setVisibility(View.GONE);
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
findViewById(R.id.next_button).setVisibility(View.VISIBLE);
}
2018-05-03 20:06:13 +02:00
} else {
findViewById(R.id.finish_button).setVisibility(View.GONE);
2018-05-09 18:49:42 +02:00
findViewById(R.id.next_button).setVisibility(View.GONE);
2018-05-03 20:06:13 +02:00
}
2018-05-09 18:49:42 +02:00
if (currentWizardPage == 0)
findViewById(R.id.previous_button).setVisibility(View.GONE);
else
findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
currentScreen.processVisibility();
});
2018-04-26 15:59:56 +02:00
}
2018-04-23 14:58:27 +02:00
2019-12-03 15:29:14 +01:00
@Override
public void onBackPressed() {
if (currentWizardPage == 0)
2019-12-30 00:53:44 +01:00
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.exitwizard), this::finish);
2019-12-03 15:29:14 +01:00
else showPreviousPage(null);
}
public void exitPressed(View view) {
2019-12-30 00:53:44 +01:00
sp.putBoolean(R.string.key_setupwizard_processed, true);
OKDialog.showConfirmation(this, resourceHelper.gs(R.string.exitwizard), this::finish);
2019-12-03 15:29:14 +01:00
}
2018-04-23 16:03:22 +02:00
public void showNextPage(View view) {
2018-05-03 20:06:13 +02:00
this.finish();
2018-04-23 16:03:22 +02:00
Intent intent = new Intent(this, SetupWizardActivity.class);
2018-05-09 18:49:42 +02:00
intent.putExtra(INTENT_MESSAGE, nextPage());
2018-04-23 16:03:22 +02:00
startActivity(intent);
}
public void showPreviousPage(View view) {
2018-05-03 20:06:13 +02:00
this.finish();
2018-04-23 16:03:22 +02:00
Intent intent = new Intent(this, SetupWizardActivity.class);
2018-05-09 18:49:42 +02:00
intent.putExtra(INTENT_MESSAGE, previousPage());
2018-04-23 16:03:22 +02:00
startActivity(intent);
}
2018-04-26 15:59:56 +02:00
// Go back to overview
2018-05-03 16:04:56 +02:00
public void finishSetupWizard(View view) {
2019-12-30 00:53:44 +01:00
sp.putBoolean(R.string.key_setupwizard_processed, true);
2018-05-03 16:04:56 +02:00
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
2018-05-03 16:04:56 +02:00
startActivity(intent);
2018-05-22 20:02:38 +02:00
finish();
2018-04-26 15:59:56 +02:00
}
2018-04-30 15:41:40 +02:00
2018-05-09 18:49:42 +02:00
private int nextPage() {
int page = currentWizardPage + 1;
while (page < screens.size()) {
if (screens.get(page).visibility == null || screens.get(page).visibility.isValid())
return page;
page++;
}
2019-12-22 21:37:26 +01:00
return Math.min(currentWizardPage, screens.size() - 1);
2018-05-09 18:49:42 +02:00
}
private int previousPage() {
int page = currentWizardPage - 1;
2018-05-23 13:20:24 +02:00
while (page >= 0) {
2018-05-09 18:49:42 +02:00
if (screens.get(page).visibility == null || screens.get(page).visibility.isValid())
return page;
page--;
}
2019-12-12 21:37:09 +01:00
return Math.max(currentWizardPage, 0);
2018-05-09 18:49:42 +02:00
}
2018-05-22 17:34:09 +02:00
@Override
2019-12-22 21:37:26 +01:00
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
2018-05-22 17:34:09 +02:00
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (permissions.length != 0) {
if (ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case AndroidPermission.CASE_STORAGE:
//show dialog after permission is granted
2019-12-30 00:53:44 +01:00
OKDialog.show(this, resourceHelper.gs(R.string.permission), resourceHelper.gs(R.string.alert_dialog_storage_permission_text));
2018-05-22 17:34:09 +02:00
break;
case AndroidPermission.CASE_LOCATION:
case AndroidPermission.CASE_SMS:
case AndroidPermission.CASE_BATTERY:
break;
}
}
}
2018-05-22 20:02:38 +02:00
updateButtons();
2018-05-22 17:34:09 +02:00
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AndroidPermission.CASE_BATTERY)
updateButtons();
}
}