Search in sources :

Example 16 with DownloadManager

use of android.app.DownloadManager in project LshUtils by SenhLinsh.

the class LshDownloadManager method queryProgress.

public void queryProgress(final QueryCallback callback) {
    if (mRequestId == 0) {
        callback.onFailed("没有当前任务");
        return;
    }
    final DownloadManager manager = (DownloadManager) LshApplicationUtils.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
    final DownloadManager.Query query = new DownloadManager.Query().setFilterById(mRequestId);
    if (query == null) {
        callback.onFailed("没有当前任务");
        return;
    }
    if (mIsQuerying) {
        callback.onFailed("正在查询中");
        return;
    }
    mIsQuerying = true;
    queryProgress(manager, query, callback);
}
Also used : DownloadManager(android.app.DownloadManager)

Example 17 with DownloadManager

use of android.app.DownloadManager in project LshUtils by SenhLinsh.

the class LshDownloadManager method getProgress.

private static float getProgress(long requestId) {
    if (requestId != 0) {
        DownloadManager manager = (DownloadManager) LshApplicationUtils.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Query query = new DownloadManager.Query().setFilterById(requestId);
        return getProgress(manager, query);
    }
    return -1;
}
Also used : DownloadManager(android.app.DownloadManager)

Example 18 with DownloadManager

use of android.app.DownloadManager in project LshUtils by SenhLinsh.

the class LshDownloadManager method cancel.

public void cancel() {
    if (mRequestBuilder != null && mRequestId != 0) {
        DownloadManager manager = (DownloadManager) LshApplicationUtils.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        manager.remove(mRequestId);
        mRequestId = 0;
    }
}
Also used : DownloadManager(android.app.DownloadManager)

Example 19 with DownloadManager

use of android.app.DownloadManager in project saga-android by AnandChowdhary.

the class Updater method checkForUpdates.

public static void checkForUpdates(final Context context, boolean visibility) {
    final int versionCode = getVersionCode(context);
    final ProgressDialog progressDialog = new ProgressDialog(context);
    String updateUrl = "https://www.dropbox.com/s/bka9o3p43oki217/saga.json?raw=1";
    if (visibility) {
        progressDialog.setTitle(context.getString(R.string.update));
        progressDialog.setMessage(context.getString(R.string.update_checking));
        progressDialog.setCancelable(true);
        progressDialog.show();
    }
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, updateUrl, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                int updateVersionCode = response.getInt("versionCode");
                if (updateVersionCode > versionCode && versionCode != 0) {
                    if (progressDialog.isShowing()) {
                        progressDialog.cancel();
                    }
                    final String apkUrl = response.getString("apkUrl");
                    String changelog = response.getString("changelog");
                    AlertDialog dialog = new AlertDialog.Builder(context).setTitle(context.getString(R.string.new_update)).setMessage(changelog).setPositiveButton(context.getString(R.string.update_now), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            File myFile = new File(Utils.getStoragePath(context), "update.apk");
                            if (myFile.exists())
                                myFile.delete();
                            Uri uri = Uri.parse(apkUrl);
                            DownloadManager dMgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                            DownloadManager.Request dr = new DownloadManager.Request(uri);
                            String filename = "update.apk";
                            dr.setTitle(context.getString(R.string.app_name) + " " + context.getString(R.string.update));
                            dr.setDestinationUri(Uri.fromFile(new File(Utils.getStoragePath(context) + "/" + filename)));
                            //                                        dr.setDestinationInExternalPublicDir("/Saga/", filename);
                            dMgr.enqueue(dr);
                        }
                    }).setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).setCancelable(false).create();
                    dialog.show();
                } else {
                    if (progressDialog.isShowing()) {
                        progressDialog.cancel();
                        Toast.makeText(context, context.getString(R.string.no_update), Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (progressDialog.isShowing()) {
                progressDialog.cancel();
                Toast.makeText(context, context.getString(R.string.error_connect), Toast.LENGTH_SHORT).show();
            }
        }
    });
    request.setShouldCache(false);
    VolleySingleton.getInstance(context).getRequestQueue().add(request);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) VolleyError(com.android.volley.VolleyError) DialogInterface(android.content.DialogInterface) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Request(com.android.volley.Request) JSONException(org.json.JSONException) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) File(java.io.File)

Example 20 with DownloadManager

use of android.app.DownloadManager in project AndroidChromium by JackyAndroid.

the class DownloadBroadcastReceiver method openDownload.

/**
     * Called to open a given download item that is downloaded by the android DownloadManager.
     * @param context Context of the receiver.
     * @param intent Intent from the android DownloadManager.
     */
private void openDownload(final Context context, Intent intent) {
    long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
    if (ids == null || ids.length == 0) {
        DownloadManagerService.openDownloadsPage(context);
        return;
    }
    long id = ids[0];
    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = manager.getUriForDownloadedFile(id);
    if (uri == null) {
        // Open the downloads page
        DownloadManagerService.openDownloadsPage(context);
    } else {
        String downloadFilename = IntentUtils.safeGetStringExtra(intent, DownloadNotificationService.EXTRA_DOWNLOAD_FILE_PATH);
        boolean isSupportedMimeType = IntentUtils.safeGetBooleanExtra(intent, DownloadNotificationService.EXTRA_IS_SUPPORTED_MIME_TYPE, false);
        Intent launchIntent = DownloadManagerService.getLaunchIntentFromDownloadId(context, downloadFilename, id, isSupportedMimeType);
        if (!DownloadUtils.fireOpenIntentForDownload(context, launchIntent)) {
            DownloadManagerService.openDownloadsPage(context);
        }
    }
}
Also used : Intent(android.content.Intent) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri)

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