NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.

Search This Blog


Tuesday, 29 October 2013

Samsung Pen Package – Some Fundamentals

This is in continuation from my previous post. Samsung’s latest devices (Samsung Galaxy Note 3 and the Galaxy Note 10.1 (2014 edition) tablet) are unique with the introduction of the S Pen. The focus of the Samsung SmartApp Challenge that is currently open expects developers to use the pen and the look packages that are part of the Samsung Mobile SDK briefed in my earlier two posts.
In this post I would like to dive a bit deeper on the pen package and in a subsequent post on the look package. We will see what are they, as we go along.

S Pen is opening up a new horizon to Smartphones that support it. In fact I would not be surprised if Samsung soon open sources this project for encouraging wider adoption.

First, what is an S Pen?
Note that S Pen is an innovative stylus-type input device that comes with the Galaxy Note range of devices. It seems to have started off with the idea of making drawing or writing easy on smartphone where a finger touch doesn’t provide a great experience. It is not a capacitive stylus that typically phones came with but uses the Wacom’s EMR (Electro-Magnetic Resonance) patented Technology. For more on the technology behind the S Pen, you can read this article on the XDA Developers Forum or the ‘Android Authority’ post.
The tip of an S Pen allows for its usage in apps that need sensitivity to pressure applied and precision. The side buttons provide for press and release events based on which actions can be initiated.

In order to support developers to build apps for the S Pen, a pen package has been introduced as part of the Samsung Mobile SDK.

Next, what is the Pen package?
It is a package that allows developers to write applications that can take hand-written inputs. It allows the use of a pen, finger or any other kind of input tools or virtual pens to aid precise user input in the most natural way possible. It feels like you are actually writing or drawing on the device and would you call that a luxury? I am sure it is an understatement for thos who use their devices extensively for all day to day activities. J

The pen package enables to
  • ·      Draw using a pen/finger
  • ·      Change user preferences for pens, erasers and text
  • ·      Edit and save the inputs
  • ·      Undo or redo thus managing history of inputs
  • ·      Support both touch and hover events

A few words about the architecture of the pen package before we look at snippets of program using the package:

 This diagram is taken from the Samsung documentation.

As you can see the pen package is a layer over the Android platform using which many applications can be developed like the S-Note or S-Planner etc.

The pen core is organized into various packages with clear separation of responsibilities.  The relevant methods that can be used to develop apps have been made ‘public’ through what is shown as the ‘Pen API’.  It also provides a whole host of listeners that help in handling the touch, the hover, the zoom, the long press, the replay and the text change actions, to name a few.

The Engine provides the core of the package. It a way of managing the runtime objects and extensions to the Android view, line a canvas, text area, multi view, a context menu etc., that can take in inputs from the pen or a finger.

The Model module is literally the model of the pen package.  It gives APIs to save a pen document and retrieve it.  It could be a note that is to be saved and retrieved or a page with a set of strokes to be maintained in the history or even a image, a stroke or a text box with support for all text formatting – these could be persisted and retrieved.

The Setting module is one that helps in understand whether the device support s pen and manage the settings on the pen, eraser and text -  a small utility package.

The UI Control module consists of the various classes that help in managing the UI layouts and associated context menu.

The Plug-in module has a recognition class that allows for signature, text, shape and equation recognition that can be used for very interesting apps. The S pen Object Runtime allows developers to embed video clips or special text box as sandbox.

In order to start developing using the pen package, you need to download the Samsung Mobile SDK. It looks like that the programs need to be tested on a real device and are not supported on an emulator. I did try enabling on the emulator as given in the article on ‘Testing S Pen Apps on an Emulator’. However it seems that it works with what is called the S Pen SDK and not with the pen package, which is the focus of the developer challenge.

Now, unzip the Mobile SDK and you will find a folder by name libs. In libs, you find pen folder in which you have 2 jar files – “pen-v1.0.0.jar” and “sdk-v1.0.0.jar”.  Both of these need to be copied into your Android project libs folder and you are ready to create a pen based app.

You can start with a simple hello world program:

In the onCreate() method, you will have to check the support for the s pen feature first:

