mirror of
https://github.com/yuliskov/LeanKeyboard.git
synced 2026-06-11 09:04:17 -04:00
refactor
This commit is contained in:
@@ -183,7 +183,7 @@ public class LeanbackKeyboardContainer {
|
||||
mVoiceButtonView = (RecognizerView) mRootView.findViewById(R.id.voice);
|
||||
mActionButtonView = (Button) mRootView.findViewById(R.id.enter);
|
||||
mSelector = mRootView.findViewById(R.id.selector);
|
||||
mSelectorAnimation = new LeanbackKeyboardContainer.ScaleAnimation((FrameLayout) mSelector);
|
||||
mSelectorAnimation = new ScaleAnimation((FrameLayout) mSelector);
|
||||
mOverestimate = mContext.getResources().getFraction(R.fraction.focused_scale, 1, 1);
|
||||
final float scale = context.getResources().getFraction(R.fraction.clicked_scale, 1, 1);
|
||||
mClickAnimDur = context.getResources().getInteger(R.integer.clicked_anim_duration);
|
||||
@@ -217,7 +217,7 @@ public class LeanbackKeyboardContainer {
|
||||
initKeyboards();
|
||||
}
|
||||
|
||||
private void configureFocus(LeanbackKeyboardContainer.KeyFocus focus, Rect rect, int index, int type) {
|
||||
private void configureFocus(KeyFocus focus, Rect rect, int index, int type) {
|
||||
focus.type = type;
|
||||
focus.index = index;
|
||||
focus.rect.set(rect);
|
||||
@@ -231,7 +231,7 @@ public class LeanbackKeyboardContainer {
|
||||
* @param key {@link Key}
|
||||
* @param type {@link KeyFocus#type} constant
|
||||
*/
|
||||
private void configureFocus(LeanbackKeyboardContainer.KeyFocus focus, Rect rect, int index, Key key, int type) {
|
||||
private void configureFocus(KeyFocus focus, Rect rect, int index, Key key, int type) {
|
||||
focus.type = type;
|
||||
if (key != null) {
|
||||
if (key.codes != null) {
|
||||
@@ -435,7 +435,7 @@ public class LeanbackKeyboardContainer {
|
||||
* @param forceFocusChange force focus
|
||||
* @param animate animate transition
|
||||
*/
|
||||
private void setKbFocus(final LeanbackKeyboardContainer.KeyFocus focus, final boolean forceFocusChange, final boolean animate) {
|
||||
private void setKbFocus(final KeyFocus focus, final boolean forceFocusChange, final boolean animate) {
|
||||
boolean clicked = true;
|
||||
if (!focus.equals(mCurrKeyInfo) || forceFocusChange) {
|
||||
LeanbackKeyboardView prevView = mPrevView;
|
||||
@@ -968,18 +968,21 @@ public class LeanbackKeyboardContainer {
|
||||
mVoiceButtonView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
public void setDismissListener(LeanbackKeyboardContainer.DismissListener listener) {
|
||||
public void setDismissListener(DismissListener listener) {
|
||||
mDismissListener = listener;
|
||||
}
|
||||
|
||||
public void setFocus(LeanbackKeyboardContainer.KeyFocus focus) {
|
||||
public void setFocus(KeyFocus focus) {
|
||||
setKbFocus(focus, false, true);
|
||||
}
|
||||
|
||||
public void setFocus(LeanbackKeyboardContainer.KeyFocus focus, final boolean animate) {
|
||||
public void setFocus(KeyFocus focus, final boolean animate) {
|
||||
setKbFocus(focus, false, animate);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: Selection animation when moving from one button to another
|
||||
*/
|
||||
public void setSelectorToFocus(Rect rect, boolean overestimateWidth, boolean overestimateHeight, boolean animate) {
|
||||
if (mSelector.getWidth() != 0 && mSelector.getHeight() != 0 && rect.width() != 0 && rect.height() != 0) {
|
||||
final float width = (float) rect.width();
|
||||
@@ -996,9 +999,11 @@ public class LeanbackKeyboardContainer {
|
||||
|
||||
float deltaY = heightOver;
|
||||
float deltaX = widthOver;
|
||||
if ((double) (Math.max(widthOver, heightOver) / Math.min(widthOver, heightOver)) < 1.1D) {
|
||||
deltaY = Math.max(widthOver, heightOver);
|
||||
deltaX = deltaY;
|
||||
float maxDelta = Math.max(widthOver, heightOver);
|
||||
float minDelta = Math.min(widthOver, heightOver);
|
||||
if ((double) (maxDelta / minDelta) < 1.1D) {
|
||||
deltaY = maxDelta;
|
||||
deltaX = maxDelta;
|
||||
}
|
||||
|
||||
final float x = rect.exactCenterX() - deltaX / 2.0F;
|
||||
|
||||
@@ -79,9 +79,9 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
|
||||
|
||||
}
|
||||
};
|
||||
mTouchEventListener = new LeanbackKeyboardController.TouchEventListener();
|
||||
mDownFocus = new LeanbackKeyboardContainer.KeyFocus();
|
||||
mTempFocus = new LeanbackKeyboardContainer.KeyFocus();
|
||||
mTouchEventListener = new TouchEventListener();
|
||||
mDownFocus = new KeyFocus();
|
||||
mTempFocus = new KeyFocus();
|
||||
mKeyChangeHistory = new ArrayList<>(11);
|
||||
mTempPoint = new PointF();
|
||||
mKeyDownReceived = false;
|
||||
@@ -142,10 +142,10 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
|
||||
}
|
||||
|
||||
private void clearKeyIfNecessary() {
|
||||
++this.mMoveCount;
|
||||
if (this.mMoveCount >= 3) {
|
||||
this.mMoveCount = 0;
|
||||
this.mKeyDownKeyFocus = null;
|
||||
++mMoveCount;
|
||||
if (mMoveCount >= 3) {
|
||||
mMoveCount = 0;
|
||||
mKeyDownKeyFocus = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -158,7 +158,7 @@ public class LeanbackKeyboardController implements LeanbackKeyboardContainer.Voi
|
||||
* NOTE: Where all magic happens. Input from virtual kbd is processed here.
|
||||
* @param focus current key
|
||||
*/
|
||||
private void commitKey(LeanbackKeyboardContainer.KeyFocus focus) {
|
||||
private void commitKey(KeyFocus focus) {
|
||||
if (mContainer != null && focus != null) {
|
||||
switch (focus.type) {
|
||||
case KeyFocus.TYPE_VOICE:
|
||||
|
||||
@@ -14,11 +14,13 @@ import android.graphics.Typeface;
|
||||
import android.inputmethodservice.Keyboard;
|
||||
import android.inputmethodservice.Keyboard.Key;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import com.liskovsoft.leankeyboard.utils.LeanKeySettings;
|
||||
import com.liskovsoft.leankeykeyboard.R;
|
||||
|
||||
import java.util.Iterator;
|
||||
@@ -209,12 +211,19 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
int iconWidth = key.width; // originally used key.icon.getIntrinsicWidth();
|
||||
int iconHeight = key.height; // originally used key.icon.getIntrinsicHeight();
|
||||
|
||||
if (key.width == key.height) {
|
||||
if (key.width == key.height) { // square key proper fit
|
||||
int newSize = Math.round(key.width * mSquareIconScaleFactor);
|
||||
iconWidth = newSize;
|
||||
iconHeight = newSize;
|
||||
}
|
||||
|
||||
if (key.codes[0] == ASCII_SPACE && LeanKeySettings.instance(getContext()).getEnlargeKeyboard()) {
|
||||
// space fix for large interface
|
||||
float gap = getResources().getDimension(R.dimen.keyboard_horizontal_gap);
|
||||
float gapDelta = (gap * 1.3f) - gap;
|
||||
iconWidth -= gapDelta * (ASCII_PERIOD_LEN - 1);
|
||||
}
|
||||
|
||||
int dx = (key.width - padding.left - padding.right - iconWidth) / 2 + padding.left;
|
||||
int dy = (key.height - padding.top - padding.bottom - iconHeight) / 2 + padding.top;
|
||||
|
||||
@@ -255,6 +264,7 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
|
||||
image.setImageAlpha(opacity);
|
||||
image.setVisibility(View.VISIBLE);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -300,7 +310,6 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
Key key = iterator.next();
|
||||
mKeys[i] = new KeyHolder(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean dismissMiniKeyboard() {
|
||||
@@ -506,7 +515,7 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
}
|
||||
|
||||
/**
|
||||
* Move focus to the key specified by index
|
||||
* NOTE: Increase size of currently focused or clicked key
|
||||
* @param index index of the key
|
||||
* @param clicked key state
|
||||
* @param showFocusScale increase size
|
||||
@@ -535,8 +544,8 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
|
||||
if (mCurrentFocusView != null) {
|
||||
mCurrentFocusView.animate()
|
||||
.scaleX(1.0F)
|
||||
.scaleY(1.0F)
|
||||
.scaleX(scale)
|
||||
.scaleY(scale)
|
||||
.setInterpolator(LeanbackKeyboardContainer.sMovementInterpolator)
|
||||
.setStartDelay(mUnfocusStartDelay);
|
||||
|
||||
@@ -558,7 +567,7 @@ public class LeanbackKeyboardView extends FrameLayout {
|
||||
.scaleX(scale)
|
||||
.scaleY(scale)
|
||||
.setInterpolator(LeanbackKeyboardContainer.sMovementInterpolator)
|
||||
.setDuration((long) mClickAnimDur)
|
||||
.setDuration(mClickAnimDur)
|
||||
.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package com.liskovsoft.leankeyboard.keyboard.android.leanback.ime;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.text.InputType;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import com.liskovsoft.leankeyboard.keyboard.leanback.ime.LeanbackImeService;
|
||||
|
||||
public class LeanbackUtils {
|
||||
private static final int ACCESSIBILITY_DELAY_MS = 250;
|
||||
private static final String EDITOR_LABEL = "label";
|
||||
private static final Handler sAccessibilityHandler = new Handler();
|
||||
private static final String TAG = LeanbackUtils.class.getSimpleName();
|
||||
|
||||
public static int getImeAction(EditorInfo info) {
|
||||
return info.imeOptions & (EditorInfo.IME_FLAG_NO_ENTER_ACTION | EditorInfo.IME_MASK_ACTION);
|
||||
@@ -106,4 +112,27 @@ public class LeanbackUtils {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static DisplayMetrics createMetricsFrom(Context context, float factor) {
|
||||
DisplayMetrics metrics = null;
|
||||
Object service = context.getSystemService(Context.WINDOW_SERVICE);
|
||||
|
||||
if (service instanceof WindowManager) {
|
||||
WindowManager manager = (WindowManager) service;
|
||||
metrics = new DisplayMetrics();
|
||||
manager.getDefaultDisplay().getMetrics(metrics);
|
||||
Log.d(TAG, metrics.toString());
|
||||
|
||||
// new values
|
||||
metrics.density *= factor;
|
||||
metrics.densityDpi *= factor;
|
||||
metrics.heightPixels *= factor;
|
||||
metrics.widthPixels *= factor;
|
||||
metrics.scaledDensity *= factor;
|
||||
metrics.xdpi *= factor;
|
||||
metrics.ydpi *= factor;
|
||||
}
|
||||
|
||||
return metrics;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,19 @@ package com.liskovsoft.leankeyboard.keyboard.leanback.ime;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.inputmethodservice.InputMethodService;
|
||||
import android.os.Build.VERSION;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.InputDevice;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.view.inputmethod.CompletionInfo;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
@@ -64,6 +67,7 @@ public class LeanbackImeService extends InputMethodService {
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
//setupDensity();
|
||||
super.onCreate();
|
||||
|
||||
Log.d(TAG, "onCreate");
|
||||
@@ -71,6 +75,16 @@ public class LeanbackImeService extends InputMethodService {
|
||||
initSettings();
|
||||
}
|
||||
|
||||
private void setupDensity() {
|
||||
if (LeanKeySettings.instance(this).getEnlargeKeyboard()) {
|
||||
DisplayMetrics metrics = LeanbackUtils.createMetricsFrom(this, 1.3f);
|
||||
|
||||
if (metrics != null) {
|
||||
getResources().getDisplayMetrics().setTo(metrics);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void initSettings() {
|
||||
mForceShowKbd = LeanKeySettings.instance(this).getForceShowKeyboard();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ public class RestartServiceReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
sendMessageToService(context);
|
||||
//restartService(context);
|
||||
}
|
||||
|
||||
private void sendMessageToService(Context context) {
|
||||
@@ -20,4 +21,8 @@ public class RestartServiceReceiver extends BroadcastReceiver {
|
||||
intent.putExtra(LeanbackImeService.COMMAND_RESTART, true);
|
||||
context.startService(intent);
|
||||
}
|
||||
|
||||
private void restartService(Context context) {
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ public class MiscFragment extends BaseSettingsFragment {
|
||||
|
||||
mPrefs = LeanKeySettings.instance(getActivity());
|
||||
addCheckedAction(R.string.keep_on_screen, R.string.keep_on_screen_desc, mPrefs::getForceShowKeyboard, mPrefs::setForceShowKeyboard);
|
||||
addCheckedAction(R.string.increase_kbd_size, R.string.increase_kbd_size_desc, mPrefs::getEnlargeKeyboard, mPrefs::setEnlargeKeyboard);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -9,6 +9,7 @@ public final class LeanKeySettings {
|
||||
private static final String BOOTSTRAP_SELECTED_LANGUAGE = "bootstrapSelectedLanguage";
|
||||
private static final String APP_KEYBOARD_INDEX = "appKeyboardIndex";
|
||||
private static final String FORCE_SHOW_KEYBOARD = "forceShowKeyboard";
|
||||
private static final String ENLARGE_KEYBOARD = "enlargeKeyboard";
|
||||
private static LeanKeySettings sInstance;
|
||||
private final Context mContext;
|
||||
private SharedPreferences mPrefs;
|
||||
@@ -63,4 +64,14 @@ public final class LeanKeySettings {
|
||||
.putBoolean(FORCE_SHOW_KEYBOARD, force)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public boolean getEnlargeKeyboard() {
|
||||
return mPrefs.getBoolean(ENLARGE_KEYBOARD, false);
|
||||
}
|
||||
|
||||
public void setEnlargeKeyboard(boolean enlarge) {
|
||||
mPrefs.edit()
|
||||
.putBoolean(ENLARGE_KEYBOARD, enlarge)
|
||||
.apply();
|
||||
}
|
||||
}
|
||||
|
||||
5
leankeykeyboard/src/main/res/layout/selector_test.xml
Normal file
5
leankeykeyboard/src/main/res/layout/selector_test.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout android:id="@+id/selector" android:clipChildren="false" android:layout_width="@dimen/selector_size" android:layout_height="@dimen/selector_size"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<ImageView android:id="@+id/key_selector" android:background="@color/blue_cyan" android:clipChildren="false" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerInside" />
|
||||
</FrameLayout>
|
||||
@@ -31,4 +31,6 @@
|
||||
<string name="keep_on_screen">Auf dem Display einblenden</string>
|
||||
<string name="misc">Andere</string>
|
||||
<string name="misc_desc">Andere Einstellungen</string>
|
||||
<string name="increase_kbd_size">Vergrößern Sie</string>
|
||||
<string name="increase_kbd_size_desc">Vergrößern Sie die Tastatur</string>
|
||||
</resources>
|
||||
|
||||
@@ -33,4 +33,6 @@
|
||||
<string name="keep_on_screen_desc">Показывать на экране вместе физической</string>
|
||||
<string name="misc">Разное</string>
|
||||
<string name="misc_desc">Разные настройки</string>
|
||||
<string name="increase_kbd_size">Увеличить</string>
|
||||
<string name="increase_kbd_size_desc">Увеличить клавиатуру</string>
|
||||
</resources>
|
||||
|
||||
@@ -44,4 +44,6 @@
|
||||
<string name="misc_desc">Misc Settings</string>
|
||||
<string name="keep_on_screen">Keep on Screen</string>
|
||||
<string name="keyboardview_keycode_clipboard">Clipboard</string>
|
||||
<string name="increase_kbd_size">Enlarge keyboard</string>
|
||||
<string name="increase_kbd_size_desc">Increase keyboard size</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user