Search in sources :

Example 36 with Query

use of android.app.DownloadManager.Query in project baker-android by bakerframework.

the class DownloaderTask method doInBackground.

@Override
protected String doInBackground(String... params) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl));
    request.setDescription(fileDescription);
    request.setTitle(fileTitle);
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(visibility);
    }
    if (this.isOverwrite() && !this.isDownloading()) {
        String filepath = downloadPath + File.separator + fileName;
        Log.d(this.getClass().toString(), "DownloaderTask will overwrite the file " + filepath);
        boolean result = this.fileExists(filepath);
        if (result) {
            this.deleteFile(filepath);
        }
    }
    String result = "";
    try {
        Log.d(this.getClass().toString(), "USING RELATIVE PATH FOR DOWNLOAD: " + downloadPath);
        File downloadDirectory = new File(this.downloadPath);
        // If the download directory (or parent directories) does not exist, we create it.
        if (!downloadDirectory.exists()) {
            downloadDirectory.mkdirs();
        }
        request.setDestinationUri(Uri.parse("file://".concat(downloadPath.concat(File.separator).concat(fileName))));
        if (downloadId == -1L) {
            downloadId = dm.enqueue(request);
            this.storeDownloadId();
        }
        Query query = new Query();
        query.setFilterById(downloadId);
        boolean downloading = true;
        while (downloading) {
            Cursor c = this.dm.query(query);
            c.moveToFirst();
            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch(status) {
                case DownloadManager.STATUS_PAUSED:
                    if (!Configuration.hasNetworkConnection(this.context)) {
                        result = "ERROR";
                        dm.remove(downloadId);
                        downloading = false;
                    }
                    break;
                case DownloadManager.STATUS_PENDING:
                    //Do nothing
                    break;
                case DownloadManager.STATUS_RUNNING:
                    long totalBytes = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                    long bytesSoFar = c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    long progress = (bytesSoFar * 100 / totalBytes);
                    Log.d(this.getClass().getName(), "RUNNING Download of " + this.fileName + " progress: " + progress + "%");
                    publishProgress(progress, bytesSoFar, totalBytes);
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
                    publishProgress(100L, c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)), c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)));
                    downloading = false;
                    Log.d(this.getClass().getName(), "SUCCESSFULLY Downloaded " + this.fileName);
                    result = "SUCCESS";
                    downloadedFile = dm.getUriForDownloadedFile(downloadId);
                    break;
                case DownloadManager.STATUS_FAILED:
                    result = "ERROR";
                    Log.e(this.getClass().getName(), "ERROR Downloading " + this.fileName);
                    dm.remove(downloadId);
                    downloading = false;
                    break;
            }
            c.close();
        }
    } catch (IllegalStateException ex) {
        result = "DIRECTORY_NOT_FOUND";
        ex.printStackTrace();
    } catch (android.database.CursorIndexOutOfBoundsException ex) {
    // This exception might be thrown when the user presses the back
    // button and cancels the current downloads, so the index
    // does not exist anymore.
    }
    return result;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 37 with Query

use of android.app.DownloadManager.Query in project XposedInstaller by rovo89.

the class DownloadsUtil method removeOutdated.

public static void removeOutdated(Context context, long cutoff) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnLastMod = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP);
    List<Long> idsList = new ArrayList<>();
    while (c.moveToNext()) {
        if (c.getLong(columnLastMod) < cutoff)
            idsList.add(c.getLong(columnId));
    }
    c.close();
    if (idsList.isEmpty())
        return;
    long[] ids = new long[idsList.size()];
    for (int i = 0; i < ids.length; i++) ids[i] = idsList.get(0);
    dm.remove(ids);
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager)

Example 38 with Query

use of android.app.DownloadManager.Query in project Xposed-Tinted-Status-Bar by MohammadAG.

the class DownloadsUtil method getById.

public static DownloadInfo getById(Context context, long id) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query().setFilterById(id));
    if (!c.moveToFirst())
        return null;
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnUri = c.getColumnIndexOrThrow(DownloadManager.COLUMN_URI);
    int columnTitle = c.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE);
    int columnLastMod = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP);
    int columnFilename = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME);
    int columnStatus = c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS);
    int columnTotalSize = c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
    int columnBytesDownloaded = c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
    int columnReason = c.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON);
    String localFilename = c.getString(columnFilename);
    if (localFilename != null && !localFilename.isEmpty() && !new File(localFilename).isFile()) {
        dm.remove(c.getLong(columnId));
        return null;
    }
    return new DownloadInfo(c.getLong(columnId), c.getString(columnUri), c.getString(columnTitle), c.getLong(columnLastMod), localFilename, c.getInt(columnStatus), c.getInt(columnTotalSize), c.getInt(columnBytesDownloaded), c.getInt(columnReason));
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 39 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by ParanoidAndroid.

the class ConnectionUtil method downloadSuccessful.

/**
     * Determines if a given download was successful by querying the DownloadManager.
     *
     * @param enqueue the id used to identify/query the DownloadManager with.
     * @return true if download was successful, false otherwise.
     */
private boolean downloadSuccessful(long enqueue) {
    Query query = new Query();
    query.setFilterById(enqueue);
    Cursor c = mDownloadManager.query(query);
    if (c.moveToFirst()) {
        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
            Log.v(LOG_TAG, "Successfully downloaded file!");
            return true;
        }
    }
    return false;
}
Also used : Query(android.app.DownloadManager.Query) Cursor(android.database.Cursor)

Example 40 with Query

use of android.app.DownloadManager.Query in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerBaseTest method removeAllCurrentDownloads.

/**
     * Helper to remove all downloads that are registered with the DL Manager.
     *
     * Note: This gives us a clean slate b/c it includes downloads that are pending, running,
     * paused, or have completed.
     */
protected void removeAllCurrentDownloads() {
    Log.i(LOG_TAG, "Removing all current registered downloads...");
    ArrayList<Long> ids = new ArrayList<Long>();
    Cursor cursor = mDownloadManager.query(new Query());
    try {
        if (cursor.moveToFirst()) {
            do {
                int index = cursor.getColumnIndex(DownloadManager.COLUMN_ID);
                long downloadId = cursor.getLong(index);
                ids.add(downloadId);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    // delete all ids
    for (long id : ids) {
        mDownloadManager.remove(id);
    }
    // make sure the database is empty
    cursor = mDownloadManager.query(new Query());
    try {
        assertEquals(0, cursor.getCount());
    } finally {
        cursor.close();
    }
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

Query (android.app.DownloadManager.Query)76 Cursor (android.database.Cursor)76 DownloadManager (android.app.DownloadManager)15 ArrayList (java.util.ArrayList)13 ParcelFileDescriptor (android.os.ParcelFileDescriptor)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 TimeoutException (java.util.concurrent.TimeoutException)12 File (java.io.File)11 IOException (java.io.IOException)7 Request (android.app.DownloadManager.Request)6 Uri (android.net.Uri)6 FileNotFoundException (java.io.FileNotFoundException)6 Random (java.util.Random)6