Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use keyup/keydown ECP commands instead of keypress for a smoother operation. #45

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
Expand All @@ -36,7 +37,10 @@

import com.wseemann.ecp.api.ResponseCallback;
import com.wseemann.ecp.core.KeyPressKeyValues;
import com.wseemann.ecp.core.ECPRequest;
import com.wseemann.ecp.request.KeyPressRequest;
import com.wseemann.ecp.request.KeyupRequest;
import com.wseemann.ecp.request.KeydownRequest;
import com.wseemann.ecp.request.QueryDeviceInfoRequest;

import java.util.List;
Expand All @@ -53,7 +57,6 @@
import wseemann.media.romote.utils.CommandHelper;
import wseemann.media.romote.utils.Constants;
import wseemann.media.romote.utils.PreferenceUtils;
import wseemann.media.romote.view.RepeatingImageButton;
import wseemann.media.romote.view.VibratingImageButton;

/**
Expand Down Expand Up @@ -102,15 +105,15 @@ public void onClick(View v) {
mVoiceSearcButton.requestFocus();*/

linkButton(KeyPressKeyValues.BACK, R.id.back_button);
linkRepeatingRemoteButton(KeyPressKeyValues.UP, R.id.up_button);
linkButton(KeyPressKeyValues.UP, R.id.up_button);
linkButton(KeyPressKeyValues.HOME, R.id.home_button);

linkRepeatingRemoteButton(KeyPressKeyValues.LEFT, R.id.left_button);
linkButton(KeyPressKeyValues.LEFT, R.id.left_button);
linkButton(KeyPressKeyValues.SELECT, R.id.ok_button);
linkRepeatingRemoteButton(KeyPressKeyValues.RIGHT, R.id.right_button);
linkButton(KeyPressKeyValues.RIGHT, R.id.right_button);

linkButton(KeyPressKeyValues.INTANT_REPLAY, R.id.instant_replay_button);
linkRepeatingRemoteButton(KeyPressKeyValues.DOWN, R.id.down_button);
linkButton(KeyPressKeyValues.DOWN, R.id.down_button);
linkButton(KeyPressKeyValues.INFO, R.id.info_button);

linkButton(KeyPressKeyValues.REV, R.id.rev_button);
Expand Down Expand Up @@ -168,31 +171,38 @@ public void onDestroy() {
getActivity().unregisterReceiver(mUpdateReceiver);
}

private void linkRepeatingRemoteButton(final KeyPressKeyValues keypressKeyValue, int id) {
RepeatingImageButton button = getView().findViewById(id);

button.setOnClickListener(view -> {
performKeypress(keypressKeyValue);
});

button.setRepeatListener((v, duration, repeatcount) -> performKeypress(keypressKeyValue), 400);
}

private void linkButton(final KeyPressKeyValues keypressKeyValue, int id) {
ImageButton button = getView().findViewById(id);
View button = getView().findViewById(id);

button.setOnClickListener(view -> {
performKeypress(keypressKeyValue);

if (id == R.id.back_button ||
if (id == R.id.back_button ||
id == R.id.home_button ||
id == R.id.ok_button) {
BroadcastUtils.Companion.sendUpdateDeviceBroadcast(requireContext());
}
});
BroadcastUtils.Companion.sendUpdateDeviceBroadcast(requireContext());
}
performKeypress(keypressKeyValue);
});

button.setOnTouchListener((view, event) -> {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (id == R.id.back_button ||
id == R.id.home_button ||
id == R.id.ok_button) {
BroadcastUtils.Companion.sendUpdateDeviceBroadcast(requireContext());
}
performKeydown(keypressKeyValue);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
performKeyup(keypressKeyValue);
break;
}
return false;
});
}

