Java Code Examples for android.app.DownloadManager#STATUS_PAUSED

The following examples show how to use android.app.DownloadManager#STATUS_PAUSED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadStorageProvider.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
       final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
       final String docId = String.valueOf(id);

       final String displayName = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
       String summary = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
       String mimeType = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
       if (mimeType == null) {
           // Provide fake MIME type so it's openable
           mimeType = "vnd.android.document/file";
       }
       Long size = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
       if (size == -1) {
           size = null;
       }

       final int status = cursor.getInt(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
       switch (status) {
           case DownloadManager.STATUS_SUCCESSFUL:
               break;
           case DownloadManager.STATUS_PAUSED:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_PENDING:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_RUNNING:
               final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
                       DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
               if (size != null) {
                   final long percent = progress * 100 / size;
                   summary = getContext().getString(R.string.download_running_percent, percent);
               } else {
                   summary = getContext().getString(R.string.download_running);
               }
               break;
           case DownloadManager.STATUS_FAILED:
           default:
               summary = getContext().getString(R.string.download_error);
               break;
       }

       int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
       if (mimeType != null && mimeType.startsWith("image/")) {
           flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
       }

       final long lastModified = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

       final RowBuilder row = result.newRow();
       row.add(Document.COLUMN_DOCUMENT_ID, docId);
       row.add(Document.COLUMN_DISPLAY_NAME, displayName);
       row.add(Document.COLUMN_SUMMARY, summary);
       row.add(Document.COLUMN_SIZE, size);
       row.add(Document.COLUMN_MIME_TYPE, mimeType);
       row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
       row.add(Document.COLUMN_FLAGS, flags);
   }
 
Example 2
Source File: DownloadView.java    From EdXposedManager with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    while (true) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            return;
        }

        try {
            mInfo = DownloadsUtil.getById(getContext(), mInfo.id);
        } catch (NullPointerException ignored) {
        }

        refreshView();
        if (mInfo == null)
            return;

        if (mInfo.status != DownloadManager.STATUS_PENDING
                && mInfo.status != DownloadManager.STATUS_PAUSED
                && mInfo.status != DownloadManager.STATUS_RUNNING)
            return;
    }
}
 
Example 3
Source File: DownloadInfo.java    From IslamicLibraryAndroid with GNU General Public License v3.0 6 votes vote down vote up
@StringRes
public int getStatusTextResId() {
    int statusText = 0;
    switch (status) {
        case DownloadManager.STATUS_FAILED:
            statusText = (R.string.STATUS_FAILED);
            break;
        case DownloadManager.STATUS_PAUSED:
            statusText = (R.string.STATUS_PAUSED);

            break;
        case DownloadManager.STATUS_PENDING:
            statusText = (R.string.STATUS_PENDING);
            break;
        case DownloadManager.STATUS_RUNNING:
            statusText = (R.string.STATUS_RUNNING);
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            statusText = (R.string.STATUS_SUCCESSFUL);
            break;
    }
    return statusText;
}
 
Example 4
Source File: ClipDownloadControllerImpl.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
protected boolean downloadInProgress() {
    long downloadId = getDownloadID();
    if (downloadId == -1) {
        return false;
    }

    try {
        switch (getDownloadManagerStatus(downloadId)) {
            case DownloadManager.STATUS_FAILED:
                removeCurrentDownloadReferences();
                return false;

            case DownloadManager.STATUS_SUCCESSFUL:
                removeCurrentDownloadReferences();
                return false;

            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                return true;

            default:
            case -1:
                return false;
        }
    }
    catch (Exception ex) {
        logger.error("Could not process download ID [{}]", downloadId, ex);
    }

    return false;
}
 
Example 5
Source File: Downloader.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
private void checkStatus() {
    //cause SQLiteException at 乐视 LE X820 Android 6.0.1,level 23
    try{
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadId);
        Cursor c = downloadManager.query(query);
        if (c.moveToFirst()) {
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch (status) {
                case DownloadManager.STATUS_PAUSED:
                    break;
                case DownloadManager.STATUS_PENDING:
                    break;
                case DownloadManager.STATUS_RUNNING:
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
                    String tip = mContext.getString(R.string.download_complete)
                            .concat("\n").concat(getFilePath());
                    Toasty.success(mContext, tip).show();
                    unregister();
                    break;
                case DownloadManager.STATUS_FAILED:
                    Toasty.error(mContext, mContext.getString(R.string.download_failed)).show();
                    unregister();
                    break;
            }
        }
        c.close();
    }catch (SQLiteException e){
        Logger.d(e);
        unregister();
    }

}
 
