Skip to content

Commit

Permalink
s2: Refactor Dirac setup
Browse files Browse the repository at this point in the history
- Convert DiracUtils to be an instantiable class.

- Stop tracking initialization state with a boolean and just
  fallback to checking the instance's nullity.

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
  • Loading branch information
msfjarvis authored and Akash98Sky committed Aug 25, 2019
1 parent 51a0c57 commit 3b37264
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
3 changes: 1 addition & 2 deletions parts/src/org/lineageos/settings/BootCompletedReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void onReceive(final Context context, Intent intent) {
if (DEBUG) Log.d(TAG, "Starting Doze service");
DozeUtils.startService(context);
}
DiracUtils.initialize(context);
new DiracUtils(context);
}

}
15 changes: 10 additions & 5 deletions parts/src/org/lineageos/settings/dirac/DiracSettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ 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);

mDiracUtils = new DiracUtils(getContext());

final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

boolean enhancerEnabled = DiracUtils.isDiracEnabled();
boolean enhancerEnabled = mDiracUtils.isDiracEnabled();

mHeadsetType = (ListPreference) findPreference(PREF_HEADSET);
mHeadsetType.setOnPreferenceChangeListener(this);
Expand All @@ -80,7 +85,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
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 ?
Expand All @@ -101,18 +106,18 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
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;
}
}

@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);
Expand Down
52 changes: 29 additions & 23 deletions parts/src/org/lineageos/settings/dirac/DiracUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,35 @@
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 static DiracUtils mInstance;
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 static DiracUtils getInstance() {
if (mInstance == null) {
throw new IllegalArgumentException("Trying to get instance without initializing!");
}
return mInstance;
}

protected static void refreshPlaybackIfNecessary(){
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());
mInstance = this;
}

protected void refreshPlaybackIfNecessary(){
if (mMediaSessionManager == null) {
mMediaSessionManager = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
}
Expand All @@ -63,7 +69,7 @@ protected static void refreshPlaybackIfNecessary(){
}
}

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);
Expand Down Expand Up @@ -95,7 +101,7 @@ public void run() {
}, 520);
}

private static int getMediaControllerPlaybackState(MediaController controller) {
private int getMediaControllerPlaybackState(MediaController controller) {
if (controller != null) {
final PlaybackState playbackState = controller.getPlaybackState();
if (playbackState != null) {
Expand All @@ -104,27 +110,27 @@ private static int getMediaControllerPlaybackState(MediaController controller) {
}
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){
if (enable) {
refreshPlaybackIfNecessary();
}
}

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++) {
mDiracSound.setLevel(band, Float.valueOf(level[band]));
}
}

protected static String getLevel() {
protected String getLevel() {
String selected = "";
for (int band = 0; band <= 6; band++) {
int temp = (int) mDiracSound.getLevel(band);
Expand All @@ -134,7 +140,7 @@ protected static String getLevel() {
return selected;
}

protected static void setHeadsetType(int paramInt) {
protected void setHeadsetType(int paramInt) {
mDiracSound.setHeadsetType(paramInt);
}
}

0 comments on commit 3b37264

Please sign in to comment.