Search in sources :

Example 6 with DownloadManager

use of android.app.DownloadManager in project Signal-Android by WhisperSystems.

the class UpdateApkReadyListener method isMatchingDigest.

private boolean isMatchingDigest(Context context, long downloadId, String theirEncodedDigest) {
    try {
        if (theirEncodedDigest == null)
            return false;
        byte[] theirDigest = Hex.fromStringCondensed(theirEncodedDigest);
        DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        FileInputStream fin = new FileInputStream(downloadManager.openDownloadedFile(downloadId).getFileDescriptor());
        byte[] ourDigest = FileUtils.getFileDigest(fin);
        fin.close();
        return MessageDigest.isEqual(ourDigest, theirDigest);
    } catch (IOException e) {
        Log.w(TAG, e);
        return false;
    }
}
Also used : IOException(java.io.IOException) DownloadManager(android.app.DownloadManager) FileInputStream(java.io.FileInputStream)

Example 7 with DownloadManager

use of android.app.DownloadManager in project Klyph by jonathangerbaud.

the class KlyphDownloadManager method downloadFile.

private static void downloadFile(Context context, String url, String title, String desc, boolean notifOnProgress, boolean notifOnComplete, String systemDir, String appDir) {
    File direct = new File(Environment.getExternalStorageDirectory() + systemDir + "/" + appDir);
    if (!direct.exists()) {
        boolean success = direct.mkdirs();
        if (!success) {
            direct = new File(Environment.getExternalStoragePublicDirectory(systemDir) + "/" + appDir);
            if (!direct.exists()) {
                success = direct.mkdirs();
            }
        }
    }
    DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    if (!Android.isMinAPI(14)) {
        url = url.replace("https://", "http://");
    }
    Uri Download_Uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
    if (Android.isMinAPI(11)) {
        request.allowScanningByMediaScanner();
        int visibility = 0;
        if (notifOnComplete == true) {
            visibility = DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED;
        } else if (notifOnProgress == true) {
            visibility = DownloadManager.Request.VISIBILITY_VISIBLE;
        }
        request.setNotificationVisibility(visibility);
    }
    request.setTitle(title);
    request.setDescription(desc);
    request.setDestinationInExternalPublicDir(systemDir, "/" + appDir + "/" + url.substring(url.lastIndexOf("/") + 1));
    downloadManager.enqueue(request);
}
Also used : File(java.io.File) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri)

Example 8 with DownloadManager

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

the class DownloadsUtil method getAllForUrl.

public static List<DownloadInfo> getAllForUrl(Context context, String url) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    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);
    List<DownloadInfo> downloads = new ArrayList<>();
    while (c.moveToNext()) {
        if (!url.equals(c.getString(columnUri)))
            continue;
        int status = c.getInt(columnStatus);
        String localFilename = c.getString(columnFilename);
        if (status == DownloadManager.STATUS_SUCCESSFUL && !new File(localFilename).isFile()) {
            dm.remove(c.getLong(columnId));
            continue;
        }
        downloads.add(new DownloadInfo(c.getLong(columnId), c.getString(columnUri), c.getString(columnTitle), c.getLong(columnLastMod), localFilename, status, c.getInt(columnTotalSize), c.getInt(columnBytesDownloaded), c.getInt(columnReason)));
    }
    c.close();
    Collections.sort(downloads);
    return downloads;
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 9 with DownloadManager

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

the class DownloadsUtil method removeAllForLocalFile.

public static void removeAllForLocalFile(Context context, File file) {
    file.delete();
    String filename;
    try {
        filename = file.getCanonicalPath();
    } catch (IOException e) {
        Log.w(XposedApp.TAG, "Could not resolve path for " + file.getAbsolutePath(), e);
        return;
    }
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnFilename = c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME);
    List<Long> idsList = new ArrayList<>(1);
    while (c.moveToNext()) {
        String itemFilename = c.getString(columnFilename);
        if (itemFilename != null) {
            if (filename.equals(itemFilename)) {
                idsList.add(c.getLong(columnId));
            } else {
                try {
                    if (filename.equals(new File(itemFilename).getCanonicalPath())) {
                        idsList.add(c.getLong(columnId));
                    }
                } catch (IOException ignored) {
                }
            }
        }
    }
    c.close();
    if (idsList.isEmpty())
        return;
    long[] ids = new long[idsList.size()];
    for (int i = 0; i < ids.length; i++) ids[i] = idsList.get(i);
    dm.remove(ids);
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 10 with DownloadManager

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

the class DownloadsUtil method removeAllForUrl.

public static void removeAllForUrl(Context context, String url) {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Cursor c = dm.query(new Query());
    int columnId = c.getColumnIndexOrThrow(DownloadManager.COLUMN_ID);
    int columnUri = c.getColumnIndexOrThrow(DownloadManager.COLUMN_URI);
    List<Long> idsList = new ArrayList<>(1);
    while (c.moveToNext()) {
        if (url.equals(c.getString(columnUri)))
            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(i);
    dm.remove(ids);
}
Also used : Query(android.app.DownloadManager.Query) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager)

Aggregations

DownloadManager (android.app.DownloadManager)53 File (java.io.File)14 Cursor (android.database.Cursor)13 Request (android.app.DownloadManager.Request)12 Uri (android.net.Uri)12 Suppress (android.test.suitebuilder.annotation.Suppress)10 Query (android.app.DownloadManager.Query)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Intent (android.content.Intent)5 UiObject (android.support.test.uiautomator.UiObject)5 Response (com.android.volley.Response)3 Context (android.content.Context)2 Nullable (android.support.annotation.Nullable)2 AlertDialog (android.support.v7.app.AlertDialog)2 View (android.view.View)2 Request (com.android.volley.Request)2 VolleyError (com.android.volley.VolleyError)2 FileInputStream (java.io.FileInputStream)2 JSONObject (org.json.JSONObject)2