Example 6
Source File: SplashActivity.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
private String statusMessage(@NonNull Cursor c) {
    String msg;

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
        case DownloadManager.STATUS_FAILED:
            msg = "DownloadInfo failed";
            break;

        case DownloadManager.STATUS_PAUSED:
            msg = "DownloadInfo paused";
            break;

        case DownloadManager.STATUS_PENDING:
            msg = "DownloadInfo pending";
            break;

        case DownloadManager.STATUS_RUNNING:
            msg = "DownloadInfo in progress";
            break;

        case DownloadManager.STATUS_SUCCESSFUL:
            msg = "DownloadInfo complete";
            break;

        default:
            msg = "DownloadInfo is nowhere in sight";
            break;
    }

    return (msg);
}
 
Example 7
Source File: Download.java    From FirefoxReality with Mozilla Public License 2.0 5 votes vote down vote up
public static Download from(Cursor cursor) {
    Download download = new Download();
    download.mId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
    download.mUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
    int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
    switch (status) {
        case DownloadManager.STATUS_RUNNING:
            download.mStatus = RUNNING;
            break;
        case DownloadManager.STATUS_FAILED:
            download.mStatus = FAILED;
            break;
        case DownloadManager.STATUS_PAUSED:
            download.mStatus = PAUSED;
            break;
        case DownloadManager.STATUS_PENDING:
            download.mStatus = PENDING;
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            download.mStatus = SUCCESSFUL;
            break;
        default:
            download.mStatus = UNAVAILABLE;
    }
    download.mMediaType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
    download.mTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));
    download.mOutputFile = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    download.mDescription = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION));
    download.mSizeBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
    download.mDownloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
    download.mLastModified = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
    download.mReason = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
    return download;
}
 
Example 8
Source File: ClipDownloadControllerImpl.java    From arcusandroid with Apache License 2.0 5 votes vote down vote up
protected void doGetStatus(long downloadId) {
    try {
        switch (getDownloadManagerStatus(downloadId)) {
            case DownloadManager.STATUS_FAILED:
                onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_CANCELED);
                removeCurrentDownloadReferences();
                break;

            case DownloadManager.STATUS_SUCCESSFUL:
                onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
                removeCurrentDownloadReferences();
                break;

            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                int progress = doGetDownloadProgress();
                if (progress == 100) {
                    onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
                    removeCurrentDownloadReferences();
                }
                else {
                    onDownloadProgressChanged(progress, STATUS_DOWNLOAD_RUNNING);
                }
                break;
        }
    }
    catch (Exception ex) {
        removeCurrentDownloadReferences();
        onDownloadFatalError(ex);
    }
}
 
