Commit 48749e6c authored by Sébastien Toque's avatar Sébastien Toque
Browse files

fix callback leaks + don't store the context in AudioServiceController

parent 6e1cbb8d
......@@ -43,6 +43,18 @@ public class AudioActivityGroup extends ActivityGroup {
startChildAcitvity("AudioBrowserActivity", intent);
}
@Override
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
super.onResume();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
public void startChildAcitvity(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(
id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
......@@ -80,10 +92,4 @@ public class AudioActivityGroup extends ActivityGroup {
super.onBackPressed();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService();
super.onPause();
}
}
......@@ -75,6 +75,25 @@ public class AudioListActivity extends ListActivity {
updateList();
}
@Override
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
super.onResume();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
@Override
protected void onDestroy() {
mMediaLibrary.removeUpdateHandler(mHandler);
mSongsAdapter.clear();
super.onDestroy();
}
public static void set(Intent intent, String name, int mode) {
intent.putExtra(EXTRA_NAME, name);
intent.putExtra(EXTRA_MODE, mode);
......@@ -129,13 +148,6 @@ public class AudioListActivity extends ListActivity {
return super.onContextItemSelected(item);
}
@Override
protected void onDestroy() {
mMediaLibrary.removeUpdateHandler(mHandler);
mSongsAdapter.clear();
super.onDestroy();
}
/**
* Handle changes on the list
*/
......
......@@ -35,7 +35,7 @@ import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class AudioPlayerActivity extends Activity implements AudioPlayer {
public final static String TAG = "VLC/AudioPlayerActiviy";
public final static String TAG = "VLC/AudioPlayerActivity";
private ImageView mCover;
private TextView mTitle;
......@@ -89,16 +89,18 @@ public class AudioPlayerActivity extends Activity implements AudioPlayer {
}
@Override
protected void onStart() {
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
mAudioController.addAudioPlayer(this);
update();
super.onStart();
super.onResume();
}
@Override
protected void onStop() {
protected void onPause() {
mAudioController.removeAudioPlayer(this);
super.onStop();
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
@Override
......@@ -243,10 +245,4 @@ public class AudioPlayerActivity extends Activity implements AudioPlayer {
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService();
super.onPause();
}
}
......@@ -22,6 +22,7 @@ package org.videolan.vlc.android;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
......@@ -60,7 +61,7 @@ public class AudioService extends Service {
private ArrayList<Media> mMediaList;
private Stack<Media> mPrevious;
private Media mCurrentMedia;
private ArrayList<IAudioServiceCallback> mCallback;
private HashMap<IAudioServiceCallback, Integer> mCallback;
private EventManager mEventManager;
private Notification mNotification;
private boolean mShuffling = false;
......@@ -78,7 +79,7 @@ public class AudioService extends Service {
e.printStackTrace();
}
mCallback = new ArrayList<IAudioServiceCallback>();
mCallback = new HashMap<IAudioServiceCallback, Integer>();
mMediaList = new ArrayList<Media>();
mPrevious = new Stack<Media>();
mEventManager = EventManager.getIntance();
......@@ -193,9 +194,9 @@ public class AudioService extends Service {
}
private void executeUpdate(Boolean updateWidget) {
for (int i = 0; i < mCallback.size(); i++) {
for (IAudioServiceCallback callback : mCallback.keySet()) {
try {
mCallback.get(i).update();
callback.update();
} catch (RemoteException e) {
e.printStackTrace();
}
......@@ -418,18 +419,25 @@ public class AudioService extends Service {
}
@Override
public void addAudioCallback(IAudioServiceCallback cb)
public synchronized void addAudioCallback(IAudioServiceCallback cb)
throws RemoteException {
mCallback.add(cb);
Integer count = mCallback.get(cb);
if (count == null)
count = 0;
mCallback.put(cb, count + 1);
executeUpdate();
}
@Override
public void removeAudioCallback(IAudioServiceCallback cb)
public synchronized void removeAudioCallback(IAudioServiceCallback cb)
throws RemoteException {
if (mCallback.contains(cb)) {
Integer count = mCallback.get(cb);
if (count == null)
count = 0;
if (count > 1)
mCallback.put(cb, count - 1);
else
mCallback.remove(cb);
}
}
@Override
......
......@@ -24,7 +24,6 @@ import java.util.ArrayList;
import java.util.List;
import org.videolan.vlc.android.AudioPlayer.AudioPlayerControl;
import org.videolan.vlc.android.AudioPlayer;
import android.content.ComponentName;
import android.content.Context;
......@@ -42,7 +41,6 @@ public class AudioServiceController implements AudioPlayerControl {
private static AudioServiceController mInstance;
private static boolean mIsBound = false;
private Context mContext;
private IAudioService mAudioServiceBinder;
private ServiceConnection mAudioServiceConnection;
private ArrayList<AudioPlayer> mAudioPlayer;
......@@ -55,9 +53,6 @@ public class AudioServiceController implements AudioPlayerControl {
private AudioServiceController() {
// Get context from MainActivity
mContext = MainActivity.getInstance();
mAudioPlayer = new ArrayList<AudioPlayer>();
// Setup audio service connection
......@@ -91,9 +86,6 @@ public class AudioServiceController implements AudioPlayerControl {
if (mInstance == null) {
mInstance = new AudioServiceController();
}
if (!mIsBound) {
mInstance.bindAudioService();
}
return mInstance;
}
......@@ -125,15 +117,17 @@ public class AudioServiceController implements AudioPlayerControl {
/**
* Bind to audio service if it is running
*/
public void bindAudioService() {
if (mContext == null) {
public void bindAudioService(Context context) {
if (context == null) {
Log.w(TAG, "bindAudioService() with null Context. Ooops" );
return;
}
context = context.getApplicationContext();
if (mAudioServiceBinder == null) {
Intent service = new Intent(mContext, AudioService.class);
mContext.startService(service);
mIsBound = mContext.bindService(service, mAudioServiceConnection, Context.BIND_AUTO_CREATE);
Intent service = new Intent(context, AudioService.class);
context.startService(service);
mIsBound = context.bindService(service, mAudioServiceConnection, Context.BIND_AUTO_CREATE);
} else {
// Register controller to the service
try {
......@@ -144,12 +138,18 @@ public class AudioServiceController implements AudioPlayerControl {
}
}
public void unbindAudioService() {
public void unbindAudioService(Context context) {
if (context == null) {
Log.w(TAG, "unbindAudioService() with null Context. Ooops" );
return;
}
context = context.getApplicationContext();
if (mAudioServiceBinder != null) {
try {
mAudioServiceBinder.removeAudioCallback(mCallback);
if (mIsBound) {
mContext.unbindService(mAudioServiceConnection);
context.unbindService(mAudioServiceConnection);
mIsBound = false;
}
} catch (RemoteException e) {
......@@ -181,8 +181,8 @@ public class AudioServiceController implements AudioPlayerControl {
* Update all AudioPlayer
*/
private void updateAudioPlayer() {
for (int i = 0; i < mAudioPlayer.size(); i++)
mAudioPlayer.get(i).update();
for (AudioPlayer player : mAudioPlayer)
player.update();
}
public void stop() {
......
......@@ -141,15 +141,15 @@ public class MainActivity extends TabActivity {
}
@Override
protected void onStart() {
protected void onResume() {
mAudioController.addAudioPlayer(mAudioPlayer);
super.onStart();
super.onResume();
}
@Override
protected void onStop() {
protected void onPause() {
mAudioController.removeAudioPlayer(mAudioPlayer);
super.onStop();
super.onPause();
}
/** Create menu from XML
......
......@@ -80,6 +80,18 @@ public class SearchActivity extends ListActivity {
mSearchText.requestFocus();
}
@Override
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
super.onResume();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
@Override
protected void onDestroy() {
if (mHistoryAdapter != null)
......@@ -261,5 +273,4 @@ public class SearchActivity extends ListActivity {
return super.onKeyDown(keyCode, event);
}
}
......@@ -46,6 +46,18 @@ public class VideoActivityGroup extends ActivityGroup {
startChildAcitvity("VideoListActivity", intent);
}
@Override
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
super.onResume();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
public void startChildAcitvity(String id, Intent intent) {
Window window = getLocalActivityManager().startActivity(
id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
......@@ -86,10 +98,4 @@ public class VideoActivityGroup extends ActivityGroup {
}
super.onBackPressed();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService();
super.onPause();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment