Split the main file into relevant classes
This commit is contained in:
@@ -3,7 +3,6 @@ package cfpi_fpsi.eu.titanboard;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.inputmethodservice.InputMethodService;
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
@@ -11,60 +10,16 @@ import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TitanboardIME extends InputMethodService {
|
||||
private HashMap<Integer, int[]> mKeymap;
|
||||
|
||||
private MetaKey mMetaKeyPressed = MetaKey.NONE;
|
||||
private MetaKey mMetaKeyLatched = MetaKey.NONE;
|
||||
private MetaKey mMetaKeyLocked = MetaKey.NONE;
|
||||
private boolean mMetaKeyUsed = false;
|
||||
private TitanboardKeymap mKeymap;
|
||||
private final TitanboardMetaKeyManager mMetaKeyManager = new TitanboardMetaKeyManager();
|
||||
|
||||
private LinearLayout mInputView;
|
||||
private Vibrator mVibrator;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
mKeymap = new HashMap<>();
|
||||
mVibrator = (Vibrator) getBaseContext().getSystemService(VIBRATOR_SERVICE);
|
||||
XmlPullParser parser = getResources().getXml(R.xml.keymap_titan);
|
||||
int code = 0, value = 0, valueAlt = 0;
|
||||
|
||||
try {
|
||||
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
|
||||
int type = parser.getEventType();
|
||||
if (type == XmlPullParser.START_TAG && parser.getName().equals("key")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
String attr = parser.getAttributeName(i);
|
||||
switch (attr) {
|
||||
case "code":
|
||||
code = Integer.parseInt(parser.getAttributeValue(i));
|
||||
break;
|
||||
case "value":
|
||||
value = Integer.parseInt(String.valueOf(parser.getAttributeValue(i)
|
||||
.codePointAt(0)));
|
||||
break;
|
||||
case "valueAlt":
|
||||
valueAlt = Integer.parseInt(String.valueOf(
|
||||
parser.getAttributeValue(i).codePointAt(0)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (type == XmlPullParser.END_TAG && parser.getName().equals("key")) {
|
||||
mKeymap.put(code, new int[]{value, valueAlt});
|
||||
}
|
||||
|
||||
parser.next();
|
||||
}
|
||||
} catch (XmlPullParserException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
mKeymap = new TitanboardKeymap(getResources().getXml(R.xml.keymap_titan));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,7 +36,7 @@ public class TitanboardIME extends InputMethodService {
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
updateMetaKeyStateDown(keyCode, event);
|
||||
mMetaKeyManager.updateMetaStateDown(keyCode, event);
|
||||
updateMetaStateTextView();
|
||||
|
||||
InputConnection conn = getCurrentInputConnection();
|
||||
@@ -97,7 +52,7 @@ public class TitanboardIME extends InputMethodService {
|
||||
return false;
|
||||
// If it's the backspace key, we remove one character.
|
||||
case KeyEvent.KEYCODE_DEL:
|
||||
adjustMetaKeyStateAfterKeypress();
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
if (conn.getSelectedText(0) == null) {
|
||||
conn.deleteSurroundingText(1, 0);
|
||||
return true;
|
||||
@@ -105,13 +60,13 @@ public class TitanboardIME extends InputMethodService {
|
||||
return false;
|
||||
case KeyEvent.KEYCODE_SPACE:
|
||||
conn.commitText(" ", 1);
|
||||
adjustMetaKeyStateAfterKeypress();
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
return true;
|
||||
default:
|
||||
String key = getKey(keyCode);
|
||||
if (key != null) {
|
||||
conn.commitText(getKey(keyCode), 1);
|
||||
adjustMetaKeyStateAfterKeypress();
|
||||
mMetaKeyManager.adjustMetaStateAfterKeypress();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -120,75 +75,11 @@ public class TitanboardIME extends InputMethodService {
|
||||
}
|
||||
|
||||
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||
updateMetaKeyStateUp(keyCode);
|
||||
mMetaKeyManager.updateMetaStateUp(keyCode);
|
||||
updateMetaStateTextView();
|
||||
return false;
|
||||
}
|
||||
|
||||
public void updateMetaKeyStateDown(int keyCode, KeyEvent event) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_ALT_LEFT:
|
||||
case KeyEvent.KEYCODE_ALT_RIGHT:
|
||||
mMetaKeyUsed = false;
|
||||
mMetaKeyPressed = MetaKey.ALT;
|
||||
mMetaKeyLatched = mMetaKeyLocked != MetaKey.NONE ? mMetaKeyLocked : mMetaKeyLatched;
|
||||
mMetaKeyLocked = event.getRepeatCount() > 5 ? MetaKey.ALT : MetaKey.NONE;
|
||||
// We vibrate once when we lock a meta key.
|
||||
if (event.getRepeatCount() == 6) {
|
||||
mVibrator.vibrate(70);
|
||||
}
|
||||
break;
|
||||
case KeyEvent.KEYCODE_SHIFT_LEFT:
|
||||
case KeyEvent.KEYCODE_SHIFT_RIGHT:
|
||||
mMetaKeyUsed = false;
|
||||
mMetaKeyPressed = MetaKey.SHIFT;
|
||||
mMetaKeyLatched = mMetaKeyLocked != MetaKey.NONE ? mMetaKeyLocked : mMetaKeyLatched;
|
||||
mMetaKeyLocked = event.getRepeatCount() > 5 ? MetaKey.SHIFT : MetaKey.NONE;
|
||||
// We vibrate once when we lock a meta key.
|
||||
if (event.getRepeatCount() == 6) {
|
||||
mVibrator.vibrate(70);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void updateMetaKeyStateUp(int keyCode) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_ALT_LEFT:
|
||||
case KeyEvent.KEYCODE_ALT_RIGHT:
|
||||
mMetaKeyPressed = MetaKey.NONE;
|
||||
if (mMetaKeyLatched == MetaKey.NONE && mMetaKeyLocked == MetaKey.NONE
|
||||
&& !mMetaKeyUsed) {
|
||||
mMetaKeyLatched = MetaKey.ALT;
|
||||
} else {
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
}
|
||||
break;
|
||||
case KeyEvent.KEYCODE_SHIFT_LEFT:
|
||||
case KeyEvent.KEYCODE_SHIFT_RIGHT:
|
||||
mMetaKeyPressed = MetaKey.NONE;
|
||||
if (mMetaKeyLatched == MetaKey.NONE && mMetaKeyLocked == MetaKey.NONE
|
||||
&& !mMetaKeyUsed) {
|
||||
mMetaKeyLatched = MetaKey.SHIFT;
|
||||
} else {
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void adjustMetaKeyStateAfterKeypress() {
|
||||
if (mMetaKeyLatched != MetaKey.NONE) {
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
} else if (mMetaKeyPressed != MetaKey.NONE) {
|
||||
mMetaKeyUsed = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateInputView() {
|
||||
return mInputView;
|
||||
@@ -196,64 +87,52 @@ public class TitanboardIME extends InputMethodService {
|
||||
|
||||
@Override
|
||||
public void onFinishInput() {
|
||||
mMetaKeyPressed = MetaKey.NONE;
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
mMetaKeyLocked = MetaKey.NONE;
|
||||
mMetaKeyManager.resetMetaKeyState();
|
||||
updateMetaStateTextView();
|
||||
mInputView.findViewById(R.id.extra_view).setVisibility(View.GONE);
|
||||
Button key = (Button) mInputView.findViewById(R.id.expand_button);
|
||||
Button key = mInputView.findViewById(R.id.expand_button);
|
||||
key.setText("▲");
|
||||
}
|
||||
|
||||
private String getKey(int keyCode) {
|
||||
int[] key = mKeymap.get(keyCode);
|
||||
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mMetaKeyPressed == MetaKey.ALT || mMetaKeyLatched == MetaKey.ALT
|
||||
|| mMetaKeyLocked == MetaKey.ALT) {
|
||||
return new String(Character.toChars(key[1]));
|
||||
} else if (mMetaKeyPressed == MetaKey.SHIFT || mMetaKeyLatched == MetaKey.SHIFT
|
||||
|| mMetaKeyLocked == MetaKey.SHIFT) {
|
||||
return new String(Character.toChars(key[0])).toUpperCase();
|
||||
if (mMetaKeyManager.getMetaKeyPressed() == TitanboardMetaKeyManager.MetaKey.ALT
|
||||
|| mMetaKeyManager.getMetaKeyLatched() == TitanboardMetaKeyManager.MetaKey.ALT
|
||||
|| mMetaKeyManager.getMetaKeyLocked() == TitanboardMetaKeyManager.MetaKey.ALT) {
|
||||
return mKeymap.getValueAlt(keyCode);
|
||||
} else if (mMetaKeyManager.getMetaKeyPressed() == TitanboardMetaKeyManager.MetaKey.SHIFT
|
||||
|| mMetaKeyManager.getMetaKeyLatched() == TitanboardMetaKeyManager.MetaKey.SHIFT
|
||||
|| mMetaKeyManager.getMetaKeyLocked() == TitanboardMetaKeyManager.MetaKey.SHIFT) {
|
||||
return mKeymap.getValueShift(keyCode);
|
||||
} else {
|
||||
return new String(Character.toChars(key[0]));
|
||||
return mKeymap.getValue(keyCode);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
private void updateMetaStateTextView() {
|
||||
TextView stateView = mInputView.findViewById(R.id.state);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
stateView.setTextAppearance(R.style.KeyboardState);
|
||||
}
|
||||
|
||||
if (mMetaKeyLatched == MetaKey.ALT) {
|
||||
if (mMetaKeyManager.getMetaKeyLatched() == TitanboardMetaKeyManager.MetaKey.ALT) {
|
||||
stateView.setText("#?");
|
||||
} else if (mMetaKeyLatched == MetaKey.SHIFT) {
|
||||
} else if (mMetaKeyManager.getMetaKeyLatched() == TitanboardMetaKeyManager.MetaKey.SHIFT) {
|
||||
stateView.setText("Aa");
|
||||
} else {
|
||||
stateView.setText("aa");
|
||||
}
|
||||
|
||||
if (mMetaKeyPressed == MetaKey.ALT) {
|
||||
if (mMetaKeyManager.getMetaKeyPressed() == TitanboardMetaKeyManager.MetaKey.ALT
|
||||
|| mMetaKeyManager.getMetaKeyLocked() == TitanboardMetaKeyManager.MetaKey.ALT) {
|
||||
stateView.setText("#!");
|
||||
} else if (mMetaKeyPressed == MetaKey.SHIFT) {
|
||||
} else if (mMetaKeyManager.getMetaKeyPressed() == TitanboardMetaKeyManager.MetaKey.SHIFT
|
||||
|| mMetaKeyManager.getMetaKeyLocked() == TitanboardMetaKeyManager.MetaKey.SHIFT) {
|
||||
stateView.setText("AA");
|
||||
}
|
||||
|
||||
if (mMetaKeyLocked == MetaKey.ALT) {
|
||||
stateView.setText("#!");
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
stateView.setTextAppearance(R.style.KeyboardStateLocked);
|
||||
}
|
||||
} else if (mMetaKeyLocked == MetaKey.SHIFT) {
|
||||
stateView.setText("AA");
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
stateView.setTextAppearance(R.style.KeyboardStateLocked);
|
||||
}
|
||||
if (mMetaKeyManager.getMetaKeyLocked() != TitanboardMetaKeyManager.MetaKey.NONE
|
||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
stateView.setTextAppearance(R.style.KeyboardStateLocked);
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
stateView.setTextAppearance(R.style.KeyboardState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,10 +155,4 @@ public class TitanboardIME extends InputMethodService {
|
||||
conn.commitText(key.getText(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
private enum MetaKey {
|
||||
ALT,
|
||||
NONE,
|
||||
SHIFT
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package cfpi_fpsi.eu.titanboard;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TitanboardKeymap extends HashMap<Integer, String[]> {
|
||||
public TitanboardKeymap(XmlPullParser parser) {
|
||||
int code = 0;
|
||||
String value = null, valueAlt = null;
|
||||
|
||||
try {
|
||||
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
|
||||
int type = parser.getEventType();
|
||||
if (type == XmlPullParser.START_TAG && parser.getName().equals("key")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
String attr = parser.getAttributeName(i);
|
||||
switch (attr) {
|
||||
case "code":
|
||||
code = Integer.parseInt(parser.getAttributeValue(i));
|
||||
break;
|
||||
case "value":
|
||||
value = parser.getAttributeValue(i);
|
||||
break;
|
||||
case "valueAlt":
|
||||
valueAlt = parser.getAttributeValue(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (type == XmlPullParser.END_TAG && parser.getName().equals("key")) {
|
||||
Log.d("STATE", "Keymap key: " + code + " " + value + " " + valueAlt);
|
||||
put(code, new String[]{value, valueAlt});
|
||||
}
|
||||
|
||||
parser.next();
|
||||
}
|
||||
} catch (XmlPullParserException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String getValue(int keyCode) {
|
||||
String[] vals = get(keyCode);
|
||||
if (vals != null) {
|
||||
return vals[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getValueAlt(int keyCode) {
|
||||
String[] vals = get(keyCode);
|
||||
if (vals != null) {
|
||||
return vals[1];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getValueShift(int keyCode) {
|
||||
String[] vals = get(keyCode);
|
||||
if (vals != null) {
|
||||
return vals[0].toUpperCase();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package cfpi_fpsi.eu.titanboard;
|
||||
|
||||
import android.view.KeyEvent;
|
||||
|
||||
public class TitanboardMetaKeyManager {
|
||||
private final int mRepeatCount = 5;
|
||||
private MetaKey mMetaKeyPressed = MetaKey.NONE;
|
||||
private MetaKey mMetaKeyLatched = MetaKey.NONE;
|
||||
private MetaKey mMetaKeyLocked = MetaKey.NONE;
|
||||
private boolean mMetaKeyUsed = false;
|
||||
|
||||
public void updateMetaStateDown(int keyCode, KeyEvent event) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_ALT_LEFT:
|
||||
case KeyEvent.KEYCODE_ALT_RIGHT:
|
||||
updateMetaKeyDown(MetaKey.ALT, event.getRepeatCount());
|
||||
break;
|
||||
case KeyEvent.KEYCODE_SHIFT_LEFT:
|
||||
case KeyEvent.KEYCODE_SHIFT_RIGHT:
|
||||
updateMetaKeyDown(MetaKey.SHIFT, event.getRepeatCount());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void updateMetaKeyDown(MetaKey key, int repeatCount) {
|
||||
// Perhaps this function should be renamed to something...
|
||||
mMetaKeyUsed = false;
|
||||
mMetaKeyPressed = key;
|
||||
mMetaKeyLatched = mMetaKeyLocked != MetaKey.NONE ? mMetaKeyLocked : mMetaKeyLatched;
|
||||
mMetaKeyLocked = repeatCount > mRepeatCount ? key : MetaKey.NONE;
|
||||
}
|
||||
|
||||
public void updateMetaStateUp(int keyCode) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_ALT_LEFT:
|
||||
case KeyEvent.KEYCODE_ALT_RIGHT:
|
||||
updateMetaKeyUp(MetaKey.ALT);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_SHIFT_LEFT:
|
||||
case KeyEvent.KEYCODE_SHIFT_RIGHT:
|
||||
updateMetaKeyUp(MetaKey.SHIFT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void updateMetaKeyUp(MetaKey key) {
|
||||
mMetaKeyPressed = MetaKey.NONE;
|
||||
if (mMetaKeyLatched == MetaKey.NONE && mMetaKeyLocked == MetaKey.NONE
|
||||
&& !mMetaKeyUsed) {
|
||||
mMetaKeyLatched = key;
|
||||
} else {
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
public void adjustMetaStateAfterKeypress() {
|
||||
if (mMetaKeyLatched != MetaKey.NONE) {
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
mMetaKeyUsed = false;
|
||||
} else if (mMetaKeyPressed != MetaKey.NONE) {
|
||||
mMetaKeyUsed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void resetMetaKeyState() {
|
||||
mMetaKeyPressed = MetaKey.NONE;
|
||||
mMetaKeyLatched = MetaKey.NONE;
|
||||
mMetaKeyLocked = MetaKey.NONE;
|
||||
mMetaKeyUsed = false;
|
||||
}
|
||||
|
||||
public MetaKey getMetaKeyPressed() {
|
||||
return mMetaKeyPressed;
|
||||
}
|
||||
|
||||
public MetaKey getMetaKeyLatched() {
|
||||
return mMetaKeyLatched;
|
||||
}
|
||||
|
||||
public MetaKey getMetaKeyLocked() {
|
||||
return mMetaKeyLocked;
|
||||
}
|
||||
|
||||
public enum MetaKey {
|
||||
ALT,
|
||||
NONE,
|
||||
SHIFT
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user