diff options
author | Steve Kondik <steve@cyngn.com> | 2016-04-25 11:22:46 -0700 |
---|---|---|
committer | Gerrit Code Review <gerrit@cyngn.com> | 2016-04-28 06:38:02 +0000 |
commit | e5570ef58e12d645122cabef41c4f97099ace0af (patch) | |
tree | 93be42277fe41160bfa1269cc548c4c54e1af2ca /src | |
parent | a3a728ce783185ed9d30aa80405e48f7718649e1 (diff) | |
download | android_packages_apps_AudioFX-e5570ef58e12d645122cabef41c4f97099ace0af.tar.gz android_packages_apps_AudioFX-e5570ef58e12d645122cabef41c4f97099ace0af.tar.bz2 android_packages_apps_AudioFX-e5570ef58e12d645122cabef41c4f97099ace0af.zip |
audiofx: Fix the QS tile
* And clean up device tracking.
Change-Id: I4dc935c6a1e17b7dd346b1197876089c3436c7a5
Diffstat (limited to 'src')
4 files changed, 124 insertions, 85 deletions
diff --git a/src/com/cyngn/audiofx/service/AudioFxService.java b/src/com/cyngn/audiofx/service/AudioFxService.java index 88cc15b..00d03ee 100644 --- a/src/com/cyngn/audiofx/service/AudioFxService.java +++ b/src/com/cyngn/audiofx/service/AudioFxService.java @@ -23,12 +23,14 @@ import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.content.res.Configuration; +import android.media.AudioDeviceInfo; import android.media.AudioManager; import android.media.audiofx.AudioEffect; import android.os.Binder; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.cyngn.audiofx.R; @@ -52,7 +54,8 @@ import cyanogenmod.media.CMAudioManager; * the service is also responsible for applying the effects properly based on user configuration, * and the current device output state. */ -public class AudioFxService extends Service { +public class AudioFxService extends Service + implements AudioOutputChangeListener.AudioOutputChangedCallback { static final String TAG = AudioFxService.class.getSimpleName(); @@ -88,10 +91,13 @@ public class AudioFxService extends Service { private CustomTile mTile; private CustomTile.Builder mTileBuilder; - private DevicePreferenceManager mPreferenceManager; + private AudioOutputChangeListener mOutputListener; + private DevicePreferenceManager mDevicePrefs; private SessionManager mSessionManager; private Handler mHandler; + private AudioDeviceInfo mCurrentDevice; + public static class LocalBinder extends Binder { final WeakReference<AudioFxService> mService; @@ -136,10 +142,10 @@ public class AudioFxService extends Service { handlerThread.start(); mHandler = new Handler(handlerThread.getLooper()); - mSessionManager = new SessionManager(getApplicationContext(), mHandler); + mOutputListener = new AudioOutputChangeListener(getApplicationContext(), mHandler); - mPreferenceManager = new DevicePreferenceManager(getApplicationContext(), mSessionManager); - if (!mPreferenceManager.initDefaults()) { + mDevicePrefs = new DevicePreferenceManager(getApplicationContext()); + if (!mDevicePrefs.initDefaults()) { stopSelf(); return; } @@ -223,6 +229,26 @@ public class AudioFxService extends Service { } } + @Override + public synchronized void onAudioOutputChanged(boolean firstChange, + AudioDeviceInfo outputDevice) { + if (outputDevice == null) { + return; + } + + mCurrentDevice = outputDevice; + + if (DEBUG) + Log.d(TAG, "Broadcasting device changed event"); + + // Update the UI with the change + Intent intent = new Intent(ACTION_DEVICE_OUTPUT_CHANGED); + intent.putExtra("device", outputDevice.getId()); + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); + + updateQsTile(); + } + private void updateQsTile() { if (mTileBuilder == null) { mTileBuilder = new CustomTile.Builder(this); @@ -239,11 +265,11 @@ public class AudioFxService extends Service { .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0); String label = getString(R.string.qs_tile_label, - MasterConfigControl.getDeviceDisplayString(this, mSessionManager.getCurrentDevice())); + MasterConfigControl.getDeviceDisplayString(this, mCurrentDevice)); mTileBuilder .hasSensitiveData(false) - .setIcon(mPreferenceManager.isGlobalEnabled() ? R.drawable.ic_qs_visualizer_on + .setIcon(mDevicePrefs.isGlobalEnabled() ? R.drawable.ic_qs_visualizer_on : R.drawable.ic_qs_visualizer_off) .setLabel(label) .setContentDescription(R.string.qs_tile_content_description) @@ -260,6 +286,7 @@ public class AudioFxService extends Service { public void onDestroy() { if (DEBUG) Log.i(TAG, "Stopping service."); + mOutputListener.removeCallback(this, mSessionManager, mDevicePrefs); mSessionManager.onDestroy(); CMStatusBarManager.getInstance(this).removeTile(TILE_ID); diff --git a/src/com/cyngn/audiofx/service/AudioOutputChangeListener.java b/src/com/cyngn/audiofx/service/AudioOutputChangeListener.java index a96b67a..f85d9c1 100644 --- a/src/com/cyngn/audiofx/service/AudioOutputChangeListener.java +++ b/src/com/cyngn/audiofx/service/AudioOutputChangeListener.java @@ -1,6 +1,8 @@ package com.cyngn.audiofx.service; +import static android.media.AudioDeviceInfo.convertDeviceTypeToInternalDevice; + import android.content.Context; import android.media.AudioDeviceCallback; import android.media.AudioDeviceInfo; @@ -9,11 +11,10 @@ import android.os.Handler; import android.util.Log; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import static android.media.AudioDeviceInfo.*; - -public abstract class AudioOutputChangeListener extends AudioDeviceCallback { +public class AudioOutputChangeListener extends AudioDeviceCallback { private static final String TAG = "AudioFx-" + AudioOutputChangeListener.class.getSimpleName(); @@ -24,44 +25,73 @@ public abstract class AudioOutputChangeListener extends AudioDeviceCallback { private final Handler mHandler; private int mLastDevice = -1; + private final ArrayList<AudioOutputChangedCallback> mCallbacks = new ArrayList<AudioOutputChangedCallback>(); + + public interface AudioOutputChangedCallback { + public void onAudioOutputChanged(boolean firstChange, AudioDeviceInfo outputDevice); + } + public AudioOutputChangeListener(Context context, Handler handler) { mContext = context; mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); mHandler = handler; } - public void register() { - mAudioManager.registerAudioDeviceCallback(this, mHandler); - callback(); + public void addCallback(AudioOutputChangedCallback... callbacks) { + synchronized (mCallbacks) { + boolean initial = mCallbacks.size() == 0; + mCallbacks.addAll(Arrays.asList(callbacks)); + if (initial) { + mAudioManager.registerAudioDeviceCallback(this, mHandler); + } + } } - public void unregister() { - mAudioManager.unregisterAudioDeviceCallback(this); + public void removeCallback(AudioOutputChangedCallback... callbacks) { + synchronized (mCallbacks) { + mCallbacks.removeAll(Arrays.asList(callbacks)); + if (mCallbacks.size() == 0) { + mAudioManager.unregisterAudioDeviceCallback(this); + } + } } - public abstract void onAudioOutputChanged(boolean firstChange, AudioDeviceInfo outputType); - private void callback() { - AudioDeviceInfo device = getCurrentDevice(); + synchronized (mCallbacks) { + final AudioDeviceInfo device = getCurrentDevice(); - if (device == null) { - Log.w(TAG, "Unable to determine audio device!"); - return; - } + if (device == null) { + Log.w(TAG, "Unable to determine audio device!"); + return; + } - if (mInitial || device.getId() != mLastDevice) { - Log.d(TAG, "onAudioOutputChanged id: " + device.getId() + - " type: " + device.getType() + - " name: " + device.getProductName() + - " address: " + device.getAddress() + - " [" + device.toString() + "]"); - mLastDevice = device.getId(); - onAudioOutputChanged(mInitial, device); + if (mInitial || device.getId() != mLastDevice) { + Log.d(TAG, "onAudioOutputChanged id: " + device.getId() + + " type: " + device.getType() + + " name: " + device.getProductName() + + " address: " + device.getAddress() + + " [" + device.toString() + "]"); + mLastDevice = device.getId(); + mHandler.post(new Runnable() { + @Override + public void run() { + synchronized (mCallbacks) { + for (AudioOutputChangedCallback callback : mCallbacks) { + callback.onAudioOutputChanged(mInitial, device); + } + } + } + }); + + if (mInitial) { + mInitial = false; + } + } } + } - if (mInitial) { - mInitial = false; - } + public void refresh() { + callback(); } @Override diff --git a/src/com/cyngn/audiofx/service/DevicePreferenceManager.java b/src/com/cyngn/audiofx/service/DevicePreferenceManager.java index d774bee..759828a 100644 --- a/src/com/cyngn/audiofx/service/DevicePreferenceManager.java +++ b/src/com/cyngn/audiofx/service/DevicePreferenceManager.java @@ -27,11 +27,13 @@ import static com.cyngn.audiofx.Constants.SAVED_DEFAULTS; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.media.AudioDeviceInfo; import android.text.TextUtils; import android.util.Log; import com.cyngn.audiofx.Constants; import com.cyngn.audiofx.R; +import com.cyngn.audiofx.activity.MasterConfigControl; import com.cyngn.audiofx.backends.EffectSet; import com.cyngn.audiofx.backends.EffectsFactory; @@ -40,17 +42,17 @@ import java.util.Arrays; import java.util.List; import java.util.Locale; -class DevicePreferenceManager { +class DevicePreferenceManager implements AudioOutputChangeListener.AudioOutputChangedCallback { private static final String TAG = AudioFxService.TAG; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private final Context mContext; - private final SessionManager mSessionManager; - public DevicePreferenceManager(Context context, SessionManager sm) { + private AudioDeviceInfo mCurrentDevice; + + public DevicePreferenceManager(Context context) { mContext = context; - mSessionManager = sm; } public boolean initDefaults() { @@ -65,8 +67,14 @@ class DevicePreferenceManager { return true; } - public String getCurrentDeviceIdentifier() { - return mSessionManager.getCurrentDeviceIdentifier(); + @Override + public void onAudioOutputChanged(boolean firstChange, AudioDeviceInfo outputDevice) { + mCurrentDevice = outputDevice; + } + + public SharedPreferences getCurrentDevicePrefs() { + return mContext.getSharedPreferences( + MasterConfigControl.getDeviceIdentifierString(mCurrentDevice), 0); } public SharedPreferences prefsFor(final String name) { @@ -78,7 +86,7 @@ class DevicePreferenceManager { } public boolean isGlobalEnabled() { - return prefsFor(getCurrentDeviceIdentifier()).getBoolean(DEVICE_AUDIOFX_GLOBAL_ENABLE, false); + return getCurrentDevicePrefs().getBoolean(DEVICE_AUDIOFX_GLOBAL_ENABLE, false); } /** diff --git a/src/com/cyngn/audiofx/service/SessionManager.java b/src/com/cyngn/audiofx/service/SessionManager.java index ade6eb4..a05d5d4 100644 --- a/src/com/cyngn/audiofx/service/SessionManager.java +++ b/src/com/cyngn/audiofx/service/SessionManager.java @@ -12,7 +12,6 @@ import static com.cyngn.audiofx.Constants.DEVICE_AUDIOFX_VIRTUALIZER_ENABLE; import static com.cyngn.audiofx.Constants.DEVICE_AUDIOFX_VIRTUALIZER_STRENGTH; import static com.cyngn.audiofx.Constants.DEVICE_DEFAULT_GLOBAL_ENABLE; import static com.cyngn.audiofx.activity.MasterConfigControl.getDeviceIdentifierString; -import static com.cyngn.audiofx.service.AudioFxService.ACTION_DEVICE_OUTPUT_CHANGED; import static com.cyngn.audiofx.service.AudioFxService.ALL_CHANGED; import static com.cyngn.audiofx.service.AudioFxService.BASS_BOOST_CHANGED; import static com.cyngn.audiofx.service.AudioFxService.ENABLE_REVERB; @@ -23,7 +22,6 @@ import static com.cyngn.audiofx.service.AudioFxService.VIRTUALIZER_CHANGED; import static com.cyngn.audiofx.service.AudioFxService.VOLUME_BOOST_CHANGED; import android.content.Context; -import android.content.Intent; import android.content.SharedPreferences; import android.media.AudioDeviceInfo; import android.media.AudioManager; @@ -32,7 +30,6 @@ import android.media.audiofx.PresetReverb; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.util.SparseArray; @@ -43,23 +40,23 @@ import com.cyngn.audiofx.eq.EqUtils; import cyanogenmod.media.AudioSessionInfo; import cyanogenmod.media.CMAudioManager; -class SessionManager extends AudioOutputChangeListener { +class SessionManager implements AudioOutputChangeListener.AudioOutputChangedCallback { private static final String TAG = AudioFxService.TAG; private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private final Context mContext; + private final Handler mHandler; + private final DevicePreferenceManager mDevicePrefs; + private final CMAudioManager mCMAudio; /** * All fields ending with L should be locked on {@link #mAudioSessionsL} */ private final SparseArray<EffectSet> mAudioSessionsL = new SparseArray<EffectSet>(); - private Handler mHandler; - private final CMAudioManager mCMAudio; - private AudioDeviceInfo mCurrentDevice; - private AudioDeviceInfo mPreviousDevice; + private AudioDeviceInfo mCurrentDevice = null; // audio priority handler messages private static final int MSG_UPDATE_DSP = 100; @@ -68,25 +65,17 @@ class SessionManager extends AudioOutputChangeListener { private static final int MSG_UPDATE_FOR_SESSION = 103; private static final int MSG_UPDATE_EQ_OVERRIDE = 104; - public SessionManager(Context context, Handler handler) { - super (context, handler); + public SessionManager(Context context, Handler handler, DevicePreferenceManager devicePrefs) { mContext = context; - mCMAudio = CMAudioManager.getInstance(context); - + mDevicePrefs = devicePrefs; mHandler = new Handler(handler.getLooper(), new AudioServiceHandler()); - register(); } public void onDestroy() { synchronized (mAudioSessionsL) { - unregister(); - - if (mHandler != null) { - mHandler.removeCallbacksAndMessages(null); - mHandler.getLooper().quit(); - mHandler = null; - } + mHandler.removeCallbacksAndMessages(null); + mHandler.getLooper().quit(); } } @@ -101,10 +90,6 @@ class SessionManager extends AudioOutputChangeListener { mHandler.obtainMessage(MSG_UPDATE_EQ_OVERRIDE, band, 0, level).sendToTarget(); } - public AudioDeviceInfo getPreviousDevice() { - return mPreviousDevice; - } - /** * Callback which listens for session updates from AudioPolicyManager. This is a * feature added by CM which notifies when sessions are created or @@ -148,7 +133,7 @@ class SessionManager extends AudioOutputChangeListener { } public String getCurrentDeviceIdentifier() { - return getDeviceIdentifierString(getCurrentDevice()); + return getDeviceIdentifierString(mCurrentDevice); } public boolean hasActiveSessions() { @@ -173,7 +158,7 @@ class SessionManager extends AudioOutputChangeListener { throw new IllegalStateException("updateBackend must not be called on the UI thread!"); } - final SharedPreferences prefs = mContext.getSharedPreferences(getCurrentDeviceIdentifier(), 0); + final SharedPreferences prefs = mDevicePrefs.getCurrentDevicePrefs(); if (DEBUG) { Log.i(TAG, "+++ updateBackend() called with flags=[" + flags + "], session=[" + session + "]"); @@ -286,10 +271,11 @@ class SessionManager extends AudioOutputChangeListener { @Override public boolean handleMessage(Message msg) { - EffectSet session = null; - Integer sessionId = 0; - int flags = 0; synchronized (mAudioSessionsL) { + EffectSet session = null; + Integer sessionId = 0; + int flags = 0; + switch (msg.what) { case MSG_ADD_SESSION: /** @@ -393,33 +379,21 @@ class SessionManager extends AudioOutputChangeListener { @Override public void onAudioOutputChanged(boolean firstChange, AudioDeviceInfo outputDevice) { synchronized (mAudioSessionsL) { - mPreviousDevice = mCurrentDevice; - mCurrentDevice = outputDevice; + if (mCurrentDevice == null || + (outputDevice != null && mCurrentDevice.getId() != outputDevice.getId())) { + mCurrentDevice = outputDevice; + } - int sessionId = 0; EffectSet session = null; // Update all the sessions for this output which are moving final int N = mAudioSessionsL.size(); for (int i = 0; i < N; i++) { - sessionId = mAudioSessionsL.keyAt(i); session = mAudioSessionsL.valueAt(i); - if (DEBUG) Log.d(TAG, "UPDATE_DEVICE prev=" + - (mPreviousDevice == null ? "none" : mPreviousDevice.getType()) + - " new=" + (mCurrentDevice == null ? "none" : mCurrentDevice.getType() + - " session=" + sessionId + " session-device=" + - (session.getDevice() == null ? "none" : session.getDevice().getType()))); session.setDevice(mCurrentDevice); updateBackendLocked(ALL_CHANGED, session); } } - - if (DEBUG) Log.d(TAG, "Broadcasting device changed event"); - - Intent intent = new Intent(ACTION_DEVICE_OUTPUT_CHANGED); - intent.putExtra("device", mCurrentDevice.getId()); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent); } - } |