MultiTouch.java

/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.paint.touch;

/*
 * Generic  wrapper class for handling multitouch events,
 * if they are supported.
 */
public abstract class MultiTouch {

    private static MultiTouch multiTouch = getImplementation();
    protected static MultiTouchListener touchListener;

    protected abstract int getPointers();

    /**
     * Set multi-touch listener
     * @param listener
     */
    public static void setTouchListener(MultiTouchListener listener) {
        touchListener = listener;
    }

    /**
     * Get the amount of possible touch points
     * @return Amount of touch points. 1 if multi-touch is not available
     */
    public static int getMaxPointers() {
        return multiTouch != null ? multiTouch.getPointers() : 1;
    }

    /**
     * Loads up the isolated implementation class
     * @return MultiTouch Returns multi-touch implementation or null, if multi-touch is not supported
     */
    private static MultiTouch getImplementation() {
        MultiTouch implementation = null;
        try {
            Class.forName("com.nokia.mid.ui.multipointtouch.MultipointTouchListener");

            Class c = Class.forName("com.nokia.example.paint.touch.MultiTouchImpl");
            implementation = (MultiTouch) (c.newInstance());
        }
        catch (Exception e) {
            // Multi-touch not available - using single-touch
        }
        return implementation;
    }
}