diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java index e12a987bd1..210cc9b489 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWDefinition.java @@ -3,8 +3,11 @@ package info.nightscout.androidaps.startupwizard; import java.util.ArrayList; import java.util.List; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; +import info.nightscout.utils.LocaleHelper; import info.nightscout.utils.SP; public class SWDefinition { @@ -16,6 +19,7 @@ public class SWDefinition { swDefinition = new SWDefinition(); return swDefinition; } + android.content.Context context = MainApp.instance().getApplicationContext(); static List screens = new ArrayList<>(); @@ -29,8 +33,19 @@ public class SWDefinition { } SWDefinition() { - add(new SWScreen(R.string.nsclientinternal_title) - .skippable(false) + // List all the screens here + // todo: SWValidator ?!? + add(new SWScreen(R.string.language) + .skippable(false) + .add(new SWRadioButton().option(R.array.languagesArray, R.array.languagesValues).preferenceId(R.string.key_language).label(R.string.language).comment(R.string.setupwizard_language_prompt)) + .validator(() -> { + context = MainApp.instance().getApplicationContext(); + LocaleHelper.setLocale(context, SP.getString(R.string.key_language, "en")); + MainApp.bus().post(new EventRefreshGui(true)); + return SP.contains(R.string.key_language);} + )) + .add(new SWScreen(R.string.nsclientinternal_title) + .skippable(true) .add(new SWUrl().preferenceId(R.string.key_nsclientinternal_url).label(R.string.nsclientinternal_url_title).comment(R.string.nsclientinternal_url_dialogmessage)) .add(new SWString().preferenceId(R.string.key_nsclientinternal_api_secret).label(R.string.nsclientinternal_secret_dialogtitle).comment(R.string.nsclientinternal_secret_dialogmessage)) .validator(() -> NSClientPlugin.getPlugin().nsClientService.isConnected && NSClientPlugin.getPlugin().nsClientService.hasWriteAuth) @@ -40,6 +55,7 @@ public class SWDefinition { .add(new SWRadioButton().option(R.array.ageArray, R.array.ageValues).preferenceId(R.string.key_age).label(R.string.patientage).comment(R.string.patientage_summary)) .validator(() -> SP.contains(R.string.key_age)) ) + ; } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java index 4e41b4c0c7..d0bccd11e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWItem.java @@ -1,10 +1,20 @@ package info.nightscout.androidaps.startupwizard; +import android.content.Context; +import android.view.View; +import android.widget.LinearLayout; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.utils.SP; public class SWItem { + private static Logger log = LoggerFactory.getLogger(SWItem.class); enum Type { NONE, URL, @@ -19,6 +29,8 @@ public class SWItem { Integer label; Integer comment; int preferenceId; + private List labels; + private List values; public SWItem(Type type) { @@ -59,4 +71,18 @@ public class SWItem { SP.putString(preferenceId, value); MainApp.bus().post(new EventPreferenceChange(preferenceId)); } + + public void setOptions(List labels, List values){ + this.labels = labels; + this.values = values; + } + + public static LinearLayout generateLayout(View view) { + LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); + layout.removeAllViews(); + return layout; + } + + public void generateDialog(View view, LinearLayout layout){ + } } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java index 5dc5c74438..09f6b625ea 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWRadioButton.java @@ -1,11 +1,25 @@ package info.nightscout.androidaps.startupwizard; +import android.content.Context; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.RadioButton; +import android.widget.RadioGroup; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.utils.SP; public class SWRadioButton extends SWItem { + private static Logger log = LoggerFactory.getLogger(SWRadioButton.class); int labelsArray; int valuesArray; + private RadioGroup radioGroup; + public boolean somethingChecked = false; public SWRadioButton() { super(Type.RADIOBUTTON); @@ -25,4 +39,73 @@ public class SWRadioButton extends SWItem { return MainApp.sResources.getStringArray(valuesArray); } + @Override + public void generateDialog(View view, LinearLayout layout){ + Context context = view.getContext(); +// LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); +// layout.removeAllViews(); + String[] labels = context.getResources().getStringArray(labelsArray); + String[] values = context.getResources().getStringArray(valuesArray); + // Get if there is already value in SP + String previousValue = SP.getString(preferenceId, "unset"); +// log.debug("Value for "+view.getContext().getString(preferenceId)+" is "+previousValue); + radioGroup = new RadioGroup(context); + radioGroup.clearCheck(); + + for (int row = 0; row < 1; row++) { + + radioGroup.setOrientation(LinearLayout.VERTICAL); + radioGroup.setVisibility(View.VISIBLE); + + for (int i = 0; i < labels.length; i++) { + RadioButton rdbtn = new RadioButton(context); + rdbtn.setId((row * 2) + i); + rdbtn.setText(labels[i]); + if(previousValue.equals(values[i])) + rdbtn.setChecked(true); + radioGroup.addView(rdbtn); + } + } + + radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + save(); + } + + }); + layout.addView(radioGroup); + + } + + public RadioGroup getRadioGroup(){ + return this.radioGroup; + } + + public String getCheckedValue(){ + if(radioGroup != null && radioGroup.getCheckedRadioButtonId() > -1){ + Context context = radioGroup.getRootView().getContext(); + String[] values = context.getResources().getStringArray(valuesArray); + return values[radioGroup.getCheckedRadioButtonId()]; + } else { + return "none"; + } + } + + public boolean isSomethingChecked(){ + return this.somethingChecked; + } + + public void save(){ + if(!getCheckedValue().equals("none")) { + SP.putString(preferenceId, getCheckedValue()); + MainApp.bus().post(new EventPreferenceChange(preferenceId)); + } + } + + public String preferenceSet(){ + return SP.getString(preferenceId, "none"); + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java index 1378ca8e0f..202a3ccc06 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWString.java @@ -1,8 +1,56 @@ package info.nightscout.androidaps.startupwizard; +import android.content.Context; +import android.text.InputType; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + + public class SWString extends SWItem { + private static Logger log = LoggerFactory.getLogger(SWString.class); + private List labels; + private List values; + private String groupName; public SWString() { super(Type.STRING); } + + public void setName(String name){ + this.groupName = name; + } + + public void setOptions(List labels, List values){ + this.labels = labels; + this.values = values; + } + + @Override + public void generateDialog(View view, LinearLayout layout) { + Context context = view.getContext(); +// LinearLayout layout = (LinearLayout) view.findViewById(view.getId()); +// layout.removeAllViews(); + + TextView textlabel = new TextView(context); + textlabel.setText(groupName); + + layout.addView(textlabel); + + if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) { + EditText editText = new EditText(context); + editText.setId(view.generateViewId()); + editText.setText(values.get(values.size()-1)); + editText.setInputType(InputType.TYPE_CLASS_TEXT); + editText.setMaxLines(1); + layout.addView(editText); + } + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java index d9ad6076ca..02b6f1bd79 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SWUrl.java @@ -1,8 +1,49 @@ package info.nightscout.androidaps.startupwizard; -public class SWUrl extends SWItem { +import android.content.Context; +import android.text.InputType; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +public class SWUrl extends SWItem { + private static Logger log = LoggerFactory.getLogger(SWUrl.class); + private List labels; + private List values; + private String groupName; public SWUrl() { super(Type.URL); } + + public void setName(String name){ + this.groupName = name; + } + + public void setOptions(List labels, List values){ + this.labels = labels; + this.values = values; + } + + @Override + public void generateDialog(View view, LinearLayout layout) { + Context context = view.getContext(); + + + if(values.get(values.size()-1) != "" && values.get(values.size()-1) != null) { + EditText editText = new EditText(context); + editText.setId(View.generateViewId()); + // get the last value in list + editText.setText(values.get(values.size()-1)); + editText.setInputType(InputType.TYPE_CLASS_TEXT); + editText.setMaxLines(1); + layout.addView(editText); + } + } + + } diff --git a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java index 3fa9cd1501..f8e2330901 100644 --- a/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/startupwizard/SetupWizardActivity.java @@ -1,14 +1,34 @@ package info.nightscout.androidaps.startupwizard; import android.annotation.SuppressLint; +import android.content.Intent; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.MotionEvent; import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.RadioGroup; +import android.widget.TextView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +import info.nightscout.androidaps.MainActivity; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.utils.LocaleHelper; +import info.nightscout.utils.SP; + +import static info.nightscout.androidaps.startupwizard.SWItem.Type.RADIOBUTTON; +import static info.nightscout.androidaps.startupwizard.SWItem.Type.STRING; +import static info.nightscout.androidaps.startupwizard.SWItem.Type.URL; /** * An example full-screen activity that shows and hides the system UI (i.e. @@ -34,6 +54,21 @@ public class SetupWizardActivity extends AppCompatActivity { private static final int UI_ANIMATION_DELAY = 300; private final Handler mHideHandler = new Handler(); private View mContentView; + private LinearLayout linearLayout; + private TextView radioLabel; + private int numberOfButtons = 0; + private List labels = new ArrayList(); + private List comments = new ArrayList(); + + private LinearLayout layout; + private TextView textlabel; + private TextView screenName; + private Button skipButton; + //logiing + private static Logger log = LoggerFactory.getLogger(SetupWizardActivity.class); + + private int currentWizardPage = 0; + public static final String INTENT_MESSAGE = "WIZZARDPAGE"; private final Runnable mHidePart2Runnable = new Runnable() { @SuppressLint("InlinedApi") @Override @@ -88,7 +123,6 @@ public class SetupWizardActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_setupwizard); mVisible = true; @@ -104,10 +138,51 @@ public class SetupWizardActivity extends AppCompatActivity { } }); + + Intent intent = getIntent(); + int showPage = intent.getIntExtra(SetupWizardActivity.INTENT_MESSAGE,0); + SWDefinition swDefinition = SWDefinition.getInstance(); + List screens = swDefinition.getScreens(); + if(screens.size() > 0 && showPage < screens.size()){ + SWScreen currentScreen = screens.get(showPage); + currentWizardPage = showPage; + // show/hide prev/next buttons if we are at the beninning/end + //showNextButton(showPage, screens.size()-1); + + if(showPage == 0) + ((Button) findViewById(R.id.previous_button)).setVisibility(View.GONE); + //Set screen name + screenName = (TextView) findViewById(R.id.fullscreen_content); + screenName.setText(currentScreen.getHeader()); + //Display screen items in the order entered + linearLayout = (LinearLayout) findViewById(R.id.fullscreen_content_controls); + // is it skippable ? + if(currentScreen.skippable) { + //display skip button + skipButton = (Button) findViewById(R.id.skip_button); + + } + //Generate layout first + LinearLayout layout = info.nightscout.androidaps.startupwizard.SWItem.generateLayout(this.findViewById(R.id.fullscreen_content_fields)); + for(int i = 0; i < currentScreen.items.size(); i++){ + SWItem currentItem = currentScreen.items.get(i); + labels.add(i,currentItem.getLabel()); + comments.add(i,currentItem.getComment()); + currentItem.setOptions(labels, comments); + currentItem.generateDialog(this.findViewById(R.id.fullscreen_content_fields), layout); + } + // Check if input isValid or screen is sckippable + if(currentScreen.validator.isValid() || currentScreen.skippable) { + showNextButton(showPage, screens.size() - 1); + show(); + } + + } + // Upon interacting with UI controls, delay any scheduled hide() // operations to prevent the jarring behavior of controls going away // while interacting with the UI. - findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener); + findViewById(R.id.next_button).setOnTouchListener(mDelayHideTouchListener); } @Override @@ -120,6 +195,23 @@ public class SetupWizardActivity extends AppCompatActivity { delayedHide(100); } + @Override + protected void onResume() { + + super.onResume(); + // check is current locale is different from the one in preferences +// log.debug("Current: "+LocaleHelper.getLanguage(this)+" preferences: "+SP.getString("language", "en")); + if(!LocaleHelper.getLanguage(this).equals(SP.getString("language", "en"))) { + // it is so change it in locale and restart SetupWizard +// log.debug("Setting locale to: "+SP.getString("language", "en")+" and restarting"); + LocaleHelper.setLocale(this, SP.getString(R.string.key_language, "en")); + MainApp.bus().post(new EventRefreshGui(true)); + Intent intent = getIntent(); + this.finish(); + startActivity(intent); + } + } + private void toggle() { if (mVisible) { hide(); @@ -162,4 +254,35 @@ public class SetupWizardActivity extends AppCompatActivity { mHideHandler.removeCallbacks(mHideRunnable); mHideHandler.postDelayed(mHideRunnable, delayMillis); } + + private void showNextButton(int currentPage, int maxPages){ + if(currentPage == maxPages) { + ((Button) findViewById(R.id.finish_button)).setVisibility(View.VISIBLE); + ((Button) findViewById(R.id.next_button)).setVisibility(View.GONE); + } else + ((Button) findViewById(R.id.next_button)).setVisibility(View.VISIBLE); + show(); + } + + public void showNextPage(View view) { + Intent intent = new Intent(this, SetupWizardActivity.class); + intent.putExtra(INTENT_MESSAGE, currentWizardPage + 1); + startActivity(intent); + } + + public void showPreviousPage(View view) { + Intent intent = new Intent(this, SetupWizardActivity.class); + if(currentWizardPage > 0) + intent.putExtra(INTENT_MESSAGE, currentWizardPage - 1); + else + intent.putExtra(INTENT_MESSAGE, 0); + startActivity(intent); + } + + // Go back to overview + public void finishSetupWizard(View view){ + Intent intent = new Intent(this, MainActivity.class); + startActivity(intent); + } + } diff --git a/app/src/main/res/layout/activity_setupwizard.xml b/app/src/main/res/layout/activity_setupwizard.xml index 706ab33863..b2ba03a37f 100644 --- a/app/src/main/res/layout/activity_setupwizard.xml +++ b/app/src/main/res/layout/activity_setupwizard.xml @@ -27,6 +27,15 @@ android:layout_height="match_parent" android:fitsSystemWindows="true"> + + + +