aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaneren2005 <daneren2005@gmail.com>2013-08-22 09:33:32 -0700
committerdaneren2005 <daneren2005@gmail.com>2013-08-22 09:33:32 -0700
commit2987d426798a4132597d3c13e9f1fd79526885f2 (patch)
tree13db68a3426485c4655931f6bcb1f6eaa8bd031a /src
parent4afb4b8cd6ac2d7ed6bff5ccc3aa1972cb4e143c (diff)
downloaddsub-2987d426798a4132597d3c13e9f1fd79526885f2.tar.gz
dsub-2987d426798a4132597d3c13e9f1fd79526885f2.tar.bz2
dsub-2987d426798a4132597d3c13e9f1fd79526885f2.zip
Move the other events into eventHandler
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java113
1 files changed, 55 insertions, 58 deletions
diff --git a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java
index 3a2617f3..0c801fd6 100644
--- a/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java
+++ b/src/github/daneren2005/dsub/service/DownloadServiceLifecycleSupport.java
@@ -111,8 +111,8 @@ public class DownloadServiceLifecycleSupport {
executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleWithFixedDelay(downloadChecker, 5, 5, TimeUnit.SECONDS);
- @Override
new Thread(new Runnable() {
+ @Override
public void run() {
Looper.prepare();
eventLooper = Looper.myLooper();
@@ -127,9 +127,14 @@ public class DownloadServiceLifecycleSupport {
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Headset event for: " + intent.getExtras().get("name"));
if (intent.getExtras().getInt("state") == 0) {
- if(!downloadService.isRemoteEnabled()) {
- downloadService.pause();
- }
+ eventHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ if(!downloadService.isRemoteEnabled()) {
+ downloadService.pause();
+ }
+ }
+ });
}
}
};
@@ -217,11 +222,18 @@ public class DownloadServiceLifecycleSupport {
return;
}
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- new SerializeTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
- } else {
- new SerializeTask().execute();
- }
+ eventHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ if(lock.tryLock()) {
+ try {
+ serializeDownloadQueueNow();
+ } finally {
+ lock.unlock();
+ }
+ }
+ }
+ });
}
public void serializeDownloadQueueNow() {
@@ -238,11 +250,18 @@ public class DownloadServiceLifecycleSupport {
}
private void deserializeDownloadQueue() {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- new DeserializeTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
- } else {
- new DeserializeTask().execute();
- }
+ eventHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ lock.lock();
+ deserializeDownloadQueueNow();
+ setup.set(true);
+ } finally {
+ lock.unlock();
+ }
+ }
+ });
}
private void deserializeDownloadQueueNow() {
State state = FileUtil.deserialize(downloadService, FILENAME_DOWNLOADS_SER);
@@ -320,23 +339,28 @@ public class DownloadServiceLifecycleSupport {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
- switch (state) {
- case TelephonyManager.CALL_STATE_RINGING:
- case TelephonyManager.CALL_STATE_OFFHOOK:
- if (downloadService.getPlayerState() == PlayerState.STARTED && !downloadService.isRemoteEnabled()) {
- resumeAfterCall = true;
- downloadService.pause();
- }
- break;
- case TelephonyManager.CALL_STATE_IDLE:
- if (resumeAfterCall) {
- resumeAfterCall = false;
- downloadService.start();
- }
- break;
- default:
- break;
- }
+ eventHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ switch (state) {
+ case TelephonyManager.CALL_STATE_RINGING:
+ case TelephonyManager.CALL_STATE_OFFHOOK:
+ if (downloadService.getPlayerState() == PlayerState.STARTED && !downloadService.isRemoteEnabled()) {
+ resumeAfterCall = true;
+ downloadService.pause();
+ }
+ break;
+ case TelephonyManager.CALL_STATE_IDLE:
+ if (resumeAfterCall) {
+ resumeAfterCall = false;
+ downloadService.start();
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ });
}
}
@@ -347,31 +371,4 @@ public class DownloadServiceLifecycleSupport {
private int currentPlayingIndex;
private int currentPlayingPosition;
}
-
- private class SerializeTask extends AsyncTask<Void, Void, Void> {
- @Override
- protected Void doInBackground(Void... params) {
- if(lock.tryLock()) {
- try {
- serializeDownloadQueueNow();
- } finally {
- lock.unlock();
- }
- }
- return null;
- }
- }
- private class DeserializeTask extends AsyncTask<Void, Void, Void> {
- @Override
- protected Void doInBackground(Void... params) {
- try {
- lock.lock();
- deserializeDownloadQueueNow();
- setup.set(true);
- } finally {
- lock.unlock();
- }
- return null;
- }
- }
}