Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DownloadService extends OrmLiteBaseService<DatabaseHelper> {
- private static final String PACKAGE = DownloadService.class.getName();
- public static final String DOWNLOAD_ACTION = PACKAGE + ".download_action";
- public static final String REDOWNLOAD_ACTION = PACKAGE + ".redownload_action";
- public static final String ADD_DATA = PACKAGE + ".add_data";
- public static final String NOTIFY_RESULT_DATA = PACKAGE + ".result_data";
- public static final String NOTIFY_RESULT_PROGRESS = PACKAGE + ".result_progress";
- private static final int DEFAULT_THREAD = 8;
- private static final int DELAY_TIME_CHECK = 1000 * 2;
- private ExecutorService fileExecutor;
- private CompletionService<File> fileHandleService;
- private Handler handler;
- @Override
- public int onStartCommand(final Intent intent, int flags, int startId) {
- try {
- handleRequest(intent);
- } catch (Exception ex) {
- Ln.e(ex);
- stopSelf();
- }
- return super.onStartCommand(intent, flags, startId);
- }
- private void handleRequest(final Intent intent) {
- if(intent == null){
- return;
- }
- if (DOWNLOAD_ACTION.equals(intent.getAction())) {
- addDownloadTask(intent);
- } else if (REDOWNLOAD_ACTION.equals(intent.getAction())) {
- redownload();
- }
- }
- private void redownload() {
- try {
- List<FileDownload> undownloadFiles = getHelper().getDownloadDao().getFilesByStatus(DownloadStatus.DOWNLOADING.getValue());
- if (undownloadFiles != null && undownloadFiles.size() > 0) {
- for (FileDownload fileDownload : undownloadFiles) {
- DownloadTask task = new DownloadTask(DownloadService.this, fileDownload, new CloudHandler(DownloadService.this), getHelper(), new IINotyfiProgress() {
- @Override
- public void onProgress(FileDownload result, int progress) {
- publishProgress(result, progress);
- }
- @Override
- public void onFinish() {
- checkForStopService();
- }
- });
- getServiceExcutor().submit(task);
- }
- }else{
- checkForStopService();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private Handler getHandler(){
- if(handler == null){
- handler = new Handler();
- }
- return handler;
- }
- private void checkForStopService(){
- getHandler().postDelayed(new Runnable() {
- @Override
- public void run() {
- if(fileExecutor != null && fileExecutor instanceof ThreadPoolExecutor){
- ThreadPoolExecutor executor = (ThreadPoolExecutor)fileExecutor;
- if(executor.getQueue() != null && executor.getQueue().isEmpty()){
- Ln.d("there is no task to execute, should stop service");
- stopSelf();
- }
- }
- }
- }, DELAY_TIME_CHECK);
- }
- private void addDownloadTask(final Intent intent) {
- Thread addTaskThread = new Thread(new Runnable() {
- @Override
- public void run() {
- FileDownload data = (FileDownload) intent.getSerializableExtra(ADD_DATA);
- if (data != null) {
- DownloadTask task = new DownloadTask(DownloadService.this, new IINotyfiProgress() {
- @Override
- public void onProgress(FileDownload result, int progress) {
- publishProgress(result, progress);
- }
- @Override
- public void onFinish() {
- checkForStopService();
- }
- });
- getServiceExcutor().submit(task);
- }
- }
- });
- addTaskThread.start();
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- Ln.d("onDestroy download service");
- try{
- if(fileExecutor != null){
- fileExecutor.shutdown();
- }
- }catch(Exception ex){
- Ln.e(ex);
- }finally{
- fileExecutor = null;
- fileHandleService = null;
- }
- }
- protected int getThreadCount() {
- return DEFAULT_THREAD;
- }
- protected CompletionService<File> getServiceExcutor() {
- if(fileExecutor == null){
- fileExecutor = Executors.newFixedThreadPool(getThreadCount());
- }
- if (fileHandleService == null) {
- fileHandleService = new ExecutorCompletionService<File>(fileExecutor);
- }
- return fileHandleService;
- }
- private void publishProgress(FileDownload result, int progress) {
- if(result != null){
- Intent i = new Intent();
- i.setAction(DOWNLOAD_ACTION);
- i.putExtra(NOTIFY_RESULT_DATA, result);
- i.putExtra(NOTIFY_RESULT_PROGRESS, progress);
- LocalBroadcastManager.getInstance(this).sendBroadcast(i);
- }
- }
- @Override
- public IBinder onBind(Intent arg0) {
- return null;
- }
- private class DownloadTask implements Callable<File> {
- IINotyfiProgress notify;
- public DownloadTask(Context context, IINotyfiProgress notify) {
- this.notify = notify;
- }
- public interface IINotyfiProgress {
- void onProgress(FileDownload result, int progress);
- void onFinish();
- }
- @Override
- public File call() throws Exception {
- //download file here
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment