Skip to content

Commit

Permalink
improve keyboard extra function
Browse files Browse the repository at this point in the history
  • Loading branch information
autoandshare committed Feb 22, 2021
1 parent 5ab8ff9 commit 3b8d0e8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package autoandshare.headvr.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -49,6 +50,7 @@ private void initUI() {
private void initSwitchs(View view) {
initSwitch(view, R.id.distortionCorrectionSwitch, Setting.id.DisableDistortionCorrection);
initSwitch(view, R.id.headControlSwitch, Setting.id.HeadControl);
initSwitch(view, R.id.extraFunctionSwitch, Setting.id.DisableExtraControl);
}

private void initSwitch(View view, int id, Setting.id settingId) {
Expand Down Expand Up @@ -79,6 +81,7 @@ void initSeekBars(View view, int id, Setting.id propertyName, int textId) {

seekBar.setProgress(setting.get(propertyName) - setting.getMin(propertyName));
textView.setText(String.format("%s (%d)", setting.getDescription(propertyName), setting.get(propertyName)));
textView.setTextColor(Color.BLACK);


seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/autoandshare/headvr/activity/VideoActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package autoandshare.headvr.activity;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.opengl.GLES20;
import android.opengl.Matrix;
Expand Down Expand Up @@ -119,6 +121,12 @@ private void updateScreenSize(int i) {
videoRenderer.updateVideoPosition();
}

private void updateScreenVertical(int i) {
int newScreenVertial = setting.update(Setting.id.VerticalDistance, i);
videoRenderer.getState().message = "setting " + Setting.id.VerticalDistance
+ " to " + newScreenVertial;
}

private Boolean playMediaFromList(int offset) {
if (!playList.isReady()) {
videoRenderer.getState().message = "Loading play list";
Expand Down Expand Up @@ -307,6 +315,7 @@ private void updateUIVisibility() {
public boolean dispatchKeyEvent(KeyEvent event) {
Log.d(TAG, "got key event " + event.toString());
Event e = KeyControl.processKeyEvent(event,
(!setting.getBoolean(Setting.id.DisableExtraControl)) &&
(videoRenderer != null) && videoRenderer.paused());
if (e.action != Actions.NoAction) {
appendEvent(e);
Expand All @@ -330,7 +339,7 @@ void appendEvent(Event e) {
}

synchronized (this) {
Log.d(TAG , "append event " + e.toString());
Log.d(TAG, "append event " + e.toString());
if (events == null) {
events = new ArrayList<>();
}
Expand Down Expand Up @@ -376,6 +385,21 @@ private void setupActionTable() {
actionTable.put(Actions.Back, (e) -> returnHome());
actionTable.put(Actions.IncreaseScreenSize, (e) -> updateScreenSize(3));
actionTable.put(Actions.DecreaseScreenSize, (e) -> updateScreenSize(-3));
actionTable.put(Actions.MoveScreenUp, (e) -> updateScreenVertical(3));
actionTable.put(Actions.MoveScreenDown, (e) -> updateScreenVertical(-3));
actionTable.put(Actions.IncreaseVolume, (e) -> adjustVolume(true));
actionTable.put(Actions.DecreaseVolume, (e) -> adjustVolume(false));
}

private AudioManager audioManager;

private void adjustVolume(boolean increase) {
if (audioManager == null) {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
increase ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
}

private void processEvent(Event e) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/autoandshare/headvr/lib/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public enum Actions {
Back,
IncreaseScreenSize,
DecreaseScreenSize,
MoveScreenUp,
MoveScreenDown,
IncreaseVolume,
DecreaseVolume,
}
1 change: 1 addition & 0 deletions src/main/java/autoandshare/headvr/lib/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum id {
MotionSensitivity,
DisableDistortionCorrection,
HeadControl,
DisableExtraControl,
}

private static class Item {
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/autoandshare/headvr/lib/controller/KeyControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.util.Log;
import android.view.KeyEvent;

import java.util.HashMap;
import java.util.HashSet;

import autoandshare.headvr.lib.Actions;
import autoandshare.headvr.lib.Event;
import autoandshare.headvr.lib.VideoRenderer;
Expand All @@ -28,25 +31,44 @@ private static void extraFunction(KeyEvent event, Event e) {
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_DPAD_UP:
setIfFirstDown(event, e, Actions.IncreaseScreenSize);
setActionForPressAndLongPress(event, e,
Actions.MoveScreenUp, Actions.IncreaseScreenSize);
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_DPAD_DOWN:
setIfFirstDown(event, e, Actions.DecreaseScreenSize);
setActionForPressAndLongPress(event, e,
Actions.MoveScreenDown, Actions.DecreaseScreenSize);
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
case KeyEvent.KEYCODE_DPAD_RIGHT:
setIfFirstDown(event, e, Actions.IncreaseEyeDistance);
setActionForPressAndLongPress(event, e,
Actions.IncreaseEyeDistance, Actions.IncreaseVolume);
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_DPAD_LEFT:
setIfFirstDown(event, e, Actions.DecreaseEyeDistance);
setActionForPressAndLongPress(event, e,
Actions.DecreaseEyeDistance, Actions.DecreaseVolume);
break;
default:
break;
}
}

private static HashSet<Integer> keyTracking = new HashSet<Integer>();

private static void setActionForPressAndLongPress(
KeyEvent event, Event e, Actions pressAction, Actions longPressAction) {
if ((event.getRepeatCount() + 1) % keyRepeatCountForOneSecond == 0) {
keyTracking.add(event.getKeyCode());
e.action = longPressAction;
}
if (event.getAction() == KeyEvent.ACTION_UP) {
if (!keyTracking.remove(event.getKeyCode())) {
e.action = pressAction;
}
}
}

private static void setIfFirstDown(KeyEvent event, Event e, Actions action) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
Expand Down
13 changes: 10 additions & 3 deletions src/main/res/layout/setting_ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@android:color/darker_gray" />
android:layout_height="0dp"
android:layout_margin="10dp" />

<TextView
android:id="@+id/textViewBrightness"
Expand Down Expand Up @@ -145,10 +145,17 @@
android:layout_margin="10dp"
android:background="@android:color/darker_gray" />

<Switch
android:id="@+id/extraFunctionSwitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disable Controller Extra Function" />

<Button
android:id="@+id/buttonReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Reset" />

</LinearLayout>
Expand Down

0 comments on commit 3b8d0e8

Please sign in to comment.