Browse Source

Downloads/ Notification changes

pull/24/head
Ezerous 8 years ago
parent
commit
c44f9d161a
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  3. 44
      app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java
  4. 61
      app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -47,8 +47,8 @@ import mthmmy.utils.Report;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE; import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL; import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.toQuoteList;

2
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -47,8 +47,8 @@ import okhttp3.OkHttpClient;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWNLOADS_TITLE; import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWNLOADS_TITLE;
import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWNLOADS_URL; import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWNLOADS_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME; import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME;
public abstract class BaseActivity extends AppCompatActivity { public abstract class BaseActivity extends AppCompatActivity {

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

@ -2,23 +2,35 @@ package gr.thmmy.mthmmy.receiver;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.NotificationCompat; import android.support.v7.app.NotificationCompat;
import android.webkit.MimeTypeMap;
import java.io.File;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import mthmmy.utils.Report;
import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD; import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD;
import static gr.thmmy.mthmmy.services.DownloadService.COMPLETED;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_ID; 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_DOWNLOAD_STATE;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_EXTENSION;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_NAME;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TEXT; 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_TICKER;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE; import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE;
import static gr.thmmy.mthmmy.services.DownloadService.SAVE_DIR;
import static gr.thmmy.mthmmy.services.DownloadService.STARTED; import static gr.thmmy.mthmmy.services.DownloadService.STARTED;
public class Receiver extends BroadcastReceiver { public class Receiver extends BroadcastReceiver {
private static final String TAG = "BroadcastReceiver";
public Receiver() { public Receiver() {
} }
@ -27,14 +39,13 @@ public class Receiver extends BroadcastReceiver {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(intent.getAction().equals(ACTION_DOWNLOAD)) if (intent.getAction().equals(ACTION_DOWNLOAD)) {
{
Bundle extras = intent.getExtras(); Bundle extras = intent.getExtras();
int id = extras.getInt(EXTRA_DOWNLOAD_ID); int id = extras.getInt(EXTRA_DOWNLOAD_ID);
String state = extras.getString(EXTRA_DOWNLOAD_STATE, "NONE"); String state = extras.getString(EXTRA_DOWNLOAD_STATE, "NONE");
String title = extras.getString(EXTRA_NOTIFICATION_TITLE); String title = extras.getString(EXTRA_NOTIFICATION_TITLE);
String text =extras.getString(EXTRA_NOTIFICATION_TEXT); String text = extras.getString(EXTRA_NOTIFICATION_TEXT);
String ticker =extras.getString(EXTRA_NOTIFICATION_TICKER); String ticker = extras.getString(EXTRA_NOTIFICATION_TICKER);
builder.setContentTitle(title) builder.setContentTitle(title)
.setContentText(text) .setContentText(text)
@ -42,13 +53,34 @@ public class Receiver extends BroadcastReceiver {
.setAutoCancel(true) //??? .setAutoCancel(true) //???
.setSmallIcon(R.mipmap.ic_launcher); .setSmallIcon(R.mipmap.ic_launcher);
if(state.equals(STARTED)) if (state.equals(STARTED))
builder.setOngoing(true); builder.setOngoing(true);
else if (state.equals(COMPLETED)) {
String fileName = extras.getString(EXTRA_FILE_NAME, "NONE");
String extension = extras.getString(EXTRA_FILE_EXTENSION, "extension");
File file = new File(SAVE_DIR, fileName);
if (file.exists()) {
// String type = "application/" + extension;
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Uri pathUri = Uri.fromFile(file);
Intent chooserIntent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooser = Intent.createChooser(chooserIntent, "Open With...");
intent.setDataAndType(pathUri, type);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, chooser, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
}
else
Report.w(TAG,"File doesn't exist.");
}
Notification notification = builder.build(); Notification notification = builder.build();
notificationManager.notify(id, notification); notificationManager.notify(id, notification);
} }
} }
} }

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

@ -30,11 +30,15 @@ public class DownloadService extends IntentService {
private Receiver receiver; private Receiver receiver;
public static final String SAVE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "mthmmy";
public static final String ACTION_DOWNLOAD = "gr.thmmy.mthmmy.services.action.DOWNLOAD"; public static final String ACTION_DOWNLOAD = "gr.thmmy.mthmmy.services.action.DOWNLOAD";
public static final String EXTRA_DOWNLOAD_URL = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_URL"; 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_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_DOWNLOAD_STATE = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_STATE";
public static final String EXTRA_FILE_NAME = "gr.thmmy.mthmmy.services.extra.FILE_NAME";
public static final String EXTRA_FILE_EXTENSION = "gr.thmmy.mthmmy.services.extra.FILE_EXTENSION";
public static final String EXTRA_NOTIFICATION_TITLE = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TITLE"; 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_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 EXTRA_NOTIFICATION_TICKER = "gr.thmmy.mthmmy.services.extra.NOTIFICATION_TICKER";
@ -95,7 +99,8 @@ public class DownloadService extends IntentService {
private void handleActionDownload(String downloadLink) { private void handleActionDownload(String downloadLink) {
OkHttpClient client = BaseApplication.getInstance().getClient(); OkHttpClient client = BaseApplication.getInstance().getClient();
BufferedSink sink = null; BufferedSink sink = null;
String trueName = null; String fileName = "file";
String extension = "extension";
int downloadId = sDownloadId; int downloadId = sDownloadId;
sDownloadId++; sDownloadId++;
@ -106,9 +111,9 @@ public class DownloadService extends IntentService {
String contentType = response.headers("Content-Type").toString(); //check if link provides a binary file String contentType = response.headers("Content-Type").toString(); //check if link provides a binary file
if(contentType.equals("[application/octet-stream]")) if(contentType.equals("[application/octet-stream]"))
{ {
String fileName = response.headers("Content-Disposition").toString().split("\"")[1]; fileName = response.headers("Content-Disposition").toString().split("\"")[1];
File dirPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "mthmmy"); File dirPath = new File(SAVE_DIR);
if(!dirPath.isDirectory()) if(!dirPath.isDirectory())
{ {
if(dirPath.mkdirs()) if(dirPath.mkdirs())
@ -123,11 +128,15 @@ public class DownloadService extends IntentService {
if(tokens.length!=2) if(tokens.length!=2)
{ {
Report.w(TAG, "Error getting file extension"); Report.w(TAG, "Couldn't get file extension...");
nameFormat = fileName + "(%d)"; nameFormat = fileName + "(%d)";
} }
else else
{
nameFormat = tokens[0] + "(%d)." + tokens[1]; nameFormat = tokens[0] + "(%d)." + tokens[1];
extension = tokens[1];
}
File file = new File(dirPath, fileName); File file = new File(dirPath, fileName);
@ -140,35 +149,29 @@ public class DownloadService extends IntentService {
file = new File(dirPath, String.format(nameFormat, i)); file = new File(dirPath, String.format(nameFormat, i));
} }
trueName = file.getName(); fileName = file.getName();
Report.v(TAG, "Started saving file " + trueName); Report.v(TAG, "Started saving file " + fileName);
sendNotification(downloadId, STARTED, trueName); sendNotification(downloadId, STARTED, fileName, extension);
sink = Okio.buffer(Okio.sink(file)); sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source()); sink.writeAll(response.body().source());
sink.flush(); sink.flush();
Report.i(TAG, "Download OK!"); Report.i(TAG, "Download OK!");
sendNotification(downloadId, COMPLETED, trueName); sendNotification(downloadId, COMPLETED, fileName, extension);
} }
else else
Report.e(TAG, "Response not a binary!"); Report.e(TAG, "Response not a binary file!");
} }
catch (FileNotFoundException e){ catch (FileNotFoundException e){
Report.e(TAG, "FileNotFound", e); Report.e(TAG, "FileNotFound", e);
Report.i(TAG, "Download failed..."); Report.i(TAG, "Download failed...");
if(trueName!=null) sendNotification(downloadId, FAILED, fileName, extension);
sendNotification(downloadId, FAILED, trueName);
else
sendNotification(downloadId, FAILED, "file");
} }
catch (IOException e){ catch (IOException e){
Report.e(TAG, "IOException", e); Report.e(TAG, "IOException", e);
Report.i(TAG, "Download failed..."); Report.i(TAG, "Download failed...");
if(trueName!=null) sendNotification(downloadId, FAILED, fileName, extension);
sendNotification(downloadId, FAILED, trueName);
else
sendNotification(downloadId, FAILED, "file");
} finally { } finally {
if (sink!= null) { if (sink!= null) {
try { try {
@ -180,40 +183,38 @@ public class DownloadService extends IntentService {
} }
} }
private void sendNotification(int downloadId, String type, @NonNull String fileName) private void sendNotification(int downloadId, String type, @NonNull String fileName, String fileExtension)
{ {
Intent intent = new Intent(ACTION_DOWNLOAD);
switch (type) { 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_TITLE, "Download Started");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" downloading..."); intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" downloading...");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Downloading..."); intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Downloading...");
sendBroadcast(intent);
break; 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_TITLE, "Download Completed");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" finished downloading."); intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" finished downloading.");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Completed"); intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Completed");
sendBroadcast(intent);
break; 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_TITLE, "Download Failed");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" failed."); intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" failed.");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Failed"); intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Failed");
sendBroadcast(intent);
break; break;
} }
default:{
Report.wtf(TAG, "Invalid notification case!");
return;
}
} }
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId);
intent.putExtra(EXTRA_DOWNLOAD_STATE, type);
intent.putExtra(EXTRA_FILE_NAME, fileName);
intent.putExtra(EXTRA_FILE_EXTENSION, fileExtension);
sendBroadcast(intent);
} }

Loading…
Cancel
Save