Przeglądaj źródła

修复下载失败时没有提示用户空间不足的bug,以及因此无法在下载管理中直接删除该下载记录的bug

zengjiebin 7 lat temu
rodzic
commit
f1d38ae6b4

+ 1 - 1
app/build.gradle

@@ -23,7 +23,7 @@ android {
         flavorDimensions "versionCode"
         flavorDimensions "versionCode"
     }
     }
     greendao {
     greendao {
-        schemaVersion 14
+        schemaVersion 15
         targetGenDir 'src/main/java'
         targetGenDir 'src/main/java'
     }
     }
 //    compileOptions{
 //    compileOptions{

+ 7 - 4
app/src/main/java/com/kfzs/duanduan/view/DialogStorageLow.java

@@ -1,12 +1,15 @@
 package com.kfzs.duanduan.view;
 package com.kfzs.duanduan.view;
 
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.Context;
 import android.content.Intent;
 import android.content.Intent;
 import android.provider.Settings;
 import android.provider.Settings;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AlertDialog;
+import android.text.TextUtils;
 import android.view.View;
 import android.view.View;
 
 
 import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.DialogConfig;
+import com.sheep.gamegroup.util.ActivityManager;
 import com.sheep.gamegroup.util.ViewUtil;
 import com.sheep.gamegroup.util.ViewUtil;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 
 
