Make DownloadService state static
This commit is contained in:
1 parent
e6d1dbc1e6
commit
bdc80578cf
4 files changed
+66
-101
No files matched your search
@@ -31,7 +31,6 @@ import com.hippo.ehviewer.client.EhCookieStore;
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail;
|
||||
import com.hippo.ehviewer.client.data.LargePreviewSet;
|
||||
import com.hippo.ehviewer.download.DownloadManager;
|
||||
import com.hippo.ehviewer.download.DownloadService;
|
||||
import com.hippo.ehviewer.spider.SpiderDen;
|
||||
import com.hippo.network.StatusCodeException;
|
||||
import com.hippo.okhttp.CookieDB;
|
||||
@@ -40,8 +39,6 @@ import com.hippo.text.Html;
|
||||
import com.hippo.util.ReadableTime;
|
||||
import com.hippo.yorozuya.FileUtils;
|
||||
import com.hippo.yorozuya.SimpleHandler;
|
||||
import com.hippo.yorozuya.sparse.SparseJBArray;
|
||||
import com.hippo.yorozuya.sparse.SparseJLArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -68,12 +65,6 @@ public class EhApplication extends SceneApplication {
|
||||
private LruCacheEx<Long, Integer> mPreviewPagesCache;
|
||||
private DownloadManager mDownloadManager;
|
||||
|
||||
private SparseJBArray mItemStateArray;
|
||||
private SparseJLArray<String> mItemTitleArray;
|
||||
private int mFailedCount;
|
||||
private int mFinishedCount;
|
||||
private int mDownloadedCount;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
@@ -250,34 +241,4 @@ public class EhApplication extends SceneApplication {
|
||||
}
|
||||
return application.mDownloadManager;
|
||||
}
|
||||
|
||||
|
||||
public static void initDownloadService(Context context, DownloadService service) {
|
||||
EhApplication application = ((EhApplication) context.getApplicationContext());
|
||||
if (application.mItemStateArray == null) {
|
||||
application.mItemStateArray = new SparseJBArray();
|
||||
}
|
||||
if (application.mItemTitleArray == null) {
|
||||
application.mItemTitleArray = new SparseJLArray<>();
|
||||
}
|
||||
|
||||
service.init(application.mItemStateArray, application.mItemTitleArray,
|
||||
application.mFailedCount, application.mFinishedCount, application.mDownloadedCount);
|
||||
}
|
||||
|
||||
public static void clearDownloadService(Context context, DownloadService service) {
|
||||
EhApplication application = ((EhApplication) context.getApplicationContext());
|
||||
application.mFailedCount = 0;
|
||||
application.mFinishedCount = 0;
|
||||
application.mDownloadedCount = 0;
|
||||
service.clear();
|
||||
}
|
||||
|
||||
public static void backupDownloadService(Context context,
|
||||
int failedCount, int finishedCount, int downloadedCount) {
|
||||
EhApplication application = ((EhApplication) context.getApplicationContext());
|
||||
application.mFailedCount = failedCount;
|
||||
application.mFinishedCount = finishedCount;
|
||||
application.mDownloadedCount = downloadedCount;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.IntDef;
|
||||
@@ -81,34 +82,20 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
private NotificationDelay mDownloadedDelay;
|
||||
private NotificationDelay m509Delay;
|
||||
|
||||
@Nullable
|
||||
private SparseJBArray mItemStateArray;
|
||||
@Nullable
|
||||
private SparseJLArray<String> mItemTitleArray;
|
||||
|
||||
private int mFailedCount;
|
||||
private int mFinishedCount;
|
||||
private int mDownloadedCount;
|
||||
private final static SparseJBArray sItemStateArray = new SparseJBArray();
|
||||
private final static SparseJLArray<String> sItemTitleArray = new SparseJLArray<>();
|
||||
|
||||
public void init(SparseJBArray stateArray, SparseJLArray<String> titleArray,
|
||||
int failedCount, int finishedCount, int downloadedCount) {
|
||||
mItemStateArray = stateArray;
|
||||
mItemTitleArray = titleArray;
|
||||
mFailedCount = failedCount;
|
||||
mFinishedCount = finishedCount;
|
||||
mDownloadedCount = downloadedCount;
|
||||
}
|
||||
private static int sFailedCount;
|
||||
private static int sFinishedCount;
|
||||
private static int sDownloadedCount;
|
||||
|
||||
public void clear() {
|
||||
mFailedCount = 0;
|
||||
mFinishedCount = 0;
|
||||
mDownloadedCount = 0;
|
||||
if (mItemStateArray != null) {
|
||||
mItemStateArray.clear();
|
||||
}
|
||||
if (mItemTitleArray != null) {
|
||||
mItemTitleArray.clear();
|
||||
}
|
||||
public static void clear() {
|
||||
sFailedCount = 0;
|
||||
sFinishedCount = 0;
|
||||
sDownloadedCount = 0;
|
||||
sItemStateArray.clear();
|
||||
sItemTitleArray.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -118,7 +105,6 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
mDownloadManager = EhApplication.getDownloadManager(getApplicationContext());
|
||||
mDownloadManager.setDownloadListener(this);
|
||||
EhApplication.initDownloadService(getApplicationContext(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,10 +128,6 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
if (m509Delay != null) {
|
||||
m509Delay.release();
|
||||
}
|
||||
EhApplication.backupDownloadService(getApplicationContext(),
|
||||
mFailedCount, mFinishedCount, mDownloadedCount);
|
||||
mItemStateArray = null;
|
||||
mItemTitleArray = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -204,7 +186,7 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
mDownloadManager.deleteRangeDownload(gidList);
|
||||
}
|
||||
} else if (ACTION_CLEAR.equals(action)) {
|
||||
EhApplication.clearDownloadService(getApplicationContext(), this);
|
||||
clear();
|
||||
}
|
||||
|
||||
checkStopSelf();
|
||||
@@ -226,9 +208,12 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
stopAllIntent.setAction(ACTION_STOP_ALL);
|
||||
PendingIntent piStopAll = PendingIntent.getService(this, 0, stopAllIntent, 0);
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString(DownloadsScene.KEY_ACTION, DownloadsScene.ACTION_CLEAR_DOWNLOAD_SERVICE);
|
||||
Intent activityIntent = new Intent(this, MainActivity.class);
|
||||
activityIntent.setAction(StageActivity.ACTION_START_SCENE);
|
||||
activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName());
|
||||
activityIntent.putExtra(StageActivity.KEY_SCENE_ARGS, bundle);
|
||||
PendingIntent piActivity = PendingIntent.getActivity(DownloadService.this, 0,
|
||||
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
@@ -254,9 +239,12 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
clearIntent.setAction(ACTION_CLEAR);
|
||||
PendingIntent piClear = PendingIntent.getService(this, 0, clearIntent, 0);
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString(DownloadsScene.KEY_ACTION, DownloadsScene.ACTION_CLEAR_DOWNLOAD_SERVICE);
|
||||
Intent activityIntent = new Intent(this, MainActivity.class);
|
||||
activityIntent.setAction(StageActivity.ACTION_START_SCENE);
|
||||
activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName());
|
||||
activityIntent.putExtra(StageActivity.KEY_SCENE_ARGS, bundle);
|
||||
PendingIntent piActivity = PendingIntent.getActivity(DownloadService.this, 0,
|
||||
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
@@ -353,7 +341,7 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
|
||||
@Override
|
||||
public void onFinish(DownloadInfo info) {
|
||||
if (mNotifyManager == null || mItemStateArray == null || mItemTitleArray == null) {
|
||||
if (mNotifyManager == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -363,59 +351,59 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
|
||||
boolean finish = info.state == DownloadInfo.STATE_FINISH;
|
||||
long gid = info.gid;
|
||||
int index = mItemStateArray.indexOfKey(gid);
|
||||
int index = sItemStateArray.indexOfKey(gid);
|
||||
if (index < 0) { // Not contain
|
||||
mItemStateArray.put(gid, finish);
|
||||
mItemTitleArray.put(gid, info.title);
|
||||
mDownloadedCount++;
|
||||
sItemStateArray.put(gid, finish);
|
||||
sItemTitleArray.put(gid, info.title);
|
||||
sDownloadedCount++;
|
||||
if (finish) {
|
||||
mFinishedCount++;
|
||||
sFinishedCount++;
|
||||
} else {
|
||||
mFailedCount++;
|
||||
sFailedCount++;
|
||||
}
|
||||
} else { // Contain
|
||||
boolean oldFinish = mItemStateArray.valueAt(index);
|
||||
mItemStateArray.put(gid, finish);
|
||||
mItemTitleArray.put(gid, info.title);
|
||||
boolean oldFinish = sItemStateArray.valueAt(index);
|
||||
sItemStateArray.put(gid, finish);
|
||||
sItemTitleArray.put(gid, info.title);
|
||||
if (oldFinish && !finish) {
|
||||
mFinishedCount--;
|
||||
mFailedCount++;
|
||||
sFinishedCount--;
|
||||
sFailedCount++;
|
||||
} else if (!oldFinish && finish) {
|
||||
mFinishedCount++;
|
||||
mFailedCount--;
|
||||
sFinishedCount++;
|
||||
sFailedCount--;
|
||||
}
|
||||
}
|
||||
|
||||
String text;
|
||||
boolean needStyle;
|
||||
if (mFinishedCount != 0 && mFailedCount == 0) {
|
||||
if (mFinishedCount == 1) {
|
||||
if (mItemTitleArray.size() >= 1) {
|
||||
text = getString(R.string.stat_download_done_line_succeeded, mItemTitleArray.valueAt(0));
|
||||
if (sFinishedCount != 0 && sFailedCount == 0) {
|
||||
if (sFinishedCount == 1) {
|
||||
if (sItemTitleArray.size() >= 1) {
|
||||
text = getString(R.string.stat_download_done_line_succeeded, sItemTitleArray.valueAt(0));
|
||||
} else {
|
||||
Log.d("TAG", "WTF, mItemTitleArray is null");
|
||||
Log.d("TAG", "WTF, sItemTitleArray is null");
|
||||
text = getString(R.string.error_unknown);
|
||||
}
|
||||
needStyle = false;
|
||||
} else {
|
||||
text = getString(R.string.stat_download_done_text_succeeded, mFinishedCount);
|
||||
text = getString(R.string.stat_download_done_text_succeeded, sFinishedCount);
|
||||
needStyle = true;
|
||||
}
|
||||
} else if (mFinishedCount == 0 && mFailedCount != 0) {
|
||||
if (mFailedCount == 1) {
|
||||
if (mItemTitleArray.size() >= 1) {
|
||||
text = getString(R.string.stat_download_done_line_failed, mItemTitleArray.valueAt(0));
|
||||
} else if (sFinishedCount == 0 && sFailedCount != 0) {
|
||||
if (sFailedCount == 1) {
|
||||
if (sItemTitleArray.size() >= 1) {
|
||||
text = getString(R.string.stat_download_done_line_failed, sItemTitleArray.valueAt(0));
|
||||
} else {
|
||||
Log.d("TAG", "WTF, mItemTitleArray is null");
|
||||
Log.d("TAG", "WTF, sItemTitleArray is null");
|
||||
text = getString(R.string.error_unknown);
|
||||
}
|
||||
needStyle = false;
|
||||
} else {
|
||||
text = getString(R.string.stat_download_done_text_failed, mFailedCount);
|
||||
text = getString(R.string.stat_download_done_text_failed, sFailedCount);
|
||||
needStyle = true;
|
||||
}
|
||||
} else {
|
||||
text = getString(R.string.stat_download_done_text_mix, mFinishedCount, mFailedCount);
|
||||
text = getString(R.string.stat_download_done_text_mix, sFinishedCount, sFailedCount);
|
||||
needStyle = true;
|
||||
}
|
||||
|
||||
@@ -423,8 +411,8 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
if (needStyle) {
|
||||
style = new NotificationCompat.InboxStyle();
|
||||
style.setBigContentTitle(getString(R.string.stat_download_done_title));
|
||||
SparseJBArray stateArray = mItemStateArray;
|
||||
SparseJLArray<String> titleArray = mItemTitleArray;
|
||||
SparseJBArray stateArray = sItemStateArray;
|
||||
SparseJLArray<String> titleArray = sItemTitleArray;
|
||||
for (int i = 0, n = stateArray.size(); i < n; i++) {
|
||||
long id = stateArray.keyAt(i);
|
||||
boolean fin = stateArray.valueAt(i);
|
||||
@@ -442,7 +430,7 @@ public class DownloadService extends Service implements DownloadManager.Download
|
||||
mDownloadedBuilder.setContentText(text)
|
||||
.setStyle(style)
|
||||
.setWhen(System.currentTimeMillis())
|
||||
.setNumber(mDownloadedCount);
|
||||
.setNumber(sDownloadedCount);
|
||||
|
||||
mDownloadedDelay.show();
|
||||
|
||||
|
||||
@@ -92,8 +92,11 @@ public class DownloadsScene extends ToolbarScene
|
||||
EasyRecyclerView.OnItemLongClickListener,
|
||||
FabLayout.OnClickFabListener {
|
||||
|
||||
public static final String KEY_ACTION = "action";
|
||||
private static final String KEY_LABEL = "label";
|
||||
|
||||
public static final String ACTION_CLEAR_DOWNLOAD_SERVICE = "clear_download_service";
|
||||
|
||||
@Nullable
|
||||
@ViewLifeCircle
|
||||
private EasyRecyclerView mRecyclerView;
|
||||
@@ -120,10 +123,23 @@ public class DownloadsScene extends ToolbarScene
|
||||
return R.id.nav_downloads;
|
||||
}
|
||||
|
||||
private void handleArguments(Bundle args) {
|
||||
if (args != null && ACTION_CLEAR_DOWNLOAD_SERVICE.equals(args.getString(KEY_ACTION))) {
|
||||
DownloadService.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewArguments(@NonNull Bundle args) {
|
||||
handleArguments(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
handleArguments(getArguments());
|
||||
|
||||
DownloadManager manager = EhApplication.getDownloadManager(getContext());
|
||||
manager.addDownloadInfoListener(this);
|
||||
|
||||
|
||||
+1
-1
Submodule beerbelly updated: 92c9080bfa...2dde5592ad.
Reference in new issue
Block a user