package anywheresoftware.b4a.objects;
import java.lang.ref.WeakReference;
import android.content.Context;
import android.content.Intent;
import android.service.dreams.DreamService;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper;
@Hide
public class DreamServiceWrapper extends DreamService{
public static DreamServiceWrapper instance;
public PanelWrapper panel;
public CanvasWrapper canvas = new CanvasWrapper();
public ViewGroup main;
public Daydream dd;
@Override
public void onCreate() {
super.onCreate();
try {
Intent i = new Intent(this, Class.forName(getPackageName() + ".dreamservice"));
startService(i);
instance = this;
float deviceScale = getApplicationContext().getResources().getDisplayMetrics().density;
BALayout.setDeviceScale(deviceScale);
} catch (ClassNotFoundException e) {
throw new RuntimeException("DreamService not found.");
}
}
@Override
public void onAttachedToWindow () {
super.onAttachedToWindow();
main = new MyBALayout(this);
setContentView(main);
panel = new PanelWrapper();
}
private class MyBALayout extends BALayout {
public MyBALayout(Context context) {
super(context);
}
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
main.removeAllViews();
BALayout layout = new BALayout(DreamServiceWrapper.this);
main.addView(layout, new BALayout.LayoutParams(0, 0, w, h));
panel.setObject(layout);
if (dd != null)
dd.ba.raiseEventFromUI(null, dd.eventName + "_sizechanged");
}
}
@Override
public void onDreamingStarted () {
dd.ba.raiseEvent(null, dd.eventName + "_dreamstarted");
}
@Override
public void onDreamingStopped() {
dd.ba.raiseEvent(null, dd.eventName + "_dreamstopped");
}
@Override
public void onDetachedFromWindow() {
Common.Log("onDetachedFromWindow");
}
/**
* Daydream is a new "screen saver" feature introduced in Android 4.2.
*See the Daydream tutorial for more information.
*/
@Events(values={"DreamStarted", "SizeChanged", "DreamStopped"})
@ShortName("Daydream")
@Version(1.0f)
public static class Daydream {
@Hide
public BA ba;
@Hide
public String eventName;
/**
* Initializes the object and sets the subs that will handle the events.
*/
public void Initialize(BA ba, String EventName) {
instance.dd = this;
this.ba = ba;
this.eventName = EventName.toLowerCase(BA.cul);
}
/**
* Manually finishes the dream.
*/
public void Finish() {
if (instance != null)
instance.finish();
}
/**
* Returns the main panel.
*/
public PanelWrapper getPanel() {
return instance.panel;
}
/**
* A placeholder for Canvas.
*/
public CanvasWrapper getCanvas() {
return instance.canvas;
}
/**
* Gets or sets whether user interacts will be handled instead of finishing the dream.
*/
public void setInteractive(boolean v) {
instance.setInteractive(v);
}
public boolean getInteractive() {
return instance.isInteractive();
}
/**
* Gets or sets whether the system bar appears.
*/
public boolean getFullScreen() {
return instance.isFullscreen();
}
public void setFullScreen(boolean v) {
instance.setFullscreen(v);
}
/**
* Gets or sets whether the screen should stay bright.
*/
public void setScreenBright(boolean v) {
instance.setScreenBright(v);
}
public boolean getScreenBright() {
return instance.isScreenBright();
}
}
}