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");
  }
  
  @Override
  public void onWindowVisibilityChanged (int visibility){

    timer.cancel();
    timer = new Timer();
    
    switch (visibility){
    case GONE:
      System.out.println("NativeSurface visibility changed to GONE");
      break;
    case INVISIBLE:
      System.out.println("NativeSurface visibility changed to INVISIBLE");
      break;
    case VISIBLE:
      System.out.println("NativeSurface visibility changed to VISIBLE");
      timer.scheduleAtFixedRate(drawTimer, UPDATE_FREQUENCY, UPDATE_FREQUENCY);
      break;
    }
    
  }

}

@@ -1902,24 +1902,64 @@
 r.cancel();%0A
+    drawTimer = new DrawTimer(getId());%0A
     timer = 
@@ -2397,20 +2397,15 @@
 ;%0A    %7D%0A
-    %0A
   %7D%0A%0A%7D%0A

Fix drawTimer reset