I'm developing an engineering app for android. The thing is that I need to draw: rectangles, figures made of rectangles, and their dimensions. Then if you touch one extreme of one dimension you are able to make that dimension of the rectangle longer or shorter.
I am implementing the next scheme in order to achieve my goal:
class DrawFigureWithDimensions extends View{
// implementation of the draw methods
// ...
class Rectangles{
// characterization of the attributes needed for each "rectangle"
// ...
class DimensionPositionType{ ... }
class DimensionExtremeType{ ... }
}
class DrawRectangleWithDimension extends DrawFiguresWithDimensions{ ... }
class DrawBoxWithDimension extends DrawFiguresWithDimensions{ ... }
...
Then, I have a problems implementing the inner classes of DimensionPositionType and DimensionExtremeType, I can't find out how to declare them in a suitable way. I need to be able to decide in the extended classes, like DrawRectangleWithDimensions, for example, what type of extreme of the dimension I need: fixed or movable. Something like this:
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {
public DrawRectangleWithDimensions(Context context) {
super(context);
}
public void setFigure(double width, double height) {
figureRectangles = new Rectangle[1];
figureRectangles[0] = new Rectangle(0, 0, width, height);
figureRectangles[0].setHorizontalDimensionLeftExtremeType(FIXED);
figureRectangles[0].setHorizontalDimensionRightExtremeType(MOVABLE);
}
For instance, this is the code that I have for the inner class DimensionExtremeType:
class DimensionExtremeType{
boolean FIXED;
boolean MOVABLE;
DimensionExtremeType(String arg){
if(arg == "FIXED"){
setFixedExtreme();
}else if(arg == "MOVABLE"){
setMovableExtreme();
}
}
public void setFixedExtreme(){
FIXED = true;
MOVABLE = false;
}
public void setMovableExtreme(){
FIXED = false;
MOVABLE = true;
}
public String getDimensionExtremeType(){
if(FIXED==true){
return "FIXED";
}else if(MOVABLE==true){
return "MOVABLE";
}else {
return null;
}
}
}
I just know about the existence of the class enum, which solves the problem of design that I had. It's a much easier way of carrying it out. According to the example that I wrote in the wording of the question, this is the code using the class enum:
static class Rectangle{
// ...
DimensionExtremeType horizontalDimensionLeftExtremeType;
DimensionExtremeType horizontalDimensionRightExtremeType;
DimensionExtremeType verticalDimensionUpperExtremeType;
DimensionExtremeType verticalDimensionLowerExtremeType;
// =-=-=-= DIMENSION POSITION TYPES =-=-=-=
public enum DimensionExtremeType{FIXED, MOVABLE}
}
Then, in the extended classes, for instance DrawRectanglesWithDimensions:
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {
public DrawRectangleWithDimensions(Context context) {
super(context);
}
public void setFigure(float width, float height) {
figureRectangles = new Rectangle[1];
figureRectangles[0] = new Rectangle(0, 0, width, height);
figureRectangles[0].horizontalDimensionLeftExtremeType = Rectangle.DimensionExtremeType.FIXED;
figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
figureRectangles[0].verticalDimensionUpperExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
figureRectangles[0].horizontalDimensionRightExtremeType = Rectangle.DimensionExtremeType.MOVABLE;
}
}
Related
I'm trying to create a Lint rule in my Android code that checks the number if injections in a constructor, so if I exceed a certain number for a view model, for example, I will raise a lint warning.
I know that I have to implement a UastScanner in my Lint Detector, but I am getting lost because I cannot find good documentation. Has someone else ever done something like this? Or where can I find good deocumentation about it?
Thanks!
* NOTE - Read entire answer for edited solution. *
I was able to write the UAST conversion like so:
public class NumberOfDependenciesInjectedDetector extends Detector implements Detector.UastScanner {
private static final int NUMBER_OF_INJECTIONS_ALLOWED = 5;
private static final Class<? extends Detector> DETECTOR = NumberOfDependenciesInjectedDetector.class;
private static final EnumSet<Scope> SCOPE = Scope.JAVA_FILE_SCOPE;
private static final Implementation IMPLEMENTATION = new Implementation(DETECTOR, SCOPE);
public static final Issue ISSUE = Issue.create(
"NumberOfDependenciesInjected",
"Limit number of injections in a class via constructor",
"A longer description here",
Category.CORRECTNESS,
10,
Severity.ERROR,
IMPLEMENTATION
);
#Override
public List<Class<? extends UElement>> getApplicableUastTypes() {
return Collections.<Class<? extends UElement>>singletonList(UClass.class);
}
#Override
public UElementHandler createUastHandler(JavaContext context) {
return new ConstructorVisitor(context);
}
private static class ConstructorVisitor extends UElementHandler {
private JavaContext javaContext;
private ConstructorVisitor(JavaContext javaContext) {
this.javaContext = javaContext;
}
#Override
public void visitClass(UClass clazz){
UMethod[] methods = clazz.getMethods();
for(UMethod method : methods){
if(!method.isConstructor()) continue;
if (method.getParameterList().getParametersCount() > NUMBER_OF_INJECTIONS_ALLOWED) {
javaContext.report(ISSUE, method, javaContext.getLocation(method), "Injections exceed allowed value of " + NUMBER_OF_INJECTIONS_ALLOWED);
}
}
}
}
}
However, it seems that this still doesn't pick up Kotlin source files... very confusing.
EDIT: 12/19/17 - FIXED
The problem was two-fold:
1) There was indeed a hidden usage of a Psi method that was blocking the check from working. The visitClass method should not use getParameterList() but instead, getUastParameters(). Replace visitclass above with:
#Override
public void visitClass(UClass clazz){
UMethod[] methods = clazz.getMethods();
for(UMethod method : methods){
if(!method.isConstructor()) continue;
if (method.getUastParameters().size() > NUMBER_OF_INJECTIONS_ALLOWED) {
javaContext.report(ISSUE, clazz, javaContext.getLocation(method), "Injections exceed allowed value of " + NUMBER_OF_INJECTIONS_ALLOWED);
}
}
}
2) After speaking with Tor Norbye directly on the lint-dev group, he pointed out that Android Studio 3.0 in fact lint does not work externally with kotlin and therefore was not expected to work as described here. Upgrading to Android Studio 3.1 Canary and running the linter produced the expected report.
So I was able to find a solution using JavaScanner, but I haven't found anything yet using UastScanner (which it is what I want to use, since we have Kotlin classes too):
public class NumberOfDependenciesInjectedDetector extends Detector implements Detector.JavaScanner {
private static final int NUMBER_OF_INJECTIONS_ALLOWED = 5;
private static final Class<? extends Detector> DETECTOR = NumberOfDependenciesInjectedDetector.class;
private static final EnumSet<Scope> SCOPE = Scope.JAVA_FILE_SCOPE;
private static final Implementation IMPLEMENTATION = new Implementation(DETECTOR, SCOPE);
public static final Issue ISSUE = Issue.create(
"NumberOfDependenciesInjected",
"Limit number of injections in a class via constructor",
"A longer description here",
Category.CORRECTNESS,
10,
Severity.ERROR,
IMPLEMENTATION
);
#Override
public boolean appliesTo(Context context, File file) {
return true;
}
#Override
public Speed getSpeed(Issue issue) {
return Speed.FAST;
}
#Override
public List<Class<? extends Node>> getApplicableNodeTypes() {
return Collections.<Class<? extends Node>>singletonList(ConstructorDeclaration.class);
}
#Override
public AstVisitor createJavaVisitor(JavaContext context) {
return new ConstructorVisitor(context);
}
private static class ConstructorVisitor extends ForwardingAstVisitor {
private JavaContext javaContext;
private ConstructorVisitor(JavaContext javaContext) {
this.javaContext = javaContext;
}
#Override
public boolean visitConstructorDeclaration(ConstructorDeclaration node) {
if (node.astParameters().size() > NUMBER_OF_INJECTIONS_ALLOWED) {
javaContext.report(ISSUE, node, javaContext.getLocation(node), "My message goes here");
return true;
}
return false;
}
}
}
I want to use a value from a json file
Here is the Json file
{
"button" : [
{
"x" : 50.0
},
{
"x" : 150.0
}
]
}
I have the following classes
(Button Class)
public class Button extends Sprite{
float x;
public Button() {
super(new Texture("button.png"));
}
#Override
public void setX(float x) {
this.x = x;
}
}
(Data Class)
public class Data {
public Array<Button> buttons;
public void load() {
buttons = new Array<Button>();
Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(JsonWriter.OutputType.json);
json.fromJson(Data.class, Gdx.files.internal("buttons.json"));
}
}
(Main Class)
public class GameMain extends ApplicationAdapter {
SpriteBatch batch;
Data data;
#Override
public void create () {
batch = new SpriteBatch();
data = new Data();
data.load();
for(Button b : data.buttons) {
b.setX(b.x);
}
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
for(Button b : data.buttons) {
b.draw(batch);
}
batch.end();
}
}
I want to draw buttons in specific x positions that is held in json file
but it gives me nothing .
What is wrong in my code ?
Any ideas ?
at the end of load() you haven't asign the results. Just add buttons = :
public void load() {
//instead of this line:
//buttons = new Array<Button>();
Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(JsonWriter.OutputType.json);
// set buttons here:
buttons = json.fromJson(Data.class, Gdx.files.internal("buttons.json"));
}
Your Data class loads another instance of a Data class and doesn't assign it to anything. It's circular and doesn't make sense. The load method should be static, return a Data object (which is what the last line of your current load method provides), and not be trying to instantiate an empty buttons array that goes unused.
Your button class hides both the x field and the setX method of the superclass, making it impossible to change the actual X position of the sprite that is used when it is drawn. Sprite already has an x parameter, so you should not be adding your own. If you merely remove those two things from your Button class, it should work.
That said, you should not be loading another copy of the same texture for each button. That's a waste of memory and texture swapping. And unless you are very careful about disposing the textures "owned" by these sprites, you are also leaking memory.
I looked all over the net in order to find out if its possible to change the renderer of a GLSurfaceView on the flight. The reason is that I want to change the OpenGl program, and initiate all the attributes and unified params from its vertex and fragment shader and I don't want the any change would require to create a brand new GLSurfaceView with a brand new Renderer.
It seems like reasonable operation that should be doable.
Note: I haven't implemented the following.
GLSurfaceView.Renderer is an interface. Implement it three times. Twice for your different OpenGL renderers, and one time attached to the GLSurfaceView. The latter only dispatches to one of the former, and allows to change the renderer to which it dispatches. The code must hold a reference to this renderer, and eventually must be synchronized to the draw calls (though I don't know).
Be aware that you cannot easily switch OpenGLES context data. It is shared between all renderer instances.
class DispatchingRenderer implements GLSurfaceView.Renderer {
private class Renderer1 implements GLSurfaceView.Renderer {
...
}
private class Renderer2 implements GLSurfaceView.Renderer {
...
}
public DispatchingRenderer() {
this.r1 = new Renderer1();
this.r2 = new Renderer2();
this.currentRenderer = this.r1;
}
public void ToggleRenderer() {
if(this.currentRenderer == this.r1) {
this.currentRenderer = this.r2;
} else if (this.currentRenderer == this.r2) {
this.currentRenderer = this.r1;
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// do one-time setup
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
this.currentRenderer.onSurfaceChanged(gl, w, h);
}
public void onDrawFrame(GL10 gl) {
this.currentRenderer.onDrawFrame(gl);
}
}
I've been experimenting with using flow and mortar as an alternative architecture for our Android apps. I've been working on an app which at the minute is only a single phone layout, but I was wondering how the flow and mortar architecture might work if you want to have a different layout for tablets. Master details might be the simplest example, but there are obviously other examples.
I have a few ideas how this could work, but I wanted to know what the square developers might have thought about already around this topic.
We're still working on a canonical answer for this, but the basic idea is that you let the resource system change what views you're showing in which situation. So your activity sets its content view to, say, R.layout.root_view. The tablet version of that layout (we put it in res/layout-sw600dp) can be tied to different views, which might inject different presenters, and so on.
For cases where you need to make a runtime decision, define a boolean resource in values/bools .xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="show_tablet_ui">false</bool>
</resources>
and values-sw600dp/bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="show_tablet_ui">true</bool>
</resources>
Expose it to the rest of the app via dagger. Use this binding annotation:
/**
* Whether we should show a tablet UI.
*/
#Retention(RUNTIME) #Qualifier
public #interface ShowTabletUi {
int ID = R.bool.show_tablet_ui;
}
and a provider method like:
/**
* Singleton because there's no reason to read it from resources again,
* it won't change.
*/
#Provides #ShowTabletUi #Singleton boolean showTabletUi(Resources resources) {
return resources.getBoolean(ShowTabletUi.ID);
}
But wait there's more! Suppose you want to have a single screen / blueprint definition that manufactures different modules for different form factors. We've started using an annotation scheme to simplify that kind of thing. Instead of making our screen classes all implement BluePrint, we've started using some annotations to declare their interface class. In that world here's how a screen can selectively choose what modules to use for tablet or mobile.
#Layout(R.layout.some_view) #WithModuleFactory(SomeScreen.ModuleFactory.class)
public class SomeScreen {
public static class ModuleFactory extends ResponsiveModuleFactory<HomeScreen> {
#Override protected Object createTabletModule(HomeScreen screen) {
return new TabletModule();
}
#Override protected Object createMobileModule(HomeScreen screen) {
return new MobileModule();
}
}
Magic, right? Here's what's behind the curtain. First, a ModuleFactory is some static class that is given access to the screen and the resources and spits out a dagger module.
public abstract class ModuleFactory<T> {
final Blueprint createBlueprint(final Resources resources, final MortarScreen screen) {
return new Blueprint() {
#Override public String getMortarScopeName() {
return screen.getName();
}
#Override public Object getDaggerModule() {
return ModuleFactory.this.createDaggerModule(resources, (T) screen);
}
};
}
protected abstract Object createDaggerModule(Resources resources, T screen);
}
Our trixie ResponsiveModuleFactory subclass looks like this. (Remember how ShowTabletUi.java defined the resource id as a constant? This is why.)
public abstract class ResponsiveModuleFactory<T> extends ModuleFactory<T> {
#Override protected final Object createDaggerModule(Resources resources, T screen) {
boolean showTabletUi = resources.getBoolean(ShowTabletUi.ID);
return showTabletUi ? createTabletModule(screen) : createMobileModule(screen);
}
protected abstract Object createTabletModule(T screen);
protected abstract Object createMobileModule(T screen);
}
To make all this go, we have a ScreenScoper class (below). In the Mortar sample code, you'd make the ScreenConductor use one of these to create and destroy scopes. Sooner or later (soon I hope) Mortar and/or its samples will be updated to include this stuff.
package mortar;
import android.content.Context;
import android.content.res.Resources;
import com.squareup.util.Objects;
import dagger.Module;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.Map;
import static java.lang.String.format;
/**
* Creates {#link MortarScope}s for screens that may be annotated with {#link WithModuleFactory},
* {#link WithModule} or {#link Module}.
*/
public class ScreenScoper {
private static final ModuleFactory NO_FACTORY = new ModuleFactory() {
#Override protected Object createDaggerModule(Resources resources, Object screen) {
throw new UnsupportedOperationException();
}
};
private final Map<Class, ModuleFactory> moduleFactoryCache = new LinkedHashMap<>();
public MortarScope getScreenScope(Context context, final MortarScreen screen) {
MortarScope parentScope = Mortar.getScope(context);
return getScreenScope(context.getResources(), parentScope, screen);
}
/**
* Finds or creates the scope for the given screen, honoring its optoinal {#link
* WithModuleFactory} or {#link WithModule} annotation. Note the scopes are also created
* for unannotated screens.
*/
public MortarScope getScreenScope(Resources resources, MortarScope parentScope,
final MortarScreen screen) {
ModuleFactory moduleFactory = getModuleFactory(screen);
MortarScope childScope;
if (moduleFactory != NO_FACTORY) {
Blueprint blueprint = moduleFactory.createBlueprint(resources, screen);
childScope = parentScope.requireChild(blueprint);
} else {
// We need every screen to have a scope, so that anything it injects is scoped. We need
// this even if the screen doesn't declare a module, because Dagger allows injection of
// objects that are annotated even if they don't appear in a module.
Blueprint blueprint = new Blueprint() {
#Override public String getMortarScopeName() {
return screen.getName();
}
#Override public Object getDaggerModule() {
return null;
}
};
childScope = parentScope.requireChild(blueprint);
}
return childScope;
}
private ModuleFactory getModuleFactory(MortarScreen screen) {
Class<?> screenType = Objects.getClass(screen);
ModuleFactory moduleFactory = moduleFactoryCache.get(screenType);
if (moduleFactory != null) return moduleFactory;
WithModule withModule = screenType.getAnnotation(WithModule.class);
if (withModule != null) {
Class<?> moduleClass = withModule.value();
Constructor<?>[] constructors = moduleClass.getDeclaredConstructors();
if (constructors.length != 1) {
throw new IllegalArgumentException(
format("Module %s for screen %s should have exactly one public constructor",
moduleClass.getName(), screen.getName()));
}
Constructor constructor = constructors[0];
Class[] parameters = constructor.getParameterTypes();
if (parameters.length > 1) {
throw new IllegalArgumentException(
format("Module %s for screen %s should have 0 or 1 parameter", moduleClass.getName(),
screen.getName()));
}
Class screenParameter;
if (parameters.length == 1) {
screenParameter = parameters[0];
if (!screenParameter.isInstance(screen)) {
throw new IllegalArgumentException(format("Module %s for screen %s should have a "
+ "constructor parameter that is a super class of %s", moduleClass.getName(),
screen.getName(), screen.getClass().getName()));
}
} else {
screenParameter = null;
}
try {
if (screenParameter == null) {
moduleFactory = new NoArgsFactory(constructor);
} else {
moduleFactory = new SingleArgFactory(constructor);
}
} catch (Exception e) {
throw new RuntimeException(
format("Failed to instantiate module %s for screen %s", moduleClass.getName(),
screen.getName()), e);
}
}
if (moduleFactory == null) {
WithModuleFactory withModuleFactory = screenType.getAnnotation(WithModuleFactory.class);
if (withModuleFactory != null) {
Class<? extends ModuleFactory> mfClass = withModuleFactory.value();
try {
moduleFactory = mfClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(format("Failed to instantiate module factory %s for screen %s",
withModuleFactory.value().getName(), screen.getName()), e);
}
}
}
if (moduleFactory == null) moduleFactory = NO_FACTORY;
moduleFactoryCache.put(screenType, moduleFactory);
return moduleFactory;
}
private static class NoArgsFactory extends ModuleFactory<Object> {
final Constructor moduleConstructor;
private NoArgsFactory(Constructor moduleConstructor) {
this.moduleConstructor = moduleConstructor;
}
#Override protected Object createDaggerModule(Resources resources, Object ignored) {
try {
return moduleConstructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
private static class SingleArgFactory extends ModuleFactory {
final Constructor moduleConstructor;
public SingleArgFactory(Constructor moduleConstructor) {
this.moduleConstructor = moduleConstructor;
}
#Override protected Object createDaggerModule(Resources resources, Object screen) {
try {
return moduleConstructor.newInstance(screen);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
}
EDIT: I'll try and make it clearer sorry for the confusion.
I have two livewallpapers one is called the Past one is called the Future, what I want to do is put both into one livewallpaper (sort of a two for one deal) but let the user decide which one they want to load.
How I had it set up for one (let's say the Past) I had the onDraw method running in a class called the Past (it did not impliment anything) just past the onDraw and put the whole livewallpaper togeather.
In the livewallpaper engine I had this.
public class ThePastActivity extends WallpaperService {
#Override
public Engine onCreateEngine() {
return new ThePastActivityEngine();
}
class ThePastActivityEngine extends Engine {
private Past _past;
public ThePastActivityEngine() {
this._past = new Past();
this._past.initialize(getBaseContext(), getSurfaceHolder());
}
#Override
public void onVisibilityChanged(boolean visible) {
if(visible){
this._past.render();
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
#Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
this._past.start();
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this._past.stop();
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset,float xStep, float yStep, int xPixels, int yPixels) {
this._past.drawXOff = Math.round((this._blimp.theBackgroundImage.getWidth() - initFrameParamsWidth()) * -xOffset);
this._past.drawYOff = Math.round((this._blimp.theBackgroundImage.getHeight() - initFrameParams()) * -yOffset);
this._past.render();
}
}
Now I have two instead of one. The new one is called Future so now I have it like so:
public class ThePastActivity extends WallpaperService {
public static final String SHARED_PREFS_NAME = "livewallpapersettings";
public static final String PREFERENCE_BACK = "livewallpaper_back";
#Override
public Engine onCreateEngine() {
return new ThePastActivityEngine();
}
class ThePastActivityEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener{
private SharedPreferences prefs;
private String whichEra;
private Past _past;
private Future _future;
//make a new name ChooseEra and let it become either Past or Future
private ChooseEra _chooseEra;
public ThePastActivityEngine() {
this._chooseEra = new ChooseEra();
this._past.initialize(getBaseContext(), getSurfaceHolder());
prefs = TheSteampunkCityActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(prefs, null);
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
whichEra=(prefs.getString(PREFERENCE_BACK, "past"));
// from here I want to get either the "past" or "future"
// make the ChooseEra to be either Past or Future
// and use that for this livewallpaper engine instead
}
#Override
public void onVisibilityChanged(boolean visible) {
if(visible){
this._chooseEra.render();
}
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
#Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
this._past.start();
}
#Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this._chooseEra.stop();
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset,float xStep, float yStep, int xPixels, int yPixels) {
this._chooseEra.drawXOff = Math.round((this._chooseEra.theBackgroundImage.getWidth() - initFrameParamsWidth()) * -xOffset);
this._chooseEra.drawYOff = Math.round((this._chooseEra.theBackgroundImage.getHeight() - initFrameParams()) * -yOffset);
this._chooseEra.render();
}
}
So chooseEra has to become either Future or Past so it reads one of the two classes only and passes the arguments along through the engine.
The issue I am having is making ChooseEra to become either Past or Future. Normally using a method is easy to do but this is the first time I am trying to make it change the class name so when I put
private ChooseEra _chooseEra;
it makes no sense at all, I tried ChooseEra = Past and ChooseEra = Future in an if else statement comparing the prefs of "past" and "future" but no luck.
Again any help is greatly appreciated.
Sam
Your having typing issues. If you want to return the implementation to use (either Future or Past) then they need a shared interface or base class that you can use. I'll pick the name to be Era. Define all of your variables of that shared type and it will work. For example:
public Era readTheEra(SharedPreferences prefs) {
String whichEra = prefs.getString(PREFERENCE_BACK, "past");
Era era = whichEra.equals("past") ? new Past() : new Future();
return era;
}
Notice Era is marked as the return type of the method (you had void which won't work). Also notice I simply passed the SharedPreferences to the method, and encapsulated the code to extract the preference value in the method. That way you aren't writing to instance variables (that you don't need), then reading in other methods. Just pass the information to the method, and don't save the intermediate steps. The only thing you need is the Era reference to use. The value of the preference isn't needed after you instantiate the correct class.
You'll need to mark the two concrete implementations as implementing the Era Interface:
public interface Era {
// put methods here you need both implementations to
// have so you can work from Era interface and not the
// individual concrete clases.
}
public class Past implements Era {
...
}
public class Future implements Era {
...
}
public class Engine {
private Era era;
...
private Era readTheEra(SharedPreferences prefs) {
String whichEra = prefs.getString(PREFERENCE_BACK, "past");
Era era = whichEra.equals("past") ? new Past() : new Future();
return era;
}
}
I picked to use Interfaces because your question isn't clear enough to know if you need classes or can use simply Interfaces. But all of the same thing applies if you need to use some class like Activity or whatever. Subclass Activty with an abstract base class, and Past and Future should subclass the abstract base class.