@@ -4,6 +4,7 @@ import android.content.Context
44import android.content.SharedPreferences
55import android.hardware.display.DisplayManager
66import android.media.AudioManager
7+ import android.media.MediaMetadata
78import android.media.session.MediaController
89import android.media.session.PlaybackState
910import android.os.Handler
@@ -19,15 +20,20 @@ import ru.hepolise.volumekeytrackcontrol.module.util.LogHelper
1920import ru.hepolise.volumekeytrackcontrol.module.util.RemotePrefsHelper
2021import ru.hepolise.volumekeytrackcontrol.module.util.StatusHelper
2122import ru.hepolise.volumekeytrackcontrol.util.AppFilterType
23+ import ru.hepolise.volumekeytrackcontrol.util.RewindActionType
2224import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil
2325import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.LAUNCHED_COUNT
2426import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getAppFilterType
2527import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getApps
2628import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLaunchedCount
2729import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getLongPressDuration
30+ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getRewindActionType
31+ import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.getRewindDuration
2832import ru.hepolise.volumekeytrackcontrol.util.SharedPreferencesUtil.isSwapButtons
2933import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.getVibrator
3034import ru.hepolise.volumekeytrackcontrol.util.VibratorUtil.triggerVibration
35+ import kotlin.math.max
36+ import kotlin.math.min
3137
3238
3339object VolumeKeyControlModuleHandlers {
@@ -43,6 +49,7 @@ object VolumeKeyControlModuleHandlers {
4349 private lateinit var powerManager: PowerManager
4450 private lateinit var displayManager: DisplayManager
4551 private lateinit var vibrator: Vibrator
52+ private lateinit var sessionHelper: Any
4653
4754 private var prefs: SharedPreferences ? = null
4855
@@ -129,31 +136,35 @@ object VolumeKeyControlModuleHandlers {
129136 displayManager = getSystemService(Context .DISPLAY_SERVICE ) as DisplayManager ?
130137 ? : throw NullPointerException (" Unable to obtain display service" )
131138 vibrator = getVibrator()
139+ sessionHelper = getMediaSessionLegacyHelper()
132140 }
133141
134142 private fun initPrefs () {
135143 prefs = SharedPreferencesUtil .prefs()
136144 }
137145
138- private fun Context.initControllers () {
146+ private fun Context.getMediaSessionLegacyHelper (): Any {
139147 val context = this
140148 val classLoader = javaClass.classLoader
141149 val mediaSessionHelperClass = XposedHelpers .findClass(
142150 CLASS_MEDIA_SESSION_LEGACY_HELPER ,
143151 classLoader
144152 )
145- val helper = XposedHelpers .callStaticMethod(
153+ return XposedHelpers .callStaticMethod(
146154 mediaSessionHelperClass,
147155 " getHelper" ,
148156 context
149157 )
150- val mSessionManager = XposedHelpers .getObjectField(helper, " mSessionManager" )
158+ }
159+
160+ private fun Context.initControllers () {
161+ val sessionManager = XposedHelpers .getObjectField(sessionHelper, " mSessionManager" )
151162 val componentNameClass =
152163 XposedHelpers .findClass(CLASS_COMPONENT_NAME , classLoader)
153164
154165 @Suppress(" UNCHECKED_CAST" )
155166 mediaControllers = XposedHelpers .callMethod(
156- mSessionManager ,
167+ sessionManager ,
157168 " getActiveSessions" ,
158169 arrayOf(componentNameClass),
159170 null
@@ -191,7 +202,7 @@ object VolumeKeyControlModuleHandlers {
191202 log(" Volume unpressed action received, down: $isDownPressed , up: $isUpPressed " )
192203 abortAll()
193204 if (! isLongPress && getMediaController().isMusicActive()) {
194- log(" Adjusting music volume" )
205+ log(" Adjusting stream volume" )
195206 keyHelper.adjustStreamVolume(audioManager)
196207 }
197208 }
@@ -239,8 +250,29 @@ object VolumeKeyControlModuleHandlers {
239250 if (controller.isMusicActive()) controls.pause() else controls.play()
240251 }
241252
242- MediaEvent .Next -> controls.skipToNext()
243- MediaEvent .Prev -> controls.skipToPrevious()
253+ MediaEvent .Next -> {
254+ if (prefs.getRewindActionType() == RewindActionType .TRACK_CHANGE ) {
255+ controls.skipToNext()
256+ } else {
257+ val currentPosition = controller.playbackState?.position ? : 0
258+ val duration =
259+ controller.metadata?.getLong(MediaMetadata .METADATA_KEY_DURATION )
260+ ? : Long .MAX_VALUE
261+ val newPosition =
262+ min(currentPosition + prefs.getRewindDuration() * 1000 , duration)
263+ controls.seekTo(newPosition)
264+ }
265+ }
266+
267+ MediaEvent .Prev -> {
268+ if (prefs.getRewindActionType() == RewindActionType .TRACK_CHANGE ) {
269+ controls.skipToPrevious()
270+ } else {
271+ val currentPosition = controller.playbackState?.position ? : 0
272+ val newPosition = max(currentPosition - prefs.getRewindDuration() * 1000 , 0 )
273+ controls.seekTo(newPosition)
274+ }
275+ }
244276 }
245277 vibrator.triggerVibration()
246278 }
@@ -339,11 +371,32 @@ object VolumeKeyControlModuleHandlers {
339371 }
340372
341373 fun adjustStreamVolume (audioManager : AudioManager ) {
342- val direction = when (origKey) {
343- Key .UP -> AudioManager .ADJUST_RAISE
344- Key .DOWN -> AudioManager .ADJUST_LOWER
374+ try {
375+ val direction = when (origKey) {
376+ Key .UP -> KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_VOLUME_UP )
377+ Key .DOWN -> KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_VOLUME_DOWN )
378+ }
379+
380+ XposedHelpers .callMethod(
381+ sessionHelper,
382+ " sendVolumeKeyEvent" ,
383+ arrayOf(
384+ KeyEvent ::class .java,
385+ Int ::class .javaPrimitiveType,
386+ Boolean ::class .javaPrimitiveType
387+ ),
388+ direction, AudioManager .USE_DEFAULT_STREAM_TYPE , false
389+ )
390+ } catch (e: Exception ) {
391+ log(" Failed to adjust stream volume: ${e.message} " )
392+ log(" Falling back to adjustStreamVolume" )
393+
394+ val direction = when (origKey) {
395+ Key .UP -> AudioManager .ADJUST_RAISE
396+ Key .DOWN -> AudioManager .ADJUST_LOWER
397+ }
398+ audioManager.adjustStreamVolume(AudioManager .USE_DEFAULT_STREAM_TYPE , direction, 0 )
345399 }
346- audioManager.adjustStreamVolume(AudioManager .STREAM_MUSIC , direction, 0 )
347400 }
348401
349402 private companion object {
0 commit comments