Example 9
Source File: Downloader.java    From react-native-simple-download-manager with MIT License 4 votes vote down vote up
private HashMap<String, String> getDownloadStatus(Cursor cursor, long downloadId) {

        int columnStatusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
        int STATUS = cursor.getInt(columnStatusIndex);
        int columnReasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
        int REASON = cursor.getInt(columnReasonIndex);
        int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String filename = cursor.getString(filenameIndex);

        String statusText = "";
        String reasonText = "";

        switch (STATUS) {
            case DownloadManager.STATUS_FAILED:
                statusText = "STATUS_FAILED";
                switch (REASON) {
                    case DownloadManager.ERROR_CANNOT_RESUME:
                        reasonText = "ERROR_CANNOT_RESUME";
                        break;
                    case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                        reasonText = "ERROR_DEVICE_NOT_FOUND";
                        break;
                    case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                        reasonText = "ERROR_FILE_ALREADY_EXISTS";
                        break;
                    case DownloadManager.ERROR_FILE_ERROR:
                        reasonText = "ERROR_FILE_ERROR";
                        break;
                    case DownloadManager.ERROR_HTTP_DATA_ERROR:
                        reasonText = "ERROR_HTTP_DATA_ERROR";
                        break;
                    case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                        reasonText = "ERROR_INSUFFICIENT_SPACE";
                        break;
                    case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                        reasonText = "ERROR_TOO_MANY_REDIRECTS";
                        break;
                    case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                        reasonText = "ERROR_UNHANDLED_HTTP_CODE";
                        break;
                    default:
                        reasonText = "ERROR_UNKNOWN";
                        break;
                }
                break;
            case DownloadManager.STATUS_PAUSED:
                statusText = "STATUS_PAUSED";
                switch (REASON) {
                    case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                        reasonText = "PAUSED_QUEUED_FOR_WIFI";
                        break;
                    case DownloadManager.PAUSED_UNKNOWN:
                        reasonText = "PAUSED_UNKNOWN";
                        break;
                    case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                        reasonText = "PAUSED_WAITING_FOR_NETWORK";
                        break;
                    case DownloadManager.PAUSED_WAITING_TO_RETRY:
                        reasonText = "PAUSED_WAITING_TO_RETRY";
                        break;
                    default:
                        reasonText = "UNKNOWN";
                }
                break;
            case DownloadManager.STATUS_PENDING:
                statusText = "STATUS_PENDING";
                break;
            case DownloadManager.STATUS_RUNNING:
                statusText = "STATUS_RUNNING";
                break;
            case DownloadManager.STATUS_SUCCESSFUL:
                statusText = "STATUS_SUCCESSFUL";
                reasonText = filename;
                break;
            default:
                statusText = "STATUS_UNKNOWN";
                reasonText = String.valueOf(STATUS);
                break;
        }

        HashMap<String, String> result = new HashMap<>();
        result.put("status", statusText);
        result.put("reason", reasonText);
        result.put("downloadId", String.valueOf(downloadId));
        return result;
    }
 
Example 10
Source File: DownloadInfo.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
public static String getReasonDebugString(int status, int reason) {
    String reasonText = "unKnown_status";
    switch (status) {
        case DownloadManager.STATUS_FAILED:
            switch (reason) {
                case DownloadManager.ERROR_CANNOT_RESUME:
                    reasonText = "ERROR_CANNOT_RESUME";
                    break;
                case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                    reasonText = "(ERROR_DEVICE_NOT_FOUND)";
                    break;
                case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                    reasonText = "(ERROR_FILE_ALREADY_EXISTS)";
                    break;
                case DownloadManager.ERROR_FILE_ERROR:
                    reasonText = "(ERROR_FILE_ERROR)";
                    break;
                case DownloadManager.ERROR_HTTP_DATA_ERROR:
                    reasonText = "(ERROR_HTTP_DATA_ERROR)";
                    break;
                case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                    reasonText = "(ERROR_INSUFFICIENT_SPACE)";
                    break;
                case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                    reasonText = "(ERROR_TOO_MANY_REDIRECTS)";
                    break;
                case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                    reasonText = "(ERROR_UNHANDLED_HTTP_CODE)";
                    break;
                case DownloadManager.ERROR_UNKNOWN:
                    reasonText = "(ERROR_UNKNOWN)";
                    break;
            }
            break;
        case DownloadManager.STATUS_PAUSED: {
            switch (reason) {
                case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                    reasonText = "PAUSED_QUEUED_FOR_WIFI";
                    break;
                case DownloadManager.PAUSED_UNKNOWN:
                    reasonText = "PAUSED_UNKNOWN";
                    break;
                case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                    reasonText = "PAUSED_WAITING_FOR_NETWORK";
                    break;
                case DownloadManager.PAUSED_WAITING_TO_RETRY:
                    reasonText = "PAUSED_WAITING_TO_RETRY";
                    break;
            }
            break;
        }
    }
    return reasonText;
}
 
