DownloadService no longer shows its notifications
This commit is contained in:
parent
a6df2fa2e4
commit
95a7a11fa3
|
@ -9,6 +9,7 @@
|
|||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-sdk android:minSdkVersion="10" />
|
||||
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
|
||||
|
||||
<application
|
||||
android:icon="@drawable/ic_launcher"
|
||||
|
|
|
@ -26,6 +26,7 @@ import android.media.MediaPlayer;
|
|||
import android.os.IBinder;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
@ -47,6 +48,9 @@ public class DownloadService extends Service {
|
|||
private int NOTIFICATION_ID = 2;
|
||||
/** Needed to determine the duration of a media file */
|
||||
private MediaPlayer mediaplayer;
|
||||
private DownloadManager downloadManager;
|
||||
|
||||
private volatile boolean shutdownInitiated = false;
|
||||
|
||||
// Objects for communication
|
||||
private final Messenger mMessenger = new Messenger(new IncomingHandler());
|
||||
|
@ -62,6 +66,7 @@ public class DownloadService extends Service {
|
|||
manager = FeedManager.getInstance();
|
||||
requester = DownloadRequester.getInstance();
|
||||
mediaplayer = new MediaPlayer();
|
||||
downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
setupNotification();
|
||||
}
|
||||
|
||||
|
@ -75,6 +80,7 @@ public class DownloadService extends Service {
|
|||
Log.d(TAG, "Service shutting down");
|
||||
sendBroadcast(new Intent(ACTION_FEED_SYNC_COMPLETED));
|
||||
mediaplayer.release();
|
||||
unregisterReceiver(downloadReceiver);
|
||||
}
|
||||
|
||||
private IntentFilter createIntentFilter() {
|
||||
|
@ -116,8 +122,9 @@ public class DownloadService extends Service {
|
|||
R.drawable.stat_notify_sync_noanim);
|
||||
notificationBuilder = new NotificationCompat.Builder(this)
|
||||
.setContentTitle("Downloading Podcast data")
|
||||
.setContentText(requester.getNumberOfDownloads() + " Downloads left").setOngoing(true)
|
||||
.setContentIntent(pIntent).setLargeIcon(icon)
|
||||
.setContentText(
|
||||
requester.getNumberOfDownloads() + " Downloads left")
|
||||
.setOngoing(true).setContentIntent(pIntent).setLargeIcon(icon)
|
||||
.setSmallIcon(R.drawable.stat_notify_sync_noanim);
|
||||
|
||||
startForeground(NOTIFICATION_ID, notificationBuilder.getNotification());
|
||||
|
@ -127,9 +134,21 @@ public class DownloadService extends Service {
|
|||
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
int status = -1;
|
||||
|
||||
Log.d(TAG, "Received 'Download Complete' - message.");
|
||||
long downloadId = intent.getLongExtra(
|
||||
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
|
||||
// get status
|
||||
DownloadManager.Query q = new DownloadManager.Query();
|
||||
q.setFilterById(downloadId);
|
||||
Cursor c = downloadManager.query(q);
|
||||
if (c.moveToFirst()) {
|
||||
status = c.getInt(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||
}
|
||||
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
Feed feed = requester.getFeed(downloadId);
|
||||
if (feed != null) {
|
||||
handleCompletedFeedDownload(context, feed);
|
||||
|
@ -145,13 +164,22 @@ public class DownloadService extends Service {
|
|||
}
|
||||
}
|
||||
queryDownloads();
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
int reason = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
Log.d(TAG, "reason code is " + reason);
|
||||
|
||||
}
|
||||
|
||||
c.close();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
/** Check if there's something else to download, otherwise stop */
|
||||
private void queryDownloads() {
|
||||
if (requester.getNumberOfDownloads() == 0) {
|
||||
unregisterReceiver(downloadReceiver);
|
||||
private synchronized void queryDownloads() {
|
||||
if (!shutdownInitiated && requester.getNumberOfDownloads() == 0) {
|
||||
shutdownInitiated = true;
|
||||
initiateShutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import android.webkit.URLUtil;
|
|||
|
||||
public class DownloadRequester {
|
||||
private static final String TAG = "DownloadRequester";
|
||||
private static final int currentApi = android.os.Build.VERSION.SDK_INT;
|
||||
|
||||
public static String EXTRA_DOWNLOAD_ID = "extra.de.podfetcher.storage.download_id";
|
||||
public static String EXTRA_ITEM_ID = "extra.de.podfetcher.storage.item_id";
|
||||
|
@ -38,7 +39,6 @@ public class DownloadRequester {
|
|||
public static String FEED_DOWNLOADPATH = "cache/";
|
||||
public static String MEDIA_DOWNLOADPATH = "media/";
|
||||
|
||||
|
||||
private static DownloadRequester downloader;
|
||||
private DownloadManager manager;
|
||||
|
||||
|
@ -60,47 +60,59 @@ public class DownloadRequester {
|
|||
return downloader;
|
||||
}
|
||||
|
||||
private long download(Context context, ArrayList<FeedFile> type, FeedFile item, File dest, boolean visibleInUI) {
|
||||
private long download(Context context, ArrayList<FeedFile> type,
|
||||
FeedFile item, File dest) {
|
||||
Log.d(TAG, "Requesting download of url " + item.getDownload_url());
|
||||
type.add(item);
|
||||
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(item.getDownload_url()));
|
||||
//request.allowScanningByMediaScanner();
|
||||
DownloadManager.Request request = new DownloadManager.Request(
|
||||
Uri.parse(item.getDownload_url()))
|
||||
.setDestinationUri(Uri.fromFile(dest));
|
||||
Log.d(TAG, "Version is " + currentApi);
|
||||
if (currentApi >= 11) {
|
||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
|
||||
} else {
|
||||
request.setVisibleInDownloadsUi(false);
|
||||
request.setShowRunningNotification(false);
|
||||
}
|
||||
|
||||
request.setDestinationUri(Uri.fromFile(dest));
|
||||
request.setVisibleInDownloadsUi(visibleInUI);
|
||||
// TODO Set Allowed Network Types
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager manager = (DownloadManager) context
|
||||
.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
context.startService(new Intent(context, DownloadService.class));
|
||||
long downloadId = manager.enqueue(request);
|
||||
item.setDownloadId(downloadId);
|
||||
item.setFile_url(dest.toString());
|
||||
return downloadId;
|
||||
}
|
||||
|
||||
public long downloadFeed(Context context, Feed feed) {
|
||||
return download(context, feeds, feed,
|
||||
new File(getFeedfilePath(context), getFeedfileName(feed)),
|
||||
true);
|
||||
return download(context, feeds, feed, new File(
|
||||
getFeedfilePath(context), getFeedfileName(feed)));
|
||||
}
|
||||
|
||||
public long downloadImage(Context context, FeedImage image) {
|
||||
return download(context, images, image,
|
||||
new File(getImagefilePath(context), getImagefileName(image)),
|
||||
true);
|
||||
return download(context, images, image, new File(
|
||||
getImagefilePath(context), getImagefileName(image)));
|
||||
}
|
||||
|
||||
public long downloadMedia(Context context, FeedMedia feedmedia) {
|
||||
return download(context, media, feedmedia,
|
||||
new File(getMediafilePath(context, feedmedia), getMediafilename(feedmedia)),
|
||||
true);
|
||||
new File(getMediafilePath(context, feedmedia),
|
||||
getMediafilename(feedmedia)));
|
||||
}
|
||||
|
||||
/** Cancels a running download.
|
||||
* @param context A context needed to get the DownloadManager service
|
||||
* @param id ID of the download to cancel
|
||||
/**
|
||||
* Cancels a running download.
|
||||
*
|
||||
* @param context
|
||||
* A context needed to get the DownloadManager service
|
||||
* @param id
|
||||
* ID of the download to cancel
|
||||
* */
|
||||
public void cancelDownload(final Context context, final long id) {
|
||||
Log.d(TAG, "Cancelling download with id " + id);
|
||||
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
DownloadManager manager = (DownloadManager) context
|
||||
.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
int removed = manager.remove(id);
|
||||
if (removed > 0) {
|
||||
// Delete downloads in lists
|
||||
|
@ -141,7 +153,6 @@ public class DownloadRequester {
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
/** Get media by its download id */
|
||||
public FeedMedia getFeedMedia(long id) {
|
||||
for (FeedFile f : media) {
|
||||
|
@ -194,12 +205,16 @@ public class DownloadRequester {
|
|||
}
|
||||
|
||||
public String getMediafilePath(Context context, FeedMedia media) {
|
||||
return context.getExternalFilesDir(MEDIA_DOWNLOADPATH
|
||||
+ media.getItem().getFeed().getTitle() + "/").toString();
|
||||
return context
|
||||
.getExternalFilesDir(
|
||||
MEDIA_DOWNLOADPATH
|
||||
+ media.getItem().getFeed().getTitle() + "/")
|
||||
.toString();
|
||||
}
|
||||
|
||||
public String getMediafilename(FeedMedia media) {
|
||||
return URLUtil.guessFileName(media.getDownload_url(), null, media.getMime_type());
|
||||
return URLUtil.guessFileName(media.getDownload_url(), null,
|
||||
media.getMime_type());
|
||||
}
|
||||
|
||||
public boolean isDownloaded(Feed feed) {
|
||||
|
@ -226,7 +241,10 @@ public class DownloadRequester {
|
|||
return m.getFile_url() != null && media.contains(m);
|
||||
}
|
||||
|
||||
/* ------------ Methods for communicating with the DownloadService ------------- */
|
||||
/*
|
||||
* ------------ Methods for communicating with the DownloadService
|
||||
* -------------
|
||||
*/
|
||||
private Messenger mService = null;
|
||||
boolean mIsBound;
|
||||
|
||||
|
@ -235,11 +253,13 @@ public class DownloadRequester {
|
|||
mService = new Messenger(service);
|
||||
|
||||
try {
|
||||
Message msg = Message.obtain(null, DownloadService.MSG_QUERY_DOWNLOADS_LEFT);
|
||||
Message msg = Message.obtain(null,
|
||||
DownloadService.MSG_QUERY_DOWNLOADS_LEFT);
|
||||
Log.d(TAG, "Sending message to DownloadService.");
|
||||
mService.send(msg);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "An Exception happened while communication with the DownloadService");
|
||||
Log.e(TAG,
|
||||
"An Exception happened while communication with the DownloadService");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -249,10 +269,10 @@ public class DownloadRequester {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
/** Notifies the DownloadService to check if there are any Downloads left */
|
||||
public void notifyDownloadService(Context context) {
|
||||
context.bindService(new Intent(context, DownloadService.class), mConnection, Context.BIND_AUTO_CREATE);
|
||||
context.bindService(new Intent(context, DownloadService.class),
|
||||
mConnection, Context.BIND_AUTO_CREATE);
|
||||
mIsBound = true;
|
||||
context.unbindService(mConnection);
|
||||
mIsBound = false;
|
||||
|
|
Loading…
Reference in New Issue