Browse Source

Downloads/notifications improvements

pull/24/head
Ezerous 8 years ago
parent
commit
944fb432fb
  1. 11
      app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java
  2. 32
      app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java

11
app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java

@ -12,9 +12,11 @@ import gr.thmmy.mthmmy.R;
import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_ID;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_STATE;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TEXT;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TICKER;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE;
import static gr.thmmy.mthmmy.services.DownloadService.STARTED;
public class Receiver extends BroadcastReceiver {
public Receiver() {
@ -29,6 +31,7 @@ public class Receiver extends BroadcastReceiver {
{
Bundle extras = intent.getExtras();
int id = extras.getInt(EXTRA_DOWNLOAD_ID);
String state = extras.getString(EXTRA_DOWNLOAD_STATE, "NONE");
String title = extras.getString(EXTRA_NOTIFICATION_TITLE);
String text =extras.getString(EXTRA_NOTIFICATION_TEXT);
String ticker =extras.getString(EXTRA_NOTIFICATION_TICKER);
@ -36,9 +39,11 @@ public class Receiver extends BroadcastReceiver {
builder.setContentTitle(title)
.setContentText(text)
.setTicker(ticker)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
;
.setAutoCancel(true) //???
.setSmallIcon(R.mipmap.ic_launcher);
if(state.equals(STARTED))
builder.setOngoing(true);
Notification notification = builder.build();
notificationManager.notify(id, notification);

32
app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java

@ -34,10 +34,16 @@ public class DownloadService extends IntentService {
public static final String EXTRA_DOWNLOAD_URL = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_URL";
public static final String EXTRA_DOWNLOAD_ID = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_ID";
public static final String EXTRA_DOWNLOAD_STATE = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_STATE";
public static final String EXTRA_NOTIFICATION_TITLE = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TITLE";
public static final String EXTRA_NOTIFICATION_TEXT = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TEXT";
public static final String EXTRA_NOTIFICATION_TICKER = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TICKER";
public static final String STARTED = "Started";
public static final String COMPLETED = "Completed";
public static final String FAILED = "Failed";
public DownloadService() {
super("DownloadService");
@ -137,13 +143,13 @@ public class DownloadService extends IntentService {
trueName = file.getName();
Report.v(TAG, "Started saving file " + trueName);
sendNotification("Started", trueName);
sendNotification(downloadId, STARTED, trueName);
sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.flush();
Report.i(TAG, "Download OK!");
sendNotification("Completed", trueName);
sendNotification(downloadId, COMPLETED, trueName);
}
else
Report.e(TAG, "Response not a binary!");
@ -152,17 +158,17 @@ public class DownloadService extends IntentService {
Report.e(TAG, "FileNotFound", e);
Report.i(TAG, "Download failed...");
if(trueName!=null)
sendNotification("Failed", trueName);
sendNotification(downloadId, FAILED, trueName);
else
sendNotification("Failed", "file");
sendNotification(downloadId, FAILED, "file");
}
catch (IOException e){
Report.e(TAG, "IOException", e);
Report.i(TAG, "Download failed...");
if(trueName!=null)
sendNotification("Failed", trueName);
sendNotification(downloadId, FAILED, trueName);
else
sendNotification("Failed", "file");
sendNotification(downloadId, FAILED, "file");
} finally {
if (sink!= null) {
try {
@ -174,27 +180,33 @@ public class DownloadService extends IntentService {
}
}
private void sendNotification(String type, @NonNull String fileName)
private void sendNotification(int downloadId, String type, @NonNull String fileName)
{
switch (type) {
case "Started": {
case STARTED: {
Intent intent = new Intent(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_DOWNLOAD_STATE, STARTED);
intent.putExtra(EXTRA_NOTIFICATION_TITLE, "Download Started");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" downloading...");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Downloading...");
sendBroadcast(intent);
break;
}
case "Completed": {
case COMPLETED: {
Intent intent = new Intent(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_DOWNLOAD_STATE, COMPLETED);
intent.putExtra(EXTRA_NOTIFICATION_TITLE, "Download Completed");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" finished downloading.");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Completed");
sendBroadcast(intent);
break;
}
case "Failed": {
case FAILED: {
Intent intent = new Intent(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_DOWNLOAD_STATE, FAILED);
intent.putExtra(EXTRA_NOTIFICATION_TITLE, "Download Failed");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" failed.");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Failed");

Loading…
Cancel
Save