Example 11
Source File: DownloadInfo.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
@StringRes
public static int getReasonTextResId(int status, int reason) {
    int reasonText = R.string.unKnown_status;
    switch (status) {
        case DownloadManager.STATUS_FAILED:
            switch (reason) {
                case DownloadManager.ERROR_CANNOT_RESUME:
                    reasonText = R.string.ERROR_CANNOT_RESUME;
                    break;
                case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                    reasonText = (R.string.ERROR_DEVICE_NOT_FOUND);
                    break;
                case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                    reasonText = (R.string.ERROR_FILE_ALREADY_EXISTS);
                    break;
                case DownloadManager.ERROR_FILE_ERROR:
                    reasonText = (R.string.ERROR_FILE_ERROR);
                    break;
                case DownloadManager.ERROR_HTTP_DATA_ERROR:
                    reasonText = (R.string.ERROR_HTTP_DATA_ERROR);
                    break;
                case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                    reasonText = (R.string.ERROR_INSUFFICIENT_SPACE);
                    break;
                case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                    reasonText = (R.string.ERROR_TOO_MANY_REDIRECTS);
                    break;
                case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                    reasonText = (R.string.ERROR_UNHANDLED_HTTP_CODE);
                    break;
                case DownloadManager.ERROR_UNKNOWN:
                    reasonText = (R.string.ERROR_UNKNOWN);
                    break;
            }
            break;
        case DownloadManager.STATUS_PAUSED: {
            switch (reason) {
                case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                    reasonText = (R.string.PAUSED_QUEUED_FOR_WIFI);
                    break;
                case DownloadManager.PAUSED_UNKNOWN:
                    reasonText = (R.string.PAUSED_UNKNOWN);
                    break;
                case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                    reasonText = (R.string.PAUSED_WAITING_FOR_NETWORK);
                    break;
                case DownloadManager.PAUSED_WAITING_TO_RETRY:
                    reasonText = (R.string.PAUSED_WAITING_TO_RETRY);
                    break;
            }
            break;
        }
    }
    return reasonText;
}
 
Example 12
Source File: DownloadView.java    From EdXposedManager with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    if (mUrl == null) {
        btnDownload.setVisibility(View.GONE);
        btnSave.setVisibility(View.GONE);
        btnDownloadCancel.setVisibility(View.GONE);
        btnRemove.setVisibility(View.GONE);
        btnInstall.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        txtInfo.setVisibility(View.VISIBLE);
        txtInfo.setText(R.string.download_view_no_url);
    } else if (mInfo == null) {
        btnDownload.setVisibility(View.VISIBLE);
        btnSave.setVisibility(View.VISIBLE);
        btnDownloadCancel.setVisibility(View.GONE);
        btnRemove.setVisibility(View.GONE);
        btnInstall.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        txtInfo.setVisibility(View.GONE);
    } else {
        switch (mInfo.status) {
            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_PAUSED:
            case DownloadManager.STATUS_RUNNING:
                btnDownload.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnDownloadCancel.setVisibility(View.VISIBLE);
                btnRemove.setVisibility(View.GONE);
                btnInstall.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                txtInfo.setVisibility(View.VISIBLE);
                if (mInfo.totalSize <= 0 || mInfo.status != DownloadManager.STATUS_RUNNING) {
                    progressBar.setIndeterminate(true);
                    txtInfo.setText(R.string.download_view_waiting);
                } else {
                    progressBar.setIndeterminate(false);
                    progressBar.setMax(mInfo.totalSize);
                    progressBar.setProgress(mInfo.bytesDownloaded);
                    txtInfo.setText(getContext().getString(
                            R.string.download_view_running,
                            mInfo.bytesDownloaded / 1024,
                            mInfo.totalSize / 1024));
                }
                break;

            case DownloadManager.STATUS_FAILED:
                btnDownload.setVisibility(View.VISIBLE);
                btnSave.setVisibility(View.VISIBLE);
                btnDownloadCancel.setVisibility(View.GONE);
                btnRemove.setVisibility(View.GONE);
                btnInstall.setVisibility(View.GONE);
                progressBar.setVisibility(View.GONE);
                txtInfo.setVisibility(View.VISIBLE);
                txtInfo.setText(getContext().getString(
                        R.string.download_view_failed, mInfo.reason));
                break;

            case DownloadManager.STATUS_SUCCESSFUL:
                btnDownload.setVisibility(View.GONE);
                btnSave.setVisibility(View.GONE);
                btnDownloadCancel.setVisibility(View.GONE);
                btnRemove.setVisibility(View.VISIBLE);
                btnInstall.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
                txtInfo.setVisibility(View.VISIBLE);
                txtInfo.setText(R.string.download_view_successful);
                break;
        }
    }
}
 
