r4j

DownloadService

r4j
Aug 27th, 2014
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. public class DownloadService extends OrmLiteBaseService<DatabaseHelper> {
  2.     private static final String PACKAGE = DownloadService.class.getName();
  3.     public static final String DOWNLOAD_ACTION = PACKAGE + ".download_action";
  4.     public static final String REDOWNLOAD_ACTION = PACKAGE + ".redownload_action";
  5.  
  6.     public static final String ADD_DATA = PACKAGE + ".add_data";
  7.     public static final String NOTIFY_RESULT_DATA = PACKAGE + ".result_data";
  8.     public static final String NOTIFY_RESULT_PROGRESS = PACKAGE + ".result_progress";
  9.     private static final int DEFAULT_THREAD = 8;
  10.     private static final int DELAY_TIME_CHECK = 1000 * 2;
  11.     private ExecutorService fileExecutor;
  12.     private CompletionService<File> fileHandleService;
  13.     private Handler handler;
  14.  
  15.     @Override
  16.     public int onStartCommand(final Intent intent, int flags, int startId) {
  17.         try {
  18.             handleRequest(intent);
  19.         } catch (Exception ex) {
  20.             Ln.e(ex);
  21.             stopSelf();
  22.         }
  23.  
  24.         return super.onStartCommand(intent, flags, startId);
  25.     }
  26.  
  27.     private void handleRequest(final Intent intent) {
  28.         if(intent == null){
  29.             return;
  30.         }
  31.  
  32.         if (DOWNLOAD_ACTION.equals(intent.getAction())) {
  33.             addDownloadTask(intent);
  34.         } else if (REDOWNLOAD_ACTION.equals(intent.getAction())) {
  35.             redownload();
  36.         }
  37.     }
  38.  
  39.     private void redownload() {
  40.         try {
  41.             List<FileDownload> undownloadFiles = getHelper().getDownloadDao().getFilesByStatus(DownloadStatus.DOWNLOADING.getValue());
  42.             if (undownloadFiles != null && undownloadFiles.size() > 0) {
  43.                 for (FileDownload fileDownload : undownloadFiles) {
  44.                     DownloadTask task = new DownloadTask(DownloadService.this, fileDownload, new CloudHandler(DownloadService.this), getHelper(), new IINotyfiProgress() {
  45.  
  46.                         @Override
  47.                         public void onProgress(FileDownload result, int progress) {
  48.                             publishProgress(result, progress);
  49.                         }
  50.  
  51.                         @Override
  52.                         public void onFinish() {
  53.                             checkForStopService();
  54.                         }
  55.                     });
  56.  
  57.                     getServiceExcutor().submit(task);
  58.                 }
  59.             }else{
  60.                 checkForStopService();
  61.             }
  62.         } catch (SQLException e) {
  63.             e.printStackTrace();
  64.         }
  65.     }
  66.  
  67.     private Handler getHandler(){
  68.         if(handler == null){
  69.             handler = new Handler();
  70.         }
  71.  
  72.         return handler;
  73.     }
  74.  
  75.     private void checkForStopService(){
  76.         getHandler().postDelayed(new Runnable() {
  77.  
  78.             @Override
  79.             public void run() {
  80.                 if(fileExecutor != null && fileExecutor instanceof ThreadPoolExecutor){
  81.                     ThreadPoolExecutor executor = (ThreadPoolExecutor)fileExecutor;
  82.                     if(executor.getQueue() != null && executor.getQueue().isEmpty()){
  83.                         Ln.d("there is no task to execute, should stop service");
  84.                         stopSelf();
  85.                     }
  86.                 }
  87.             }
  88.         }, DELAY_TIME_CHECK);
  89.     }
  90.  
  91.     private void addDownloadTask(final Intent intent) {
  92.         Thread addTaskThread = new Thread(new Runnable() {
  93.  
  94.             @Override
  95.             public void run() {
  96.                 FileDownload data = (FileDownload) intent.getSerializableExtra(ADD_DATA);
  97.                 if (data != null) {
  98.                     DownloadTask task = new DownloadTask(DownloadService.this, new IINotyfiProgress() {
  99.  
  100.                         @Override
  101.                         public void onProgress(FileDownload result, int progress) {
  102.                             publishProgress(result, progress);
  103.                         }
  104.  
  105.                         @Override
  106.                         public void onFinish() {
  107.                             checkForStopService();
  108.                         }
  109.                     });
  110.  
  111.                     getServiceExcutor().submit(task);
  112.                 }
  113.             }
  114.         });
  115.  
  116.         addTaskThread.start();
  117.     }
  118.  
  119.     @Override
  120.     public void onDestroy(){
  121.         super.onDestroy();
  122.         Ln.d("onDestroy download service");
  123.         try{
  124.             if(fileExecutor != null){
  125.                 fileExecutor.shutdown();
  126.             }
  127.         }catch(Exception ex){
  128.             Ln.e(ex);
  129.         }finally{
  130.             fileExecutor = null;
  131.             fileHandleService = null;
  132.         }
  133.     }
  134.  
  135.     protected int getThreadCount() {
  136.         return DEFAULT_THREAD;
  137.     }
  138.  
  139.     protected CompletionService<File> getServiceExcutor() {
  140.         if(fileExecutor == null){
  141.             fileExecutor = Executors.newFixedThreadPool(getThreadCount());
  142.         }
  143.  
  144.         if (fileHandleService == null) {
  145.             fileHandleService = new ExecutorCompletionService<File>(fileExecutor);
  146.         }
  147.  
  148.         return fileHandleService;
  149.     }
  150.  
  151.     private void publishProgress(FileDownload result, int progress) {
  152.         if(result != null){
  153.             Intent i = new Intent();
  154.             i.setAction(DOWNLOAD_ACTION);
  155.             i.putExtra(NOTIFY_RESULT_DATA, result);
  156.             i.putExtra(NOTIFY_RESULT_PROGRESS, progress);
  157.             LocalBroadcastManager.getInstance(this).sendBroadcast(i);
  158.         }
  159.     }
  160.  
  161.     @Override
  162.     public IBinder onBind(Intent arg0) {
  163.         return null;
  164.     }
  165.    
  166.     private class DownloadTask implements Callable<File> {
  167.     IINotyfiProgress notify;
  168.         public DownloadTask(Context context, IINotyfiProgress notify) {
  169.         this.notify = notify;
  170.         }
  171.  
  172.         public interface IINotyfiProgress {
  173.             void onProgress(FileDownload result, int progress);
  174.             void onFinish();
  175.         }
  176.  
  177.         @Override
  178.         public File call() throws Exception {
  179.            //download file here
  180.             return null;
  181.         }
  182. }
  183. }
Add Comment
Please, Sign In to add comment