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

225 lines
8.7 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
2018-04-26 15:59:56 +02:00
import info.nightscout.androidaps.MainActivity;
2018-04-30 15:41:40 +02:00
import info.nightscout.androidaps.MainApp;
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-10-14 17:23:31 +02:00
import info.nightscout.androidaps.plugins.bus.RxBus;
2019-02-28 23:16:50 +01:00
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientStatus;
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;
import info.nightscout.androidaps.utils.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-10-14 17:23:31 +02:00
private CompositeDisposable disposable = new CompositeDisposable();
2018-05-03 16:04:56 +02:00
ScrollView scrollView;
2018-05-09 15:10:47 +02:00
private SWDefinition swDefinition = new SWDefinition();
private List<SWScreen> screens = swDefinition.getScreens();
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);
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-10-14 17:23:31 +02:00
disposable.add(RxBus.INSTANCE
.toObservable(EventPumpStatusChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), FabricPrivacy::logException)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventNSClientStatus.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), FabricPrivacy::logException)
);
2019-10-14 23:16:51 +02:00
disposable.add(RxBus.INSTANCE
.toObservable(EventProfileNeedsUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), FabricPrivacy::logException)
);
disposable.add(RxBus.INSTANCE
.toObservable(EventProfileStoreChanged.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateButtons(), FabricPrivacy::logException)
);
2019-10-15 13:31:55 +02:00
disposable.add(RxBus.INSTANCE
.toObservable(EventSWUpdate.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> {
if (event.getRedraw()) generateLayout();
updateButtons();
}, FabricPrivacy::logException)
);
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)
OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish);
else showPreviousPage(null);
}
public void exitPressed(View view) {
SP.putBoolean(R.string.key_setupwizard_processed, true);
OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish);
}
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) {
2018-05-11 13:14:59 +02: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-22 21:37:26 +01:00
OKDialog.show(this, MainApp.gs(R.string.permission), MainApp.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();
}
}