Skip to content

Commit

Permalink
Allow keyboard resizing based on language (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed May 10, 2019
1 parent d5a55ec commit a1e8c7d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
Expand Up @@ -166,6 +166,12 @@ protected void onCreate(Bundle savedInstanceState) {
workaroundGeckoSigAction();
}
mUiThread = Thread.currentThread();
mUiThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
}
});

Bundle extras = getIntent() != null ? getIntent().getExtras() : null;
SessionStore.get().setContext(this, extras);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import android.view.inputmethod.EditorInfo;

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.StringUtils;

import java.util.Locale;
Expand Down Expand Up @@ -41,4 +42,8 @@ public String getComposingText(String aComposing, String aCode) {
public String getModeChangeKeyText() {
return mContext.getString(R.string.keyboard_mode_change);
}

public float getAlphabeticKeyboardWidth() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_alphabetic_width);
}
}
Expand Up @@ -29,6 +29,7 @@ public enum Action {
public String composing;
}
@NonNull CustomKeyboard getAlphabeticKeyboard();
float getAlphabeticKeyboardWidth();
default @Nullable CandidatesResult getCandidates(String aComposingText) { return null; }
default boolean supportsAutoCompletion() { return false; }
default boolean usesComposingText() { return false; }
Expand Down
Expand Up @@ -16,14 +16,14 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.CursorAnchorInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

