Skip to content

Commit

Permalink
Refactor setup wizard; add device pairing support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed May 6, 2014
1 parent 39c7db0 commit 3e69fde
Show file tree
Hide file tree
Showing 19 changed files with 777 additions and 353 deletions.
3 changes: 3 additions & 0 deletions .idea/dictionaries/mikey.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.kegbot.app.config.AppConfiguration;
import org.kegbot.app.setup.SetupActivity;
import org.kegbot.app.setup.SetupTask;

/**
* Main launcher activity; simply redirects to {@link SetupActivity} or
Expand Down Expand Up @@ -64,9 +63,9 @@ protected void onResume() {
}

final int setupVersion = mConfig.getSetupVersion();
if (setupVersion < SetupTask.SETUP_VERSION) {
if (setupVersion < SetupActivity.SETUP_VERSION) {
Log.d(TAG, "Setup is not complete, version=" + setupVersion + "current="
+ SetupTask.SETUP_VERSION);
+ SetupActivity.SETUP_VERSION);

try {
if (setupVersion == 0) {
Expand All @@ -77,7 +76,7 @@ protected void onResume() {
Long.valueOf(0));
} else {
EasyTracker.getTracker().sendEvent("Upgrade",
String.format("ToVersion %s", Integer.valueOf(SetupTask.SETUP_VERSION)),
String.format("ToVersion %s", Integer.valueOf(SetupActivity.SETUP_VERSION)),
"", Long.valueOf(0));
}
} catch (Exception e) {
Expand Down
131 changes: 74 additions & 57 deletions kegtab/src/main/java/org/kegbot/app/setup/SetupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ public class SetupActivity extends Activity {

private static final String TAG = SetupActivity.class.getSimpleName();

private SetupTask mCurrentTask = null;
private final List<SetupTask> mTaskHistory = Lists.newArrayList();
public static final int SETUP_VERSION = 6;

private SetupStep mCurrentStep = null;
private final List<SetupStep> mTaskHistory = Lists.newArrayList();

private Button mBackButton;
private Button mNextButton;
private DialogFragment mDialog;

private AsyncTask<Void, Void, String> mValidatorTask;
private AsyncTask<Void, Void, Void> mValidatorTask;

public static final String EXTRA_REASON = "reason";
public static final String EXTRA_REASON_UPGRADE = "upgrade";
Expand All @@ -71,6 +73,23 @@ public class SetupActivity extends Activity {
private static final int MESSAGE_START_VALIDATION = 101;
private static final int MESSAGE_VALIDATION_ABORTED = 102;

public interface SetupState {
public void setNextButtonEnabled(boolean enabled);
public void setNextButtonText(int resource);
}

private final SetupState mSetupState = new SetupState() {
@Override
public void setNextButtonEnabled(boolean enabled) {
mNextButton.setEnabled(enabled);
}

@Override
public void setNextButtonText(int resource) {
mNextButton.setText(resource);
}
};

private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Expand Down Expand Up @@ -133,17 +152,17 @@ protected void onResume() {
mNextButton = (Button) findViewById(R.id.setupNextButton);
mNextButton.setOnClickListener(mNextListener);

if (mCurrentTask == null) {
if (mCurrentStep == null) {
final String reason = getIntent().getStringExtra(EXTRA_REASON);
final SetupTask initialTask;
final SetupStep initialStep;
if (EXTRA_REASON_USER.equals(reason)) {
initialTask = SetupTask.FIRST_SETUP_STEP;
initialStep = new SetupSelectBackendStep(mSetupState);
} else if (EXTRA_REASON_UPGRADE.equals(reason)) {
initialTask = SetupTask.UPGRADE;
initialStep = new SetupWelcomeStep(mSetupState);
} else {
initialTask = SetupTask.WELCOME;
initialStep = new SetupWelcomeStep(mSetupState);
}
setTask(initialTask);
setTask(initialStep);
}
}

Expand Down Expand Up @@ -172,33 +191,40 @@ private void hideDialog() {
}

private void startValidation() {
Log.d(TAG, "Starting validation: " + mCurrentTask);
mValidatorTask = new AsyncTask<Void, Void, String>() {
Log.d(TAG, "Starting validation: " + mCurrentStep);
mValidatorTask = new AsyncTask<Void, Void, Void>() {

private SetupValidationException mValidationError;
private SetupStep mNextStep;

@Override
protected void onPreExecute() {
showProgressDialog();
}

@Override
protected String doInBackground(Void... params) {
final Fragment fragment = mCurrentTask.getFragment();
String result = "";
if (fragment instanceof SetupFragment) {
final SetupFragment setupFragment = (SetupFragment) fragment;
result = setupFragment.validate();
if (Strings.isNullOrEmpty(result)) {
EasyTracker.getTracker().sendEvent("SetupTask", mCurrentTask.toString(), "",
Long.valueOf(1));
}
protected Void doInBackground(Void... params) {
mValidationError = null;
mNextStep = null;

try {
mNextStep = mCurrentStep.advance();
} catch (SetupValidationException e) {
mValidationError = e;
}
return result;

return null;
}

@Override
protected void onPostExecute(String result) {
protected void onPostExecute(Void avoid) {
if (!isCancelled()) {
hideDialog();
onValidationResult(result);
if (mValidationError == null) {
onValidationSuccess(mNextStep);
} else {
onValidationFailure(mValidationError);
}
}
}

Expand All @@ -212,50 +238,41 @@ private void cancelValidation() {
}
}

private void onValidationResult(final String result) {
final AppConfiguration config = ((KegbotApplication) getApplication()).getConfig();
if (Strings.isNullOrEmpty(result)) {
Log.d(TAG, "Validation for " + mCurrentTask + " successful!");
mCurrentTask.onExitSuccess(config);
setTask(mCurrentTask.next(config));
} else {
Log.d(TAG, "Validation for " + mCurrentTask + " unsuccessful: " + result);
final Fragment fragment = mCurrentTask.getFragment();
if (fragment instanceof SetupFragment) {
final SetupFragment setupFragment = (SetupFragment) fragment;
setupFragment.onValidationFailed();
}
showAlertDialog(result);
}
private void onValidationFailure(final SetupValidationException error) {
Log.d(TAG, "Validation unsuccessful: " + error, error);
showAlertDialog(error.getMessage());
}

private void onValidationSuccess(final SetupStep nextStep) {
Log.d(TAG, "Validation successful, next step=" + nextStep);
setTask(nextStep);
}

private void setTask(SetupTask task) {
if (task == null) {
private void setTask(SetupStep step) {
if (step == null) {
Log.d(TAG, "Null task, finishing.");
KegbotApplication.get(this).getConfig().setSetupVersion(SETUP_VERSION);
setResult(RESULT_OK);
mTaskHistory.clear();
finish();
return;
}

Log.d(TAG, "Loading SetupTask: " + task);
mTaskHistory.add(task);
Log.d(TAG, "Loading SetupStep: " + step);
step.onDisplay();
mCurrentStep = step;

Fragment bodyFragment = task.getFragment();
if (bodyFragment == null) {
bodyFragment = new SetupEmptyFragment();
mTaskHistory.add(step);
Fragment contentFragment = step.getContentFragment();
Fragment controlsFragment = step.getControlsFragment();
if (controlsFragment == null) {
controlsFragment = new SetupEmptyFragment();
}

final String title = getResources().getString(task.getTitle());
final String description = getResources().getString(task.getDescription());
final SetupTextFragment textFragment = new SetupTextFragment(title, description);

mCurrentTask = task;

FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.setupTextFragment, textFragment);
transaction.replace(R.id.setupBodyFragment, bodyFragment);
transaction.replace(R.id.setupContentFragment, contentFragment);
transaction.replace(R.id.setupControlsFragment, controlsFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
Expand All @@ -264,16 +281,16 @@ private void setTask(SetupTask task) {
private void popTask() {
mTaskHistory.remove(mTaskHistory.size() - 1);
if (!mTaskHistory.isEmpty()) {
mCurrentTask = mTaskHistory.get(mTaskHistory.size() - 1);
Log.d(TAG, "Popped task, current=" + mCurrentTask);
mCurrentStep = mTaskHistory.get(mTaskHistory.size() - 1);
} else {
Log.d(TAG, "Popped last task.");
Log.d(TAG, "Popped last step.");
}
}

@Override
public void onBackPressed() {
popTask();
mCurrentStep.onDisplay();
if (mTaskHistory.isEmpty()) {
setResult(RESULT_CANCELED);
finish();
Expand Down
44 changes: 44 additions & 0 deletions kegtab/src/main/java/org/kegbot/app/setup/SetupCompleteStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.kegbot.app.setup;

import android.app.Fragment;

import com.google.common.base.Strings;

import org.kegbot.app.KegbotApplication;
import org.kegbot.app.R;
import org.kegbot.app.config.AppConfiguration;

public class SetupCompleteStep extends SetupStep {

private final Fragment mContentFragment = new SetupTextFragment() {
@Override
public String getTitle() {
return getString(R.string.setup_finished_title);
}

@Override
public String getDescription() {
return getString(R.string.setup_finished_description);
}
};

public SetupCompleteStep(SetupActivity.SetupState state) {
super(state);
}

@Override
public Fragment getContentFragment() {
mState.setNextButtonText(R.string.setup_button_finish);
return mContentFragment;
}

@Override
public Fragment getControlsFragment() {
return null;
}

@Override
public SetupStep advance() throws SetupValidationException {
return null;
}
}
64 changes: 64 additions & 0 deletions kegtab/src/main/java/org/kegbot/app/setup/SetupKegbotUrlStep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.kegbot.app.setup;

import android.app.Fragment;
import android.os.Build;

import com.google.common.base.Strings;

import org.kegbot.api.KegbotApiException;
import org.kegbot.api.KegbotApiImpl;
import org.kegbot.app.KegbotApplication;
import org.kegbot.app.R;
import org.kegbot.app.config.AppConfiguration;

public class SetupKegbotUrlStep extends SetupStep {

private final Fragment mContentFragment = new SetupTextFragment() {
@Override
public String getTitle() {
return getString(R.string.setup_kegbot_url_title);
}

@Override
public String getDescription() {
return getString(R.string.setup_kegbot_url_description);
}
};

private final SetupKegbotUrlFragment mControlsFragment = new SetupKegbotUrlFragment();

public SetupKegbotUrlStep(SetupActivity.SetupState state) {
super(state);
}

@Override
public Fragment getContentFragment() {
return mContentFragment;
}

@Override
public Fragment getControlsFragment() {
return mControlsFragment;
}

@Override
public SetupStep advance() throws SetupValidationException {
final String error = mControlsFragment.validate();
if (!Strings.isNullOrEmpty(error)) {
mControlsFragment.onValidationFailed();
throw new SetupValidationException(error);
}

final AppConfiguration config = KegbotApplication.get(mControlsFragment.getActivity()).getConfig();
final KegbotApiImpl api = new KegbotApiImpl(config);

try {
if (api.supportsDeviceLink()) {
return new SetupPairStep(mState);
}
return new SetupLoginStep(mState);
} catch (KegbotApiException e) {
throw new SetupValidationException("Error connecting to server: " + SetupFragment.toHumanError(e));
}
}
}
Loading

0 comments on commit 3e69fde

Please sign in to comment.