Skip to content

Commit

Permalink
Allow keyboard to be repositioned
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed Jul 26, 2019
1 parent 2199c1a commit b2e74bd
Show file tree
Hide file tree
Showing 22 changed files with 468 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ add_library( # Sets the name of the library.
src/main/cpp/VRLayer.cpp
src/main/cpp/VRLayerNode.cpp
src/main/cpp/Widget.cpp
src/main/cpp/WidgetMover.cpp
src/main/cpp/WidgetPlacement.cpp
src/main/cpp/WidgetResizer.cpp
)
Expand Down
23 changes: 23 additions & 0 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,17 @@ void handleResize(final int aHandle, final float aWorldWidth, final float aWorld
});
}

@Keep
@SuppressWarnings("unused")
void handleMoveEnd(final int aHandle, final float aDeltaX, final float aDeltaY, final float aDeltaZ, final float aRotation) {
runOnUiThread(() -> {
Widget widget = mWidgets.get(aHandle);
if (widget != null) {
widget.handleMoveEvent(aDeltaX, aDeltaY, aDeltaZ, aRotation);
}
});
}

@Keep
@SuppressWarnings("unused")
void registerExternalContext(long aContext) {
Expand Down Expand Up @@ -1017,6 +1028,16 @@ public void finishWidgetResize(final Widget aWidget) {
queueRunnable(() -> finishWidgetResizeNative(aWidget.getHandle()));
}

@Override
public void startWidgetMove(final Widget aWidget, @WidgetMoveBehaviourFlags int aMoveBehaviour) {
queueRunnable(() -> startWidgetMoveNative(aWidget.getHandle(), aMoveBehaviour));
}

@Override
public void finishWidgetMove() {
queueRunnable(() -> finishWidgetMoveNative());
}

@Override
public void addUpdateListener(UpdateListener aUpdateListener) {
if (!mWidgetUpdateListeners.contains(aUpdateListener)) {
Expand Down Expand Up @@ -1215,6 +1236,8 @@ public void setCylinderDensity(final float aDensity) {
private native void removeWidgetNative(int aHandle);
private native void startWidgetResizeNative(int aHandle);
private native void finishWidgetResizeNative(int aHandle);
private native void startWidgetMoveNative(int aHandle, int aMoveBehaviour);
private native void finishWidgetMoveNative();
private native void setWorldBrightnessNative(float aBrigthness);
private native void setTemporaryFilePath(String aPath);
private native void exitImmersiveNative();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import android.graphics.Color;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.geckoview.GeckoSessionSettings;
import org.mozilla.telemetry.TelemetryHolder;
import org.mozilla.vrbrowser.BuildConfig;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.DeviceType;
import org.mozilla.vrbrowser.utils.LocaleUtils;
import org.mozilla.vrbrowser.utils.StringUtils;
Expand Down Expand Up @@ -521,5 +525,40 @@ public void setNotificationsEnabled(boolean isEnabled) {
editor.putBoolean(mContext.getString(R.string.settings_key_notifications), isEnabled);
editor.commit();
}


public void restoreKeyboardMove(WidgetPlacement aPlacement) {
try {
String value = mPrefs.getString(mContext.getString(R.string.settings_key_keyboard_move), null);
if (StringUtils.isEmpty(value)) {
return;
}
JSONObject json = new JSONObject(value);
aPlacement.translationX = (float)json.optDouble("x", aPlacement.translationX);
aPlacement.translationY = (float)json.optDouble("y", aPlacement.translationY);
aPlacement.translationZ = (float)json.optDouble("z", aPlacement.translationZ);
aPlacement.rotation = (float)json.optDouble("r", aPlacement.rotation);
}
catch (JSONException ex) {
Log.e(LOGTAG, "Error restoring keyboard move: " + ex.toString());
}
}

public void saveKeyboardMove(WidgetPlacement aPlacement) {
try {
SharedPreferences.Editor editor = mPrefs.edit();
JSONObject json = new JSONObject();
json.put("x", aPlacement.translationX);
json.put("y", aPlacement.translationY);
json.put("z", aPlacement.translationZ);
json.put("r", aPlacement.rotation);
editor.putString(mContext.getString(R.string.settings_key_keyboard_move), json.toString());
editor.commit();
}
catch (JSONException ex) {
Log.e(LOGTAG, "Error saving keyboard move: " + ex.toString());
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.CursorAnchorInfo;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class KeyboardWidget extends UIWidget implements CustomKeyboardView.OnKey
private int mKeyWidth;
private int mKeyboardPopupTopMargin;
private ImageButton mCloseKeyboardButton;
private ImageButton mKeyboardMoveButton;
private boolean mIsLongPress;
private boolean mIsMultiTap;
private boolean mIsCapsLock;
Expand All @@ -97,6 +99,43 @@ public class KeyboardWidget extends UIWidget implements CustomKeyboardView.OnKey
private String mComposingDisplayText = "";
private boolean mInternalDeleteHint = false;

private class MoveTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
v.setPressed(true);
mWidgetManager.startWidgetMove(KeyboardWidget.this, WidgetManagerDelegate.WIDGET_MOVE_BEHAVIOUR_KEYBOARD);
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
v.setPressed(false);
mWidgetManager.finishWidgetMove();
break;
default:
return false;

}
return true;
}
}

private class MoveHoverListener implements OnHoverListener {
@Override
public boolean onHover(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER:
v.setHovered(true);
break;
case MotionEvent.ACTION_HOVER_EXIT:
v.setHovered(false);
}
return false;
}
}

public KeyboardWidget(Context aContext) {
super(aContext);
initialize(aContext);
Expand Down Expand Up @@ -179,7 +218,9 @@ private void initialize(Context aContext) {
mCapsLockOnIcon = getResources().getDrawable(R.drawable.ic_icon_keyboard_caps, getContext().getTheme());
mCloseKeyboardButton = findViewById(R.id.keyboardCloseButton);
mCloseKeyboardButton.setOnClickListener(v -> dismiss());

mKeyboardMoveButton = findViewById(R.id.keyboardMoveButton);
mKeyboardMoveButton.setOnTouchListener(new MoveTouchListener());
mKeyboardMoveButton.setOnHoverListener(new MoveHoverListener());
mKeyWidth = getResources().getDimensionPixelSize(R.dimen.keyboard_key_width);
mKeyboardPopupTopMargin = getResources().getDimensionPixelSize(R.dimen.keyboard_key_pressed_padding) * 2;

Expand Down Expand Up @@ -226,6 +267,13 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.worldWidth = WidgetPlacement.floatDimension(context, R.dimen.keyboard_world_width);
aPlacement.visible = false;
aPlacement.cylinder = true;
SettingsStore.getInstance(getContext()).restoreKeyboardMove(mWidgetPlacement);
}

@Override
public void handleMoveEvent(float aDeltaX, float aDeltaY, float aDeltaZ, float aRotation) {
super.handleMoveEvent(aDeltaX, aDeltaY, aDeltaZ, aRotation);
SettingsStore.getInstance(getContext()).saveKeyboardMove(mWidgetPlacement);
}

private int getKeyboardWidth(float aAlphabeticWidth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ public void handleResizeEvent(float aWorldWidth, float aWorldHeight) {
mWidgetManager.updateWidget(this);
}

@Override
public void handleMoveEvent(float aDeltaX, float aDeltaY, float aDeltaZ, float aRotation) {
mWidgetPlacement.translationX += aDeltaX;
mWidgetPlacement.translationY += aDeltaY;
mWidgetPlacement.translationZ += aDeltaZ;
mWidgetPlacement.rotation = aRotation;
}

@Override
public void releaseWidget() {
if (mRenderer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface Widget {
void handleTouchEvent(MotionEvent aEvent);
void handleHoverEvent(MotionEvent aEvent);
void handleResizeEvent(float aWorldWidth, float aWorldHeight);
void handleMoveEvent(float aDeltaX, float aDeltaY, float aDeltaZ, float aRotation);
void releaseWidget();
void setFirstDraw(boolean aIsFirstDraw);
boolean getFirstDraw();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.mozilla.geckoview.GeckoSession;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;

public interface WidgetManagerDelegate {
Expand All @@ -23,12 +24,19 @@ interface WorldClickListener {
float DEFAULT_DIM_BRIGHTNESS = 0.25f;
float DEFAULT_NO_DIM_BRIGHTNESS = 1.0f;

@IntDef(value = { WIDGET_MOVE_BEHAVIOUR_GENERAL, WIDGET_MOVE_BEHAVIOUR_KEYBOARD})
public @interface WidgetMoveBehaviourFlags {}
public static final int WIDGET_MOVE_BEHAVIOUR_GENERAL = 0;
public static final int WIDGET_MOVE_BEHAVIOUR_KEYBOARD = 1;

int newWidgetHandle();
void addWidget(@NonNull Widget aWidget);
void updateWidget(@NonNull Widget aWidget);
void removeWidget(@NonNull Widget aWidget);
void startWidgetResize(@NonNull Widget aWidget);
void finishWidgetResize(@NonNull Widget aWidget);
void startWidgetMove(@NonNull Widget aWidget, @WidgetMoveBehaviourFlags int aMoveBehaviour);
void finishWidgetMove();
void addUpdateListener(@NonNull UpdateListener aUpdateListener);
void removeUpdateListener(@NonNull UpdateListener aUpdateListener);
void pushBackHandler(@NonNull Runnable aRunnable);
Expand Down

0 comments on commit b2e74bd

Please sign in to comment.