package com.sometrik.framework;

import java.util.Timer;
import java.util.TimerTask;

import android.view.SurfaceView;
import android.view.View;

public class NativeSurface extends SurfaceView implements NativeCommandHandler {
  
  private FrameWork framework;
  private Timer timer;
  private DrawTimer drawTimer;
  //draw and update call frequency in millis
  private static final int UPDATE_FREQUENCY = 16;
  
  public NativeSurface(FrameWork framework) {
    super(framework);
    this.framework = framework;
    timer = new Timer();
    System.out.println("Native Surface constructor complete");
  }
  
  @Override
  public void showView() {
    FrameWork frame = framework;
    frame.setContentView(this);
    drawTimer = new DrawTimer(getId());
    FrameWork.currentView = getId();
    timer.scheduleAtFixedRate(drawTimer, UPDATE_FREQUENCY, UPDATE_FREQUENCY);
  }
  
  class DrawTimer extends TimerTask {

    int viewId;
   
    public DrawTimer(int viewId){
      this.viewId = viewId;
    }

    @Override
    public void run() {
      System.out.println("onUpdate");
      FrameWork.onUpdate(System.currentTimeMillis() / 1000.0, viewId);
    }
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    System.out.println("NativeSurface onSizeChanged called");
    
    float width = w;
    float height = h;
    FrameWork.onResize(System.currentTimeMillis() / 1000.0, width, height, getId());
    
  }

  @Override
  public int getElementId() {
    return getId();
  }

  @Override
  public void addChild(View view) {
    System.out.println("Native Surface couldn't handle addChild");
    //TODO
  }
  @Override
  public void addOption(int position, String text) {
    System.out.println("Native Surface couldn't handle command");
  }

}

@@ -1811,12 +1811,566 @@
 d%22);%0A  %7D
+%0A  %0A  @Override%0A  public void onWindowVisibilityChanged (int visibility)%7B%0A%0A    timer.cancel();%0A    timer = new Timer();%0A    %0A    switch (visibility)%7B%0A    case GONE:%0A      System.out.println(%22NativeSurface visibility changed to GONE%22);%0A      break;%0A    case INVISIBLE:%0A      System.out.println(%22NativeSurface visibility changed to INVISIBLE%22);%0A      break;%0A    case VISIBLE:%0A      System.out.println(%22NativeSurface visibility changed to VISIBLE%22);%0A      timer.scheduleAtFixedRate(drawTimer, UPDATE_FREQUENCY, UPDATE_FREQUENCY);%0A      break;%0A    %7D%0A    %0A  %7D
 %0A%0A%7D%0A

Add visibility detection