Android Open Source - translationKeyboard Compose Base






From Project

Back to project page translationKeyboard.

License

The source code is released under:

GNU General Public License

If you think the Android project translationKeyboard listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011 Darren Salt/*from  w  w  w .  j av  a 2s.c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "Licence"); you may
 * not use this file except in compliance with the Licence. You may obtain
 * a copy of the Licence at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * Licence for the specific language governing permissions and limitations
 * under the Licence.
 */

package org.distantshoresmedia.translationkeyboard;

import android.view.inputmethod.EditorInfo;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

interface ComposeSequencing {
    public void onText(CharSequence text);
    public void updateShiftKeyState(EditorInfo attr);
    public EditorInfo getCurrentInputEditorInfo();
}

public abstract class ComposeBase {
    private static final String TAG = "HK/ComposeBase";
    
    protected static final Map<String, String> mMap =
      new HashMap<String, String>();

    protected static final Set<String> mPrefixes =
      new HashSet<String>();

    protected static String get(String key) {
        if (key == null || key.length() == 0) {
            return null;
        }
        //Log.i(TAG, "ComposeBase get, key=" + showString(key) + " result=" + mMap.get(key));
        return mMap.get(key);
    }

    private static String showString(String in) {
        // TODO Auto-generated method stub
        StringBuilder out = new StringBuilder(in);
        out.append("{");
        for (int i = 0; i < in.length(); ++i) {
            if (i > 0) out.append(",");
            out.append((int) in.charAt(i));
        }
        out.append("}");
        return out.toString();
    }

    private static boolean isValid(String partialKey) {
        if (partialKey == null || partialKey.length() == 0) {
            return false;
        }
        return mPrefixes.contains(partialKey);
    }

    protected static void put(String key, String value) {
      mMap.put(key, value);
      for (int i = 1; i < key.length(); ++i) {
        mPrefixes.add(key.substring(0, i));
      }
    }

    protected StringBuilder composeBuffer = new StringBuilder(10);
    protected ComposeSequencing composeUser;

    protected void init(ComposeSequencing user) {
        clear();
        composeUser = user;
    }

    public void clear() {
        composeBuffer.setLength(0);
    }

    public void bufferKey(char code) {
      composeBuffer.append(code);
      //Log.i(TAG, "bufferKey code=" + (int) code + " => " + showString(composeBuffer.toString()));
    }

    // returns true if the compose sequence is valid but incomplete
    public String executeToString(int code) {
        KeyboardSwitcher ks = KeyboardSwitcher.getInstance();
        if (ks.getInputView().isShiftCaps()
                && ks.isAlphabetMode()
                && Character.isLowerCase(code)) {
            code = Character.toUpperCase(code);
        }
        bufferKey((char) code);
        composeUser.updateShiftKeyState(composeUser.getCurrentInputEditorInfo());

        String composed = get(composeBuffer.toString());
        if (composed != null) {
            // If we get here, we have a complete compose sequence
            return composed;
        } else if (!isValid(composeBuffer.toString())) {
            // If we get here, then the sequence typed isn't recognised
            return "";
        }
        return null;
    }

    public boolean execute(int code) {
        String composed = executeToString(code);
        if (composed != null) {
            clear();
            composeUser.onText(composed);
            return false;
        }
        return true;
    }

    public boolean execute(CharSequence sequence) {
        int i, len = sequence.length();
        boolean result = true;
        for (i = 0; i < len; ++i) {
            result = execute(sequence.charAt(i));
        }
        return result; // only last one matters
    }
}




Java Source Code List

org.distantshoresmedia.translationkeyboard.AutoDictionary.java
org.distantshoresmedia.translationkeyboard.AutoSummaryEditTextPreference.java
org.distantshoresmedia.translationkeyboard.AutoSummaryListPreference.java
org.distantshoresmedia.translationkeyboard.BinaryDictionary.java
org.distantshoresmedia.translationkeyboard.CandidateView.java
org.distantshoresmedia.translationkeyboard.ComposeBase.java
org.distantshoresmedia.translationkeyboard.ComposeSequence.java
org.distantshoresmedia.translationkeyboard.ContactsDictionary.java
org.distantshoresmedia.translationkeyboard.DeadAccentSequence.java
org.distantshoresmedia.translationkeyboard.Dictionary.java
org.distantshoresmedia.translationkeyboard.EditingUtil.java
org.distantshoresmedia.translationkeyboard.ExpandableDictionary.java
org.distantshoresmedia.translationkeyboard.GlobalKeyboardSettings.java
org.distantshoresmedia.translationkeyboard.InputLanguageSelection.java
org.distantshoresmedia.translationkeyboard.KeyDetector.java
org.distantshoresmedia.translationkeyboard.KeyboardSwitcher.java
org.distantshoresmedia.translationkeyboard.Keyboard.java
org.distantshoresmedia.translationkeyboard.LanguageSwitcher.java
org.distantshoresmedia.translationkeyboard.LatinIMEBackupAgent.java
org.distantshoresmedia.translationkeyboard.LatinIMEDebugSettings.java
org.distantshoresmedia.translationkeyboard.LatinIMESettings.java
org.distantshoresmedia.translationkeyboard.LatinIMEUtil.java
org.distantshoresmedia.translationkeyboard.LatinIME.java
org.distantshoresmedia.translationkeyboard.LatinImeLogger.java
org.distantshoresmedia.translationkeyboard.LatinKeyboardBaseView.java
org.distantshoresmedia.translationkeyboard.LatinKeyboardView.java
org.distantshoresmedia.translationkeyboard.LatinKeyboard.java
org.distantshoresmedia.translationkeyboard.Main.java
org.distantshoresmedia.translationkeyboard.MiniKeyboardKeyDetector.java
org.distantshoresmedia.translationkeyboard.ModifierKeyState.java
org.distantshoresmedia.translationkeyboard.NotificationReceiver.java
org.distantshoresmedia.translationkeyboard.PluginManager.java
org.distantshoresmedia.translationkeyboard.PointerTracker.java
org.distantshoresmedia.translationkeyboard.PrefScreenActions.java
org.distantshoresmedia.translationkeyboard.PrefScreenFeedback.java
org.distantshoresmedia.translationkeyboard.PrefScreenView.java
org.distantshoresmedia.translationkeyboard.ProximityKeyDetector.java
org.distantshoresmedia.translationkeyboard.SeekBarPreferenceString.java
org.distantshoresmedia.translationkeyboard.SeekBarPreference.java
org.distantshoresmedia.translationkeyboard.SharedPreferencesCompat.java
org.distantshoresmedia.translationkeyboard.Suggest.java
org.distantshoresmedia.translationkeyboard.SwipeTracker.java
org.distantshoresmedia.translationkeyboard.TextEntryState.java
org.distantshoresmedia.translationkeyboard.Tutorial.java
org.distantshoresmedia.translationkeyboard.UserBigramDictionary.java
org.distantshoresmedia.translationkeyboard.UserDictionary.java
org.distantshoresmedia.translationkeyboard.VibratePreference.java
org.distantshoresmedia.translationkeyboard.WordComposer.java