Example usage for android.app DownloadManager EXTRA_DOWNLOAD_ID

List of usage examples for android.app DownloadManager EXTRA_DOWNLOAD_ID

Introduction

In this page you can find the example usage for android.app DownloadManager EXTRA_DOWNLOAD_ID.

Prototype

String EXTRA_DOWNLOAD_ID

To view the source code for android.app DownloadManager EXTRA_DOWNLOAD_ID.

Click Source Link

Document

Intent extra included with #ACTION_DOWNLOAD_COMPLETE intents, indicating the ID (as a long) of the download that just completed.

Usage

From source file:org.openbmap.activities.DialogPreferenceCatalogs.java

/**
 * Initialises download manager for GINGERBREAD and newer
 *///from  www  .  j ava 2 s .co m
@SuppressLint("NewApi")
private void initDownloadManager() {
    mDownloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);

    mReceiver = new BroadcastReceiver() {
        @SuppressLint("NewApi")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                final DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                final Cursor c = mDownloadManager.query(query);
                if (c.moveToFirst()) {
                    final int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                        // we're not checking download id here, that is done in handleDownloads
                        final String uriString = c
                                .getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        handleDownloads(uriString);
                    } else {
                        final int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                        Log.e(TAG, "Download failed: " + reason);
                    }
                }
            }
        }
    };

    getContext().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

From source file:org.openbmap.activities.DialogPreferenceMaps.java

/**
 * Initialises download manager for GINGERBREAD and newer
 *//*from   ww  w .j  a v  a 2 s .c  o m*/
@SuppressLint("NewApi")
private void initDownloadManager() {

    mDownloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);

    mReceiver = new BroadcastReceiver() {
        @SuppressLint("NewApi")
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                final DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                final Cursor c = mDownloadManager.query(query);
                if (c.moveToFirst()) {
                    final int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                        // we're not checking download id here, that is done in handleDownloads
                        final String uriString = c
                                .getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        handleDownloads(uriString);
                    } else {
                        final int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
                        Log.e(TAG, "Download failed: " + reason);
                    }
                }
            }
        }
    };

    getContext().registerReceiver(mReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

From source file:com.musenkishi.wally.activities.MainActivity.java

@Override
protected void handleReceivedIntent(Context context, Intent intent) {
    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
    if (WallyApplication.getDownloadIDs().containsKey(id)) {
        WallyApplication.getDownloadIDs().remove(id);
        if (fileChangeReceiver != null) {
            Intent fileChangeIntent = new Intent(FileChangeReceiver.FILES_CHANGED);
            fileChangeReceiver.onReceive(context, fileChangeIntent);
        }//from   w  ww. j  a va  2  s. c  o  m
        View heartTabImageView = tabBarView.getTab(4).getImageView();
        startHeartPopoutAnimation(heartTabImageView, Color.WHITE);
    }
}

From source file:com.bitants.wally.activities.MainActivity.java

@Override
protected void handleReceivedIntent(Context context, Intent intent) {
    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
    if (WallyApplication.getDownloadIDs().containsKey(id)) {
        WallyApplication.getDownloadIDs().remove(id);
        if (fileReceiver != null) {
            Intent fileChangeIntent = new Intent(FileReceiver.GET_FILES);
            fileReceiver.onReceive(context, fileChangeIntent);
        }/*from w  ww.  j  a  v a2s . c  om*/
        View heartTabImageView = tabBarView.getTab(4).getImageView();
        startHeartPopoutAnimation(heartTabImageView, Color.WHITE);
    }
}

From source file:com.bluros.updater.service.DownloadService.java

private void downloadIncremental(UpdateInfo incrementalUpdateInfo) {
    Log.v(TAG, "Downloading incremental zip: " + incrementalUpdateInfo.getDownloadUrl());
    // Build the name of the file to download, adding .partial at the end.  It will get
    // stripped off when the download completes
    String sourceIncremental = Utils.getIncremental();
    String targetIncremental = mInfo.getIncremental();
    String fileName = "incremental-" + sourceIncremental + "-" + targetIncremental + ".zip";
    String incrementalFilePath = "file://" + getUpdateDirectory().getAbsolutePath() + "/" + fileName
            + ".partial";

    long downloadId = enqueueDownload(incrementalUpdateInfo.getDownloadUrl(), incrementalFilePath);

    // Store in shared preferences
    mPrefs.edit().putLong(Constants.DOWNLOAD_ID, downloadId)
            .putString(Constants.DOWNLOAD_MD5, incrementalUpdateInfo.getMD5Sum())
            .putString(Constants.DOWNLOAD_INCREMENTAL_FOR, mInfo.getFileName()).apply();

    Utils.cancelNotification(this);

    Intent intent = new Intent(DownloadReceiver.ACTION_DOWNLOAD_STARTED);
    intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId);
    sendBroadcast(intent);/*from www.jav a  2 s . com*/
}

From source file:com.bluros.updater.service.DownloadService.java

private void downloadFullZip() {
    Log.v(TAG, "Downloading full zip");

    // Build the name of the file to download, adding .partial at the end.  It will get
    // stripped off when the download completes
    String fullFilePath = "file://" + getUpdateDirectory().getAbsolutePath() + "/" + mInfo.getFileName()
            + ".partial";

    long downloadId = enqueueDownload(mInfo.getDownloadUrl(), fullFilePath);

    // Store in shared preferences
    mPrefs.edit().putLong(Constants.DOWNLOAD_ID, downloadId)
            .putString(Constants.DOWNLOAD_MD5, mInfo.getMD5Sum()).apply();

    Utils.cancelNotification(this);

    Intent intent = new Intent(DownloadReceiver.ACTION_DOWNLOAD_STARTED);
    intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, downloadId);
    sendBroadcast(intent);//from www. j a va  2 s. c  o m
}

