/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.lcltest;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.Display;
import android.view.WindowManager;
import android.graphics.*;

import android.widget.Toast;
import android.view.MotionEvent;


                /** Begin of Main Activity class - name LCLActiv                */

public class LCLActiv extends Activity
{
    /** Called when the activity is first created. */
   
/* declarate function StartLCL from our native library libplasma1.so */

    /* implementend by libplasma1.so */


private static native int StartLCL();


@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Display display = getWindowManager().getDefaultDisplay();


StartLCL();

//create the instance of special class LCLActivity to create and draw the Bitmap
//pass the dimensions of smartphone screen as arguments

        setContentView(new LCLActivity(this, display.getWidth(), display.getHeight()));
          
        Toast toast = Toast.makeText(this, "Activity was created! Display set to Default! mBitmap is created! Draw on mBitmap!",Toast.LENGTH_LONG);
        toast.show(); 
      


    }



    /* load all libraries required to work the native library properly.
                Do not delete.
							*/
    static {
	
	
	System.loadLibrary("c");
System.loadLibrary("stdc++");	
	System.loadLibrary("dl");
//	System.loadLibrary("log");

 
	System.loadLibrary("jnigraphics"); 


    /* load our native library */
	
        System.loadLibrary("plasma1");
    }



}


/** End of Main Activity class                */







/** Begin of Class LCLActivity to Visualize Form and to dispatch Event OnTouch */


class LCLActivity extends View {

/** Create var mBitmap - Java object from package android.graphics.Bitmap;     */

    private Bitmap mBitmap;  


 
private int result1=0; /** var to store the result for LCLDrawToBitmap   */
    


public int width1=0, height1=0; 

/** vars to store the widght and height - dimensions of the screen

they are public, not private

because public variables we will use in another Java class - onDraw.

*/



    /* declaration of functions from our native library libplasma1.so */

    /* implementend by libplasma1.so */




    private static native int LCLDrawToBitmap(int width, int height, Bitmap  bitmap);
  
    private static native int LCLOnTouch(float x, float y, int action);




    public LCLActivity(Context context, int width, int height) {
        super(context);


     /**create Bitmap with dimentions of smartphone screen   */

        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        


	width1=width; // save widgth and height to public vars to use in another onDraw function
	height1=height;

    }






    @Override protected void onDraw(Canvas canvas) {
      //  canvas.drawColor(0xFFCCCCCC);



//call LCLDLCLDrawToBitmap and pass mBitmap - Java object to native library

        result1=LCLDrawToBitmap (width1,height1, mBitmap);



/**check result1 of function LCLDrawToBitmap
and create message from Java code 
using toast  
This messages might be deleted or replaced with your own */
	
Toast toast = Toast.makeText(getContext(), "Tap the screen to see Bitmap redrawn and color changes",Toast.LENGTH_LONG);
        toast.show(); 




	//draw Java Bitmap on the screen of smartphone
	
        canvas.drawBitmap(mBitmap, 0, 0, null);
  





        // force a redraw, with a different time-based pattern 

       invalidate();


    }




/** OnTouchEvent - Java function
    Inside it there is Pascal function LCLOnTouch
    In Case of Success starts postInvalidate() - onDraw Java function again
 
         */


@Override public boolean onTouchEvent (MotionEvent event)
    {

      //call LCLOnTouch from native library

      int eventResult = LCLOnTouch(event.getX(), event.getY(), event.getAction());

     //check eventReslultt of LCLonTouch
     
      if ((eventResult | 1) != 0) postInvalidate();

      return true;
    }


}

/** End of Classes to Visualize Form and to dispatch Event OnTouch         */