Example 13
Source File: DownloadStorageProvider.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
       final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
       final String docId = String.valueOf(id);

       final String displayName = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
       String summary = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
       String mimeType = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
       if (mimeType == null) {
           // Provide fake MIME type so it's openable
           mimeType = "vnd.android.document/file";
       }
       Long size = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
       if (size == -1) {
           size = null;
       }

       final int status = cursor.getInt(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
       switch (status) {
           case DownloadManager.STATUS_SUCCESSFUL:
               break;
           case DownloadManager.STATUS_PAUSED:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_PENDING:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_RUNNING:
               final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
                       DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
               if (size != null) {
                   final long percent = progress * 100 / size;
                   summary = getContext().getString(R.string.download_running_percent, percent);
               } else {
                   summary = getContext().getString(R.string.download_running);
               }
               break;
           case DownloadManager.STATUS_FAILED:
           default:
               summary = getContext().getString(R.string.download_error);
               break;
       }

       int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
       if (mimeType != null && mimeType.startsWith("image/")) {
           flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
       }

       final long lastModified = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

       final RowBuilder row = result.newRow();
       row.add(Document.COLUMN_DOCUMENT_ID, docId);
       row.add(Document.COLUMN_DISPLAY_NAME, displayName);
       row.add(Document.COLUMN_SUMMARY, summary);
       row.add(Document.COLUMN_SIZE, size);
       row.add(Document.COLUMN_MIME_TYPE, mimeType);
       row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
       row.add(Document.COLUMN_FLAGS, flags);
   }
 
Example 14
Source File: DownloadStorageProvider.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
       final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
       final String docId = String.valueOf(id);

       final String displayName = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
       String summary = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
       String mimeType = cursor.getString(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
       if (mimeType == null) {
           // Provide fake MIME type so it's openable
           mimeType = "vnd.android.document/file";
       }
       Long size = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
       if (size == -1) {
           size = null;
       }

       final int status = cursor.getInt(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
       switch (status) {
           case DownloadManager.STATUS_SUCCESSFUL:
               break;
           case DownloadManager.STATUS_PAUSED:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_PENDING:
               summary = getContext().getString(R.string.download_queued);
               break;
           case DownloadManager.STATUS_RUNNING:
               final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
                       DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
               if (size != null) {
                   final long percent = progress * 100 / size;
                   summary = getContext().getString(R.string.download_running_percent, percent);
               } else {
                   summary = getContext().getString(R.string.download_running);
               }
               break;
           case DownloadManager.STATUS_FAILED:
           default:
               summary = getContext().getString(R.string.download_error);
               break;
       }

       int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
       if (mimeType != null && mimeType.startsWith("image/")) {
           flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
       }

       final long lastModified = cursor.getLong(
               cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));

       final RowBuilder row = result.newRow();
       row.add(Document.COLUMN_DOCUMENT_ID, docId);
       row.add(Document.COLUMN_DISPLAY_NAME, displayName);
       row.add(Document.COLUMN_SUMMARY, summary);
       row.add(Document.COLUMN_SIZE, size);
       row.add(Document.COLUMN_MIME_TYPE, mimeType);
       row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
       row.add(Document.COLUMN_FLAGS, flags);
   }
 
Example 15
Source File: UpdateService.java    From Ency with Apache License 2.0 4 votes vote down vote up
/**
 * 检查下载状态
 */
private void checkStatus(Context context) {
    DownloadManager.Query query = new DownloadManager.Query();
    //通过下载的id查找
    query.setFilterById(downloadId);
    Cursor c = dm.query(query);
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        switch (status) {
            //下载暂停
            case DownloadManager.STATUS_PAUSED:
                break;
            //下载延迟
            case DownloadManager.STATUS_PENDING:
                break;
            //正在下载
            case DownloadManager.STATUS_RUNNING:
                break;
            //下载完成
            case DownloadManager.STATUS_SUCCESSFUL:
                //下载完成安装APK
                File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "ency.apk");
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setDataAndType(uri, "application/vnd.android.package-archive");
                } else {
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                }
                context.startActivity(intent);
                break;
            //下载失败
            case DownloadManager.STATUS_FAILED:
                Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    c.close();
}
 
