2017-06-11 19:24:09 +08:00
|
|
|
package io.neoterm.view;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.Gravity;
|
|
|
|
import android.view.HapticFeedbackConstants;
|
|
|
|
import android.view.View;
|
2017-06-17 14:09:16 +08:00
|
|
|
import android.view.ViewGroup;
|
2017-06-11 19:24:09 +08:00
|
|
|
import android.widget.Button;
|
2017-06-17 14:09:16 +08:00
|
|
|
import android.widget.HorizontalScrollView;
|
|
|
|
import android.widget.LinearLayout;
|
2017-06-11 19:24:09 +08:00
|
|
|
import android.widget.ToggleButton;
|
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
import java.io.File;
|
2017-06-14 11:43:39 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
import io.neoterm.customize.NeoTermPath;
|
2017-06-17 15:11:27 +08:00
|
|
|
import io.neoterm.customize.font.FontManager;
|
2017-06-17 09:28:56 +08:00
|
|
|
import io.neoterm.customize.shortcut.ShortcutConfig;
|
|
|
|
import io.neoterm.customize.shortcut.ShortcutConfigParser;
|
|
|
|
import io.neoterm.utils.FileUtils;
|
|
|
|
import io.neoterm.view.eks.ControlButton;
|
|
|
|
import io.neoterm.view.eks.ExtraButton;
|
|
|
|
import io.neoterm.view.eks.StatedControlButton;
|
2017-06-11 19:24:09 +08:00
|
|
|
|
2017-06-23 00:00:26 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_ARROW_DOWN;
|
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_ARROW_LEFT;
|
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_ARROW_RIGHT;
|
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_ARROW_UP;
|
2017-06-17 14:09:16 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_CTRL;
|
2017-06-23 00:00:26 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_END;
|
2017-06-17 14:09:16 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_ESC;
|
2017-06-23 00:00:26 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_HOME;
|
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_PAGE_DOWN;
|
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_PAGE_UP;
|
2017-06-17 14:09:16 +08:00
|
|
|
import static io.neoterm.view.eks.ExtraButton.KEY_TAB;
|
|
|
|
|
2017-06-11 19:24:09 +08:00
|
|
|
/**
|
|
|
|
* A view showing extra keys (such as Escape, Ctrl, Alt) not normally available on an Android soft
|
|
|
|
* keyboard.
|
|
|
|
*/
|
2017-06-17 15:11:27 +08:00
|
|
|
public final class ExtraKeysView extends LinearLayout {
|
2017-06-14 11:43:39 +08:00
|
|
|
|
2017-06-23 00:00:26 +08:00
|
|
|
public static final StatedControlButton CTRL = new StatedControlButton(KEY_CTRL);
|
2017-06-17 09:28:56 +08:00
|
|
|
public static final ControlButton ESC = new ControlButton(KEY_ESC);
|
|
|
|
public static final ControlButton TAB = new ControlButton(KEY_TAB);
|
2017-06-23 00:00:26 +08:00
|
|
|
public static final ControlButton PAGE_UP = new ControlButton(KEY_PAGE_UP);
|
|
|
|
public static final ControlButton PAGE_DOWN = new ControlButton(KEY_PAGE_DOWN);
|
|
|
|
public static final ControlButton HOME = new ControlButton(KEY_HOME);
|
|
|
|
public static final ControlButton END = new ControlButton(KEY_END);
|
|
|
|
public static final ControlButton ARROW_UP = new ControlButton(KEY_ARROW_UP);
|
|
|
|
public static final ControlButton ARROW_DOWN = new ControlButton(KEY_ARROW_DOWN);
|
|
|
|
public static final ControlButton ARROW_LEFT = new ControlButton(KEY_ARROW_LEFT);
|
|
|
|
public static final ControlButton ARROW_RIGHT = new ControlButton(KEY_ARROW_RIGHT);
|
2017-06-14 11:43:39 +08:00
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
public static final String DEFAULT_FILE_CONTENT = "version " + ShortcutConfigParser.PARSER_VERSION + "\n" +
|
|
|
|
"program default\n" +
|
|
|
|
"define - false\n" +
|
|
|
|
"define / false\n" +
|
|
|
|
"define | false\n";
|
2017-06-11 19:24:09 +08:00
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
public static final int NORMAL_TEXT_COLOR = 0xFFFFFFFF;
|
2017-06-18 11:22:50 +08:00
|
|
|
public static final int SELECTED_TEXT_COLOR = 0xFF80DEEA;
|
2017-06-17 14:09:16 +08:00
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
private List<ExtraButton> builtinExtraKeys;
|
|
|
|
private List<ExtraButton> userDefinedExtraKeys;
|
2017-06-14 11:43:39 +08:00
|
|
|
|
2017-06-17 15:11:27 +08:00
|
|
|
private LinearLayout lineOne;
|
|
|
|
private LinearLayout lineTwo;
|
|
|
|
|
2017-06-17 14:09:16 +08:00
|
|
|
|
2017-06-11 19:24:09 +08:00
|
|
|
public ExtraKeysView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
2017-06-17 15:11:27 +08:00
|
|
|
setGravity(Gravity.TOP);
|
|
|
|
setOrientation(VERTICAL);
|
|
|
|
HorizontalScrollView scrollOne = new HorizontalScrollView(context);
|
|
|
|
HorizontalScrollView scrollTwo = new HorizontalScrollView(context);
|
2017-06-17 09:28:56 +08:00
|
|
|
loadDefaultBuiltinExtraKeys();
|
|
|
|
loadDefaultUserDefinedExtraKeys();
|
2017-06-17 15:11:27 +08:00
|
|
|
lineOne = initLine(scrollOne);
|
|
|
|
lineTwo = initLine(scrollTwo);
|
|
|
|
addView(scrollOne);
|
|
|
|
addView(scrollTwo);
|
2017-06-17 09:28:56 +08:00
|
|
|
updateButtons();
|
2017-06-11 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 15:11:27 +08:00
|
|
|
private LinearLayout initLine(HorizontalScrollView scroll) {
|
|
|
|
scroll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f));
|
|
|
|
scroll.setFillViewport(true);
|
|
|
|
scroll.setHorizontalScrollBarEnabled(false);
|
|
|
|
LinearLayout line = new LinearLayout(getContext());
|
|
|
|
line.setGravity(Gravity.START);
|
|
|
|
line.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
|
scroll.addView(line);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2017-06-11 19:24:09 +08:00
|
|
|
public boolean readControlButton() {
|
2017-06-14 11:43:39 +08:00
|
|
|
return CTRL.readState();
|
2017-06-11 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean readAltButton() {
|
2017-06-17 09:28:56 +08:00
|
|
|
return false;
|
2017-06-14 11:43:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 11:22:50 +08:00
|
|
|
public void addUserDefinedButton(ExtraButton button) {
|
|
|
|
addButton(userDefinedExtraKeys, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addBuiltinButton(ExtraButton button) {
|
|
|
|
addButton(builtinExtraKeys, button);
|
2017-06-14 11:43:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 11:22:50 +08:00
|
|
|
private void addButton(List<ExtraButton> buttons, ExtraButton button) {
|
|
|
|
if (!buttons.contains(button)) {
|
|
|
|
buttons.add(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearUserDefinedButton() {
|
2017-06-17 09:28:56 +08:00
|
|
|
userDefinedExtraKeys.clear();
|
2017-06-11 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
public void loadDefaultUserDefinedExtraKeys() {
|
2017-06-17 15:11:27 +08:00
|
|
|
userDefinedExtraKeys = new ArrayList<>(7);
|
2017-06-17 09:28:56 +08:00
|
|
|
File defaultFile = new File(NeoTermPath.EKS_DEFAULT_FILE);
|
|
|
|
if (!defaultFile.exists()) {
|
|
|
|
generateDefaultFile(defaultFile);
|
|
|
|
}
|
|
|
|
|
2017-06-18 11:22:50 +08:00
|
|
|
clearUserDefinedButton();
|
2017-06-17 09:28:56 +08:00
|
|
|
try {
|
|
|
|
ShortcutConfigParser parser = new ShortcutConfigParser();
|
|
|
|
parser.setInput(defaultFile);
|
|
|
|
ShortcutConfig config = parser.parse();
|
|
|
|
userDefinedExtraKeys.addAll(config.getShortcutKeys());
|
2017-06-17 15:11:27 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2017-06-17 09:28:56 +08:00
|
|
|
}
|
2017-06-14 11:43:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
private void generateDefaultFile(File defaultFile) {
|
|
|
|
FileUtils.INSTANCE.writeFile(defaultFile, DEFAULT_FILE_CONTENT.getBytes());
|
2017-06-14 11:43:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-17 09:28:56 +08:00
|
|
|
void loadDefaultBuiltinExtraKeys() {
|
2017-06-17 15:11:27 +08:00
|
|
|
builtinExtraKeys = new ArrayList<>(7);
|
2017-06-17 09:28:56 +08:00
|
|
|
builtinExtraKeys.clear();
|
|
|
|
builtinExtraKeys.add(ESC);
|
|
|
|
builtinExtraKeys.add(CTRL);
|
|
|
|
builtinExtraKeys.add(TAB);
|
|
|
|
builtinExtraKeys.add(ARROW_UP);
|
|
|
|
builtinExtraKeys.add(ARROW_DOWN);
|
2017-06-17 15:11:27 +08:00
|
|
|
builtinExtraKeys.add(ARROW_LEFT);
|
|
|
|
builtinExtraKeys.add(ARROW_RIGHT);
|
2017-06-23 00:00:26 +08:00
|
|
|
builtinExtraKeys.add(HOME);
|
|
|
|
builtinExtraKeys.add(END);
|
|
|
|
builtinExtraKeys.add(PAGE_UP);
|
|
|
|
builtinExtraKeys.add(PAGE_DOWN);
|
2017-06-14 11:43:39 +08:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:47:36 +08:00
|
|
|
public void updateButtons() {
|
2017-06-17 15:11:27 +08:00
|
|
|
lineOne.removeAllViews();
|
|
|
|
lineTwo.removeAllViews();
|
2017-06-17 14:09:16 +08:00
|
|
|
for (final ExtraButton extraButton : userDefinedExtraKeys) {
|
2017-06-17 15:11:27 +08:00
|
|
|
addExtraButton(lineOne, extraButton);
|
|
|
|
}
|
|
|
|
for (final ExtraButton extraButton : builtinExtraKeys) {
|
|
|
|
addExtraButton(lineTwo, extraButton);
|
2017-06-17 14:09:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 15:11:27 +08:00
|
|
|
private void addExtraButton(LinearLayout contentView, final ExtraButton extraButton) {
|
2017-06-17 14:09:16 +08:00
|
|
|
final Button button;
|
|
|
|
if (extraButton instanceof StatedControlButton) {
|
|
|
|
StatedControlButton btn = ((StatedControlButton) extraButton);
|
|
|
|
button = btn.toggleButton = new ToggleButton(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
|
|
|
button.setClickable(true);
|
2017-06-18 11:22:50 +08:00
|
|
|
if (btn.initState) {
|
|
|
|
btn.toggleButton.setChecked(true);
|
|
|
|
btn.toggleButton.setTextColor(SELECTED_TEXT_COLOR);
|
|
|
|
}
|
2017-06-17 14:09:16 +08:00
|
|
|
} else {
|
|
|
|
button = new Button(getContext(), null, android.R.attr.buttonBarButtonStyle);
|
2017-06-11 19:24:09 +08:00
|
|
|
}
|
2017-06-17 15:11:27 +08:00
|
|
|
|
|
|
|
button.setTypeface(FontManager.INSTANCE.getDefaultFont());
|
2017-06-17 14:09:16 +08:00
|
|
|
button.setText(extraButton.buttonText);
|
|
|
|
button.setTextColor(NORMAL_TEXT_COLOR);
|
|
|
|
button.setAllCaps(false);
|
|
|
|
|
|
|
|
button.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
button.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
|
|
|
|
View root = getRootView();
|
|
|
|
extraButton.onClick(root);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
contentView.addView(button);
|
2017-06-11 19:24:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|