// Path: aisen_downloader/src/main/java/org/aiwen/downloader/utils/Constants.java
// public class Constants {
// 
//     public static final String TAG = "Hawk";
// 
//     public static final String TEMP_SUFFIX = ".atp";
// 
//     /** The intent that gets sent when the service must wake up for a retry */
//     public static final String ACTION_RETRY = "android.intent.action.DOWNLOAD_WAKEUP";
// 
//     /** the intent that gets sent when clicking an incomplete/failed download  */
//     public static final String ACTION_LIST = "android.intent.action.DOWNLOAD_LIST";
// 
//     /** the intent that gets sent when clicking a successful download */
//     public static final String ACTION_OPEN = "android.intent.action.DOWNLOAD_OPEN";
// 
//     /** the intent that gets sent when deleting the notification of a completed download */
//     public static final String ACTION_HIDE = "android.intent.action.DOWNLOAD_HIDE";
// 
//     /** The minimum amount of progress that has to be done before the progress bar gets updated */
//     public static final int MIN_PROGRESS_STEP = 65536;
// 
//     /** The minimum amount of time that has to elapse before the progress bar gets updated, in ms */
//     public static final long MIN_PROGRESS_TIME = 1000;// 2000
// 
//     /**
//      * The number of times that the download manager will retry its network
//      * operations when no progress is happening before it gives up.
//      */
//     public static final int MAX_RETRIES = 5;
// 
// }
// 
// Path: aisen_downloader/src/main/java/org/aiwen/downloader/utils/Utils.java
// public class Utils {
// 
//     public static void close(Closeable closeable) {
//         try {
//             if (closeable != null)
//                 closeable.close();
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//     }
// 
//     public static String getDownloaderTAG(Request request) {
//         return Constants.TAG + "_Thread_" + request.key;
//     }
// 
//     public static long now() {
//         return System.currentTimeMillis();
//     }
// 
//     public static long realtime() {
//         return SystemClock.elapsedRealtime();
//     }
// 
//     public static boolean isNetworkActive() {
//         Hawk hawk = Hawk.getInstance();
//         if (hawk != null) {
//             ConnectivityManager connectivityManager = (ConnectivityManager) hawk.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
// 
//             return networkInfo != null && networkInfo.isConnected();
//         }
// 
//         return false;
//     }
// 
//     public static boolean isWifiActive() {
//         Hawk hawk = Hawk.getInstance();
//         if (hawk != null) {
//             ConnectivityManager connectivityManager = (ConnectivityManager) hawk.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
// 
//             return networkInfo != null &&
//                         networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
//                         networkInfo.isConnected();
//         }
// 
//         return false;
//     }
// 
// }


import org.aiwen.downloader.utils.Constants;
import org.aiwen.downloader.utils.Utils;
import java.io.File;
package org.aiwen.downloader;



/**
 * Created by 王dan on 2016/12/21.
 */

class FileManager {

