Browse Source

修复下载状态不正确的问题

zengjiebin 8 years ago
parent
commit
b5b22ad7c0

+ 11 - 6
app/src/main/java/com/kfzs/duanduan/ActDownloadMgr.java

@@ -226,6 +226,11 @@ public class ActDownloadMgr extends BaseCompatActivity {
                 }
                 removeTask(iTempPosition);
             }
+
+            @Override
+            public void downloadInit(String downloadUrl) {
+
+            }
         }).setmTag(TAG).handlDownloadResult(info);
 
     }
@@ -397,7 +402,7 @@ public class ActDownloadMgr extends BaseCompatActivity {
     //在这里处理任务执行中的状态,如进度进度条的刷新
     @Download.onTaskRunning protected void running(DownloadTask task) {
         int p = task.getPercent();    //任务进度百分比
-        downloadTaskService.setDownloadTaskPercent(p, task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
+//        downloadTaskService.setDownloadTaskPercent(p, task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
         String speed = task.getConvertSpeed();    //转换单位后的下载速度,单位转换需要在配置文件中打开
         System.out.println("Aria "+task.getKey()+",p " + p + ", speed = " + speed);
         String url = task.getKey();
@@ -415,15 +420,15 @@ public class ActDownloadMgr extends BaseCompatActivity {
     }
 
     @Download.onTaskStop void taskStop(DownloadTask task) {
-        String url = task.getKey();
-        downloadTaskService.setDownloadTaskStatus(url, DownloadTaskService.STATUS_PAUSE);
+//        String url = task.getKey();
+//        downloadTaskService.setDownloadTaskStatus(url, DownloadTaskService.STATUS_PAUSE);
     }
 
     @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
     }
 
     @Download.onTaskCancel void taskCancel(DownloadTask task) {
-        downloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
+//        downloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
         String url = task.getKey();
         int iTempPosition = -1;
         for (int position = 0; position < downloadTasks.size(); position++) {
@@ -440,7 +445,7 @@ public class ActDownloadMgr extends BaseCompatActivity {
     }
 
     @Download.onTaskFail void taskFail(DownloadTask task) {
-        downloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
+//        downloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
         String url = task.getKey();
         KFProgressButton targetBtn = (KFProgressButton) lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_BUTTON + url);
         targetBtn.setText(R.string.download_fail);
@@ -448,7 +453,7 @@ public class ActDownloadMgr extends BaseCompatActivity {
     }
 
     @Download.onTaskComplete void taskComplete(DownloadTask task) {
-        downloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
+//        downloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
         TextView tvProgress = (TextView) lvDownloadMgr.findViewWithTag(DownloadMgrAdapter.PUBLIC_TAG_PREFIX_TEXTVIEW + task.getKey());
         tvProgress.setText(R.string.download_completed);
 

+ 2 - 2
app/src/main/java/com/kfzs/duanduan/ActMain.java

@@ -20,6 +20,7 @@ import com.arialyy.aria.core.Aria;
 import com.arialyy.aria.core.download.DownloadTask;
 import com.arialyy.aria.core.inf.IEntity;
 import com.kfzs.appstore.utils.restful.KFZSNetwork;
+import com.kfzs.duanduan.bean.DownloadStatus;
 import com.kfzs.duanduan.bean.KFIntentKeys;
 import com.kfzs.duanduan.bll.UrlBll;
 import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
@@ -91,11 +92,10 @@ public class ActMain extends BaseCompatActivity{
         mImgDot = (ImageView) findViewById(R.id.img_download_manager_dot);
         tabsHelper.init();
         mDownloadTaskService = new DownloadTaskService(this);
-        mDownloadTaskService.restoreDownloadTaskStatus();
+//        mDownloadTaskService.restoreDownloadTaskStatus();
 
         initLinsener();
 
-        Aria.download(this).register();
     }
 
 

+ 4 - 0
app/src/main/java/com/kfzs/duanduan/KFZSApp.java

@@ -1,6 +1,7 @@
 package com.kfzs.duanduan;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.os.Build;
 import android.os.Environment;
@@ -13,6 +14,7 @@ import com.kfzs.appstore.utils.restful.KFZSNetwork;
 import com.kfzs.duanduan.db.DataSave;
 import com.kfzs.duanduan.utils.ApkUtils;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
+import com.sheep.jiuyan.samllsheep.service.DownloadService;
 import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
 import com.sheep.jiuyan.samllsheep.utils.G;
 
@@ -72,6 +74,7 @@ public class KFZSApp extends MultiDexApplication {
         setOnlyContext(this);
         initUtils(DEBUG);
         initOwnCatchPath();
+        startService(new Intent(this, DownloadService.class));
     }
 
     private void initUtils(boolean debug) {
@@ -139,6 +142,7 @@ public class KFZSApp extends MultiDexApplication {
     public void quit() {
         try {
             Aria.download(this).stopAllTask();
+            stopService(new Intent(this, DownloadService.class));
             for (BaseCompatActivity appCompatActivity : appCompatActivityList) {
                 appCompatActivity.finish();
             }

+ 1 - 1
app/src/main/java/com/kfzs/duanduan/adp/AdpFragmentRank.java

@@ -194,7 +194,7 @@ public class AdpFragmentRank extends AdpCommonRecy<RecyleObj> {
 
             String position = String.valueOf((i + 1) * 12345);
             mDownLoadItemLogicMgrMap.put(oTempGame.getDownloadUrl(), InstallButtonUtils
-                    .init(position, oTempGame.getDownloadUrl(), helper.getView(mBtnId[i], KFProgressButton.class),
+                    .init(position, oTempGame.getDownloadUrl(), oTempGame.getPackageName(), helper.getView(mBtnId[i], KFProgressButton.class),
                             "bindTopThreePanel" + i));
             DownBtnUtils.addDownloadBtnListener(helper.getView(mBtnId[i], KFProgressButton.class),
                     mContext,mDownLoadItemLogicMgrMap,lResponseTopThreeList.get(i),TAG);

+ 5 - 5
app/src/main/java/com/kfzs/duanduan/adp/DownloadMgrAdapter.java

@@ -89,25 +89,25 @@ public class DownloadMgrAdapter extends MDLAdpater<DownLoadInfo> {
         }
         installButtonUtils.addBtnInt(targetBtn,position);
         mInstallButtonMgrMap.put(model.getMDownloadUrl(), installButtonUtils);
+        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(model.getMPackageName());
 
         switch (model.getMStatus()) {
             case DownloadTaskService.STATUS_INIT:
             case DownloadTaskService.STATUS_DELETE:
-                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INIT);
+                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INIT, isPkgInstalled);
                 break;
             case DownloadTaskService.STATUS_ING:
-                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INSTALLING);
+                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_INSTALLING, isPkgInstalled);
                 break;
             case DownloadTaskService.STATUS_PAUSE:
-                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_PAUSED);
+                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_PAUSED, isPkgInstalled);
                 break;
             case DownloadTaskService.STATUS_FAIL:
-                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_FAIL);
+                targetBtn.updateButtonUI(KFProgressButton.STATUS.STATUS_FAIL, isPkgInstalled);
                 helper.setText(R.id.download_mgr_tv_progress, R.string.download_fail_please_retry);
                 break;
             case DownloadTaskService.STATUS_FINISH:
                 pb.setProgress(100);
-                boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(model.getMPackageName());
                 if (isPkgInstalled) {
                     int apkStatus = ApkUtils.getInstance().comparePkgVersionCode(model.getMPackageName(), model.getMVersionCode());
                     if (apkStatus == CompareResult.EQUALS || apkStatus == CompareResult.LT) {

+ 17 - 11
app/src/main/java/com/kfzs/duanduan/event/EventDownloadHandler.java

@@ -50,22 +50,22 @@ public class EventDownloadHandler {
     public void handlDownloadResult(DownloadStatus event) {
         switch (event.getStatus()) {
             case DownloadTaskService.STATUS_ING:
-                downloadTaskService.setDownloadTaskPercent(event.getDownloadPgrs(), event.getFileDownloadedSize(),
-                        event.getFileTotalSize(), event.getDownloadId(), event.getDownloadUrl());
-                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_ING);
+//                downloadTaskService.setDownloadTaskPercent(event.getDownloadPgrs(), event.getFileDownloadedSize(),
+//                        event.getFileTotalSize(), event.getDownloadId(), event.getDownloadUrl());
+//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_ING);
                 onDownCallback.downloading(event.getDownloadUrl(), event.getDownloadPgrs());
                 break;
             case DownloadTaskService.STATUS_FINISH:
-                downloadTaskService.setDownloadTaskFinish(event.getDownloadUrl(), event.getApkPath());
-                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_FINISH);
+//                downloadTaskService.setDownloadTaskFinish(event.getDownloadUrl(), event.getApkPath());
+//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(), DownloadTaskService.STATUS_FINISH);
                 onDownCallback.downloadFinish(event.getDownloadUrl());
                 if (new File(event.getApkPath()).exists()) {
                     ApkUtils.installApk(mContext, event.getApkPath());
                 }
                 break;
             case DownloadTaskService.STATUS_FAIL:
-                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
-                        DownloadTaskService.STATUS_FAIL);
+//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
+//                        DownloadTaskService.STATUS_FAIL);
                 onDownCallback.downloadFail(event.getDownloadUrl());
                 Double totalSize = event.getFileTotalSize();
                 if (totalSize > HelperUtils.getFreeSpace()) {
@@ -75,15 +75,19 @@ public class EventDownloadHandler {
                 }
                 break;
             case DownloadTaskService.STATUS_PAUSE:
-                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
-                        DownloadTaskService.STATUS_PAUSE);
+//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
+//                        DownloadTaskService.STATUS_PAUSE);
                 onDownCallback.downloadPause(event.getDownloadUrl());
                 break;
             case DownloadTaskService.STATUS_DELETE:
-                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
-                        DownloadTaskService.STATUS_DELETE);
+//                downloadTaskService.setDownloadTaskStatus(event.getDownloadUrl(),
+//                        DownloadTaskService.STATUS_DELETE);
                 onDownCallback.downloadDelete(event.getDownloadUrl());
                 break;
+            case DownloadTaskService.STATUS_INIT:
+            default:
+                onDownCallback.downloadInit(event.getDownloadUrl());
+                break;
         }
     }
 
@@ -99,6 +103,8 @@ public class EventDownloadHandler {
 
         void downloadDelete(String downloadUrl);
 
+        void downloadInit(String downloadUrl);
+
     }
 
 }

+ 109 - 0
app/src/main/java/com/kfzs/duanduan/fragment/FgtMainGame.java

@@ -1,15 +1,27 @@
 package com.kfzs.duanduan.fragment;
 
+import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.widget.ProgressBar;
+import android.widget.TextView;
 
 import com.android.volleyplus.Response;
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
 import com.kf.utils.KFLog;
 import com.kfzs.appstore.utils.restful.KFZSNetwork;
+import com.kfzs.duanduan.ActDownloadMgr;
 import com.kfzs.duanduan.ActMain;
 import com.kfzs.duanduan.BaseCompatFragment;
+import com.kfzs.duanduan.adp.DownloadMgrAdapter;
+import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
+import com.kfzs.duanduan.services.DownloadTaskService;
+import com.kfzs.duanduan.utils.InstallButtonUtils;
+import com.kfzs.duanduan.view.KFProgressButton;
 import com.sheep.gamegroup.event.MoneyChange;
 import com.sheep.gamegroup.event.UserNameChange;
 import com.sheep.jiuyan.samllsheep.R;
@@ -32,11 +44,20 @@ import com.kfzs.duanduan.utils.dlg.DownBtnUtils;
 import com.kfzs.duanduan.utils.dlg.HelperUtils;
 import com.kfzs.duanduan.utils.net.APIRequest;
 import com.kfzs.duanduan.utils.net.APIRequestInstance;
+import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
+import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 
 import org.greenrobot.eventbus.EventBus;
 import org.greenrobot.eventbus.Subscribe;
 import org.greenrobot.eventbus.ThreadMode;
 
+import java.io.File;
+import java.util.Map;
+
+import static android.content.Intent.ACTION_PACKAGE_ADDED;
+import static android.content.Intent.ACTION_PACKAGE_REMOVED;
+import static android.content.Intent.ACTION_PACKAGE_REPLACED;
+
 
 /**
  * @ Created by Dlg
@@ -87,6 +108,7 @@ public class FgtMainGame extends BaseCompatFragment {
                 mSwipeRefreshLayout.setRefreshing(false);
             }
         });
+        Aria.download(this).register();
 
     }
 
@@ -259,8 +281,95 @@ public class FgtMainGame extends BaseCompatFragment {
                 .setmTag(TAG)
                 .handlDownloadResult(info);
     }
+//        if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
+    @Subscribe
+    public void onEventMainThread(Intent intent) {
+        if(intent != null && intent.getAction() != null && intent.getDataString() != null && intent.getDataString().contains("package:")){
+            String packageName = intent.getDataString().replace("package:","");
+            for (Map.Entry<String, InstallButtonUtils> entry : mAdpMainPageRecy.getmDownLoadItemLogicMgrMap().entrySet()) {
+                if(packageName.equals(entry.getValue().getPackageName())){
+                    switch (intent.getAction()){
+                        case ACTION_PACKAGE_ADDED:
+                            entry.getValue().setStatus(KFProgressButton.STATUS.STATUS_FINISH);
+                            return;
+                        case ACTION_PACKAGE_REMOVED:
+                            if(new File(ClassFileHelper.getFilePathByPackageName(packageName)).exists()) {
+                                entry.getValue().setStatus(KFProgressButton.STATUS.STATUS_FINISH);
+                            } else {
+                                entry.getValue().setStatus(KFProgressButton.STATUS.STATUS_INIT);
+                            }
+                            return;
+                    }
+                }
+            }
+        }
+    }
+
+
+    @Download.onPre void onPre(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_INIT);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_ING);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_ING);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_ING);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
 
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_PAUSE);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
 
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_DELETE);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_FAIL);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
+
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        DownloadStatus downloadStatus = new DownloadStatus();
+        downloadStatus.setStatus(DownloadTaskService.STATUS_FINISH);
+        downloadStatus.setApkPath(task.getDownloadPath());
+        downloadStatus.setDownloadUrl(task.getKey());
+        onEventMainThread(downloadStatus);
+    }
 
 
     @Override

+ 3 - 0
app/src/main/java/com/kfzs/duanduan/oem/AppAddOrdelReceiver.java

@@ -18,6 +18,8 @@ import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.SpUtils;
 
 
+import org.greenrobot.eventbus.EventBus;
+
 import java.io.IOException;
 
 import okhttp3.Call;
@@ -74,6 +76,7 @@ public class AppAddOrdelReceiver extends BroadcastReceiver {
 
         if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction()) || Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
             ApkUtils.getInstance().clear();
+            EventBus.getDefault().post(intent);
         }
     }
 

+ 3 - 2
app/src/main/java/com/kfzs/duanduan/services/DownloadTaskService.java

@@ -197,13 +197,14 @@ public class DownloadTaskService {
      *
      * @param task
      */
-    public void addDownloadTask(DownLoadInfo task) {
+    public boolean addDownloadTask(DownLoadInfo task) {
         if (task.getMStatus() == null) {
             task.setMStatus(0);
         }
         if(!TextUtils.isEmpty(task.getMDownloadUrl())){
-            DDProviderHelper.getInstance().addOrUpDataDownloadTask(ctx, task);
+            return DDProviderHelper.getInstance().addOrUpDataDownloadTask(ctx, task);
         }
+        return false;
 
         /*ContentResolver contentResolver = ctx.getContentResolver();
         DownloadtaskSelection where = new DownloadtaskSelection();

+ 17 - 13
app/src/main/java/com/kfzs/duanduan/utils/GameStatusScanner.java

@@ -44,6 +44,11 @@ import java.util.Set;
 import okhttp3.Call;
 
 import static com.kfzs.duanduan.services.DownloadTaskService.STATUS_ING;
+import static com.kfzs.duanduan.view.KFProgressButton.STATUS.STATUS_FAIL;
+import static com.kfzs.duanduan.view.KFProgressButton.STATUS.STATUS_FINISH;
+import static com.kfzs.duanduan.view.KFProgressButton.STATUS.STATUS_INIT;
+import static com.kfzs.duanduan.view.KFProgressButton.STATUS.STATUS_INSTALLING;
+import static com.kfzs.duanduan.view.KFProgressButton.STATUS.STATUS_PAUSED;
 
 /**
  * Created by HooRang on 2017/2/24.
@@ -103,11 +108,11 @@ public abstract class GameStatusScanner {
                         break;
                     case DownloadTaskService.STATUS_ING:
                         mTarget.updateProgress(task.getMPercent() == null ? 0 : task.getMPercent());
-                        mTarget.updateButtonUI(KFProgressButton.STATUS.STATUS_PROGRESSING);
+                        mTarget.updateButtonUI(KFProgressButton.STATUS.STATUS_PROGRESSING, false);
                         break;
                     case DownloadTaskService.STATUS_PAUSE:
                         mTarget.updateProgress(task.getMPercent() == null ? 0 : task.getMPercent());
-                        mTarget.updateButtonUI(KFProgressButton.STATUS.STATUS_PAUSED);
+                        mTarget.updateButtonUI(KFProgressButton.STATUS.STATUS_PAUSED, false);
                         break;
                     case DownloadTaskService.STATUS_FAIL:
                         mTarget.setText(R.string.download_fail);
@@ -203,7 +208,7 @@ public abstract class GameStatusScanner {
                         startDownload(mContext, GameStatusScanner.this, downloadTaskService, gameId, versionCode, downloadUrl, packageName, gameName, iconUrl);
                     }
                 } else {
-                    targetBtnEventAction(downloadUrl, isPkgInstalled);
+                    targetBtnEventAction(downloadUrl);
                 }
             }
         });
@@ -247,7 +252,7 @@ public abstract class GameStatusScanner {
         DownLoadInfo downloadInfo = addNewDownloadTask(downloadTaskService, gameName, downloadUrl, packageName, versionCode, iconUrl, Integer.parseInt(gameId),STATUS_ING);
         downloadTaskService.start(downloadInfo);
         if(scanner != null)
-            scanner.onButtonClickReturnListener(downloadUrl, false, 0);
+            scanner.onButtonClickReturnListener(downloadUrl, KFProgressButton.STATUS.STATUS_INSTALLING);
         //开始任务的时候,再发请求,不判断是否为首次添加等情况
         downCountUpdate(context,gameId);
     }
@@ -275,10 +280,9 @@ public abstract class GameStatusScanner {
      * 这里是点击按钮时,更新历史状态,也就是按钮的任务已经加入TASK后的点击处理
      *
      * @param downloadUrl
-     * @param isPkgInstalled
      * @return
      */
-    private boolean targetBtnEventAction(final String downloadUrl, boolean isPkgInstalled) {
+    private boolean targetBtnEventAction(final String downloadUrl) {
 
         boolean bPaused = false;
         DownLoadInfo downloadTask = null;
@@ -305,12 +309,12 @@ public abstract class GameStatusScanner {
         switch (downloadTask.getMStatus()) {
             case DownloadTaskService.STATUS_INIT:
                 Log.d(TAG, "Still init status #" + downloadTask.getMDownloadUrl());
-                onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                onButtonClickReturnListener(downloadUrl, STATUS_INIT);
                 break;
             case DownloadTaskService.STATUS_ING:
                 bPaused = true;
                 changeTaskPersistenceStatus(downloadTask.getMDownloadUrl(), DownloadTaskService.STATUS_PAUSE, percent, true);
-                onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                onButtonClickReturnListener(downloadUrl, STATUS_INSTALLING);
                 break;
             case DownloadTaskService.STATUS_PAUSE:
                 if (!preAddDownloadTaskValid(mContext,downloadTaskService)) {
@@ -318,18 +322,18 @@ public abstract class GameStatusScanner {
                 }
                 bPaused = false;
                 changeTaskPersistenceStatus(downloadTask.getMDownloadUrl(), DownloadTaskService.STATUS_ING, percent, false);
-                onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                onButtonClickReturnListener(downloadUrl, STATUS_PAUSED);
                 break;
             case DownloadTaskService.STATUS_FAIL:
                 bPaused = false;
                 changeTaskPersistenceStatus(downloadTask.getMDownloadUrl(), DownloadTaskService.STATUS_INIT, 0, false);
-                onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                onButtonClickReturnListener(downloadUrl, STATUS_FAIL);
                 break;
             case DownloadTaskService.STATUS_FINISH:
 
                 if (new File(downloadTask.getMApkPath()).exists()) {
                     ApkUtils.installApk(mContext, downloadTask.getMApkPath());
-                    onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                    onButtonClickReturnListener(downloadUrl, STATUS_FINISH);
                 } else {
                     downloadTaskService.cancel(downloadTask);
                     downloadTaskService.deleteDownloadTaskByDownloadUrl(downloadUrl);
@@ -339,7 +343,7 @@ public abstract class GameStatusScanner {
                 break;
             default:
                 Log.d(TAG, "Unknown task status#" + downloadTask.getMStatus());
-                onButtonClickReturnListener(downloadUrl, bPaused, percent);
+                onButtonClickReturnListener(downloadUrl, STATUS_INIT);
                 break;
         }
 
@@ -400,7 +404,7 @@ public abstract class GameStatusScanner {
         return task;
     }
 
-    public abstract void onButtonClickReturnListener(String downloadUrl, boolean drawBtnUI, int percent);
+    public abstract void onButtonClickReturnListener(String downloadUrl, KFProgressButton.STATUS status);
 
 
 }

+ 15 - 4
app/src/main/java/com/kfzs/duanduan/utils/InstallButtonUtils.java

@@ -18,6 +18,7 @@ public class InstallButtonUtils {
     private int downloadedPgrs;
     private String position;
     private String downloadUrl;
+    private String packageName;
     private KFProgressButton.STATUS status;
     private HashMap<String, KFProgressButton> btns = new HashMap<>();
     public static final int mIntTag = R.id.view_tag_down_btn;
@@ -30,15 +31,16 @@ public class InstallButtonUtils {
      * @param tag      相同URL的按钮,tag来区分不同按钮,在listview里一般用position,防止重复加载很多无用btn
      * @return
      */
-    public static InstallButtonUtils init(String position, String url, KFProgressButton view, String tag) {
-        return init(position, url).addBtn(view, tag);
+    public static InstallButtonUtils init(String position, String url, String packageName, KFProgressButton view, String tag) {
+        return init(position, url, packageName).addBtn(view, tag);
     }
 
-    private static InstallButtonUtils init(String position, String url) {
+    private static InstallButtonUtils init(String position, String url, String packageName) {
         InstallButtonUtils logicMgr = new InstallButtonUtils();
         logicMgr.setStatus(KFProgressButton.STATUS.STATUS_INIT);
         logicMgr.setPosition(position);
         logicMgr.setDownloadUrl(url);
+        logicMgr.setPackageName(packageName);
         return logicMgr;
     }
 
@@ -76,16 +78,25 @@ public class InstallButtonUtils {
         this.downloadUrl = downloadUrl;
     }
 
+    public String getPackageName() {
+        return packageName;
+    }
+
+    public void setPackageName(String packageName) {
+        this.packageName = packageName;
+    }
+
     public KFProgressButton.STATUS getStatus() {
         return status;
     }
 
     public void setStatus(KFProgressButton.STATUS status) {
+        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(packageName);
         this.status = status;
         Set<String> sets = btns.keySet();
         for (String set : sets) {
             if (downloadUrl.equals(btns.get(set).getTag(mIntTag))) {
-                btns.get(set).updateButtonUI(status);
+                btns.get(set).updateButtonUI(status, isPkgInstalled);
             }
             //else 如果已经被别的复用,暂时不用删除吧。
         }

+ 44 - 9
app/src/main/java/com/kfzs/duanduan/utils/dlg/DownBtnUtils.java

@@ -4,12 +4,16 @@ import android.app.Activity;
 import android.content.Context;
 import android.util.Log;
 
+import com.kfzs.duanduan.datashare.provider.download.DownLoadInfo;
+import com.kfzs.duanduan.services.DownloadTaskService;
+import com.kfzs.duanduan.utils.ApkUtils;
 import com.kfzs.duanduan.view.KFProgressButton;
 import com.kfzs.duanduan.utils.InstallButtonUtils;
 import com.kfzs.duanduan.event.EventDownloadHandler;
 import com.kfzs.duanduan.utils.GameStatusScanner;
 import com.kfzs.duanduan.proto.GameOuterClass;
 import com.sheep.gamegroup.view.dialog.DialogAccountAbnormal;
+import com.sheep.jiuyan.samllsheep.download.DownloadInfo;
 
 import java.util.Map;
 
@@ -36,15 +40,14 @@ public class DownBtnUtils {
             @Override
             public void downloading(String downloadUrl, Integer progress) {
                 if (installButtonMgrMap.get(downloadUrl) != null) {
-                    installButtonMgrMap.get(downloadUrl).setDownloadedPgrs(progress);
+                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_PROGRESSING);
                 }
             }
 
             @Override
             public void downloadFinish(String downloadUrl) {
                 if (installButtonMgrMap.get(downloadUrl) != null) {
-                    installButtonMgrMap.get(downloadUrl)
-                            .setStatus(KFProgressButton.STATUS.STATUS_FINISH);
+                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_FINISH);
                 }
             }
 
@@ -68,6 +71,13 @@ public class DownBtnUtils {
                     installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_DELETE);
                 }
             }
+
+            @Override
+            public void downloadInit(String downloadUrl) {
+                if (installButtonMgrMap.get(downloadUrl) != null) {
+                    installButtonMgrMap.get(downloadUrl).setStatus(KFProgressButton.STATUS.STATUS_INIT);
+                }
+            }
         };
     }
 
@@ -87,10 +97,9 @@ public class DownBtnUtils {
                                         GameOuterClass.Game model, String tag) {
         GameStatusScanner mScanner = new GameStatusScanner(context, btn, tag) {
             @Override
-            public void onButtonClickReturnListener(String downloadUrl, boolean drawBtnUI, int percent) {
+            public void onButtonClickReturnListener(String downloadUrl, KFProgressButton.STATUS status) {
                 if (installButtonMgr != null && installButtonMgr.containsKey(downloadUrl)) {
-                    installButtonMgr.get(downloadUrl).
-                            setStatus(drawBtnUI ? KFProgressButton.STATUS.STATUS_PAUSED : KFProgressButton.STATUS.STATUS_INSTALLING);
+                    installButtonMgr.get(downloadUrl).setStatus(status);
                 }
             }
         };
@@ -117,13 +126,39 @@ public class DownBtnUtils {
             Log.e(TAG, "推荐页游戏下载地址错误:" + model.getGameName() + "#" + model.getDownloadUrl());
             return;
         }
+        boolean isPkgInstalled = ApkUtils.getInstance().isPkgInstalled(model.getPackageName());
         if (installButtonMgr.containsKey(model.getDownloadUrl())) {
             installButtonMgr.get(model.getDownloadUrl()).addBtn(target, tag);
-            target.updateButtonUI(installButtonMgr.get(model.getDownloadUrl()).getStatus());
+            target.updateButtonUI(installButtonMgr.get(model.getDownloadUrl()).getStatus(), isPkgInstalled);
         } else {
+            KFProgressButton.STATUS status = KFProgressButton.STATUS.STATUS_INIT;
+            DownLoadInfo downloadInfo = new DownloadTaskService(context).getDownloadTaskByUrl(model.getDownloadUrl());
+            if(downloadInfo != null && downloadInfo.getMStatus() != null){
+                switch (downloadInfo.getMStatus()){
+                    case DownloadTaskService.STATUS_ING:
+                        status = KFProgressButton.STATUS.STATUS_PROGRESSING;
+                        break;
+                    case DownloadTaskService.STATUS_PAUSE:
+                        status = KFProgressButton.STATUS.STATUS_PAUSED;
+                        break;
+                    case DownloadTaskService.STATUS_FAIL:
+                        status = KFProgressButton.STATUS.STATUS_FAIL;
+                        break;
+                    case DownloadTaskService.STATUS_FINISH:
+                        status = KFProgressButton.STATUS.STATUS_FINISH;
+                        break;
+                    case DownloadTaskService.STATUS_DELETE:
+                        status = KFProgressButton.STATUS.STATUS_DELETE;
+                        break;
+                    case DownloadTaskService.STATUS_INIT:
+                    default:
+                        status = KFProgressButton.STATUS.STATUS_INIT;
+                        break;
+                }
+            }
             installButtonMgr.put(model.getDownloadUrl(), InstallButtonUtils
-                    .init(String.valueOf(tag), model.getDownloadUrl(), target, tag));
-            target.updateButtonUI(KFProgressButton.STATUS.STATUS_INIT);
+                    .init(String.valueOf(tag), model.getDownloadUrl(), model.getPackageName(), target, tag));
+            target.updateButtonUI(status, isPkgInstalled);
         }
         DownBtnUtils.initBtnListener(context, target, installButtonMgr, model, TAG);
     }

+ 10 - 2
app/src/main/java/com/kfzs/duanduan/view/KFProgressButton.java

@@ -101,7 +101,11 @@ public class KFProgressButton extends android.support.v7.widget.AppCompatButton
         super.onDraw(canvas);
     }
 
-    public void updateButtonUI(STATUS status) {
+    public void updateButtonUI(STATUS status, boolean isPkgInstalled) {
+        if(isPkgInstalled){
+            setBtnProgressingStyle("打开");
+            return;
+        }
         this.mStatus = status;
         switch (status) {
             case STATUS_INIT://这个地方,有时候是预下载,这样重新按键后会变成安装,所以弄个defaultstr
@@ -140,7 +144,11 @@ public class KFProgressButton extends android.support.v7.widget.AppCompatButton
      * @param progress
      */
     public void updateProgress(int progress) {
-        setBtnProgressingStyle("下载中");
+        if(progress == 100)
+            setBtnProgressingStyle("安装");
+        else
+            setBtnProgressingStyle("下载中");
+
     }
 
 

+ 9 - 6
app/src/main/java/com/sheep/gamegroup/view/activity/DialogActivity.java

@@ -217,7 +217,10 @@ public class DialogActivity extends Activity implements TaskDialogContract.View
                         task.setFilePath(filePath);
                         task.start();
                     }
-                    mDownloadTaskService.addDownloadTask(downLoadInfo);
+                    if(mDownloadTaskService.addDownloadTask(downLoadInfo)){
+                        //发送红点信息
+                        EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.TIPS_DOWN_ICON));
+                    }
                     break;
                 case 3://开始安装
                     isDownLoad = PackageUtil.isAppInstalled(SheepApp.mContext, task_entity.getPackage_names());
@@ -389,7 +392,7 @@ public class DialogActivity extends Activity implements TaskDialogContract.View
     @Download.onTaskRunning protected void running(DownloadTask task) {
         if(task.getKey().equals(task_entity.getDownload_link())) {
             int p = task.getPercent();    //任务进度百分比
-            mDownloadTaskService.setDownloadTaskPercent(p, task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
+//            mDownloadTaskService.setDownloadTaskPercent(p, task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
             String speed = task.getConvertSpeed();    //转换单位后的下载速度,单位转换需要在配置文件中打开
             long speed1 = task.getSpeed(); //原始byte长度速度
             start_task_tv.setText(p + "%");
@@ -399,7 +402,7 @@ public class DialogActivity extends Activity implements TaskDialogContract.View
 
     @Download.onTaskStop void taskStop(DownloadTask task) {
         if(task.getKey().equals(task_entity.getDownload_link())) {
-            mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_PAUSE);
+//            mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_PAUSE);
             start_task_tv.setText("继续下载");
         }
     }
@@ -409,21 +412,21 @@ public class DialogActivity extends Activity implements TaskDialogContract.View
 
     @Download.onTaskCancel void taskCancel(DownloadTask task) {
         if(task.getKey().equals(task_entity.getDownload_link())) {
-            mDownloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
+//            mDownloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
             start_task_tv.setText("已经取消");
         }
     }
 
     @Download.onTaskFail void taskFail(DownloadTask task) {
         if(task.getKey().equals(task_entity.getDownload_link())) {
-            mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
+//            mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
             start_task_tv.setText("下载失败");
         }
     }
 
     @Download.onTaskComplete void taskComplete(DownloadTask task) {
         if(task.getKey().equals(task_entity.getDownload_link())) {
-            mDownloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
+//            mDownloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
             //在这里处理任务完成的状态
             start_task_tv.setText("开始安装");
             type = 3;

+ 36 - 247
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -1,28 +1,14 @@
 package com.sheep.jiuyan.samllsheep.service;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import android.app.Notification;
-import android.app.NotificationManager;
 import android.app.Service;
-import android.content.Context;
 import android.content.Intent;
-import android.net.Uri;
-import android.os.Environment;
-import android.os.Handler;
 import android.os.IBinder;
-import android.text.TextUtils;
-import android.util.Log;
-import android.widget.ProgressBar;
-import android.widget.RemoteViews;
-import android.widget.TextView;
-import android.widget.Toast;
-import com.sheep.jiuyan.samllsheep.R;
-import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
+import android.support.annotation.Nullable;
+
+import com.arialyy.annotations.Download;
+import com.arialyy.aria.core.Aria;
+import com.arialyy.aria.core.download.DownloadTask;
+import com.kfzs.duanduan.services.DownloadTaskService;
 
 /**
  *  @auther dabiaoge
@@ -31,243 +17,46 @@ import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 
 public class DownloadService extends Service{
 
-    public static final String DOWNLOAD_PATH =
-            Environment.getExternalStorageDirectory().getAbsolutePath()+
-                    "Sheep/downloads/";
-    public static final String TAG = "download";
-    private String url;//下载链接
-    private int length;//文件长度
-    private String fileName=null;//文件名
-    private Notification notification;
-    private RemoteViews contentView;
-    private NotificationManager notificationManager;
-
-    private static final int MSG_INIT = 0;
-    private static final int URL_ERROR = 1;
-    private static final int NET_ERROR = 2;
-    private static final int DOWNLOAD_SUCCESS = 3;
-    private Handler mHandler = new Handler(){
-        public void handleMessage(android.os.Message msg) {
-            switch (msg.what) {
-                case MSG_INIT:
-                    length = (int) msg.obj;
-                    new DownloadThread(url,length).start();
-                    createNotification();
-                    break;
-                case DOWNLOAD_SUCCESS:
-                    //下载完成
-                    notifyNotification(100, 100);
-                    PackageUtil.installApk(DownloadService.this, DOWNLOAD_PATH+File.separator+fileName);
-                    Toast.makeText(DownloadService.this, "下载完成", Toast.LENGTH_LONG).show();
-                    break;
-                case URL_ERROR:
-                    Toast.makeText(DownloadService.this, "下载地址错误", Toast.LENGTH_LONG).show();
-                    break;
-                case NET_ERROR:
-                    Toast.makeText(DownloadService.this, "连接失败,请检查网络设置", Toast.LENGTH_LONG).show();
-            }
-        };
-    };
-
-
-    public IBinder onBind(Intent arg0) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
+    private DownloadTaskService mDownloadTaskService;
     @Override
-    public int onStartCommand(Intent intent, int flags, int startId) {
-        if(intent != null){
-            url = intent.getStringExtra("url");
-            if(url != null && !TextUtils.isEmpty(url)){
-                new InitThread(url).start();
-            }else{
-                mHandler.sendEmptyMessage(URL_ERROR);
-            }
-
-        }
-        return super.onStartCommand(intent, flags, startId);
+    public void onCreate() {
+        super.onCreate();
+        Aria.download(this).register();
+        mDownloadTaskService = new DownloadTaskService(this);
     }
 
-    /**
-     * 初始化子线程
-     * @author dong
-     *
-     */
-    class InitThread extends Thread{
-        String url = "";
-
-        public InitThread(String url) {
-            this.url = url;
-        }
-        public void run() {
-            HttpURLConnection conn= null;
-            RandomAccessFile raf = null;
-            try {
-                //连接网络文件
-                URL url = new URL(this.url);
-                conn = (HttpURLConnection) url.openConnection();
-                conn.setConnectTimeout(3000);
-                conn.setRequestMethod("GET");
-                int length = -1;
-                if(conn.getResponseCode() == 200){
-                    //获得文件长度
-                    length = conn.getContentLength();
-                }
-                if(length <= 0){
-                    return;
-                }
-                File dir = new File(DOWNLOAD_PATH);
-                if(!dir.exists()){
-                    dir.mkdir();
-                }
-                fileName = this.url.substring(this.url.lastIndexOf("/")+1, this.url.length());
-                if(fileName==null && TextUtils.isEmpty(fileName) && !fileName.contains(".apk")){
-                    fileName = getPackageName()+".apk";
-                }
-                File file = new File(dir, fileName);
-                raf = new RandomAccessFile(file, "rwd");
-                //设置文件长度
-                raf.setLength(length);
-                mHandler.obtainMessage(MSG_INIT,length).sendToTarget();
-            } catch (Exception e) {
-                mHandler.sendEmptyMessage(URL_ERROR);
-                e.printStackTrace();
-            } finally{
-                try {
-                    conn.disconnect();
-                    raf.close();
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }
+    @Download.onPre void onPre(DownloadTask task) {
     }
-    /**
-     * 下载线程
-     * @author dong
-     *
-     */
-    class DownloadThread extends Thread{
-        String url;
-        int length;
-        public DownloadThread(String url, int length) {
-            this.url = url;
-            this.length = length;
-        }
-        @Override
-        public void run() {
-            HttpURLConnection conn = null;
-            RandomAccessFile raf = null;
-            InputStream input = null;
-            try {
-
-                URL url = new URL(this.url);
-                conn = (HttpURLConnection) url.openConnection();
-                conn.setConnectTimeout(3000);
-                conn.setRequestMethod("GET");
-                //设置下载位置
-                int start =0;
-                conn.setRequestProperty("Range", "bytes="+0+"-"+length);
-                //设置文件写入位置
-                File file = new File(DownloadService.DOWNLOAD_PATH,fileName);
-                raf = new RandomAccessFile(file, "rwd");
-                raf.seek(start);
-                long mFinished = 0;
-                //开始下载
-                if(conn.getResponseCode() == 206){
-                    //LogUtil.i("下载开始了。。。");
-                    //读取数据
-                    input = conn.getInputStream();
-                    byte[] buffer = new byte[1024*4];
-                    int len = -1;
-                    long speed = 0;
-                    long time = System.currentTimeMillis();
-                    while((len = input.read(buffer)) != -1){
-                        //写入文件
-                        raf.write(buffer,0,len);
-                        //把下载进度发送广播给Activity
-                        mFinished += len;
-                        speed += len;
-                        if(System.currentTimeMillis() - time > 1000){
-                            time = System.currentTimeMillis();
-
-                            notifyNotification(mFinished,length);
-                            Log.i(TAG, "mFinished=="+mFinished);
-                            Log.i(TAG, "length=="+length);
-                            Log.i(TAG, "speed=="+speed);
-                            speed = 0;
-                        }
-                    }
-                    mHandler.sendEmptyMessage(DOWNLOAD_SUCCESS);
-                    Log.i(TAG, "下载完成了。。。");
-                }else{
-                    Log.i(TAG, "下载出错了。。。");
-                    mHandler.sendEmptyMessage(NET_ERROR);
-                }
-
-            } catch (Exception e) {
-                e.printStackTrace();
-            } finally{
-                try {
-                    if(conn != null){
-                        conn.disconnect();
-                    }
-                    if(raf != null){
-                        raf.close();
-                    }
-                    if(input != null ){
-                        input.close();
-                    }
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-
-            }
-        }
+    @Download.onTaskStart void taskStart(DownloadTask task) {
+    }
+    @Download.onTaskResume void taskResume(DownloadTask task) {
+    }
+    //在这里处理任务执行中的状态,如进度进度条的刷新
+    @Download.onTaskRunning protected void running(DownloadTask task) {
+        mDownloadTaskService.setDownloadTaskPercent(task.getPercent(), task.getCurrentProgress()/1024d/1024d,task.getFileSize()/1024d/1024d,0,task.getKey());
     }
-    @SuppressWarnings("deprecation")
-    public void createNotification() {
-        notification = new Notification(
-                R.drawable.icon,//应用的图标
-                "安装包正在下载...",
-                System.currentTimeMillis());
-        notification.flags = Notification.FLAG_ONGOING_EVENT;
-        //notification.flags = Notification.FLAG_AUTO_CANCEL;
-
-        /*** 自定义  Notification 的显示****/
-        contentView = new RemoteViews(getPackageName(), R.layout.notification_item);
-        contentView.setProgressBar(R.id.progress, 100, 0, false);
-        contentView.setTextViewText(R.id.tv_progress, "0%");
-        notification.contentView = contentView;
 
-        /*updateIntent = new Intent(this, AboutActivity.class);
-      	updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
-     	updateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-     	pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
-      	notification.contentIntent = pendingIntent;*/
-        notification.flags = Notification.FLAG_AUTO_CANCEL;
-        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+    @Download.onTaskStop void taskStop(DownloadTask task) {
+        mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_PAUSE);
+    }
 
-        //设置notification的PendingIntent
-		/*Intent intt = new Intent(this, MainActivity.class);
-		PendingIntent pi = PendingIntent.getActivity(this,100, intt,Intent.FLAG_ACTIVITY_NEW_TASK	| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
-		notification.contentIntent = pi;*/
+    @Download.onNoSupportBreakPoint void onNoSupportBreakPoint(DownloadTask task) {
+    }
 
-        notificationManager.notify(R.layout.notification_item, notification);
+    @Download.onTaskCancel void taskCancel(DownloadTask task) {
+        mDownloadTaskService.deleteDownloadTaskByDownloadUrl(task.getKey());
     }
 
-    private void notifyNotification(long percent,long length){
+    @Download.onTaskFail void taskFail(DownloadTask task) {
+        mDownloadTaskService.setDownloadTaskStatus(task.getKey(), DownloadTaskService.STATUS_FAIL);
+    }
 
-        contentView.setTextViewText(R.id.tv_progress, (percent*100/length)+"%");
-        contentView.setProgressBar(R.id.progress, (int)length,(int)percent, false);
-        notification.contentView = contentView;
-        notificationManager.notify(R.layout.notification_item, notification);
+    @Download.onTaskComplete void taskComplete(DownloadTask task) {
+        mDownloadTaskService.setDownloadTaskFinish(task.getKey(), task.getDownloadPath());
+    }
+    @Nullable
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
     }
-    /**
-     * 安装apk
-     *
-     * @param context 上下文
-     * @param file    APK文件
-     */
 }

+ 3 - 0
app/src/main/java/com/sheep/jiuyan/samllsheep/utils/ClassFileHelper.java

@@ -365,4 +365,7 @@ public class ClassFileHelper {
         }
         return true;
     }
+    public static String getFilePathByPackageName(String packgeName){
+        return DIR+ File.separator+packgeName+ FILE_SUFFIX;
+    }
 }

+ 3 - 1
datashare/src/main/java/com/kfzs/duanduan/datashare/DDProviderHelper.java

@@ -316,7 +316,7 @@ public class DDProviderHelper {
      * @param context
      * @param info
      */
-    public void addOrUpDataDownloadTask(Context context, DownLoadInfo info) {
+    public boolean addOrUpDataDownloadTask(Context context, DownLoadInfo info) {
         DownLoadInfoDao infoDao = getDaossion(context).getDownLoadInfoDao();
         DownLoadInfo downLoadInfo = infoDao
                 .queryBuilder()
@@ -326,6 +326,7 @@ public class DDProviderHelper {
         try{
             if (downLoadInfo == null) {
                 infoDao.insert(info);
+                return true;
             } else {
                 info.setMId(downLoadInfo.getMId());
                 infoDao.update(info);
@@ -333,6 +334,7 @@ public class DDProviderHelper {
         }catch (Exception e){
             e.printStackTrace();
         }
+        return false;
     }
 
     /**