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:
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:
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.