private void performRequest(final KeyPressRequest request) {
private void performRequest(final ECPRequest<Void> request) {
request.sendAsync(new ResponseCallback<>() {
@Override
public void onSuccess(@Nullable Void unused) {
Expand Down Expand Up @@ -253,10 +263,42 @@ public void onClick(DialogInterface dialog, int whichButton) {
private void performKeypress(KeyPressKeyValues keypressKeyValue) {
String url = commandHelper.getDeviceURL();

KeyPressRequest keyPressRequest = new KeyPressRequest(url, keypressKeyValue.getValue());
KeyPressRequest keyPressRequest;
try {
keyPressRequest = new KeyPressRequest(url, keypressKeyValue.getValue());
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return;
}
performRequest(keyPressRequest);
}

private void performKeydown(KeyPressKeyValues keypressKeyValue) {
String url = commandHelper.getDeviceURL();

KeydownRequest keydownRequest;
try {
keydownRequest = new KeydownRequest(url, keypressKeyValue.getValue());
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return;
}
performRequest(keydownRequest);
}

private void performKeyup(KeyPressKeyValues keypressKeyValue) {
String url = commandHelper.getDeviceURL();

KeyupRequest keyupRequest;
try {
keyupRequest = new KeyupRequest(url, keypressKeyValue.getValue());
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return;
}
performRequest(keyupRequest);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.remote_menu, menu);
Expand Down
42 changes: 31 additions & 11 deletions app/src/main/java/wseemann/media/romote/utils/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,36 @@ private ViewUtils() {

}

public static void provideHapticFeedback(View view, int vibrateDurationMs) {
if (CommonModule.PreferenceUtilsSingleton.preferenceUtils.shouldProvideHapticFeedback()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
VibratorManager vibratorManager = (VibratorManager) view.getContext().getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
Vibrator vibrator = vibratorManager.getDefaultVibrator();
vibrator.vibrate(VibrationEffect.createOneShot(vibrateDurationMs,VibrationEffect.DEFAULT_AMPLITUDE));
} else {
Vibrator vibrator = (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(vibrateDurationMs);
}
private static Vibrator getVibrator(View view) {
if (!CommonModule.PreferenceUtilsSingleton.preferenceUtils.shouldProvideHapticFeedback())
return null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
VibratorManager vibratorManager = (VibratorManager) view.getContext()
.getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
return vibratorManager.getDefaultVibrator();
}
return (Vibrator) view.getContext().getSystemService(Context.VIBRATOR_SERVICE);
}

// effect: use one of android.os.VibrationEffect
public static void provideHapticEffect(View view, int effect_id, int fallbackVibrateDurationMs) {
Vibrator vibrator = getVibrator(view);
if (vibrator == null)
return;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
VibrationEffect effect;
if (effect_id == VibrationEffect.DEFAULT_AMPLITUDE)
effect = VibrationEffect.createOneShot(fallbackVibrateDurationMs, effect_id);
else
effect = VibrationEffect.createPredefined(effect_id);
vibrator.vibrate(effect);
} else
vibrator.vibrate(fallbackVibrateDurationMs);
}

public static void provideHapticFeedback(View view, int vibrateDurationMs) {
provideHapticEffect(view, VibrationEffect.DEFAULT_AMPLITUDE, vibrateDurationMs);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package wseemann.media.romote.view;

import android.content.Context;
import android.os.VibrationEffect;
import android.util.AttributeSet;
import android.view.View;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatImageButton;

import wseemann.media.romote.utils.ViewUtils;

public class VibratingImageButton extends AppCompatImageButton {

private static final int VIBRATE_DURATION_MS = 100;
private static final int VIBRATE_DURATION_MS = 25;

private View.OnClickListener mListener;
private View.OnClickListener mClickListener;
private View.OnTouchListener mTouchListener;

private boolean preventClick = false;

public VibratingImageButton(Context context) {
this(context, null);
Expand All @@ -25,16 +30,39 @@ public VibratingImageButton(Context context, AttributeSet attrs) {
public VibratingImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnClickListener((View view) -> {
ViewUtils.provideHapticFeedback(view, VIBRATE_DURATION_MS);
if (!preventClick) {
ViewUtils.provideHapticEffect(view, VibrationEffect.EFFECT_TICK, VIBRATE_DURATION_MS);
if (mClickListener != null)
mClickListener.onClick(view);
}
preventClick = false;
});

super.setOnTouchListener((View view, MotionEvent event) -> {
if (mTouchListener == null)
return false;

switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ViewUtils.provideHapticEffect(view, VibrationEffect.EFFECT_TICK, VIBRATE_DURATION_MS);
preventClick = true;
break;
case MotionEvent.ACTION_CANCEL:
preventClick = false;
break;
}

if (mListener != null) {
mListener.onClick(view);
}
});
return mTouchListener.onTouch(view, event);
});
}

@Override
public void setOnClickListener(View.OnClickListener listener) {
mListener = listener;
mClickListener = listener;
}

@Override
public void setOnTouchListener(View.OnTouchListener listener) {
mTouchListener = listener;
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/remote_dpad_controls_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:layout_height="match_parent"
android:layout_weight="1" />

<wseemann.media.romote.view.RepeatingImageButton
<wseemann.media.romote.view.VibratingImageButton
android:id="@+id/up_button"
android:layout_width="0dp"
android:layout_height="match_parent"
Expand All @@ -36,7 +36,7 @@
android:layout_weight="1"
android:orientation="horizontal">

<wseemann.media.romote.view.RepeatingImageButton
<wseemann.media.romote.view.VibratingImageButton
android:id="@+id/left_button"
android:layout_width="0dp"
android:layout_height="match_parent"
Expand All @@ -52,7 +52,7 @@
android:background="@drawable/background_glow_selector"
android:tag="Select" />

<wseemann.media.romote.view.RepeatingImageButton
<wseemann.media.romote.view.VibratingImageButton
android:id="@+id/right_button"
android:layout_width="0dp"
android:layout_height="match_parent"
Expand All @@ -72,7 +72,7 @@
android:layout_height="match_parent"
android:layout_weight="1" />

<wseemann.media.romote.view.RepeatingImageButton
<wseemann.media.romote.view.VibratingImageButton
android:id="@+id/down_button"
android:layout_width="0dp"
android:layout_height="match_parent"
Expand Down