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

View File

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