Example 16
Source File: Updater.java    From AndroidUpdater with Apache License 2.0 4 votes vote down vote up
public void download(UpdaterConfig updaterConfig) {

        if (!UpdaterUtils.checkDownloadState(updaterConfig.getContext())) {
            Toast.makeText(updaterConfig.getContext(), R.string.system_download_component_disable, Toast.LENGTH_SHORT).show();
            UpdaterUtils.showDownloadSetting(updaterConfig.getContext());
            return;
        }

        long downloadId = UpdaterUtils.getLocalDownloadId(updaterConfig.getContext());
        Logger.get().d("local download id is " + downloadId);
        if (downloadId != -1L) {
            FileDownloadManager fdm = FileDownloadManager.get();
            //获取下载状态
            int status = fdm.getDownloadStatus(updaterConfig.getContext(), downloadId);
            switch (status) {
                //下载成功
                case DownloadManager.STATUS_SUCCESSFUL:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_SUCCESSFUL");
                    Uri uri = fdm.getDownloadUri(updaterConfig.getContext(), downloadId);

                    if (uri != null) {
                        //本地的版本大于当前程序的版本直接安装
                        if (UpdaterUtils.compare(updaterConfig.getContext(), uri)) {
                            Logger.get().d("start install UI with local apk");
                            UpdaterUtils.startInstall(updaterConfig.getContext(), uri);
                            return;
                        } else {
                            //从FileDownloadManager中移除这个任务
                            fdm.getDM(updaterConfig.getContext()).remove(downloadId);
                        }
                    }
                    //重新下载
                    startDownload(updaterConfig);
                    break;
                //下载失败
                case DownloadManager.STATUS_FAILED:
                    Logger.get().d("download failed " + downloadId);
                    startDownload(updaterConfig);
                    break;
                case DownloadManager.STATUS_RUNNING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_RUNNING");
                    break;
                case DownloadManager.STATUS_PENDING:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PENDING");
                    break;
                case DownloadManager.STATUS_PAUSED:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PAUSED");
                    break;
                case STATUS_UN_FIND:
                    Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_UN_FIND");
                    startDownload(updaterConfig);
                    break;
                default:
                    Logger.get().d("downloadId=" + downloadId + " ,status = " + status);
                    break;
            }
        } else {
            startDownload(updaterConfig);
        }
    }
 
Example 17
Source File: DownloadManagerDemo.java    From LLApp with Apache License 2.0 4 votes vote down vote up
public static boolean isDownloading(int downloadManagerStatus) {
	return downloadManagerStatus == DownloadManager.STATUS_RUNNING
			|| downloadManagerStatus == DownloadManager.STATUS_PAUSED
			|| downloadManagerStatus == DownloadManager.STATUS_PENDING;
}
 
Example 18
Source File: OfflineVideoManager.java    From android-viewer-for-khan-academy with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected HashMap<String, Integer> doInBackground(Void... arg0) {
	// get an array of ids, for use in the download manager query
	Cursor c = dataService.getHelper().getReadableDatabase().rawQuery(sql, null);
	long[] ids = new long[c.getCount()];
	while (c.moveToNext()) {
		ids[c.getPosition()] = c.getLong(c.getColumnIndex("dlm_id"));
	}
	c.close();

	if (ids.length > 0) {
		q.setFilterById(ids);
		q.setFilterByStatus(DownloadManager.STATUS_RUNNING);
		
		Cursor cursor = getDownloadManager().query(q);
		HashMap<String, Integer> update = new HashMap<String, Integer>(cursor.getCount());
		while (cursor.moveToNext()) {
			String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
			String youtubeId = youtubeIdFromFilename(filename);
			int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
			if (youtubeId != null) {
				int pct = -1;
				switch (status) {
				case DownloadManager.STATUS_FAILED:
				case DownloadManager.STATUS_PENDING:
				default:
					pct = 0;
					break;
				case DownloadManager.STATUS_PAUSED:
				case DownloadManager.STATUS_RUNNING:
					long bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
					long size = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
					pct = (int) (size==0? 0: 100 * bytes_downloaded / size);
					break;
				case DownloadManager.STATUS_SUCCESSFUL:
					pct = 100;
					break;
				}
				update.put(youtubeId, pct);
			}
		}
		cursor.close();
		
		return update;
	}
	
	return null;
}
 
