<commit_before>package info.nightscout.androidaps.startupwizard;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.otto.Subscribe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventProfileStoreChanged;
import info.nightscout.androidaps.events.EventProfileSwitchChange;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.events.EventObjectivesSaved;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.OKDialog;
import info.nightscout.utils.SP;

public class SetupWizardActivity extends AppCompatActivity {
    //logging
    private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);

    private SWDefinition swDefinition = new SWDefinition();
    private List<SWScreen> screens = swDefinition.getScreens();
    private int currentWizardPage = 0;
    public static final String INTENT_MESSAGE = "WIZZARDPAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocaleHelper.onCreate(this, "en");
        setContentView(R.layout.activity_setupwizard);

        Intent intent = getIntent();
        currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
        if (screens.size() > 0 && currentWizardPage < screens.size()) {
            SWScreen currentScreen = screens.get(currentWizardPage);

            //Set screen name
            TextView screenName = (TextView) findViewById(R.id.sw_content);
            screenName.setText(currentScreen.getHeader());

            swDefinition.setActivity(this);
            //Generate layout first
            generateLayout();
            updateButtons();
        }
    }

    @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) {
        OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish);
    }

    @Override
    public void onPause() {
        super.onPause();
        MainApp.bus().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MainApp.bus().register(this);
        swDefinition.setActivity(this);
    }

    @Subscribe
    public void onContentUpdate(EventSWUpdate ev) {
        if (ev.redraw)
            generateLayout();
        updateButtons();
    }

    @Subscribe
    public void onEventNSClientStatus(EventNSClientStatus ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventPumpStatusChanged(EventPumpStatusChanged ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventProfileStoreChanged(EventProfileStoreChanged ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventProfileSwitchChange(EventProfileSwitchChange ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventObjectivesSaved(EventObjectivesSaved ignored) {
        updateButtons();
    }

    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);
            currentItem.generateDialog(this.findViewById(R.id.sw_content_fields), layout);
        }
    }

    private void updateButtons() {
        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);
                }
            } else {
                findViewById(R.id.finish_button).setVisibility(View.GONE);
                findViewById(R.id.next_button).setVisibility(View.GONE);
            }
            if (currentWizardPage == 0)
                findViewById(R.id.previous_button).setVisibility(View.GONE);
            else
                findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
            currentScreen.processVisibility();
        });
    }

    public void showNextPage(View view) {
        this.finish();
        Intent intent = new Intent(this, SetupWizardActivity.class);
        intent.putExtra(INTENT_MESSAGE, nextPage());
        startActivity(intent);
    }

    public void showPreviousPage(View view) {
        this.finish();
        Intent intent = new Intent(this, SetupWizardActivity.class);
        intent.putExtra(INTENT_MESSAGE, previousPage());
        startActivity(intent);
    }

    // Go back to overview
    public void finishSetupWizard(View view) {
        SP.putBoolean(R.string.key_setupwizard_processed, true);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    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++;
        }
        return currentWizardPage;
    }
<commit_msg>remove wizard from backstack and fix the Exit -> returning to SW<commit_after>package info.nightscout.androidaps.startupwizard;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.otto.Subscribe;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import info.nightscout.androidaps.MainActivity;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.events.EventProfileStoreChanged;
import info.nightscout.androidaps.events.EventProfileSwitchChange;
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.plugins.ConstraintsObjectives.events.EventObjectivesSaved;
import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus;
import info.nightscout.androidaps.startupwizard.events.EventSWUpdate;
import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.OKDialog;
import info.nightscout.utils.SP;

public class SetupWizardActivity extends AppCompatActivity {
    //logging
    private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class);

    private SWDefinition swDefinition = new SWDefinition();
    private List<SWScreen> screens = swDefinition.getScreens();
    private int currentWizardPage = 0;
    public static final String INTENT_MESSAGE = "WIZZARDPAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocaleHelper.onCreate(this, "en");
        setContentView(R.layout.activity_setupwizard);

        Intent intent = getIntent();
        currentWizardPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE, 0);
        if (screens.size() > 0 && currentWizardPage < screens.size()) {
            SWScreen currentScreen = screens.get(currentWizardPage);

            //Set screen name
            TextView screenName = (TextView) findViewById(R.id.sw_content);
            screenName.setText(currentScreen.getHeader());

            swDefinition.setActivity(this);
            //Generate layout first
            generateLayout();
            updateButtons();
        }
    }

    @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) {
        OKDialog.showConfirmation(this, MainApp.gs(R.string.exitwizard), this::finish);
    }

    @Override
    public void onPause() {
        super.onPause();
        MainApp.bus().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MainApp.bus().register(this);
        swDefinition.setActivity(this);
    }

    @Subscribe
    public void onContentUpdate(EventSWUpdate ev) {
        if (ev.redraw)
            generateLayout();
        updateButtons();
    }

    @Subscribe
    public void onEventNSClientStatus(EventNSClientStatus ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventPumpStatusChanged(EventPumpStatusChanged ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventProfileStoreChanged(EventProfileStoreChanged ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventProfileSwitchChange(EventProfileSwitchChange ignored) {
        updateButtons();
    }

    @Subscribe
    public void onEventObjectivesSaved(EventObjectivesSaved ignored) {
        updateButtons();
    }

    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);
            currentItem.generateDialog(this.findViewById(R.id.sw_content_fields), layout);
        }
    }

    private void updateButtons() {
        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);
                }
            } else {
                findViewById(R.id.finish_button).setVisibility(View.GONE);
                findViewById(R.id.next_button).setVisibility(View.GONE);
            }
            if (currentWizardPage == 0)
                findViewById(R.id.previous_button).setVisibility(View.GONE);
            else
                findViewById(R.id.previous_button).setVisibility(View.VISIBLE);
            currentScreen.processVisibility();
        });
    }

    public void showNextPage(View view) {
        this.finish();
        Intent intent = new Intent(this, SetupWizardActivity.class);
        intent.putExtra(INTENT_MESSAGE, nextPage());
        startActivity(intent);
    }

    public void showPreviousPage(View view) {
        this.finish();
        Intent intent = new Intent(this, SetupWizardActivity.class);
        intent.putExtra(INTENT_MESSAGE, previousPage());
        startActivity(intent);
    }

    // Go back to overview
    public void finishSetupWizard(View view) {
        SP.putBoolean(R.string.key_setupwizard_processed, true);
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    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++;
        }
        return currentWizardPage;
    }

{'fraction_non_alphanumeric': 0.09929813367363216, 'fraction_numerical': 0.0015951507417450949, 'mean_word_length': 2.9233416770963703, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5714809', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3376, 'n_words': 603}