sanders: Dirac fixes
* Pause/play music stream to get effects applied * Rename setMusic to setEnabled * Increase "BOOT_COMPLETED" broadcast priority Signed-off-by: Ashwin R C <ashwin2001achu@gmail.com> Signed-off-by: utsavbalar1231 <utsavbalar1231@gmail.com> Signed-off-by: ronaxdevil <pratabidya.007@gmail.com>
This commit is contained in:
committed by
ronaxdevil
parent
4be693e152
commit
dd940fbfdd
@@ -57,7 +57,7 @@ public class BootCompletedReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.startService(new Intent(context, ServiceWrapper.class));
|
context.startService(new Intent(context, ServiceWrapper.class));
|
||||||
DiracUtils.initialize();
|
DiracUtils.initialize(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void enableNavBar(boolean enable, Context context) {
|
protected static void enableNavBar(boolean enable, Context context) {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class DiracSettingsFragment extends PreferenceFragment implements
|
|||||||
final ActionBar actionBar = getActivity().getActionBar();
|
final ActionBar actionBar = getActivity().getActionBar();
|
||||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
boolean enhancerEnabled = DiracUtils.isDiracEnabled(getActivity());
|
boolean enhancerEnabled = DiracUtils.isDiracEnabled();
|
||||||
|
|
||||||
mHeadsetType = (ListPreference) findPreference(PREF_HEADSET);
|
mHeadsetType = (ListPreference) findPreference(PREF_HEADSET);
|
||||||
mHeadsetType.setOnPreferenceChangeListener(this);
|
mHeadsetType.setOnPreferenceChangeListener(this);
|
||||||
@@ -80,7 +80,7 @@ public class DiracSettingsFragment extends PreferenceFragment implements
|
|||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
|
||||||
boolean enhancerEnabled = DiracUtils.isDiracEnabled(getActivity());
|
boolean enhancerEnabled = DiracUtils.isDiracEnabled();
|
||||||
|
|
||||||
mTextView = view.findViewById(R.id.switch_text);
|
mTextView = view.findViewById(R.id.switch_text);
|
||||||
mTextView.setText(getString(enhancerEnabled ?
|
mTextView.setText(getString(enhancerEnabled ?
|
||||||
@@ -112,7 +112,7 @@ public class DiracSettingsFragment extends PreferenceFragment implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
|
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
|
||||||
DiracUtils.setMusic(isChecked);
|
DiracUtils.setEnabled(isChecked);
|
||||||
|
|
||||||
mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off));
|
mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off));
|
||||||
mSwitchBar.setActivated(isChecked);
|
mSwitchBar.setActivated(isChecked);
|
||||||
|
|||||||
@@ -18,28 +18,101 @@ package org.lineageos.settings.dirac;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.os.Handler;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.os.SystemClock;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.media.session.MediaController;
|
||||||
|
import android.media.session.MediaSessionManager;
|
||||||
|
import android.media.session.PlaybackState;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public final class DiracUtils {
|
public final class DiracUtils {
|
||||||
|
|
||||||
protected static DiracSound mDiracSound;
|
protected static DiracSound mDiracSound;
|
||||||
private static boolean mInitialized;
|
private static boolean mInitialized;
|
||||||
|
private static MediaSessionManager mMediaSessionManager;
|
||||||
|
private static Handler mHandler = new Handler();
|
||||||
|
private static Context mContext;
|
||||||
|
|
||||||
public static void initialize() {
|
public static void initialize(Context context) {
|
||||||
if (!mInitialized) {
|
if (!mInitialized) {
|
||||||
|
mContext = context;
|
||||||
|
mMediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
|
||||||
mInitialized = true;
|
mInitialized = true;
|
||||||
mDiracSound = new DiracSound(0, 0);
|
mDiracSound = new DiracSound(0, 0);
|
||||||
mDiracSound.setMusic(mDiracSound.getMusic());
|
setEnabled(mDiracSound.getMusic() == 1);
|
||||||
mDiracSound.setHeadsetType(mDiracSound.getHeadsetType());
|
mDiracSound.setHeadsetType(mDiracSound.getHeadsetType());
|
||||||
setLevel(getLevel());
|
setLevel(getLevel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void setMusic(boolean enable) {
|
protected static void refreshPlaybackIfNecessary(){
|
||||||
mDiracSound.setMusic(enable ? 1 : 0);
|
if (mMediaSessionManager == null) {
|
||||||
|
mMediaSessionManager = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
|
||||||
|
}
|
||||||
|
final List<MediaController> sessions
|
||||||
|
= mMediaSessionManager.getActiveSessionsForUser(
|
||||||
|
null, UserHandle.USER_ALL);
|
||||||
|
for (MediaController aController : sessions) {
|
||||||
|
if (PlaybackState.STATE_PLAYING ==
|
||||||
|
getMediaControllerPlaybackState(aController)) {
|
||||||
|
triggerPlayPause(aController);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static boolean isDiracEnabled(Context context) {
|
private static void triggerPlayPause(MediaController controller) {
|
||||||
|
long when = SystemClock.uptimeMillis();
|
||||||
|
final KeyEvent evDownPause = new KeyEvent(when, when, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
|
||||||
|
final KeyEvent evUpPause = KeyEvent.changeAction(evDownPause, KeyEvent.ACTION_UP);
|
||||||
|
final KeyEvent evDownPlay = new KeyEvent(when, when, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
|
||||||
|
final KeyEvent evUpPlay = KeyEvent.changeAction(evDownPlay, KeyEvent.ACTION_UP);
|
||||||
|
mHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
controller.dispatchMediaButtonEvent(evDownPause);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mHandler.postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
controller.dispatchMediaButtonEvent(evUpPause);
|
||||||
|
}
|
||||||
|
}, 20);
|
||||||
|
mHandler.postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
controller.dispatchMediaButtonEvent(evDownPlay);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
mHandler.postDelayed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
controller.dispatchMediaButtonEvent(evUpPlay);
|
||||||
|
}
|
||||||
|
}, 520);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getMediaControllerPlaybackState(MediaController controller) {
|
||||||
|
if (controller != null) {
|
||||||
|
final PlaybackState playbackState = controller.getPlaybackState();
|
||||||
|
if (playbackState != null) {
|
||||||
|
return playbackState.getState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return PlaybackState.STATE_NONE;
|
||||||
|
}
|
||||||
|
protected static void setEnabled(boolean enable) {
|
||||||
|
mDiracSound.setEnabled(enable);
|
||||||
|
mDiracSound.setMusic(enable ? 1 : 0);
|
||||||
|
if (enable){
|
||||||
|
refreshPlaybackIfNecessary();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static boolean isDiracEnabled() {
|
||||||
return mDiracSound.getMusic() == 1;
|
return mDiracSound.getMusic() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user