Android Open Source - Simple-Dilbert Download Manager Broadcast Receiver






From Project

Back to project page Simple-Dilbert.

License

The source code is released under:

Apache License

If you think the Android project Simple-Dilbert listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mareksebera.simpledilbert.utilities;
// ww w  . j  a  v a2 s. co m
import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import com.mareksebera.simpledilbert.preferences.DilbertPreferences;

import java.io.File;

public class DownloadManagerBroadcastReceiver extends BroadcastReceiver {

    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onReceive(Context context, Intent intent) {
        try {
            DilbertPreferences preferences = new DilbertPreferences(context);
            DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadedFilePath = dm.getUriForDownloadedFile(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0));
            if (downloadedFilePath == null) {
                Log.d("DownloadManagerBroadcastReceiver", "No Uri for downloaded file from DownloadManager");
                return;
            }
            File downloadedFile = new File(downloadedFilePath.getPath());
            if (downloadedFilePath.getLastPathSegment() == null) {
                return;
            }
            String scheduledTargetPath = preferences.getScheduledTargetPath(downloadedFilePath.getLastPathSegment().substring(0, 10));
            if (scheduledTargetPath != null) {
                if (!downloadedFile.renameTo(new File(scheduledTargetPath))) {
                    Toast.makeText(context, "Couldn't move picture to selected Folder", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Throwable t) {
            Log.e("DownloadManagerBroadcastReceiver", "Error while moving downloaded file to desired target folder", t);
        }
    }
}




Java Source Code List

com.mareksebera.simpledilbert.AppController.java
com.mareksebera.simpledilbert.core.DilbertFragmentActivity.java
com.mareksebera.simpledilbert.core.DilbertFragmentAdapter.java
com.mareksebera.simpledilbert.core.DilbertFragmentInterface.java
com.mareksebera.simpledilbert.core.DilbertFragment.java
com.mareksebera.simpledilbert.favorites.DilbertFavoritedActivity.java
com.mareksebera.simpledilbert.favorites.DilbertFavoritedFragmentAdapter.java
com.mareksebera.simpledilbert.favorites.FavoritedItem.java
com.mareksebera.simpledilbert.picker.FolderPickerActivity.java
com.mareksebera.simpledilbert.picker.FolderPickerAdapter.java
com.mareksebera.simpledilbert.picker.FolderPickerFragment.java
com.mareksebera.simpledilbert.preferences.DilbertPreferencesActivity.java
com.mareksebera.simpledilbert.preferences.DilbertPreferences.java
com.mareksebera.simpledilbert.utilities.ActionBarUtility.java
com.mareksebera.simpledilbert.utilities.DownloadManagerBroadcastReceiver.java
com.mareksebera.simpledilbert.utilities.FailSafeBitmapDisplayer.java
com.mareksebera.simpledilbert.utilities.FindUrls.java
com.mareksebera.simpledilbert.utilities.FixedViewPager.java
com.mareksebera.simpledilbert.utilities.GetStripUrlInterface.java
com.mareksebera.simpledilbert.utilities.GetStripUrl.java
com.mareksebera.simpledilbert.widget.WidgetProvider.java