    /**
     * 创建临时下载文件
     *
     * @param request
     * @return
     */
    static File createFile(Request request, boolean temp) throws DownloadException {
        try {
            String suffix = temp ? Constants.TEMP_SUFFIX : "";
            File tempFile = new File(request.fileUri.getPath() + suffix);
//            long rangeBytes = request.downloadInfo.rangeBytes;

            if (tempFile.exists()) {
//                DLogger.v(Utils.getDownloaderTAG(request), "临时文件已存在");
//
//                // 临时文件长度和缓存数据不一致，删除缓存文件重新下载
//                if (tempFile.length() > 0 && tempFile.length() != rangeBytes) {
//                    DLogger.w(Utils.getDownloaderTAG(request), "临时文件长度和缓存数据长度不一致，file(%d), range(%d)", tempFile.length(), rangeBytes);
//
//                    if (tempFile.delete()) {
//                        DLogger.w(Utils.getDownloaderTAG(request), "删除临时文件");
//                    }
//                    else {
//                        DLogger.w(Utils.getDownloaderTAG(request), "删除临时文件失败");
//                    }
//
//                    request.downloadInfo.rangeBytes = 0;
//                }
//                else {
//                    DLogger.d(Utils.getDownloaderTAG(request), "文件断点下载，file(%d), range(%d)", tempFile.length(), rangeBytes);
//                }
            }
            else {
                // 创建父文件夹
                File dir = tempFile.getParentFile();
                if (dir.exists()) {
                    if (!dir.isDirectory()) {
                        dir.delete();
                        dir.mkdirs();

// Path: aisen_downloader/src/main/java/org/aiwen/downloader/utils/Constants.java
// public class Constants {
// 
//     public static final String TAG = "Hawk";
// 
//     public static final String TEMP_SUFFIX = ".atp";
// 
//     /** The intent that gets sent when the service must wake up for a retry */
//     public static final String ACTION_RETRY = "android.intent.action.DOWNLOAD_WAKEUP";
// 
//     /** the intent that gets sent when clicking an incomplete/failed download  */
//     public static final String ACTION_LIST = "android.intent.action.DOWNLOAD_LIST";
// 
//     /** the intent that gets sent when clicking a successful download */
//     public static final String ACTION_OPEN = "android.intent.action.DOWNLOAD_OPEN";
// 
//     /** the intent that gets sent when deleting the notification of a completed download */
//     public static final String ACTION_HIDE = "android.intent.action.DOWNLOAD_HIDE";
// 
//     /** The minimum amount of progress that has to be done before the progress bar gets updated */
//     public static final int MIN_PROGRESS_STEP = 65536;
// 
//     /** The minimum amount of time that has to elapse before the progress bar gets updated, in ms */
//     public static final long MIN_PROGRESS_TIME = 1000;// 2000
// 
//     /**
//      * The number of times that the download manager will retry its network
//      * operations when no progress is happening before it gives up.
//      */
//     public static final int MAX_RETRIES = 5;
// 
// }
// 
// Path: aisen_downloader/src/main/java/org/aiwen/downloader/utils/Utils.java
// public class Utils {
// 
//     public static void close(Closeable closeable) {
//         try {
//             if (closeable != null)
//                 closeable.close();
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//     }
// 
//     public static String getDownloaderTAG(Request request) {
//         return Constants.TAG + "_Thread_" + request.key;
//     }
// 
//     public static long now() {
//         return System.currentTimeMillis();
//     }
// 
//     public static long realtime() {
//         return SystemClock.elapsedRealtime();
//     }
// 
//     public static boolean isNetworkActive() {
//         Hawk hawk = Hawk.getInstance();
//         if (hawk != null) {
//             ConnectivityManager connectivityManager = (ConnectivityManager) hawk.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
// 
//             return networkInfo != null && networkInfo.isConnected();
//         }
// 
//         return false;
//     }
// 
//     public static boolean isWifiActive() {
//         Hawk hawk = Hawk.getInstance();
//         if (hawk != null) {
//             ConnectivityManager connectivityManager = (ConnectivityManager) hawk.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
//             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
// 
//             return networkInfo != null &&
//                         networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
//                         networkInfo.isConnected();
//         }
// 
//         return false;
//     }
// 
// }


// Path: aisen_downloader/src/main/java/org/aiwen/downloader/FileManager.java
import org.aiwen.downloader.utils.Constants;
import org.aiwen.downloader.utils.Utils;
import java.io.File;

package org.aiwen.downloader;



/**
 * Created by 王dan on 2016/12/21.
 */

class FileManager {

    /**
     * 创建临时下载文件
     *
     * @param request
     * @return
     */
    static File createFile(Request request, boolean temp) throws DownloadException {
        try {
            String suffix = temp ? Constants.TEMP_SUFFIX : "";
            File tempFile = new File(request.fileUri.getPath() + suffix);
//            long rangeBytes = request.downloadInfo.rangeBytes;

            if (tempFile.exists()) {
//                DLogger.v(Utils.getDownloaderTAG(request), "临时文件已存在");
//
//                // 临时文件长度和缓存数据不一致，删除缓存文件重新下载
//                if (tempFile.length() > 0 && tempFile.length() != rangeBytes) {
//                    DLogger.w(Utils.getDownloaderTAG(request), "临时文件长度和缓存数据长度不一致，file(%d), range(%d)", tempFile.length(), rangeBytes);
//
//                    if (tempFile.delete()) {
//                        DLogger.w(Utils.getDownloaderTAG(request), "删除临时文件");
//                    }
//                    else {
//                        DLogger.w(Utils.getDownloaderTAG(request), "删除临时文件失败");
//                    }
//
//                    request.downloadInfo.rangeBytes = 0;
//                }
//                else {
//                    DLogger.d(Utils.getDownloaderTAG(request), "文件断点下载，file(%d), range(%d)", tempFile.length(), rangeBytes);
//                }
            }
            else {
                // 创建父文件夹
                File dir = tempFile.getParentFile();
                if (dir.exists()) {
                    if (!dir.isDirectory()) {
                        dir.delete();
                        dir.mkdirs();

                        DLogger.d(Utils.getDownloaderTAG(request), "新建下载目标文件夹(%s)", dir.getAbsolutePath());