@@ -17,15 +20,15 @@ import com.sheep.jiuyan.samllsheep.SheepApp;
 
 
 public class DialogStorageLow {
 public class DialogStorageLow {
 
 
-    public static AlertDialog showDialog(Context context) {
-        final Context mContext = context == null ? SheepApp.getInstance() : context;
-        return ViewUtil.showMsgDialog(mContext, new DialogConfig().setTitle("温馨提示").setMsgMore("存储空间不足\n请释放一些储存空间,并重新尝试安装!")
+    public static AlertDialog showDialog(String msgMore) {
+        final Activity activity = ActivityManager.getInstance().currentActivity();
+        return ViewUtil.showMsgDialog(activity, new DialogConfig().setTitle("温馨提示").setMsgMore("存储空间不足\n请释放一些储存空间,并重新尝试安装!" + (TextUtils.isEmpty(msgMore) ? "" : ("\n"+msgMore)))
             .setBtnRightText("取消").setBtnLeftText("管理应用").setBtnLeftOnClickListener(new View.OnClickListener() {
             .setBtnRightText("取消").setBtnLeftText("管理应用").setBtnLeftOnClickListener(new View.OnClickListener() {
                     @Override
                     @Override
                     public void onClick(View view) {
                     public void onClick(View view) {
                         Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                         Intent intent = new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                        mContext.startActivity(intent);
+                        activity.startActivity(intent);
                     }
                     }
                 }));
                 }));
     }
     }

+ 15 - 0
app/src/main/java/com/sheep/gamegroup/greendao/DDProviderHelper.java

@@ -197,6 +197,21 @@ public class DDProviderHelper {
     /**
     /**
      * 更新任务状态
      * 更新任务状态
      *
      *
+     * @param downloadTaskId
+     * @param downloadUrl
+     */
+    public DownLoadInfo updateDownload(int downloadTaskId, String downloadUrl, int state) {
+        DownLoadInfo downloadTask = getDownloadTask(downloadUrl);
+        if (downloadTask != null) {
+            downloadTask.setMDownloadTaskId(downloadTaskId);
+            downloadTask.setMStatus(state);
+            updateDownload(downloadTask);
+        }
+        return downloadTask;
+    }
+    /**
+     * 更新任务状态
+     *
      * @param downloadedSize
      * @param downloadedSize
      * @param downloadTaskId
      * @param downloadTaskId
      * @param downloadUrl
      * @param downloadUrl

+ 2 - 2
app/src/main/java/com/sheep/gamegroup/greendao/download/DaoMaster.java

@@ -14,10 +14,10 @@ import org.greenrobot.greendao.identityscope.IdentityScopeType;
 
 
 // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
 // THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
 /**
 /**
- * Master of DAO (schema version 14): knows all DAOs.
+ * Master of DAO (schema version 15): knows all DAOs.
  */
  */
 public class DaoMaster extends AbstractDaoMaster {
 public class DaoMaster extends AbstractDaoMaster {
-    public static final int SCHEMA_VERSION = 14;
+    public static final int SCHEMA_VERSION = 15;
 
 
     /** Creates underlying database table using DAOs. */
     /** Creates underlying database table using DAOs. */
     public static void createAllTables(Database db, boolean ifNotExists) {
     public static void createAllTables(Database db, boolean ifNotExists) {

+ 16 - 2
app/src/main/java/com/sheep/gamegroup/greendao/download/DownLoadInfo.java

@@ -125,6 +125,11 @@ public class DownLoadInfo implements Serializable{
      */
      */
     @Property(nameInDb = "average_speed")
     @Property(nameInDb = "average_speed")
     private String averageSpeed;
     private String averageSpeed;
+    /**
+     * 下载速度
+     */
+    @Property(nameInDb = "real_cause_msg")
+    private String realCauseMsg;
 
 
     public int getWhereFrom() {
     public int getWhereFrom() {
         return whereFrom;
         return whereFrom;
@@ -262,12 +267,20 @@ public class DownLoadInfo implements Serializable{
         this.averageSpeed = averageSpeed;
         this.averageSpeed = averageSpeed;
     }
     }
 
 
-    @Generated(hash = 601502121)
+    public String getRealCauseMsg() {
+        return this.realCauseMsg;
+    }
+
+    public void setRealCauseMsg(String realCauseMsg) {
+        this.realCauseMsg = realCauseMsg;
+    }
+
+    @Generated(hash = 74805486)
     public DownLoadInfo(Long mId, Long mCreateTime, Integer mDownloadTaskId,
     public DownLoadInfo(Long mId, Long mCreateTime, Integer mDownloadTaskId,
             String mGameName, String mIconUrl, Integer mPercent, @NotNull Integer mGameID,
             String mGameName, String mIconUrl, Integer mPercent, @NotNull Integer mGameID,
             Double mDownloadedSize, Double mTotalSize, Integer mStatus, String mDownloadUrl,
             Double mDownloadedSize, Double mTotalSize, Integer mStatus, String mDownloadUrl,
             String mChannelDownloadUrl, String mPackageName, Integer mVersionCode,
             String mChannelDownloadUrl, String mPackageName, Integer mVersionCode,
-            String mApkPath, int whereFrom, String averageSpeed) {
+            String mApkPath, int whereFrom, String averageSpeed, String realCauseMsg) {
         this.mId = mId;
         this.mId = mId;
         this.mCreateTime = mCreateTime;
         this.mCreateTime = mCreateTime;
         this.mDownloadTaskId = mDownloadTaskId;
         this.mDownloadTaskId = mDownloadTaskId;
@@ -285,6 +298,7 @@ public class DownLoadInfo implements Serializable{
         this.mApkPath = mApkPath;
         this.mApkPath = mApkPath;
         this.whereFrom = whereFrom;
         this.whereFrom = whereFrom;
         this.averageSpeed = averageSpeed;
         this.averageSpeed = averageSpeed;
+        this.realCauseMsg = realCauseMsg;
     }
     }
 
 
     @Generated(hash = 1743687477)
     @Generated(hash = 1743687477)

+ 16 - 2
app/src/main/java/com/sheep/gamegroup/greendao/download/DownLoadInfoDao.java

@@ -39,6 +39,7 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
         public final static Property MApkPath = new Property(14, String.class, "mApkPath", false, "apk_path");
         public final static Property MApkPath = new Property(14, String.class, "mApkPath", false, "apk_path");
         public final static Property WhereFrom = new Property(15, int.class, "whereFrom", false, "where_from");
         public final static Property WhereFrom = new Property(15, int.class, "whereFrom", false, "where_from");
         public final static Property AverageSpeed = new Property(16, String.class, "averageSpeed", false, "average_speed");
         public final static Property AverageSpeed = new Property(16, String.class, "averageSpeed", false, "average_speed");
+        public final static Property RealCauseMsg = new Property(17, String.class, "realCauseMsg", false, "real_cause_msg");
     }
     }
 
 
 
 
@@ -70,7 +71,8 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
                 "\"version_code\" INTEGER," + // 13: mVersionCode
                 "\"version_code\" INTEGER," + // 13: mVersionCode
                 "\"apk_path\" TEXT," + // 14: mApkPath
                 "\"apk_path\" TEXT," + // 14: mApkPath
                 "\"where_from\" INTEGER NOT NULL ," + // 15: whereFrom
                 "\"where_from\" INTEGER NOT NULL ," + // 15: whereFrom
-                "\"average_speed\" TEXT);"); // 16: averageSpeed
+                "\"average_speed\" TEXT," + // 16: averageSpeed
+                "\"real_cause_msg\" TEXT);"); // 17: realCauseMsg
     }
     }
 
 
     /** Drops the underlying database table. */
     /** Drops the underlying database table. */
@@ -159,6 +161,11 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
         if (averageSpeed != null) {
         if (averageSpeed != null) {
             stmt.bindString(17, averageSpeed);
             stmt.bindString(17, averageSpeed);
         }
         }
+ 
+        String realCauseMsg = entity.getRealCauseMsg();
+        if (realCauseMsg != null) {
+            stmt.bindString(18, realCauseMsg);
+        }
     }
     }
 
 
     @Override
     @Override
@@ -241,6 +248,11 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
         if (averageSpeed != null) {
         if (averageSpeed != null) {
             stmt.bindString(17, averageSpeed);
             stmt.bindString(17, averageSpeed);
         }
         }
+ 
+        String realCauseMsg = entity.getRealCauseMsg();
+        if (realCauseMsg != null) {
+            stmt.bindString(18, realCauseMsg);
+        }
     }
     }
 
 
     @Override
     @Override
