dirac: Refactor Dirac setup

* Convert DiracUtils to be an instantiable class
* Stop tracking initialization state

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:
MSF-Jarvis
2019-02-17 15:01:47 +05:30
committed by ronaxdevil
parent dd940fbfdd
commit d7a57d56ac
3 changed files with 30 additions and 29 deletions

View File

@@ -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(context); new DiracUtils(context);
} }
protected static void enableNavBar(boolean enable, Context context) { protected static void enableNavBar(boolean enable, Context context) {

View File

@@ -50,13 +50,17 @@ public class DiracSettingsFragment extends PreferenceFragment implements
private ListPreference mHeadsetType; private ListPreference mHeadsetType;
private ListPreference mPreset; private ListPreference mPreset;
private DiracUtils mDiracUtils;
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.dirac_settings); addPreferencesFromResource(R.xml.dirac_settings);
final ActionBar actionBar = getActivity().getActionBar(); final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
boolean enhancerEnabled = DiracUtils.isDiracEnabled(); mDiracUtils = new DiracUtils(getContext());
boolean enhancerEnabled = mDiracUtils.isDiracEnabled();
mHeadsetType = (ListPreference) findPreference(PREF_HEADSET); mHeadsetType = (ListPreference) findPreference(PREF_HEADSET);
mHeadsetType.setOnPreferenceChangeListener(this); mHeadsetType.setOnPreferenceChangeListener(this);
@@ -80,7 +84,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(); boolean enhancerEnabled = mDiracUtils.isDiracEnabled();
mTextView = view.findViewById(R.id.switch_text); mTextView = view.findViewById(R.id.switch_text);
mTextView.setText(getString(enhancerEnabled ? mTextView.setText(getString(enhancerEnabled ?
@@ -101,10 +105,10 @@ public class DiracSettingsFragment extends PreferenceFragment implements
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
switch (preference.getKey()) { switch (preference.getKey()) {
case PREF_HEADSET: case PREF_HEADSET:
DiracUtils.setHeadsetType(Integer.parseInt(newValue.toString())); mDiracUtils.setHeadsetType(Integer.parseInt(newValue.toString()));
return true; return true;
case PREF_PRESET: case PREF_PRESET:
DiracUtils.setLevel(String.valueOf(newValue)); mDiracUtils.setLevel(String.valueOf(newValue));
return true; return true;
default: return false; default: return false;
} }
@@ -112,7 +116,7 @@ public class DiracSettingsFragment extends PreferenceFragment implements
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
DiracUtils.setEnabled(isChecked); mDiracUtils.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);

View File

@@ -25,29 +25,26 @@ import android.view.KeyEvent;
import android.media.session.MediaController; import android.media.session.MediaController;
import android.media.session.MediaSessionManager; import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState; import android.media.session.PlaybackState;
import java.lang.IllegalArgumentException;
import java.util.List; import java.util.List;
public final class DiracUtils { public final class DiracUtils {
protected static DiracSound mDiracSound; protected DiracSound mDiracSound;
private static boolean mInitialized; private MediaSessionManager mMediaSessionManager;
private static MediaSessionManager mMediaSessionManager; private Handler mHandler = new Handler();
private static Handler mHandler = new Handler(); private Context mContext;
private static Context mContext;
public static void initialize(Context context) { public DiracUtils(final Context context) {
if (!mInitialized) { mContext = context;
mContext = context; mMediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
mMediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); mDiracSound = new DiracSound(0, 0);
mInitialized = true; setEnabled(mDiracSound.getMusic() == 1);
mDiracSound = new DiracSound(0, 0); mDiracSound.setHeadsetType(mDiracSound.getHeadsetType());
setEnabled(mDiracSound.getMusic() == 1); setLevel(getLevel());
mDiracSound.setHeadsetType(mDiracSound.getHeadsetType());
setLevel(getLevel());
}
} }
protected static void refreshPlaybackIfNecessary(){ protected void refreshPlaybackIfNecessary(){
if (mMediaSessionManager == null) { if (mMediaSessionManager == null) {
mMediaSessionManager = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE); mMediaSessionManager = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
} }
@@ -63,7 +60,7 @@ public final class DiracUtils {
} }
} }
private static void triggerPlayPause(MediaController controller) { private void triggerPlayPause(MediaController controller) {
long when = SystemClock.uptimeMillis(); long when = SystemClock.uptimeMillis();
final KeyEvent evDownPause = new KeyEvent(when, when, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0); 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 evUpPause = KeyEvent.changeAction(evDownPause, KeyEvent.ACTION_UP);
@@ -95,7 +92,7 @@ public final class DiracUtils {
}, 520); }, 520);
} }
private static int getMediaControllerPlaybackState(MediaController controller) { private int getMediaControllerPlaybackState(MediaController controller) {
if (controller != null) { if (controller != null) {
final PlaybackState playbackState = controller.getPlaybackState(); final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState != null) { if (playbackState != null) {
@@ -104,7 +101,7 @@ public final class DiracUtils {
} }
return PlaybackState.STATE_NONE; return PlaybackState.STATE_NONE;
} }
protected static void setEnabled(boolean enable) { protected void setEnabled(boolean enable) {
mDiracSound.setEnabled(enable); mDiracSound.setEnabled(enable);
mDiracSound.setMusic(enable ? 1 : 0); mDiracSound.setMusic(enable ? 1 : 0);
if (enable){ if (enable){
@@ -112,11 +109,11 @@ public final class DiracUtils {
} }
} }
protected static boolean isDiracEnabled() { protected boolean isDiracEnabled() {
return mDiracSound.getMusic() == 1; return mDiracSound.getMusic() == 1;
} }
protected static void setLevel(String preset) { protected void setLevel(String preset) {
String[] level = preset.split("\\s*,\\s*"); String[] level = preset.split("\\s*,\\s*");
for (int band = 0; band <= level.length - 1; band++) { for (int band = 0; band <= level.length - 1; band++) {
@@ -124,7 +121,7 @@ public final class DiracUtils {
} }
} }
protected static String getLevel() { protected String getLevel() {
String selected = ""; String selected = "";
for (int band = 0; band <= 6; band++) { for (int band = 0; band <= 6; band++) {
int temp = (int) mDiracSound.getLevel(band); int temp = (int) mDiracSound.getLevel(band);
@@ -134,7 +131,7 @@ public final class DiracUtils {
return selected; return selected;
} }
protected static void setHeadsetType(int paramInt) { protected void setHeadsetType(int paramInt) {
mDiracSound.setHeadsetType(paramInt); mDiracSound.setHeadsetType(paramInt);
} }
} }