From source file:org.crossconnect.bible.activity.main.ResourceFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    resourceService = ((MainActivity) getActivity()).getResourceService();

    // Prepare the loader.  Either re-connect with an existing one,
    // or start a new one.
    Bundle bundle = new Bundle();
    bundle.putParcelable("BibleText",
            Utils.loadBibleText(getActivity().getSharedPreferences("APP SETTINGS", Context.MODE_PRIVATE)));
    getLoaderManager().initLoader(0, bundle, this);

    // Create an empty adapter we will use to display the loaded data.
    mAdapter = new ResourceListAdapter(getActivity());
    setListAdapter(mAdapter);/*from  w  w  w  .  j  a v  a 2  s  .c o  m*/

    dm = ((DownloadManager) getActivity().getSystemService("download"));

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                Query query = new Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                        String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    }
                }

                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getActivity()
                        .getSystemService(ns);

                int icon = R.drawable.icon_book_rss;
                CharSequence tickerText = "Resource Download Complete";
                long when = System.currentTimeMillis();

                Notification notification = new Notification(icon, tickerText, when);

                CharSequence contentTitle = "Download Complete";
                CharSequence contentText = "Click to view downloaded resources";

                Intent notificationIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //uncomment when better
                //                    Intent notificationIntent = new Intent(getActivity(), MusicActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(getActivity(), 0, notificationIntent,
                        0);

                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notification.flags |= Notification.FLAG_AUTO_CANCEL;

                int HELLO_ID = 1;

                mNotificationManager.notify(HELLO_ID, notification);
            }
        }
    };

    getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}

From source file:com.android.providers.downloads.DownloadInfo.java

public void sendIntentIfRequested() {
    if (mPackage == null) {
        return;/*w  w w. j a  v  a 2 s  .  c o  m*/
    }

    Intent intent;
    if (mIsPublicApi) {
        intent = new Intent(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        intent.setPackage(mPackage);
        intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, mId);
    } else { // legacy behavior
        if (mClass == null) {
            return;
        }
        intent = new Intent(Downloads.Impl.ACTION_DOWNLOAD_COMPLETED);
        intent.setClassName(mPackage, mClass);
        if (mExtras != null) {
            intent.putExtra(Downloads.Impl.COLUMN_NOTIFICATION_EXTRAS, mExtras);
        }
        // We only send the content: URI, for security reasons. Otherwise, malicious
        //     applications would have an easier time spoofing download results by
        //     sending spoofed intents.
        intent.setData(getMyDownloadsUri());
    }
    mSystemFacade.sendBroadcast(intent);
}

From source file:org.chromium.chrome.browser.download.DownloadManagerService.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action))
        return;//from   ww  w.  ja v  a 2 s.  c o  m
    final DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    if (downloadId == -1)
        return;
    boolean isPendingOMADownload = mOMADownloadHandler.isPendingOMADownload(downloadId);
    boolean isInOMASharedPrefs = isDownloadIdInOMASharedPrefs(downloadId);
    if (isPendingOMADownload || isInOMASharedPrefs) {
        clearPendingOMADownload(downloadId, null);
        mPendingAutoOpenDownloads.remove(downloadId);
    } else if (mPendingAutoOpenDownloads.get(downloadId) != null) {
        Cursor c = manager.query(new DownloadManager.Query().setFilterById(downloadId));
        int statusIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
        while (c.moveToNext()) {
            int status = c.getInt(statusIndex);
            DownloadInfo info = mPendingAutoOpenDownloads.get(downloadId);
            switch (status) {
            case DownloadManager.STATUS_SUCCESSFUL:
                try {
                    mPendingAutoOpenDownloads.remove(downloadId);
                    if (OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME.equalsIgnoreCase(info.getMimeType())) {
                        mOMADownloadHandler.handleOMADownload(info, downloadId);
                        manager.remove(downloadId);
                        break;
                    }
                    Uri uri = manager.getUriForDownloadedFile(downloadId);
                    Intent launchIntent = new Intent(Intent.ACTION_VIEW);

                    launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
                    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    mContext.startActivity(launchIntent);
                } catch (ActivityNotFoundException e) {
                    Log.w(TAG, "Activity not found.");
                }
                break;
            case DownloadManager.STATUS_FAILED:
                mPendingAutoOpenDownloads.remove(downloadId);
                break;
            default:
                break;
            }
        }
    }

    if (mPendingAutoOpenDownloads.size() == 0) {
        mContext.unregisterReceiver(this);
    }
}

From source file:com.bitants.wally.activities.ImageDetailsActivity.java

@Override
protected void handleReceivedIntent(Context context, Intent intent) {
    long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
    if (WallyApplication.getDownloadIDs().containsKey(id)) {
        WallyApplication.getDownloadIDs().remove(id);
        updateSaveButton();/*from   w w w.j  a va 2  s .  co  m*/
        if (palette != null && palette.getVibrantSwatch() != null) {
            startHeartPopoutAnimation(buttonSave, palette.getVibrantSwatch().getBodyTextColor());
        } else {
            startHeartPopoutAnimation(buttonSave, Color.WHITE);
        }
        String filename = pageUri.getLastPathSegment();
        handleSavedImageData(WallyApplication.getDataProviderInstance().getFilePath(filename));
    }
}