Commit 62fcff53 authored by Sébastien Toque's avatar Sébastien Toque
Browse files

remove almost all usages of static MainActivity

see here : http://developer.android.com/resources/articles/avoiding-memory-leaks.html
parent 80d62a88
......@@ -85,7 +85,7 @@ public class AudioBrowserActivity extends Activity implements Sortable {
mHeader = (HorizontalScrollView) findViewById(R.id.header);
mAudioController = AudioServiceController.getInstance();
mMediaLibrary = MediaLibrary.getInstance();
mMediaLibrary = MediaLibrary.getInstance(this);
mMediaLibrary.addUpdateHandler(mHandler);
mSongsAdapter = new AudioSongsListAdapter(this, R.layout.audio_browser_item);
......@@ -284,7 +284,7 @@ public class AudioBrowserActivity extends Activity implements Sortable {
};
private void updateLists() {
List<Media> audioList = MediaLibrary.getInstance().getAudioItems();
List<Media> audioList = MediaLibrary.getInstance(this).getAudioItems();
mSongsAdapter.clear();
mArtistsAdapter.clear();
mAlbumsAdapter.clear();
......
......@@ -63,7 +63,7 @@ public class AudioListActivity extends ListActivity {
mAudioController = AudioServiceController.getInstance();
mMediaLibrary = MediaLibrary.getInstance();
mMediaLibrary = MediaLibrary.getInstance(this);
mMediaLibrary.addUpdateHandler(mHandler);
mTitle = (TextView) findViewById(R.id.title);
......@@ -189,11 +189,11 @@ public class AudioListActivity extends ListActivity {
if (name == null || mode == 0) {
mTitle.setText(R.string.songs);
itemList = AudioServiceController.getInstance().getItems();
audioList = MediaLibrary.getInstance().getMediaItems(itemList);
audioList = MediaLibrary.getInstance(this).getMediaItems(itemList);
}
else {
mTitle.setText(name);
audioList = MediaLibrary.getInstance().getAudioItems(name, mode);
audioList = MediaLibrary.getInstance(this).getAudioItems(name, mode);
}
mSongsAdapter.clear();
......
......@@ -74,6 +74,7 @@ public class AudioService extends Service {
// Get libVLC instance
try {
LibVLC.useIOMX(this);
mLibVLC = LibVLC.getInstance();
} catch (LibVlcException e) {
e.printStackTrace();
......@@ -460,7 +461,7 @@ public class AudioService extends Service {
mEventManager.addHandler(mEventHandler);
mMediaList.clear();
mPrevious.clear();
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(AudioService.this);
for (int i = 0; i < mediaPathList.size(); i++) {
String path = mediaPathList.get(i);
Media media = db.getMedia(AudioService.this, path);
......@@ -483,7 +484,7 @@ public class AudioService extends Service {
return;
}
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(AudioService.this);
for (int i = 0; i < mediaPathList.size(); i++) {
String path = mediaPathList.get(i);
Media media = db.getMedia(AudioService.this, path);
......
......@@ -52,34 +52,7 @@ public class AudioServiceController implements AudioPlayerControl {
};
private AudioServiceController() {
mAudioPlayer = new ArrayList<AudioPlayer>();
// Setup audio service connection
mAudioServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "Service Disconnected");
mAudioServiceBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "Service Connected");
mAudioServiceBinder = IAudioService.Stub.asInterface(service);
// Register controller to the service
try {
mAudioServiceBinder.addAudioCallback(mCallback);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.getInstance());
boolean enableHS = prefs.getBoolean("enable_headset_detection", true);
AudioServiceController.getInstance().detectHeadset(enableHS);
} catch (RemoteException e) {
Log.e(TAG, "remote procedure call failed: addAudioCallback()");
}
updateAudioPlayer();
}
};
}
public static AudioServiceController getInstance() {
......@@ -127,6 +100,34 @@ public class AudioServiceController implements AudioPlayerControl {
if (!mIsBound) {
Intent service = new Intent(context, AudioService.class);
context.startService(service);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean enableHS = prefs.getBoolean("enable_headset_detection", true);
// Setup audio service connection
mAudioServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "Service Disconnected");
mAudioServiceBinder = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "Service Connected");
mAudioServiceBinder = IAudioService.Stub.asInterface(service);
// Register controller to the service
try {
mAudioServiceBinder.addAudioCallback(mCallback);
mAudioServiceBinder.detectHeadset(enableHS);
} catch (RemoteException e) {
Log.e(TAG, "remote procedure call failed: addAudioCallback()");
}
updateAudioPlayer();
}
};
mIsBound = context.bindService(service, mAudioServiceConnection, Context.BIND_AUTO_CREATE);
} else {
// Register controller to the service
......@@ -154,6 +155,7 @@ public class AudioServiceController implements AudioPlayerControl {
context.unbindService(mAudioServiceConnection);
mIsBound = false;
mAudioServiceBinder = null;
mAudioServiceConnection = null;
}
}
......
......@@ -104,7 +104,7 @@ public class BrowserActivity extends ListActivity {
mScollStates.push(new ScrollState(index, top));
openDir(file);
} else {
Util.toaster(R.string.nosubdirectory);
Util.toaster(this, R.string.nosubdirectory);
}
}
......@@ -127,7 +127,7 @@ public class BrowserActivity extends ListActivity {
@Override
protected void onStop() {
// Update the MediaList
MediaLibrary.getInstance().loadMediaItems();
MediaLibrary.getInstance(this).loadMediaItems();
super.onStop();
}
......
......@@ -67,7 +67,7 @@ public class BrowserAdapter extends ArrayAdapter<File>
holder = (ViewHolder) view.getTag();
final File item = getItem(position);
final DatabaseManager dbManager = DatabaseManager.getInstance();
final DatabaseManager dbManager = DatabaseManager.getInstance(view.getContext());
if (item != null && item.getName() != null) {
holder.text.setText(item.getName());
......@@ -98,7 +98,7 @@ public class BrowserAdapter extends ArrayAdapter<File>
private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final DatabaseManager dbManager = DatabaseManager.getInstance();
final DatabaseManager dbManager = DatabaseManager.getInstance(buttonView.getContext());
File item = (File) buttonView.getTag();
if (item == null)
return;
......
......@@ -89,10 +89,9 @@ public class DatabaseManager {
this.mDb = helper.getWritableDatabase();
}
public synchronized static DatabaseManager getInstance() {
public synchronized static DatabaseManager getInstance(Context context) {
if (instance == null) {
Context context = MainActivity.getInstance();
instance = new DatabaseManager(context);
instance = new DatabaseManager(context.getApplicationContext());
}
return instance;
}
......@@ -212,26 +211,6 @@ public class DatabaseManager {
new String[] { playlistName, mediaPath });
}
public Media[] getMediaFromPlaylist(String playlistName) {
ArrayList<Media> media = new ArrayList<Media>();
Cursor cursor = mDb.query(PLAYLIST_MEDIA_PLAYLISTNAME,
new String[] { PLAYLIST_MEDIA_MEDIAPATH },
PLAYLIST_MEDIA_PLAYLISTNAME + "=?", new String[] { playlistName },
null, null, "ASC");
MediaLibrary mediaLibrary = MediaLibrary.getInstance();
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
media.add(mediaLibrary.getMediaItem(cursor.getString(0)));
} while (cursor.moveToNext());
}
cursor.close();
return (Media[]) media.toArray();
}
/**
* Add a new media to the database. The picture can only added by update.
* @param meida which you like to add to the database
......
......@@ -23,12 +23,14 @@ import org.videolan.vlc.android.LibVlcException;
import android.util.Log;
import android.view.Surface;
import android.preference.PreferenceManager;
import android.content.Context;
import android.os.Build;
public class LibVLC {
private static final String TAG = "VLC/LibVLC";
private static LibVLC sInstance;
private static boolean sUseIomx = false;
/** libVLC instance C pointer */
private int mLibVlcInstance = 0; // Read-only, reserved for JNI
......@@ -120,11 +122,15 @@ public class LibVLC {
*
*/
public boolean useIOMX() {
MainActivity activity = MainActivity.getInstance();
boolean useIomx = false;
if (activity != null)
useIomx = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("enable_iomx", false);
return useIomx;
return sUseIomx;
}
public static synchronized void useIOMX(boolean enable) {
sUseIomx = enable;
}
public static synchronized void useIOMX(Context context) {
sUseIomx = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("enable_iomx", false);
}
/**
......
......@@ -81,6 +81,7 @@ public class MainActivity extends TabActivity {
/* Get settings */
mSettings = PreferenceManager.getDefaultSharedPreferences(this);
LibVLC.useIOMX(this);
/* Initialize variables */
mInstance = this;
......@@ -132,7 +133,7 @@ public class MainActivity extends TabActivity {
}
/* Load media items from database and storage */
MediaLibrary.getInstance().loadMediaItems();
MediaLibrary.getInstance(this).loadMediaItems();
}
@Override
......@@ -208,7 +209,7 @@ public class MainActivity extends TabActivity {
break;
// Refresh
case R.id.ml_menu_refresh:
MediaLibrary.getInstance().loadMediaItems();
MediaLibrary.getInstance(this).loadMediaItems();
break;
}
return super.onOptionsItemSelected(item);
......@@ -223,8 +224,10 @@ public class MainActivity extends TabActivity {
} else {
mAudioController.play();
}
break;
case R.id.show_player:
// TODO: start audio player activity
Intent intent = new Intent(this, AudioPlayerActivity.class);
startActivity(intent);
break;
case R.id.hide_mini_player:
hideAudioPlayer();
......
......@@ -109,7 +109,7 @@ public class Media implements Comparable<Media> {
}
// Add this item to database
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(context);
db.addMedia(this);
}
......@@ -179,9 +179,9 @@ public class Media implements Comparable<Media> {
return mPicture;
}
public void setPicture(Bitmap p) {
public void setPicture(Context context, Bitmap p) {
Log.d(TAG, "Set new picture for " + getTitle());
DatabaseManager.getInstance().updateMedia(
DatabaseManager.getInstance(context).updateMedia(
mFile.getPath(),
DatabaseManager.mediaColumn.MEDIA_PICTURE,
p);
......
......@@ -44,7 +44,7 @@ public class MediaInfoActivity extends Activity {
String path = getIntent().getExtras().getString("filePath");
if (path == null)
return;
mItem = MediaLibrary.getInstance().getMediaItem(path);
mItem = MediaLibrary.getInstance(this).getMediaItem(path);
// set title
TextView titleView = (TextView) findViewById(R.id.title);
......
......@@ -30,6 +30,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Stack;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Handler;
......@@ -46,11 +47,11 @@ public class MediaLibrary {
private ArrayList<Handler> mUpdateHandler;
protected Thread mLoadingThread;
private MediaLibrary() {
private MediaLibrary(Context context) {
mInstance = this;
mItemList = new ArrayList<Media>();
mUpdateHandler = new ArrayList<Handler>();
mDBManager = DatabaseManager.getInstance();
mDBManager = DatabaseManager.getInstance(context);
}
public void loadMediaItems() {
......@@ -60,9 +61,9 @@ public class MediaLibrary {
}
}
public static MediaLibrary getInstance() {
public static MediaLibrary getInstance(Context context) {
if (mInstance == null)
mInstance = new MediaLibrary();
mInstance = new MediaLibrary(context);
return mInstance;
}
......
......@@ -44,8 +44,7 @@ public class PreferencesActivity extends PreferenceActivity {
new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(MainActivity.getInstance(),
BrowserActivity.class);
Intent intent = new Intent(getApplicationContext(), BrowserActivity.class);
startActivity(intent);
return true;
}
......@@ -64,7 +63,7 @@ public class PreferencesActivity extends PreferenceActivity {
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(getApplicationContext());
db.clearSearchhistory();
}
})
......@@ -74,6 +73,17 @@ public class PreferencesActivity extends PreferenceActivity {
}
});
// HW decoding
CheckBoxPreference checkboxHW = (CheckBoxPreference) findPreference("enable_iomx");
checkboxHW.setOnPreferenceClickListener(
new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference checkboxHW = (CheckBoxPreference) preference;
LibVLC.useIOMX(checkboxHW.isChecked());
return true;
}
});
// Headset detection option
CheckBoxPreference checkboxHS = (CheckBoxPreference) findPreference("enable_headset_detection");
checkboxHS.setOnPreferenceClickListener(
......@@ -97,4 +107,15 @@ public class PreferencesActivity extends PreferenceActivity {
});
}
@Override
protected void onResume() {
AudioServiceController.getInstance().bindAudioService(this);
super.onResume();
}
@Override
protected void onPause() {
AudioServiceController.getInstance().unbindAudioService(this);
super.onPause();
}
}
......@@ -106,7 +106,7 @@ public class SearchActivity extends ListActivity {
// set result adapter to the list
mResultAdapter.clear();
String[] keys = key.toString().split("\\s+");
ArrayList<Media> allItems = MediaLibrary.getInstance().getMediaItems();
ArrayList<Media> allItems = MediaLibrary.getInstance(this).getMediaItems();
int results = 0;
for (int i = 0; i < allItems.size(); i++) {
Media item = allItems.get(i);
......@@ -157,7 +157,7 @@ public class SearchActivity extends ListActivity {
String headerText = getString(R.string.search_history);
showListHeader(headerText);
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(this);
mHistory.clear();
mHistory.addAll(db.getSearchhistory(20));
if (mHistoryAdapter == null) {
......@@ -211,7 +211,7 @@ public class SearchActivity extends ListActivity {
switch (item.getItemId()) {
// Sort by name
case R.id.search_clear_history:
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(this);
db.clearSearchhistory();
if (mHistoryAdapter == getListAdapter())
showSearchHistory();
......@@ -236,7 +236,7 @@ public class SearchActivity extends ListActivity {
mSearchText.requestFocus();
} else if (getListAdapter() == mResultAdapter) {
// add search text to the database (history)
DatabaseManager db = DatabaseManager.getInstance();
DatabaseManager db = DatabaseManager.getInstance(this);
db.addSearchhistoryItem(mSearchText.getText().toString());
// open media in the player
......
......@@ -132,7 +132,7 @@ public class ThumbnailerManager extends Thread {
Log.i(TAG, "Thumbnail created!");
item.setPicture(thumbnail);
item.setPicture(mVideoListActivity, thumbnail);
mVideoListActivity.mItemToUpdate = item;
// Post to the file browser the new item.
mVideoListActivity.mHandler.sendEmptyMessage(
......
......@@ -30,27 +30,13 @@ import android.widget.Toast;
public class Util {
/** A set of utility functions for the VLC application */
@Deprecated
public static void toaster(String message, int duration) {
Toast.makeText(MainActivity.getInstance(),
message, duration)
.show();
}
@Deprecated
public static void toaster(String message) {
toaster(message, Toast.LENGTH_SHORT);
}
/** Print an on-screen message to alert the user */
public static void toaster(int stringId, int duration) {
Toast.makeText(MainActivity.getInstance(),
stringId, duration)
.show();
public static void toaster(Context context, int stringId, int duration) {
Toast.makeText(context, stringId, duration).show();
}
public static void toaster(int stringId) {
toaster(stringId, Toast.LENGTH_SHORT);
public static void toaster(Context context, int stringId) {
toaster(context, stringId, Toast.LENGTH_SHORT);
}
/**
......
......@@ -57,7 +57,7 @@ public class VideoListActivity extends ListActivity implements Sortable {
mNoFileLayout = (LinearLayout) findViewById(R.id.video_list_empty_nofile);
mLoadFileLayout = (LinearLayout) findViewById(R.id.video_list_empty_loadfile);
mMediaLibrary = MediaLibrary.getInstance();
mMediaLibrary = MediaLibrary.getInstance(this);
mMediaLibrary.addUpdateHandler(mHandler);
mThumbnailerManager = new ThumbnailerManager(this);
......@@ -120,8 +120,6 @@ public class VideoListActivity extends ListActivity implements Sortable {
private void updateList() {
MainActivity mainActivity = MainActivity.getInstance();
List<Media> itemList = mMediaLibrary.getVideoItems();
mVideoAdapter.clear();
......@@ -139,9 +137,6 @@ public class VideoListActivity extends ListActivity implements Sortable {
mLoadFileLayout.setVisibility(View.INVISIBLE);
mNoFileLayout.setVisibility(View.VISIBLE);
}
mainActivity.mHandler.sendEmptyMessage(MainActivity.HIDE_PROGRESSBAR);
}
public void sortBy(int sortby) {
......
......@@ -130,7 +130,7 @@ public class VideoListAdapter extends ArrayAdapter<Media>
holder.thumbnail.setImageBitmap(thumbnail);
} else {
// set default thumbnail
thumbnail = BitmapFactory.decodeResource(MainActivity.getInstance().getResources(), R.drawable.thumbnail);
thumbnail = BitmapFactory.decodeResource(v.getResources(), R.drawable.thumbnail);
holder.thumbnail.setImageBitmap(thumbnail);
}
......
......@@ -160,6 +160,7 @@ public class VideoPlayerActivity extends Activity {
registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
try {
LibVLC.useIOMX(this);
mLibVLC = LibVLC.getInstance();
} catch (LibVlcException e) {
e.printStackTrace();
......
......@@ -22,7 +22,6 @@ package org.videolan.vlc.android.widget;
import org.videolan.vlc.android.AudioPlayer;
import org.videolan.vlc.android.AudioPlayerActivity;
import org.videolan.vlc.android.MainActivity;
import org.videolan.vlc.android.R;
import android.content.Context;
......@@ -131,7 +130,7 @@ public class AudioMiniPlayer extends LinearLayout implements AudioPlayer {
@Override
protected void onCreateContextMenu(ContextMenu menu) {
MenuInflater inflater = MainActivity.getInstance().getMenuInflater();
MenuInflater inflater = new MenuInflater(getContext());
inflater.inflate(R.menu.audio_player_mini, menu);
MenuItem hmi = menu.findItem(R.id.hide_mini_player);
MenuItem pp = menu.findItem(R.id.play_pause);
......
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