From 3b8d0e802690ac774b4357ed91b4746812cb7843 Mon Sep 17 00:00:00 2001 From: autoandshare Date: Mon, 22 Feb 2021 11:19:01 +0000 Subject: [PATCH] improve keyboard extra function --- .../headvr/activity/SettingFragment.java | 3 ++ .../headvr/activity/VideoActivity.java | 26 +++++++++++++++- .../java/autoandshare/headvr/lib/Actions.java | 4 +++ .../java/autoandshare/headvr/lib/Setting.java | 1 + .../headvr/lib/controller/KeyControl.java | 30 ++++++++++++++++--- src/main/res/layout/setting_ui.xml | 13 ++++++-- 6 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/main/java/autoandshare/headvr/activity/SettingFragment.java b/src/main/java/autoandshare/headvr/activity/SettingFragment.java index 28649f1..6265f27 100644 --- a/src/main/java/autoandshare/headvr/activity/SettingFragment.java +++ b/src/main/java/autoandshare/headvr/activity/SettingFragment.java @@ -1,5 +1,6 @@ package autoandshare.headvr.activity; +import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -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) { @@ -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() { diff --git a/src/main/java/autoandshare/headvr/activity/VideoActivity.java b/src/main/java/autoandshare/headvr/activity/VideoActivity.java index 6365df6..4f840d6 100644 --- a/src/main/java/autoandshare/headvr/activity/VideoActivity.java +++ b/src/main/java/autoandshare/headvr/activity/VideoActivity.java @@ -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; @@ -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"; @@ -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); @@ -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<>(); } @@ -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) { diff --git a/src/main/java/autoandshare/headvr/lib/Actions.java b/src/main/java/autoandshare/headvr/lib/Actions.java index 3078ac2..23931d9 100644 --- a/src/main/java/autoandshare/headvr/lib/Actions.java +++ b/src/main/java/autoandshare/headvr/lib/Actions.java @@ -17,4 +17,8 @@ public enum Actions { Back, IncreaseScreenSize, DecreaseScreenSize, + MoveScreenUp, + MoveScreenDown, + IncreaseVolume, + DecreaseVolume, } diff --git a/src/main/java/autoandshare/headvr/lib/Setting.java b/src/main/java/autoandshare/headvr/lib/Setting.java index f3c2bff..3e928ff 100644 --- a/src/main/java/autoandshare/headvr/lib/Setting.java +++ b/src/main/java/autoandshare/headvr/lib/Setting.java @@ -22,6 +22,7 @@ public enum id { MotionSensitivity, DisableDistortionCorrection, HeadControl, + DisableExtraControl, } private static class Item { diff --git a/src/main/java/autoandshare/headvr/lib/controller/KeyControl.java b/src/main/java/autoandshare/headvr/lib/controller/KeyControl.java index 5e058ef..4655e6f 100644 --- a/src/main/java/autoandshare/headvr/lib/controller/KeyControl.java +++ b/src/main/java/autoandshare/headvr/lib/controller/KeyControl.java @@ -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; @@ -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 keyTracking = new HashSet(); + + 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) { diff --git a/src/main/res/layout/setting_ui.xml b/src/main/res/layout/setting_ui.xml index f1f5894..e7e7990 100644 --- a/src/main/res/layout/setting_ui.xml +++ b/src/main/res/layout/setting_ui.xml @@ -6,13 +6,13 @@ + android:layout_height="0dp" + android:layout_margin="10dp" /> + +