Expand Down Expand Up @@ -74,6 +74,7 @@ public class KeyboardWidget extends UIWidget implements CustomKeyboardView.OnKey
private Drawable mShiftOffIcon;
private Drawable mCapsLockOnIcon;
private View mFocusedView;
private RelativeLayout mKeyboardContainer;
private UIWidget mBrowserWidget;
private InputConnection mInputConnection;
private EditorInfo mEditorInfo = new EditorInfo();
Expand Down Expand Up @@ -119,7 +120,11 @@ private void initialize(Context aContext) {
mPopupKeyboardview = findViewById(R.id.popupKeyboard);
mPopupKeyboardLayer = findViewById(R.id.popupKeyboardLayer);
mLanguageSelectorView = findViewById(R.id.langSelectorView);
mKeyboardContainer = findViewById(R.id.keyboardContainer);
mLanguageSelectorView.setDelegate(aItem -> handleLanguageChange((KeyboardInterface) aItem.tag));
mAutoCompletionView = findViewById(R.id.autoCompletionView);
mAutoCompletionView.setExtendedHeight((int)(mWidgetPlacement.height * mWidgetPlacement.density));
mAutoCompletionView.setDelegate(this);

mKeyboards = new ArrayList<>();
mKeyboards.add(new EnglishKeyboard(aContext));
Expand All @@ -129,10 +134,10 @@ private void initialize(Context aContext) {
mKeyboards.add(new SpanishKeyboard(aContext));
mKeyboards.add(new ChinesePinyinKeyboard(aContext));
mKeyboards.add(new ChineseZhuyinKeyboard(aContext));
setDefaultKeyboard();

mKeyboardSymbols = new CustomKeyboard(aContext.getApplicationContext(), R.xml.keyboard_symbols);
mKeyboardNumeric = new CustomKeyboard(aContext.getApplicationContext(), R.xml.keyboard_numeric);
setDefaultKeyboard();

mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setKeyboard(mCurrentKeyboard.getAlphabeticKeyboard());
Expand Down Expand Up @@ -201,7 +206,7 @@ public void releaseWidget() {
@Override
protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
Context context = getContext();
aPlacement.width = WidgetPlacement.dpDimension(context, R.dimen.keyboard_width);
aPlacement.width = getKeyboardWidth(WidgetPlacement.dpDimension(context, R.dimen.keyboard_alphabetic_width));
aPlacement.height = WidgetPlacement.dpDimension(context, R.dimen.keyboard_height);
aPlacement.height += WidgetPlacement.dpDimension(context, R.dimen.autocompletion_widget_line_height);
aPlacement.height += WidgetPlacement.dpDimension(context, R.dimen.keyboard_autocompletion_padding);
Expand All @@ -218,6 +223,14 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.cylinder = true;
}

private int getKeyboardWidth(float aAlphabeticWidth) {
float width = aAlphabeticWidth;
width += WidgetPlacement.dpDimension(getContext(), R.dimen.keyboard_autocompletion_padding);
width += WidgetPlacement.dpDimension(getContext(), R.dimen.keyboard_numeric_width);
Log.e("VRB", "Mangu " + width);
return (int) width;
}


public void setBrowserWidget(UIWidget aWidget) {
mBrowserWidget = aWidget;
Expand Down Expand Up @@ -453,8 +466,9 @@ public void swipeUp() {
}

private void setDefaultKeyboard() {
mCurrentKeyboard = getKeyboardForLocale(SettingsStore.getInstance(getContext()).getKeyboardLocale());
if (mCurrentKeyboard != null) {
KeyboardInterface keyboard = getKeyboardForLocale(SettingsStore.getInstance(getContext()).getKeyboardLocale());
if (keyboard != null) {
handleLanguageChange(keyboard);
return;
}

Expand All @@ -465,11 +479,12 @@ private void setDefaultKeyboard() {
supportedLocales[i] = mKeyboards.get(i).getLocale().toLanguageTag();
}
Locale bestMatch = localeList.getFirstMatch(supportedLocales);
mCurrentKeyboard = getKeyboardForLocale(bestMatch);
if (mCurrentKeyboard == null) {
keyboard = getKeyboardForLocale(bestMatch);
if (keyboard == null) {
// Fall back to english.
mCurrentKeyboard = getKeyboardForLocale(Locale.ENGLISH);
keyboard = getKeyboardForLocale(Locale.ENGLISH);
}
handleLanguageChange(keyboard);
}

private KeyboardInterface getKeyboardForLocale(@Nullable Locale aLocale) {
Expand Down Expand Up @@ -559,7 +574,6 @@ private void handleBackspace(final boolean isLongPress) {
connection.commitText("", 1);
}
});

}

private void handleGlobeClick() {
Expand All @@ -578,6 +592,18 @@ private void handleLanguageChange(KeyboardInterface aKeyboard) {
cleanComposingText();

mCurrentKeyboard = aKeyboard;
final int width = getKeyboardWidth(mCurrentKeyboard.getAlphabeticKeyboardWidth());
if (width != mWidgetPlacement.width) {
mWidgetPlacement.width = width;
float defaultWorldWidth = WidgetPlacement.floatDimension(getContext(), R.dimen.keyboard_world_width);
int defaultKeyboardWidth = getKeyboardWidth(mKeyboards.get(0).getAlphabeticKeyboardWidth());
mWidgetPlacement.worldWidth = defaultWorldWidth * ((float) width / (float) defaultKeyboardWidth);
mWidgetManager.updateWidget(this);
ViewGroup.LayoutParams params = mKeyboardContainer.getLayoutParams();
params.width = WidgetPlacement.convertDpToPixel(getContext(), mCurrentKeyboard.getAlphabeticKeyboardWidth());
mKeyboardContainer.setLayoutParams(params);
}

SettingsStore.getInstance(getContext()).setSelectedKeyboard(aKeyboard.getLocale());
mKeyboardView.setKeyboard(mCurrentKeyboard.getAlphabeticKeyboard());
hideOverlays();
Expand Down Expand Up @@ -621,6 +647,7 @@ private void handleModeChange() {
Keyboard current = mKeyboardView.getKeyboard();
Keyboard alphabetic = mCurrentKeyboard.getAlphabeticKeyboard();
mKeyboardView.setKeyboard(current == alphabetic ? mKeyboardSymbols : alphabetic);
mKeyboardView.setLayoutParams(mKeyboardView.getLayoutParams());
}

private void handleKey(int primaryCode, int[] keyCodes) {
Expand Down
Expand Up @@ -116,11 +116,10 @@ public static float unitFromMeters(Context aContext, int aDimensionId) {
return unitFromMeters(floatDimension(aContext, aDimensionId));
}

public static float convertDpToPixel(Context aContext, float dp){
public static int convertDpToPixel(Context aContext, float dp){
Resources resources = aContext.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
return (int) Math.ceil(dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}

public static float convertPixelsToDp(Context aContext, float px){
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/keyboard.xml
Expand Up @@ -6,12 +6,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/keyboardContainer"
android:layout_width="match_parent"
android:layout_height="@dimen/keyboard_height"
android:layout_marginTop="37dp"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/keyboardContainer"
android:layout_width="@dimen/keyboard_alphabetic_width"
android:layout_height="match_parent">

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/dimen.xml
Expand Up @@ -20,7 +20,6 @@
<item name="keyboard_y_distance_from_browser" format="float" type="dimen">-0.18</item>
<item name="keyboard_z_distance_from_browser" format="float" type="dimen">2.0</item>
<item name="keyboard_world_rotation" format="float" type="dimen">-35.0</item>
<dimen name="keyboard_width">674dp</dimen>
<dimen name="keyboard_height">188dp</dimen>
<dimen name="keyboard_alphabetic_width">526dp</dimen>
<dimen name="keyboard_numeric_width">144dp</dimen>
Expand Down

0 comments on commit a1e8c7d

Please sign in to comment.