// Copyright (c) 2014, https://github.com/rhcad/touchvg

package vgtest.testview.view;

import rhcad.touchvg.IGraphView;
import rhcad.touchvg.IViewHelper;
import rhcad.touchvg.ViewFactory;
import rhcad.touchvg.view.SFGraphView;
import vgtest.testview.TestFlags;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.Toast;
import democmds.core.DemoCmdsGate;

public class SFGraphView1 extends SFGraphView implements IGraphView.OnFirstRegenListener {
    protected static final String PATH = "mnt/sdcard/TouchVG/";

    static {
        System.loadLibrary("democmds");
    }

    public SFGraphView1(Context context) {
        this(context, null);
    }

    public SFGraphView1(Context context, Bundle savedInstanceState) {
        super(context, savedInstanceState);

        int flags = ((Activity) context).getIntent().getExtras().getInt("flags");
        final IViewHelper helper = ViewFactory.createHelper(this);

        if ((flags & TestFlags.RAND_SHAPES) != 0) {
            helper.addShapesForTest();
        }
        if (savedInstanceState == null && (flags & TestFlags.RECORD) != 0) {
            setOnFirstRegenListener(this);
        }

        flags = flags & TestFlags.CMD_MASK;
        if (flags == TestFlags.SELECT_CMD) {
            helper.setCommand("select");
        } else if (flags == TestFlags.SPLINES_CMD) {
            helper.setCommand("splines");
        } else if (flags == TestFlags.LINE_CMD) {
            helper.setCommand("line");
        } else if (flags == TestFlags.LINES_CMD) {
            helper.setCommand("lines");
        } else if (flags == TestFlags.HITTEST_CMD) {
            int n = DemoCmdsGate.registerCmds(helper.cmdViewHandle());
            helper.setCommand("hittest");
            Log.d("Test", "DemoCmdsGate.registerCmds = " + n + ", " + helper.getCommand());

        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        int flags = ((Activity) getContext()).getIntent().getExtras().getInt("flags");
        if ((flags & TestFlags.HAS_BACKDRAWABLE) != 0) {
            ViewGroup layout = (ViewGroup) getParent();
            this.setBackgroundDrawable(layout.getBackground());
        }
    }

    @Override
    public boolean onPreDoubleTap(MotionEvent e) {
        int flags = ((Activity) getContext()).getIntent().getExtras().getInt("flags");
        final IViewHelper helper = ViewFactory.createHelper(this);

        if ((flags & TestFlags.SWITCH_CMD) != 0) {
            helper.switchCommand();
            Toast.makeText(getContext(), helper.getCommand(), Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

    public void onFirstRegen(IGraphView view) {
        int flags = ((Activity) getContext()).getIntent().getExtras().getInt("flags");
        final IViewHelper helper = ViewFactory.createHelper(view);

        if ((flags & TestFlags.RECORD) != 0) {
            helper.startRecord(PATH + "record");
        }
    }
}

@@ -1072,86 +1072,34 @@
 if (
-(flags & TestFlags.RAND_SHAPES) != 0) %7B%0A            helper.addShapesForTest();
+savedInstanceState == null
 %0A   
@@ -1107,18 +1107,16 @@
     
-%7D%0A
         
 if (
@@ -1115,39 +1115,8 @@
     
-if (savedInstanceState == null 
 && (
@@ -1115,32 +1115,33 @@
     && (flags & 
+(
 TestFlags.RECORD
@@ -1132,32 +1132,57 @@
 TestFlags.RECORD
+ %7C TestFlags.RAND_SHAPES)
 ) != 0) %7B%0A      
@@ -2945,24 +2945,125 @@
 per(view);%0A%0A
+        if ((flags & TestFlags.RAND_SHAPES) != 0) %7B%0A            helper.addShapesForTest();%0A        %7D%0A
         if (

Call addShapesForTest() in onFirstRegen().