com.samplekeyboard.LatinKeyboardView.java Source code

Java tutorial

Introduction

Here is the source code for com.samplekeyboard.LatinKeyboardView.java

Source

/*
 * Copyright (C) 2008-2009 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.samplekeyboard;

import android.content.Context;
import android.content.pm.ProviderInfo;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodSubtype;

public class LatinKeyboardView extends KeyboardView {
    private static final String DEBUG_TAG = "Velocity";
    private static final String TAG = "LatinKeyboardView";
    static final int KEYCODE_OPTIONS = -100;
    private int currentKey;
    SoftKeyboard mServiceKeyboard;
    // TODO: Move this into android.inputmethodservice.Keyboard
    static final int KEYCODE_LANGUAGE_SWITCH = -101;

    public LatinKeyboardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LatinKeyboardView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        mServiceKeyboard = new SoftKeyboard();
    }

    @Override
    protected boolean onLongPress(Key key) {
        if (key.codes[0] == Keyboard.KEYCODE_CANCEL) {
            getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null);
            return true;
        } else {
            Log.i(TAG, "key: " + key.codes[0]);
            return super.onLongPress(key);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    /*  public static int getDirection(){
    if(mIsDown && mIsLeft){
        return -1;
    }
    else if(mIsDown && mIsRight){
        return 1;
    }
    return 0;
      }*/
    /*  @Override
    public boolean onTouchEvent(MotionEvent event){
    int index = event.getActionIndex();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(index);
        
    switch(action) {
        case MotionEvent.ACTION_DOWN:
            if(mVelocityTracker == null) {
                // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                mVelocityTracker = VelocityTracker.obtain();
            }
            else {
                // Reset the velocity tracker back to its initial state.
                mVelocityTracker.clear();
            }
            // Add a user's movement to the tracker.
            mVelocityTracker.addMovement(event);
            break;
        case MotionEvent.ACTION_MOVE:
            mVelocityTracker.addMovement(event);
            // When you want to determine the velocity, call
            // computeCurrentVelocity(). Then call getXVelocity()
            // and getYVelocity() to retrieve the velocity for each pointer ID.
            mVelocityTracker.computeCurrentVelocity(1000);
            // Log velocity of pixels per second
            // Best practice to use VelocityTrackerCompat where possible.
            movementTracker++;
            Log.d("", "getX: " +
                    event.getX());
            Log.d("", "getY: " +
                    event.getY());
            Log.d("", "POINTER count: "+ event.getPointerCount());
            Log.d("", "X velocity: " +
                    VelocityTrackerCompat.getXVelocity(mVelocityTracker,
                            pointerId));
            Log.d("", "Y velocity: " +
                    VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                            pointerId));
        
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // Return a VelocityTracker object back to be re-used by others.
        
        
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        
            break;
    }
    super.onTouchEvent(event);
    return false;
    }*/

    void setSubtypeOnSpaceKey(final InputMethodSubtype subtype) {
        final LatinKeyboard keyboard = (LatinKeyboard) getKeyboard();
        keyboard.setSpaceIcon(getResources().getDrawable(subtype.getIconResId()));
        invalidateAllKeys();
    }

    /*  public void setCharCount(int charCount) {
    this.charCount = charCount;
      }*/
}