允许应用被强制关闭后重启下载服务

This commit is contained in:
seven332 committed 2014-11-23 13:36:43 +08:00
1 parent ab6cc26c21
commit 24a2ec3fe8
14 files changed
+118 -75

No files matched your search

+3
View File
@@ -239,6 +239,9 @@
<string name="media_scan_summary_on">请避免他人翻看你的图库应用</string>
<string name="media_scan_summary_off">大多数图库应用将不会显示下载目录中的图片</string>
<string name="download_thread_title">下载线程数</string>
<string name="keep_download_service_title">保持下载服务</string>
<string name="keep_download_service_summary_on">下载时,若应用被系统或用户强制关闭,下载服务会重新启动</string>
<string name="keep_download_service_summary_off">下载时,若应用被系统或用户强制关闭,下载服务不会重新启动</string>
<string name="advanced_settings_title">高级</string>
<string name="http_advanced_settings_title">HTTP</string>
+1 -1
View File
@@ -68,7 +68,7 @@
auto:etpMax="100000" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/eh_others_settings_title">
+7
View File
@@ -38,4 +38,11 @@
auto:etpMin="1"
auto:etpMax="10" />
<CheckBoxPreference
android:key="keep_download_service"
android:title="@string/keep_download_service_title"
android:summaryOn="@string/keep_download_service_summary_on"
android:summaryOff="@string/keep_download_service_summary_off"
android:defaultValue="true" />
</PreferenceScreen>
+25 -15
View File
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import android.app.Application;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -32,6 +33,8 @@ import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.ehclient.EhInfo;
import com.hippo.ehviewer.ehclient.ExDownloaderManager;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Crash;
import com.hippo.ehviewer.util.EhUtils;
@@ -45,26 +48,27 @@ import com.hippo.ehviewer.widget.SlidingDrawerLayout;
public class AppContext extends Application implements UncaughtExceptionHandler {
@SuppressWarnings("unused")
private static final String TAG = "AppContext";
private static final String TAG = AppContext.class.getSimpleName();
public static final boolean DEBUG = true;
private Thread.UncaughtExceptionHandler mDefaultHandler;
private static AppContext sInstance;
public static AppContext getInstance() {
return sInstance;
}
public AppContext() {
sInstance = this;
}
private DownloadServiceConnection mServiceConn;
@Override
public void onCreate() {
super.onCreate();
// Do catch error prepare
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
// Set download service connect
mServiceConn = new DownloadServiceConnection();
Intent it = new Intent(this, DownloadService.class);
bindService(it, mServiceConn, BIND_AUTO_CREATE);
// Init everything
Config.init(this);
Log.init(this);
@@ -73,15 +77,12 @@ public class AppContext extends Application implements UncaughtExceptionHandler
EhClient.createInstance(this);
Favorite.init(this);
Data.createInstance(this);
ExDownloaderManager.createInstance();
ExDownloaderManager.createInstance(this);
MaterialToast.setContext(this);
// Set drawer margin
SlidingDrawerLayout.setDefaultMinDrawerMargin(Ui.ACTION_BAR_HEIGHT);
// Do catch error prepare
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
// Add .nomedia or delete it
File nomedia = new File(Config.getDownloadPath(), ".nomedia");
if (Config.getMediaScan()) {
@@ -143,6 +144,15 @@ public class AppContext extends Application implements UncaughtExceptionHandler
} catch (NameNotFoundException e) {}
}
@Override
protected void finalize() {
unbindService(mServiceConn);
}
public DownloadServiceConnection getDownloadServiceConnection() {
return mServiceConn;
}
/**
* {@inheritDoc}
*/
+37 -2
View File
@@ -21,12 +21,16 @@ import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.SparseArray;
import com.hippo.ehviewer.Analytics;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.AppHandler;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Log;
import com.hippo.ehviewer.util.MathUtils;
@@ -268,6 +272,8 @@ public class Data {
/****** download ******/
private synchronized void getDownloads() {
boolean startService = false;
boolean keepDownloadService = Config.getKeepDownloadService();
mDownloads = new ArrayList<DownloadInfo>();
Cursor cursor = mDatabase.rawQuery("select * from " + TABLE_DOWNLOAD
@@ -285,8 +291,14 @@ public class Data {
int mode = cursor.getInt(1);
int state = cursor.getInt(2);
int legacy = cursor.getInt(3);
if (state == DownloadInfo.STATE_WAIT || state == DownloadInfo.STATE_DOWNLOAD)
state = DownloadInfo.STATE_NONE;
if (state == DownloadInfo.STATE_WAIT || state == DownloadInfo.STATE_DOWNLOAD) {
if (keepDownloadService) {
state = DownloadInfo.STATE_WAIT;
startService = true;
} else {
state = DownloadInfo.STATE_NONE;
}
}
DownloadInfo downloadInfo = new DownloadInfo(
galleryInfo, mode, state, legacy);
@@ -296,8 +308,25 @@ public class Data {
}
}
cursor.close();
if (startService)
AppHandler.getInstance().postDelayed(mRestartDownload, 100);
}
private final Runnable mRestartDownload = new Runnable() {
@Override
public void run() {
DownloadService ds = ((AppContext) mContext.getApplicationContext()).getDownloadServiceConnection().getService();
if (ds != null) {
Intent it = new Intent(mContext, DownloadService.class);
mContext.startService(it);
ds.notifyDownloadInfoChanged();
} else {
AppHandler.getInstance().postDelayed(this, 100);
}
}
};
/**
* Return null if not found
* @return
@@ -327,6 +356,12 @@ public class Data {
return re;
}
/**
* If exits, just update
*
* @param downloadInfo
* @return
*/
public synchronized boolean addDownload(DownloadInfo downloadInfo) {
GalleryInfo galleryInfo = downloadInfo.galleryInfo;
int gid = galleryInfo.gid;
@@ -33,7 +33,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import android.content.Context;
import android.os.Process;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.util.AutoExpandArray;
import com.hippo.ehviewer.util.BgThread;
@@ -122,8 +121,8 @@ public class ExDownloader implements Runnable {
public void onDownloadOver(int gid, int legacy);
}
ExDownloader(int gid, String token, String title, int mode) {
mContext = AppContext.getInstance();
ExDownloader(Context context, int gid, String token, String title, int mode) {
mContext = context;
mManager = ExDownloaderManager.getInstance();
mGid = gid;
mToken = token;
@@ -19,8 +19,6 @@ package com.hippo.ehviewer.ehclient;
import android.content.Context;
import android.util.SparseArray;
import com.hippo.ehviewer.AppContext;
public class ExDownloaderManager {
private static final String TAG = ExDownloaderManager.class.getSimpleName();
@@ -30,23 +28,23 @@ public class ExDownloaderManager {
private static ExDownloaderManager sInstance;
public static void createInstance() {
sInstance = new ExDownloaderManager();
public static void createInstance(Context context) {
sInstance = new ExDownloaderManager(context.getApplicationContext());
}
public static ExDownloaderManager getInstance() {
return sInstance;
}
private ExDownloaderManager() {
mContext = AppContext.getInstance();
private ExDownloaderManager(Context context) {
mContext = context;
mExDownloadList = new SparseArray<ExDownloader>();
}
public synchronized ExDownloader getExDownloader(int gid, String token, String title, int mode) {
ExDownloader exDownloader = mExDownloadList.get(gid);
if (exDownloader == null) {
exDownloader = new ExDownloader(gid, token, title, mode);
exDownloader = new ExDownloader(mContext, gid, token, title, mode);
mExDownloadList.append(gid, exDownloader);
}
exDownloader.occupy();
@@ -78,8 +78,13 @@ public class DownloadService extends Service
}
private synchronized void handleIntent(Intent intent) {
if (intent == null)
if (intent == null) {
if (!Config.getKeepDownloadService()) {
mNotifyManager.cancel(DOWNLOADING_NOTIFY_ID);
stopSelf();
}
return;
}
String action = intent.getAction();
if (ACTION_STOP.equals(action)) {
@@ -96,10 +101,8 @@ public class DownloadService extends Service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
return START_STICKY;
}
@Override
@@ -131,6 +134,8 @@ public class DownloadService extends Service
mCurDownloadInfo.state = DownloadInfo.STATE_DOWNLOAD;
mCurDownloadInfo.download = -1;
mCurDownloadInfo.total = -1;
// To update download state
mData.addDownload(mCurDownloadInfo);
GalleryInfo gi = mCurDownloadInfo.galleryInfo;
mCurExDownloader = mEdManager.getExDownloader(gi.gid,
@@ -150,6 +155,8 @@ public class DownloadService extends Service
if ((di = mData.getDownload(gid)) != null) {
if (di.state != DownloadInfo.STATE_DOWNLOAD) {
di.state = DownloadInfo.STATE_WAIT;
// To update download state
mData.addDownload(di);
notifyDownloadInfoChanged();
notifyUpdate();
}
@@ -20,21 +20,20 @@ import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
public class DownloadServiceConnection implements ServiceConnection{
public class DownloadServiceConnection implements ServiceConnection {
private DownloadService mService;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = ((DownloadService.ServiceBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
public DownloadService getService() {
return mService;
}
@@ -37,6 +37,7 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.ImageLoader;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.app.MaterialAlertDialog;
@@ -47,7 +48,6 @@ import com.hippo.ehviewer.data.GalleryInfo;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable.Stroke;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.util.Theme;
@@ -60,6 +60,7 @@ import com.hippo.ehviewer.windowsanimate.WindowsAnimate;
public class DownloadActivity extends AbsActivity implements FitWindowView.OnFitSystemWindowsListener {
private AppContext mAppContext;
private WindowsAnimate mWindowsAnimate;
private int mThemeColor;
private List<DownloadInfo> mDownloads;
@@ -68,7 +69,6 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
private ExpandingListView mList;
private ListAdapter mAdapter;
private final DownloadServiceConnection mServiceConn = new DownloadServiceConnection();
private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
@@ -89,13 +89,12 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
super.onCreate(savedInstanceState);
setContentView(R.layout.download);
mAppContext = (AppContext) getApplication();
// Init windows animate
mWindowsAnimate = new WindowsAnimate();
mWindowsAnimate.init(this);
// Service
Intent it = new Intent(this, DownloadService.class);
bindService(it, mServiceConn, BIND_AUTO_CREATE);
// Receiver
IntentFilter filter = new IntentFilter();
filter.addAction(DownloadService.ACTION_UPDATE);
@@ -128,7 +127,6 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
unbindService(mServiceConn);
mWindowsAnimate.free();
}
@@ -150,13 +148,13 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
case R.id.action_start:
it = new Intent(DownloadActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().startAll();
mAppContext.getDownloadServiceConnection().getService().startAll();
return true;
case R.id.action_stop:
it = new Intent(DownloadActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().stopAll();
mAppContext.getDownloadServiceConnection().getService().stopAll();
return true;
default:
@@ -342,7 +340,7 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
if (which == MaterialAlertDialog.POSITIVE) {
Intent it = new Intent(DownloadActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().delete(mDownloadInfo);
mAppContext.getDownloadServiceConnection().getService().delete(mDownloadInfo);
// Delete dir
if (cb.isChecked()) {
GalleryInfo gi = mDownloadInfo.galleryInfo;
@@ -372,7 +370,7 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
Intent it = new Intent(DownloadActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().notifyDownloadInfoChanged();
mAppContext.getDownloadServiceConnection().getService().notifyDownloadInfoChanged();
mAdapter.notifyDataSetChanged();
}
}
@@ -391,7 +389,7 @@ public class DownloadActivity extends AbsActivity implements FitWindowView.OnFit
public void onClick(View v) {
Intent it = new Intent(DownloadActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().stop(mDownloadInfo);
mAppContext.getDownloadServiceConnection().getService().stop(mDownloadInfo);
mAdapter.notifyDataSetChanged();
}
}
@@ -63,8 +63,6 @@ import com.hippo.ehviewer.data.GalleryInfo;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable.Stroke;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Favorite;
import com.hippo.ehviewer.util.Theme;
@@ -120,8 +118,6 @@ public class FavouriteActivity extends AbsActivity
private List<GalleryInfo> mLastModifyGiList = null;
private int mLastModifyPageNum = 0;
private final DownloadServiceConnection mServiceConn = new DownloadServiceConnection();
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
@@ -163,10 +159,6 @@ public class FavouriteActivity extends AbsActivity
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
// Download service
Intent it = new Intent(FavouriteActivity.this, DownloadService.class);
bindService(it, mServiceConn, BIND_AUTO_CREATE);
// Menu
MaterialIndicatorDrawable materialIndicator = new MaterialIndicatorDrawable(this, Color.WHITE, Stroke.THIN);
materialIndicator.setIconState(MaterialIndicatorDrawable.IconState.ARROW);
@@ -337,7 +329,6 @@ public class FavouriteActivity extends AbsActivity
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConn);
}
private void showToastBar(final ActionClickedListener listener, int descriptionIconResourceId,
@@ -58,6 +58,7 @@ import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import com.hippo.ehviewer.AppContext;
import com.hippo.ehviewer.ImageLoader;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.app.MaterialAlertDialog;
@@ -79,7 +80,6 @@ import com.hippo.ehviewer.drawable.OvalDrawable;
import com.hippo.ehviewer.ehclient.DetailUrlParser;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.Constants;
import com.hippo.ehviewer.util.Favorite;
@@ -114,12 +114,11 @@ public class GalleryDetailActivity extends AbsActivity
public static final String KEY_G_INFO = "gallery_info";
private AppContext mAppContext;
private Resources mResources;
private EhClient mClient;
private WindowsAnimate mWindowsAnimate;
private GalleryInfo mGalleryInfo;
private final DownloadServiceConnection mServiceConn =
new DownloadServiceConnection();
private int mRunningAnimateNum = 0;
private boolean isCheckmark = false;
@@ -271,7 +270,6 @@ public class GalleryDetailActivity extends AbsActivity
protected void onDestroy() {
super.onDestroy();
mWindowsAnimate.free();
unbindService(mServiceConn);
}
@Override
@@ -286,6 +284,7 @@ public class GalleryDetailActivity extends AbsActivity
setContentView(R.layout.gallery_detail);
setTitle(String.valueOf(mGalleryInfo.gid));
mAppContext = (AppContext) getApplication();
mClient = EhClient.getInstance();
mWindowsAnimate = new WindowsAnimate();
mWindowsAnimate.init(this);
@@ -421,10 +420,6 @@ public class GalleryDetailActivity extends AbsActivity
mRefreshText.setDefaultRefresh("点击重试", this);
doPreLayout();
// Download service
Intent it = new Intent(GalleryDetailActivity.this, DownloadService.class);
bindService(it, mServiceConn, BIND_AUTO_CREATE);
}
@Override
@@ -809,7 +804,7 @@ public class GalleryDetailActivity extends AbsActivity
if (v == mDownloadButton) {
Intent it = new Intent(GalleryDetailActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().add(mGalleryInfo);
mAppContext.getDownloadServiceConnection().getService().add(mGalleryInfo);
MaterialToast.showToast(R.string.toast_add_download);
} else if (v == mReadButton) {
//mData.addRead(mGalleryInfo);
@@ -83,7 +83,6 @@ import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable.Stroke;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.service.DownloadService;
import com.hippo.ehviewer.service.DownloadServiceConnection;
import com.hippo.ehviewer.tile.TileSalon;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.DialogUtils;
@@ -151,6 +150,7 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
public static final int LIST_MODE_DETAIL = 0x0;
public static final int LIST_MODE_THUMB = 0x1;
private AppContext mAppContext;
private EhClient mClient;
private Resources mResources;
private WindowsAnimate mWindowsAnimate;
@@ -211,8 +211,6 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
private String mTitle;
private final DownloadServiceConnection mServiceConn = new DownloadServiceConnection();
private int mListMode;
private int mListDetailThumbWidth;
@@ -566,7 +564,7 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
case 1:
Intent it = new Intent(GalleryListActivity.this, DownloadService.class);
startService(it);
mServiceConn.getService().add(gi);
mAppContext.getDownloadServiceConnection().getService().add(gi);
MaterialToast.showToast(R.string.toast_add_download);
break;
}
@@ -973,6 +971,7 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_list);
mAppContext = (AppContext) getApplication();
mData = Data.getInstance();
mClient = EhClient.getInstance();
mResources = getResources();
@@ -999,10 +998,6 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
else
mShowDrawer = true;
// Download service
Intent it = new Intent(GalleryListActivity.this, DownloadService.class);
bindService(it, mServiceConn, BIND_AUTO_CREATE);
// Init dialog
loginDialog = createLoginDialog();
mSearchDialog = createSearchDialog();
@@ -1511,7 +1506,6 @@ public class GalleryListActivity extends AbsActivity implements View.OnClickList
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConn);
mWindowsAnimate.free();
}
+7
View File
@@ -349,6 +349,9 @@ public final class Config {
private static final String KEY_DOWNLOAD_THREAD = "download_thread";
private static final int DEFAULT_DOWNLOAD_THREAD = 3;
private static final String KEY_KEEP_DOWNLOAD_SERVICE = "keep_download_service";
private static final boolean DEFAULT_KEEP_DOWNLOAD_SERVICE = true;
public static String getDownloadPath() {
return getString(KEY_DOWNLOAD_PATH, DEFAULT_DOWNLOAD_PATH);
}
@@ -365,6 +368,10 @@ public final class Config {
return getIntFromStr(KEY_DOWNLOAD_THREAD, DEFAULT_DOWNLOAD_THREAD);
}
public static boolean getKeepDownloadService() {
return getBoolean(KEY_KEEP_DOWNLOAD_SERVICE, DEFAULT_KEEP_DOWNLOAD_SERVICE);
}
/****** Advanced ******/