@@ -267,7 +279,8 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
             cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13), // mVersionCode
             cursor.isNull(offset + 13) ? null : cursor.getInt(offset + 13), // mVersionCode
             cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // mApkPath
             cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // mApkPath
             cursor.getInt(offset + 15), // whereFrom
             cursor.getInt(offset + 15), // whereFrom
-            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16) // averageSpeed
+            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // averageSpeed
+            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17) // realCauseMsg
         );
         );
         return entity;
         return entity;
     }
     }
@@ -291,6 +304,7 @@ public class DownLoadInfoDao extends AbstractDao<DownLoadInfo, Long> {
         entity.setMApkPath(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
         entity.setMApkPath(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
         entity.setWhereFrom(cursor.getInt(offset + 15));
         entity.setWhereFrom(cursor.getInt(offset + 15));
         entity.setAverageSpeed(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
         entity.setAverageSpeed(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
+        entity.setRealCauseMsg(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
      }
      }
     
     
     @Override
     @Override

+ 1 - 1
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -476,7 +476,7 @@ public class TestUtil {
                                 G.showToast(md5);
                                 G.showToast(md5);
                                 break;
                                 break;
                             case "空间不足提示框":
                             case "空间不足提示框":
-                                DialogStorageLow.showDialog(activity);
+                                DialogStorageLow.showDialog(null);
                                 break;
                                 break;
                             case "显示已经安装应用列表":
                             case "显示已经安装应用列表":
                                 showHasInstallApkList(activity);
                                 showHasInstallApkList(activity);

+ 23 - 4
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -26,6 +26,7 @@ import com.liulishuo.okdownload.core.listener.DownloadListener4WithSpeed;
 import com.liulishuo.okdownload.core.listener.assist.Listener4SpeedAssistExtend;
 import com.liulishuo.okdownload.core.listener.assist.Listener4SpeedAssistExtend;
 import com.sheep.gamegroup.event.BigEvent;
 import com.sheep.gamegroup.event.BigEvent;
 import com.sheep.gamegroup.event.EventTypes;
 import com.sheep.gamegroup.event.EventTypes;
+import com.sheep.gamegroup.greendao.DDProviderHelper;
 import com.sheep.gamegroup.greendao.download.DownLoadInfo;
 import com.sheep.gamegroup.greendao.download.DownLoadInfo;
 import com.sheep.gamegroup.model.entity.Applications;
 import com.sheep.gamegroup.model.entity.Applications;
 import com.sheep.gamegroup.model.entity.TaskEty;
 import com.sheep.gamegroup.model.entity.TaskEty;
@@ -76,6 +77,7 @@ public class DownloadService extends Service {
         @Override
         @Override
         public void taskStart(@NonNull DownloadTask task) {
         public void taskStart(@NonNull DownloadTask task) {
             LogUtil.println("DownloadListener", "taskStart");
             LogUtil.println("DownloadListener", "taskStart");
+            DDProviderHelper.getInstance().updateDownload(task.getId(), task.getUrl(), DownloadUtil.STATUS_INIT);
         }
         }
 
 
         @Override
         @Override
@@ -125,19 +127,36 @@ public class DownloadService extends Service {
                         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(downLoadInfo));
                         EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(downLoadInfo));
                     }
                     }
                     break;
                     break;
-                case FILE_BUSY:
                 case PRE_ALLOCATE_FAILED://预先分配失败
                 case PRE_ALLOCATE_FAILED://预先分配失败
-                case ERROR://下载错误
-                    downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_FAIL);
+                    //空间不足,弹框提示
+                    try {
+                        DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                    downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
+                    downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
+                    if(realCause != null)
+                        downLoadInfo.setRealCauseMsg(realCause.getMessage());
+                    DDProviderHelper.getInstance().updateDownload(downLoadInfo);
                     EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
                     EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
+                    break;
+                case FILE_BUSY:
+                case ERROR://下载错误
                     //空间不足,弹框提示
                     //空间不足,弹框提示
                     try {
                     try {
                         if (0 <= task.getReadBufferSize() && task.getReadBufferSize() > G.getFreeSpaceB()) {
                         if (0 <= task.getReadBufferSize() && task.getReadBufferSize() > G.getFreeSpaceB()) {
-                            DialogStorageLow.showDialog(DownloadService.this);
+                            DialogStorageLow.showDialog(realCause != null ? realCause.getMessage() : null);
                         }
                         }
                     } catch (Exception e) {
                     } catch (Exception e) {
                         e.printStackTrace();
                         e.printStackTrace();
                     }
                     }
+                    downLoadInfo = downloadUtil.getDownloadTaskByUrl(task.getUrl());
+                    downLoadInfo.setMStatus(DownloadUtil.STATUS_FAIL);
+                    if(realCause != null)
+                        downLoadInfo.setRealCauseMsg(realCause.getMessage());
+                    DDProviderHelper.getInstance().updateDownload(downLoadInfo);
+                    EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_FAIL).setData(downLoadInfo));
                     break;
                     break;
                 case CANCELED://取消下载
                 case CANCELED://取消下载
                     downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);
                     downLoadInfo = downloadUtil.setDownloadTaskStatus(task.getUrl(), DownloadUtil.STATUS_PAUSE);

+ 1 - 1
app/src/main/java/com/sheep/jiuyan/samllsheep/utils/G.java

@@ -239,7 +239,7 @@ public class G {
                     file.length() > getFreeSpaceB() / 2
                     file.length() > getFreeSpaceB() / 2
                             && file.length() > getSdkFreeSpace() / 2
                             && file.length() > getSdkFreeSpace() / 2
             )) {
             )) {
-                DialogStorageLow.showDialog(context);
+                DialogStorageLow.showDialog(null);
             }
             }
         }catch (Exception e){
         }catch (Exception e){
             e.printStackTrace();
             e.printStackTrace();