Example 19
Source File: Index.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public Index(Context context, SQLiteDatabase mapsDatabase, SQLiteDatabase hillshadesDatabase) {
    mContext = context;
    mMapsDatabase = mapsDatabase;
    mHillshadeDatabase = hillshadesDatabase;
    mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

    try {
        Cursor cursor = mMapsDatabase.query(TABLE_MAPS, ALL_COLUMNS_MAPS, WHERE_MAPS_PRESENT, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            int x = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_X));
            int y = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_Y));
            short date = cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_DATE));
            byte version = (byte) cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_VERSION));
            logger.debug("index({}, {}, {}, {})", x, y, date, version);
            if (x == -1 && y == -1) {
                mBaseMapVersion = date;
                cursor.moveToNext();
                continue;
            }
            long downloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_DOWNLOADING));
            long hillshadeDownloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_HILLSHADE_DOWNLOADING));
            MapStatus mapStatus = getNativeMap(x, y);
            mapStatus.created = date;
            mapStatus.hillshadeVersion = version;
            int status = checkDownloadStatus(downloading);
            if (status == DownloadManager.STATUS_PAUSED
                    || status == DownloadManager.STATUS_PENDING
                    || status == DownloadManager.STATUS_RUNNING) {
                mapStatus.downloading = downloading;
                logger.debug("  map downloading: {}", downloading);
            } else {
                downloading = 0L;
                setDownloading(x, y, downloading, hillshadeDownloading);
                logger.debug("  cleared");
            }
            status = checkDownloadStatus(hillshadeDownloading);
            if (status == DownloadManager.STATUS_PAUSED
                    || status == DownloadManager.STATUS_PENDING
                    || status == DownloadManager.STATUS_RUNNING) {
                mapStatus.hillshadeDownloading = hillshadeDownloading;
                logger.debug("  hillshade downloading: {}", downloading);
            } else {
                hillshadeDownloading = 0L;
                setDownloading(x, y, downloading, hillshadeDownloading);
                logger.debug("  cleared");
            }
            if (date > 0)
                mLoadedMaps++;
            cursor.moveToNext();
        }
        cursor.close();
    } catch (SQLiteException e) {
        logger.error("Failed to read map index", e);
        mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_CREATE_MAPS);
        mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_INDEX_MAPS);
    }
    mHasHillshades = DatabaseUtils.queryNumEntries(mHillshadeDatabase, TABLE_TILES) > 0;

    //TODO Remove old basemap file
}
 
Example 20
Source File: DownloadTreeViewAdapter.java    From satstat with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
		// this will be called when a download finishes
		Long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
		DownloadManager.Query query = new DownloadManager.Query();
		query.setFilterById(reference);
		Cursor cursor = downloadManager.query(query);
		if (!cursor.moveToFirst()) {
			cursor.close();
			return;
		}
		int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
		//int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
		cursor.close();
		switch (status) {
		case DownloadManager.STATUS_SUCCESSFUL:
			// The file was downloaded successfully
			removeDownload(reference, true);
			break;
		case DownloadManager.STATUS_FAILED:
			// The download failed
			removeDownload(reference, false);
			break;
		case DownloadManager.STATUS_PAUSED:
			// The download was paused, update status once more
			checkProgress();
			manager.refresh();
			break;
		// The other status values are unusable because they don't fire reliably.
		}
	} else if (intent.getAction().equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
		Intent mapDownloadIntent = new Intent(getActivity().getApplicationContext(), MapDownloadActivity.class);
		mapDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		if (savedInstanceState != null)
			mapDownloadIntent.putExtra(Const.KEY_SAVED_INSTANCE_STATE, savedInstanceState);
		getActivity().getApplicationContext().startActivity(mapDownloadIntent);
	} else if (intent.getAction().equals(Const.DOWNLOAD_RECEIVER_REGISTERED)) {
		/*
		 * A new download receiver has been registered. If we're released, unregister.
		 */
		if (isReleased)
			getActivity().getApplicationContext().unregisterReceiver(downloadReceiver);
	}
}