boolean isSpenFeatureEnabled = false;
Spen spenPackage = new Spen();
try {
      isSpenFeatureEnabled = spenPackage.isFeatureEnabled(Spen.DEVICE_PEN);
     } catch (SsdkUnsupportedException e) {
        //Handle the exception here   
     } catch (Exception e1) {
         Toast.makeText(mContext, "Cannot initialize Spen.",

Once this basic and essential check is done, let us create a SpenSurfaceView and add it to a relative layout:

RelativeLayout spenViewLayout =
            (RelativeLayout) findViewById(;
        mSpenSurfaceView = new SpenSurfaceView(mContext);
        if (mSpenSurfaceView == null) {
            Toast.makeText(mContext, "Cannot create new SpenView.",

The next thing you need to do is a SpenNoteDoc which holds a SpenPageDoc within it and voila, you are ready with the first Spen app J

Display display = getWindowManager().getDefaultDisplay();
Rect rect = new Rect();
try {
     mSpenNoteDoc =
     new SpenNoteDoc(mContext, rect.width(), rect.height());
     } catch (IOException e) {
        Toast.makeText(mContext, "Cannot create new NoteDoc.",
     } catch (Exception e) {

        mSpenPageDoc = mSpenNoteDoc.appendPage();
        mSpenSurfaceView.setPageDoc(mSpenPageDoc, true);

Note that when you create a SpenNotedoc, you make it as big as the display size that is available.
Then you create a SpenPageDoc and append it to SpenNoteDoc and could optional set parameters like the background and clear the history, if any.
And finally in the onDestroy() method ensure that you clean up after yourself as shown:
    protected void onDestroy() {

        if(mSpenSurfaceView != null) {
            mSpenSurfaceView = null;

        if(mSpenNoteDoc != null) {
            try {
            } catch (Exception e) {
            mSpenNoteDoc = null;

And you are done!!  With this you have created the first pen app with a canvas that takes the written input from a pen or a finger J

Tuesday, 8 October 2013

Samsung Mobile SDK – Introducing the Complete Package

As mentioned in some of my earlier posts there are a lot of individual SDKs that Samsung has provided over time to the developers. All of these SDKs have been trying to provide things over and above the basic features provided by Android SDKs and some of them with specific focus on Samsung mobiles. However, recently Samsung has put them all together and released the Samsung Mobile SDK! It packages everything into one making it, that much easier for developers to work with.

Considering the fact that Samsung’s flagship Samsung Mobile SDK is now available and the fact that the latest application challenge thrown open by Samsung expects you to use this SDK’s Pen and Look package innovatively on Samsung Galaxy Note 3 and their Tablet Galaxy Note 10.1 (2014 Edition), here I am exploring a bit more of the Samsung Mobile SDK itself.

Apart from the 2 powerful packages Pen and Look, there are 8 more packages have been bundled together into the SDK. All of these APIs are over the Android API layer simplifying some of the tasks for application development on the Samsung phones, which have a huge share in the Android market.

Now, would it be useful to know a little more about each of these packages? Why should we use these packages against the default Android Packages?

Let me explain in brief what each of the packages provides.

1.     Chord package, I have explained in great detail in an earlier post of mine (And hence will not be dwelling further on it here).
2.     Image Filter package provides a varied set of features for image manipulation. It allows you to apply 33 filter effects, some of them with level adjustments. It also allows you to apply a level of transparency to your images.
3.     Pen package is mainly to deal with hand written input. It could be a pen like the one that comes with Samsung Note or just a finger. Apart from recognizing the input, it provides a way to edit, move around text and manipulate the same and even convert the same into a digital format. Brushes, colours and brush sizes are selectable. My next post will be a detailed one on using pen package in a sample code.
4.     Visual View is another package that is part of the SDK that provides various animations based on the OpenGL ES 2.0. It provides some basic animation, transition animation, key-frame animation, advanced geometry functions and a perspective view too.
a.     This is based on Android GLSurfaceView, which is in turn based on SurfaceView. So any code written for Android SurfaceView can be used with Visual View.
b.     The default Android animations from scene to scene offer only a blinking effect. However this offers 40 transitions!! Certainly a developers delight.
c.      A Visual View Slide offers a perspective view with a 3D space, with properties like Opacity, Shadow, Rotation etc. Something that would bring ‘gamification’ easily to enterprise apps as well.

All of the above packages are available for all Samsung devices. However, the ones mentioned below are available only on some of them.
1.     Gesture package allows you to capture events generated by hand movements.
a.     This uses the gesture sensor available on some devices. This is supported only on Android 4.3 or above.
b.     Hand movement can be recognised as up, down, left or right along with the angle. Based on that, actions like opening a specific app or playing a specific song etc. can be initiated.
2.     Look package offers specialised functions and widgets for the android view system.
a.     Look supports an editor that can be used with the pen package called the ‘WritingBuddy’.
b.     It also provides a concept of an ‘AirButton’ that is more like a quick access menu or a context sensitive menu that can be initiated by using a pen.
c.      It provides another exciting feature called the ‘SmartClip’ that helps in capturing screenshots or meta data of text and URLs and crop screenshots.
d.     It also provides a ‘PointerIcon’ feature to be able to change the look of the pointer icon when a pen is used in any application.
3.     Media Control is a package that allows you to save and retrieve media files from any DNLA (Digital Network Living Alliance) device.
a.     It provides basic functions of stop, pause, resume, seek for both music and video files that are stored on any DNLA network device
b.     It can seamlessly present still-images to a remote screen
4.     Motion is a package that allows you to use the ‘call motion’ and ‘Pedometer’ features.
a.     The call motion is triggered when you see a message or a number and move the device to the ear!
b.     The Pedometer captures data around the walking of a person with the mobile. It captures the speed, the distance, the calories etc. based on the height and weight of the user.
5.     MutliWindow allows you to run multiple windows with various applications simultaneously. This becomes useful with larger screen devices and almost gives you the power of a laptop in your palm!
a.     Multi instance tray bar is provided to handle the multiple windows that may be opened at any point
b.     It allows you to pair windows in the typical split style
c.      It allows you to resize application windows, minimize them, move or exit from a Window
6.     Professional Audio adds a high performance audio processing logic. Musical instruments can be created with relative ease.

With all of these wonderful packages together, a lot of creativity can be unleashed and I am sure the Samsung Smart App Challenge encourages the Android Developer Community to use these in innovative ways.

Of all the packages, I am particularly interested in the possibilities with pen and look packages and so will be exploring how to write simple programs using the two of them in the next few posts.