wizard moved to ACL

This commit is contained in:
serso
2013-12-13 23:09:54 +04:00
parent 8a69bf8b81
commit 8d0c631bfc
33 changed files with 393 additions and 874 deletions

View File

@@ -24,7 +24,6 @@ package org.solovyev.android.calculator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Build;
@@ -33,25 +32,22 @@ import android.preference.PreferenceManager;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.solovyev.android.Activities;
import org.solovyev.android.Android;
import org.solovyev.android.Threads;
import org.solovyev.android.calculator.about.CalculatorReleaseNotesFragment;
import org.solovyev.android.calculator.plot.CalculatorPlotActivity;
import org.solovyev.android.calculator.wizard.CalculatorWizardActivity;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.fragments.FragmentUtils;
import org.solovyev.android.prefs.Preference;
import org.solovyev.android.wizard.Wizard;
import org.solovyev.android.wizard.WizardUi;
import org.solovyev.android.wizard.Wizards;
import org.solovyev.common.Objects;
import org.solovyev.common.history.HistoryAction;
import org.solovyev.common.text.Strings;
@@ -63,9 +59,8 @@ import static android.os.Build.VERSION_CODES.GINGERBREAD_MR1;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
import static org.solovyev.android.calculator.CalculatorPreferences.Gui.preventScreenFromFading;
import static org.solovyev.android.calculator.wizard.Wizards.FIRST_TIME_WIZARD;
import static org.solovyev.android.calculator.wizard.Wizards.isWizardFinished;
import static org.solovyev.android.calculator.wizard.Wizards.isWizardStarted;
import static org.solovyev.android.wizard.WizardUi.continueWizard;
import static org.solovyev.android.wizard.WizardUi.startWizard;
public class CalculatorActivity extends SherlockFragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener, CalculatorEventListener {
@@ -154,13 +149,15 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Shar
if (!CalculatorApplication.isMonkeyRunner(context)) {
boolean dialogShown = false;
if (isWizardStarted(FIRST_TIME_WIZARD) && !isWizardFinished(FIRST_TIME_WIZARD)) {
CalculatorWizardActivity.continueWizard(FIRST_TIME_WIZARD, context);
final Wizards wizards = CalculatorApplication.getInstance().getWizards();
final Wizard wizard = wizards.getWizard(CalculatorWizards.FIRST_TIME_WIZARD);
if (wizard.isStarted() && !wizard.isFinished()) {
continueWizard(wizards, wizard.getName(), context);
dialogShown = true;
} else {
if (Objects.areEqual(savedVersion, CalculatorPreferences.appVersion.getDefaultValue())) {
// new start
context.startActivity(new Intent(context, CalculatorWizardActivity.class));
startWizard(wizards, context);
dialogShown = true;
} else {
if (savedVersion < appVersion) {

View File

@@ -44,6 +44,8 @@ import org.solovyev.android.calculator.onscreen.CalculatorOnscreenStartActivity;
import org.solovyev.android.calculator.plot.AndroidCalculatorPlotter;
import org.solovyev.android.calculator.plot.CalculatorPlotterImpl;
import org.solovyev.android.calculator.view.EditorTextProcessor;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.wizard.Wizards;
import org.solovyev.common.msg.MessageType;
import javax.annotation.Nonnull;
@@ -98,6 +100,11 @@ public class CalculatorApplication extends android.app.Application implements Sh
@Nonnull
private final CalculatorBroadcaster broadcaster = new CalculatorBroadcaster(this);
@Nonnull
private final Wizards wizards = new CalculatorWizards(this);
private final boolean withAds;
/*
**********************************************************************
*
@@ -106,10 +113,15 @@ public class CalculatorApplication extends android.app.Application implements Sh
**********************************************************************
*/
public CalculatorApplication() {
protected CalculatorApplication(boolean withAds) {
this.withAds = withAds;
instance = this;
}
public CalculatorApplication() {
this(true);
}
/*
**********************************************************************
@@ -162,18 +174,20 @@ public class CalculatorApplication extends android.app.Application implements Sh
BillingDB.init(CalculatorApplication.this);
AdsController.getInstance().init(this, ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {
if (withAds) {
AdsController.getInstance().init(this, ADMOB_USER_ID, AD_FREE_PRODUCT_ID, new BillingController.IConfiguration() {
@Override
public byte[] getObfuscationSalt() {
return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82};
}
@Override
public byte[] getObfuscationSalt() {
return new byte[]{81, -114, 32, -127, -32, -104, -40, -15, -47, 57, -13, -41, -33, 67, -114, 7, -11, 53, 126, 82};
}
@Override
public String getPublicKey() {
return CalculatorSecurity.getPK();
}
});
@Override
public String getPublicKey() {
return CalculatorSecurity.getPK();
}
});
}
BillingController.registerObserver(new DefaultBillingObserver(this, null));
@@ -229,6 +243,11 @@ public class CalculatorApplication extends android.app.Application implements Sh
return uiHandler;
}
@Nonnull
public Wizards getWizards() {
return wizards;
}
/*
**********************************************************************
*

View File

@@ -42,15 +42,17 @@ import org.solovyev.android.App;
import org.solovyev.android.ads.AdsController;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.model.AndroidCalculatorEngine;
import org.solovyev.android.calculator.wizard.CalculatorWizards;
import org.solovyev.android.msg.AndroidMessage;
import org.solovyev.android.view.VibratorContainer;
import org.solovyev.android.wizard.WizardUi;
import org.solovyev.common.msg.Message;
import org.solovyev.common.msg.MessageType;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.wizard.CalculatorWizardActivity.startWizard;
import static org.solovyev.android.calculator.wizard.Wizards.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.calculator.wizard.CalculatorWizards.DEFAULT_WIZARD_FLOW;
import static org.solovyev.android.wizard.WizardUi.startWizard;
/**
* User: serso
@@ -79,7 +81,7 @@ public class CalculatorPreferencesActivity extends SherlockPreferenceActivity im
restartWizardPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startWizard(DEFAULT_WIZARD_FLOW, CalculatorPreferencesActivity.this);
startWizard(CalculatorApplication.getInstance().getWizards(), DEFAULT_WIZARD_FLOW, CalculatorPreferencesActivity.this);
return true;
}
});

View File

@@ -22,285 +22,29 @@
package org.solovyev.android.calculator.wizard;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.R;
import org.solovyev.android.wizard.BaseWizardActivity;
import org.solovyev.android.wizard.Wizards;
import javax.annotation.Nonnull;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static org.solovyev.android.calculator.wizard.Wizards.FLOW;
import static org.solovyev.android.calculator.wizard.Wizards.STEP;
public final class CalculatorWizardActivity extends BaseWizardActivity {
/**
* User: serso
* Date: 6/16/13
* Time: 9:07 PM
*/
public final class CalculatorWizardActivity extends FragmentActivity {
@Nonnull
private Wizards wizards;
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
private WizardStep step;
private WizardFlow flow;
/*
**********************************************************************
*
* VIEWS
*
**********************************************************************
*/
private View prevButton;
private Button nextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cpp_wizard);
prevButton = findViewById(R.id.wizard_prev_button);
nextButton = (Button) findViewById(R.id.wizard_next_button);
String wizardName = getIntent().getStringExtra(FLOW);
String stepName = getIntent().getStringExtra(STEP);
if (savedInstanceState != null) {
wizardName = savedInstanceState.getString(FLOW);
stepName = savedInstanceState.getString(STEP);
}
flow = Wizards.getWizardFlow(wizardName != null ? wizardName : Wizards.FIRST_TIME_WIZARD);
WizardStep step = null;
if (stepName != null) {
step = flow.getStep(stepName);
}
if (step == null) {
step = flow.getFirstStep();
}
setStep(step);
}
void setStep(@Nonnull WizardStep step) {
if (this.step == null || !this.step.equals(step)) {
hideFragment();
this.step = step;
showFragment();
initTitle();
initNextButton();
initPrevButton();
}
}
private void initTitle() {
setTitle(step.getTitleResId());
}
private void initPrevButton() {
final WizardStep prevStep = flow.getPrevStep(step);
if (prevStep == null) {
prevButton.setVisibility(GONE);
prevButton.setOnClickListener(null);
} else {
prevButton.setVisibility(VISIBLE);
prevButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoPrev()) {
setStep(prevStep);
}
}
});
}
}
private void initNextButton() {
final WizardStep nextStep = flow.getNextStep(step);
if (nextStep == null) {
nextButton.setText(R.string.cpp_wizard_finish);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoNext()) {
finishFlow();
}
}
});
} else {
nextButton.setText(step.getNextButtonTitleResId());
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tryGoNext()) {
setStep(nextStep);
}
}
});
}
}
void finishFlow() {
finishFlow(false);
}
void finishFlow(boolean forceFinish) {
if (flow != null && step != null) {
Wizards.saveWizardFinished(flow, step, forceFinish);
}
finish();
}
private boolean tryGoPrev() {
if (this.step == null) {
return true;
} else {
final Fragment fragment = getSupportFragmentManager().findFragmentByTag(this.step.getFragmentTag());
if (fragment != null) {
return this.step.onPrev(fragment);
} else {
return true;
}
}
}
private boolean tryGoNext() {
if (this.step == null) {
return true;
} else {
final Fragment fragment = getSupportFragmentManager().findFragmentByTag(this.step.getFragmentTag());
if (fragment != null) {
return this.step.onNext(fragment);
} else {
return true;
}
}
public CalculatorWizardActivity() {
super(R.layout.cpp_wizard);
this.wizards = CalculatorApplication.getInstance().getWizards();
}
@Nonnull
private Fragment showFragment() {
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
Fragment newFragment = fm.findFragmentByTag(this.step.getFragmentTag());
if (newFragment == null) {
newFragment = Fragment.instantiate(this, this.step.getFragmentClass().getName(), this.step.getFragmentArgs());
ft.add(R.id.wizard_content, newFragment, this.step.getFragmentTag());
}
ft.commit();
fm.executePendingTransactions();
return newFragment;
public Wizards getWizards() {
return wizards;
}
private void hideFragment() {
final FragmentManager fm = getSupportFragmentManager();
final FragmentTransaction ft = fm.beginTransaction();
if (this.step != null) {
hideFragmentByTag(fm, ft, this.step.getFragmentTag());
}
ft.commit();
fm.executePendingTransactions();
public void setWizards(@Nonnull Wizards wizards) {
this.wizards = wizards;
}
private void hideFragmentByTag(@Nonnull FragmentManager fm, @Nonnull FragmentTransaction ft, @Nonnull String fragmentTag) {
final Fragment oldFragment = fm.findFragmentByTag(fragmentTag);
if (oldFragment != null) {
ft.remove(oldFragment);
}
}
@Override
protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString(FLOW, flow.getName());
out.putString(STEP, step.getName());
}
@Override
protected void onPause() {
super.onPause();
if (flow != null && step != null) {
Wizards.saveLastWizardStep(flow, step);
}
}
@Override
public void onBackPressed() {
FinishWizardConfirmationDialog.show(this);
}
WizardStep getStep() {
return step;
}
WizardFlow getFlow() {
return flow;
}
View getPrevButton() {
return prevButton;
}
View getNextButton() {
return nextButton;
}
/*
**********************************************************************
*
* STATIC/INNER
*
**********************************************************************
*/
public static void startWizard(@Nonnull String name, @Nonnull Context context) {
context.startActivity(createLaunchIntent(name, context));
}
public static void continueWizard(@Nonnull String name, @Nonnull Context context) {
final Intent intent = createLaunchIntent(name, context);
final String step = Wizards.getLastSavedWizardStepName(name);
if (step != null) {
intent.putExtra(STEP, step);
}
context.startActivity(intent);
}
@Nonnull
private static Intent createLaunchIntent(@Nonnull String name, @Nonnull Context context) {
final Intent intent = new Intent(context, CalculatorWizardActivity.class);
intent.putExtra(FLOW, name);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return intent;
}
}

View File

@@ -43,13 +43,13 @@ import static org.solovyev.android.calculator.wizard.OnScreenCalculatorWizardSte
* Date: 6/16/13
* Time: 9:17 PM
*/
enum WizardStep {
enum CalculatorWizardStep implements org.solovyev.android.wizard.WizardStep {
welcome(WelcomeWizardStep.class, R.string.cpp_wizard_welcome_title, R.string.cpp_wizard_start),
choose_layout(ChooseLayoutWizardStep.class, R.string.cpp_wizard_layout_title) {
@Override
boolean onNext(@Nonnull Fragment f) {
public boolean onNext(@Nonnull Fragment f) {
final ChooseLayoutWizardStep fragment = (ChooseLayoutWizardStep) f;
final CalculatorLayout layout = fragment.getSelectedLayout();
@@ -60,7 +60,7 @@ enum WizardStep {
@Nullable
@Override
Bundle getFragmentArgs() {
public Bundle getFragmentArgs() {
final Bundle bundle = new Bundle();
bundle.putSerializable(LAYOUT, CalculatorLayout.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(getPreferences())));
return bundle;
@@ -74,7 +74,7 @@ enum WizardStep {
choose_mode(ChooseModeWizardStep.class, R.string.cpp_wizard_mode_title) {
@Override
boolean onNext(@Nonnull Fragment f) {
public boolean onNext(@Nonnull Fragment f) {
final ChooseModeWizardStep fragment = (ChooseModeWizardStep) f;
final CalculatorMode mode = fragment.getSelectedMode();
@@ -85,7 +85,7 @@ enum WizardStep {
@Nullable
@Override
Bundle getFragmentArgs() {
public Bundle getFragmentArgs() {
final Bundle bundle = new Bundle();
bundle.putSerializable(MODE, CalculatorMode.fromGuiLayout(CalculatorPreferences.Gui.layout.getPreference(getPreferences())));
return bundle;
@@ -94,7 +94,7 @@ enum WizardStep {
on_screen_calculator(OnScreenCalculatorWizardStep.class, R.string.cpp_wizard_onscreen_calculator_title) {
@Override
boolean onNext(@Nonnull Fragment f) {
public boolean onNext(@Nonnull Fragment f) {
final OnScreenCalculatorWizardStep fragment = (OnScreenCalculatorWizardStep) f;
CalculatorPreferences.OnscreenCalculator.showAppIcon.putPreference(getPreferences(), fragment.isOnscreenCalculatorEnabled());
@@ -104,7 +104,7 @@ enum WizardStep {
@Nullable
@Override
Bundle getFragmentArgs() {
public Bundle getFragmentArgs() {
final Bundle bundle = new Bundle();
bundle.putSerializable(ONSCREEN_CALCULATOR_ENABLED, CalculatorPreferences.OnscreenCalculator.showAppIcon.getPreference(getPreferences()));
return bundle;
@@ -121,49 +121,59 @@ enum WizardStep {
private final int titleResId;
private final int nextButtonTitleResId;
WizardStep(@Nonnull Class<? extends Fragment> fragmentClass, int titleResId) {
CalculatorWizardStep(@Nonnull Class<? extends Fragment> fragmentClass, int titleResId) {
this(fragmentClass, titleResId, R.string.cpp_wizard_next);
}
WizardStep(@Nonnull Class<? extends Fragment> fragmentClass, int titleResId, int nextButtonTitleResId) {
CalculatorWizardStep(@Nonnull Class<? extends Fragment> fragmentClass, int titleResId, int nextButtonTitleResId) {
this.fragmentClass = fragmentClass;
this.titleResId = titleResId;
this.nextButtonTitleResId = nextButtonTitleResId;
}
@Nonnull
@Override
public String getFragmentTag() {
return name();
}
@Override
@Nonnull
Class<? extends Fragment> getFragmentClass() {
public Class<? extends Fragment> getFragmentClass() {
return fragmentClass;
}
int getTitleResId() {
@Override
public int getTitleResId() {
return titleResId;
}
int getNextButtonTitleResId() {
@Override
public int getNextButtonTitleResId() {
return nextButtonTitleResId;
}
boolean onNext(@Nonnull Fragment fragment) {
@Override
public boolean onNext(@Nonnull Fragment fragment) {
return true;
}
boolean onPrev(@Nonnull Fragment fragment) {
@Override
public boolean onPrev(@Nonnull Fragment fragment) {
return true;
}
@Override
@Nullable
Bundle getFragmentArgs() {
public Bundle getFragmentArgs() {
return null;
}
@Override
public boolean isVisible() {
return true;
}
@Override
@Nonnull
public String getName() {
return name();

View File

@@ -0,0 +1,75 @@
package org.solovyev.android.calculator.wizard;
import android.app.Activity;
import android.content.Context;
import org.solovyev.android.wizard.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.last;
import static org.solovyev.android.calculator.wizard.CalculatorWizardStep.welcome;
public class CalculatorWizards implements Wizards {
public static final String FIRST_TIME_WIZARD = "first-wizard";
public static final String DEFAULT_WIZARD_FLOW = "app-wizard";
@Nonnull
private final Context context;
public CalculatorWizards(@Nonnull Context context) {
this.context = context;
}
@Nonnull
@Override
public Class<? extends Activity> getActivityClassName() {
return CalculatorWizardActivity.class;
}
@Nonnull
@Override
public Wizard getWizard(@Nullable String name) {
if (name == null) {
return getWizard(FIRST_TIME_WIZARD);
}
if (FIRST_TIME_WIZARD.equals(name)) {
return newBaseWizard(FIRST_TIME_WIZARD, newFirstTimeWizardFlow());
} else if (DEFAULT_WIZARD_FLOW.equals(name)) {
return newBaseWizard(DEFAULT_WIZARD_FLOW, newDefaultWizardFlow());
} else {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
}
}
@Nonnull
private BaseWizard newBaseWizard(@Nonnull String name, @Nonnull WizardFlow flow) {
return new BaseWizard(name, context, flow);
}
@Nonnull
static WizardFlow newDefaultWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : CalculatorWizardStep.values()) {
if (wizardStep != welcome && wizardStep != last && wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(wizardSteps);
}
@Nonnull
static WizardFlow newFirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : CalculatorWizardStep.values()) {
if (wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(wizardSteps);
}
}

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.wizard;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import org.solovyev.android.calculator.R;
import org.solovyev.android.sherlock.AndroidSherlockUtils;
import javax.annotation.Nonnull;
public class FinishWizardConfirmationDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setMessage(R.string.cpp_wizard_finish_confirmation);
b.setPositiveButton(R.string.c_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((CalculatorWizardActivity) getActivity()).finishFlow(true);
}
});
b.setNegativeButton(R.string.c_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dismiss();
}
});
return b.create();
}
public static void show(@Nonnull CalculatorWizardActivity activity) {
AndroidSherlockUtils.showDialog(new FinishWizardConfirmationDialog(), FinishWizardConfirmationDialog.class.getSimpleName(), activity.getSupportFragmentManager());
}
}

View File

@@ -1,96 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.wizard;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
/**
* User: serso
* Date: 6/16/13
* Time: 9:30 PM
*/
public final class ListWizardFlow implements WizardFlow {
@Nonnull
private final String name;
@Nonnull
private final List<WizardStep> wizardSteps;
public ListWizardFlow(@Nonnull String name, @Nonnull List<WizardStep> wizardSteps) {
this.name = name;
this.wizardSteps = wizardSteps;
}
@Nonnull
@Override
public String getName() {
return name;
}
@Nullable
@Override
public WizardStep getStep(@Nonnull final String name) {
return Iterables.find(wizardSteps, new Predicate<WizardStep>() {
@Override
public boolean apply(@Nullable WizardStep step) {
assert step != null;
return step.getName().equals(name);
}
}, null);
}
@Nullable
@Override
public WizardStep getNextStep(@Nonnull WizardStep step) {
final int i = wizardSteps.indexOf(step);
if (i >= 0 && i + 1 < wizardSteps.size()) {
return wizardSteps.get(i + 1);
} else {
return null;
}
}
@Nullable
@Override
public WizardStep getPrevStep(@Nonnull WizardStep step) {
final int i = wizardSteps.indexOf(step);
if (i >= 1) {
return wizardSteps.get(i - 1);
} else {
return null;
}
}
@Nonnull
@Override
public WizardStep getFirstStep() {
return wizardSteps.get(0);
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.wizard;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 6/16/13
* Time: 9:22 PM
*/
public interface WizardFlow {
@Nonnull
String getName();
@Nullable
WizardStep getStep(@Nonnull String name);
@Nullable
WizardStep getNextStep(@Nonnull WizardStep step);
@Nullable
WizardStep getPrevStep(@Nonnull WizardStep step);
@Nonnull
WizardStep getFirstStep();
}

View File

@@ -1,142 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.solovyev.android.calculator.CalculatorApplication;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.wizard.WizardStep.last;
import static org.solovyev.android.calculator.wizard.WizardStep.welcome;
/**
* User: serso
* Date: 6/16/13
* Time: 9:23 PM
*/
public final class Wizards {
public static final String FIRST_TIME_WIZARD = "first-wizard";
public static final String DEFAULT_WIZARD_FLOW = "app-wizard";
static final String FLOW = "flow";
static final String FLOW_FINISHED = "flow_finished";
static final String STEP = "step";
private Wizards() {
throw new AssertionError();
}
@Nonnull
public static WizardFlow getWizardFlow(@Nonnull String name) {
if (FIRST_TIME_WIZARD.equals(name)) {
return newFirstTimeWizardFlow();
} else if (DEFAULT_WIZARD_FLOW.equals(name)) {
return newDefaultWizardFlow();
} else {
throw new IllegalArgumentException("Wizard flow " + name + " is not supported");
}
}
public static boolean isWizardFinished(@Nonnull String name) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
return preferences.getBoolean(makeFinishedPreferenceKey(name), false);
}
public static boolean isWizardStarted(@Nonnull String name) {
return getLastSavedWizardStepName(name) != null;
}
static void saveLastWizardStep(@Nonnull WizardFlow flow, @Nonnull WizardStep step) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final SharedPreferences.Editor editor = preferences.edit();
editor.putString(makeLastStepPreferenceKey(flow), step.name());
editor.commit();
}
@Nullable
static String getLastSavedWizardStepName(@Nonnull String name) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
return preferences.getString(makeLastStepPreferenceKey(name), null);
}
static void saveWizardFinished(@Nonnull WizardFlow flow, @Nonnull WizardStep step, boolean forceFinish) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(CalculatorApplication.getInstance());
final SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(makeFinishedPreferenceKey(flow), forceFinish || flow.getNextStep(step) == null);
editor.commit();
}
@Nonnull
private static String makeFinishedPreferenceKey(@Nonnull WizardFlow flow) {
return makeFinishedPreferenceKey(flow.getName());
}
@Nonnull
private static String makeFinishedPreferenceKey(@Nonnull String flowName) {
return FLOW_FINISHED + ":" + flowName;
}
@Nonnull
private static String makeLastStepPreferenceKey(@Nonnull WizardFlow flow) {
return makeLastStepPreferenceKey(flow.getName());
}
@Nonnull
private static String makeLastStepPreferenceKey(@Nonnull String flowName) {
return FLOW + ":" + flowName;
}
@Nonnull
static WizardFlow newDefaultWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep != welcome && wizardStep != last && wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(DEFAULT_WIZARD_FLOW, wizardSteps);
}
@Nonnull
static WizardFlow newFirstTimeWizardFlow() {
final List<WizardStep> wizardSteps = new ArrayList<WizardStep>();
for (WizardStep wizardStep : WizardStep.values()) {
if (wizardStep.isVisible()) {
wizardSteps.add(wizardStep);
}
}
return new ListWizardFlow(FIRST_TIME_WIZARD, wizardSteps);
}
}