package com.akexorcist.spenapp;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.samsung.android.sdk.SsdkUnsupportedException;
import com.samsung.android.sdk.pen.Spen;
import com.samsung.android.sdk.pen.document.SpenNoteDoc;
import com.samsung.android.sdk.pen.document.SpenPageDoc;
import com.samsung.android.sdk.pen.engine.SpenSimpleSurfaceView;
import com.samsung.android.sdk.pen.settingui.SpenSettingPenLayout;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private FrameLayout layoutDrawingWorkspace;
    private FrameLayout layoutPenSetting;
    private RelativeLayout layoutPenCanvas;
    private Button btnSave;
    private Button btnPenSetting;
    private Spen spenPackage;
    private SpenNoteDoc spenNoteDoc;
    private SpenSimpleSurfaceView spenSimpleSurfaceView;
    private SpenSettingPenLayout spenSettingPenLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layoutDrawingWorkspace = (FrameLayout) findViewById(R.id.layout_drawing_workspace);
        layoutPenSetting = (FrameLayout) findViewById(R.id.layout_pen_setting);
        layoutPenCanvas = (RelativeLayout) findViewById(R.id.layout_pen_canvas);
        btnSave = (Button) findViewById(R.id.btn_save);
        btnPenSetting = (Button) findViewById(R.id.btn_pen_setting);
        btnSave.setOnClickListener(this);
        btnPenSetting.setOnClickListener(this);

        if (setupSpen()) {
            setupDrawingWorkspace();
        } else {
            Toast.makeText(this, "Device doesn't support S pen.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDrawingWorkspace();
    }

    private void closeDrawingWorkspace() {
        if (spenSimpleSurfaceView != null) {
            spenSimpleSurfaceView.close();
            spenSimpleSurfaceView = null;
        }
        if (spenNoteDoc != null) {
            try {
                spenNoteDoc.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            spenNoteDoc = null;
        }
    }

    private void setupDrawingWorkspace() {
        try {
            spenSimpleSurfaceView = new SpenSimpleSurfaceView(this);
            spenSimpleSurfaceView.setBlankColor(Color.GRAY);
            layoutDrawingWorkspace.addView(spenSimpleSurfaceView);
            spenNoteDoc = new SpenNoteDoc(this, 1000, 1000);
            SpenPageDoc spenPageDoc = spenNoteDoc.appendPage();
            spenPageDoc.setBackgroundColor(Color.WHITE);
            spenPageDoc.clearHistory();
            spenSimpleSurfaceView.setPageDoc(spenPageDoc, true);
            spenSettingPenLayout = new SpenSettingPenLayout(this, "", layoutPenCanvas);
            layoutPenSetting.addView(spenSettingPenLayout);
            spenSettingPenLayout.setCanvasView(spenSimpleSurfaceView);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void openPenSetting() {
        spenSettingPenLayout.setViewMode(SpenSettingPenLayout.VIEW_MODE_NORMAL);
        spenSettingPenLayout.setVisibility(View.VISIBLE);
    }


    private boolean setupSpen() {
        spenPackage = new Spen();
        try {
            spenPackage.initialize(this);
            if (spenPackage.isFeatureEnabled(Spen.DEVICE_PEN)) {
                return true;
            }
        } catch (SsdkUnsupportedException e) {
            // Error Handle
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v == btnSave) {
            Bitmap drawingBitmap = spenSimpleSurfaceView.captureCurrentView(true);
            // Do something with this bitmap
        } else if (v == btnPenSetting) {
            openPenSetting();
        }
    }
}

@@ -1000,23 +1000,16 @@
 pen spen
-Package
 ;%0A    pr
@@ -1039,16 +1039,53 @@
 oteDoc;%0A
+    private SpenPageDoc spenPageDoc;%0A
     priv
@@ -2584,34 +2584,140 @@
 ) %7B%0A        
-try %7B%0A    
+setupDrawingView();%0A        setupNoteDoc();%0A        setupPenSetting();%0A    %7D%0A%0A    private void setupDrawingView() %7B%0A
         spen
@@ -2769,36 +2769,32 @@
 (this);%0A        
-    
 spenSimpleSurfac
@@ -2830,28 +2830,24 @@
 Y);%0A        
-  
-  
 layoutDrawin
@@ -2881,32 +2881,87 @@
 leSurfaceView);%0A
+    %7D%0A%0A    private void setupNoteDoc() %7B%0A        try %7B%0A
             spen
@@ -3017,28 +3017,16 @@
         
-SpenPageDoc 
 spenPage
@@ -3118,48 +3118,8 @@
 E);%0A
-            spenPageDoc.clearHistory();%0A
     
@@ -3175,32 +3175,149 @@
 Doc, true);%0A    
+    %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    private void setupPenSetting() %7B%0A
         spenSett
@@ -3380,28 +3380,24 @@
 PenCanvas);%0A
-    
         layo
@@ -3444,28 +3444,24 @@
 t);%0A        
-    
 spenSettingP
@@ -3511,85 +3511,8 @@
 w);%0A
-        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     
@@ -3696,17 +3696,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -3742,23 +3742,16 @@
     spen
-Package
  = new S
@@ -3787,23 +3787,16 @@
     spen
-Package
 .initial
@@ -3830,15 +3830,8 @@
 spen
-Package
 .isF
@@ -4165,24 +4165,49 @@
 ture
-CurrentView(true
+Page(1, SpenSimpleSurfaceView.CAPTURE_ALL
 );%0A 
@@ -4250,16 +4250,17 @@
  bitmap%0A
+%0A
         

Rewrite spen sample