Hardware keyboard support (#4486)

This commit is contained in:
asdoi 2020-10-16 14:52:59 +00:00 committed by GitHub
parent b93f515845
commit e2094c0cad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
@ -13,6 +14,7 @@ import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -23,6 +25,7 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
@ -34,6 +37,8 @@ import com.google.android.material.snackbar.Snackbar;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.util.StorageUtils;
import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
@ -517,4 +522,53 @@ public class MainActivity extends CastEnabledActivity {
public Snackbar showSnackbarAbovePlayer(int text, int duration) {
return showSnackbarAbovePlayer(getResources().getText(text), duration);
}
//Hardware keyboard support
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
Integer customKeyCode = null;
switch (keyCode) {
case KeyEvent.KEYCODE_P:
customKeyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
break;
case KeyEvent.KEYCODE_J: //Fallthrough
case KeyEvent.KEYCODE_A:
case KeyEvent.KEYCODE_COMMA:
customKeyCode = KeyEvent.KEYCODE_MEDIA_REWIND;
break;
case KeyEvent.KEYCODE_K: //Fallthrough
case KeyEvent.KEYCODE_D:
case KeyEvent.KEYCODE_PERIOD:
customKeyCode = KeyEvent.KEYCODE_MEDIA_FAST_FORWARD;
break;
case KeyEvent.KEYCODE_PLUS: //Fallthrough
case KeyEvent.KEYCODE_W:
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_MINUS: //Fallthrough
case KeyEvent.KEYCODE_S:
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_M:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_TOGGLE_MUTE, AudioManager.FLAG_SHOW_UI);
return true;
}
break;
}
if (customKeyCode != null) {
Intent intent = new Intent(this, PlaybackService.class);
intent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, customKeyCode);
ContextCompat.startForegroundService(this, intent);
return true;
}
return super.onKeyUp(keyCode, event);
}
}

View File

@ -3,14 +3,18 @@ package de.danoeh.antennapod.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import androidx.appcompat.view.menu.ActionMenuItem;
import androidx.core.view.WindowCompat;
import androidx.appcompat.app.ActionBar;
import android.text.TextUtils;
@ -481,4 +485,62 @@ public class VideoplayerActivity extends MediaplayerActivity {
}
//Hardware keyboard support
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
switch (keyCode) {
case KeyEvent.KEYCODE_P: //Fallthrough
case KeyEvent.KEYCODE_SPACE:
onPlayPause();
toggleVideoControlsVisibility();
return true;
case KeyEvent.KEYCODE_J: //Fallthrough
case KeyEvent.KEYCODE_A:
case KeyEvent.KEYCODE_COMMA:
onRewind();
showSkipAnimation(false);
return true;
case KeyEvent.KEYCODE_K: //Fallthrough
case KeyEvent.KEYCODE_D:
case KeyEvent.KEYCODE_PERIOD:
onFastForward();
showSkipAnimation(true);
return true;
case KeyEvent.KEYCODE_F: //Fallthrough
case KeyEvent.KEYCODE_ESCAPE:
//Exit fullscreen mode
onBackPressed();
return true;
case KeyEvent.KEYCODE_I:
compatEnterPictureInPicture();
return true;
case KeyEvent.KEYCODE_PLUS: //Fallthrough
case KeyEvent.KEYCODE_W:
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_MINUS: //Fallthrough
case KeyEvent.KEYCODE_S:
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_M:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_TOGGLE_MUTE, AudioManager.FLAG_SHOW_UI);
return true;
}
break;
}
//Go to x% of video:
if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
controller.seekTo((int) (0.1f * (keyCode - KeyEvent.KEYCODE_0) * controller.getDuration()));
return true;
}
return super.onKeyUp(keyCode, event);
}
}