Browse Source

Notifications init

pull/24/head
Ezerous 8 years ago
parent
commit
fe167d73c1
  1. 10
      app/src/main/AndroidManifest.xml
  2. 24
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  3. 49
      app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java
  4. 77
      app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java

10
app/src/main/AndroidManifest.xml

@ -93,6 +93,16 @@
<service
android:name=".services.DownloadService"
android:exported="false" />
<receiver
android:name=".receiver.Receiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.media.action.ACTION_DOWNLOAD" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>

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

@ -39,9 +39,9 @@ import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity;
import gr.thmmy.mthmmy.activities.main.MainActivity;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.ThmmyFile;
import gr.thmmy.mthmmy.services.DownloadService;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.model.ThmmyFile;
import okhttp3.OkHttpClient;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
@ -50,8 +50,6 @@ import static gr.thmmy.mthmmy.activities.downloads.DownloadsActivity.BUNDLE_DOWN
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_THUMBNAIL_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_USERNAME;
import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_URL;
public abstract class BaseActivity extends AppCompatActivity {
// Client & Cookies
@ -544,16 +542,13 @@ public abstract class BaseActivity extends AppCompatActivity {
}
//----------------------------------DOWNLOAD------------------
//----------------------------------DOWNLOAD----------------------
private ThmmyFile tempThmmyFile;
public void launchDownloadService(ThmmyFile thmmyFile) {
if (checkPerms()) {
Intent i = new Intent(this, DownloadService.class);
i.setAction(ACTION_DOWNLOAD);
i.putExtra(EXTRA_DOWNLOAD_URL, thmmyFile.getFileUrl().toString());
startService(i);
} else {
if (checkPerms())
DownloadService.startActionDownload(this, thmmyFile.getFileUrl().toString());
else {
tempThmmyFile = thmmyFile;
requestPerms();
}
@ -561,12 +556,9 @@ public abstract class BaseActivity extends AppCompatActivity {
//Uses temp file - called after permission grant
public void launchDownloadService() {
if (checkPerms()) {
Intent i = new Intent(this, DownloadService.class);
i.setAction(ACTION_DOWNLOAD);
i.putExtra(EXTRA_DOWNLOAD_URL, tempThmmyFile.getFileUrl().toString());
startService(i);
}
if (checkPerms())
DownloadService.startActionDownload(this, tempThmmyFile.getFileUrl().toString());
}
}

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

@ -0,0 +1,49 @@
package gr.thmmy.mthmmy.receiver;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
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_NOTIFICATION_TEXT;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TICKER;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE;
public class Receiver extends BroadcastReceiver {
public Receiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(intent.getAction().equals(ACTION_DOWNLOAD))
{
Bundle extras = intent.getExtras();
int id = extras.getInt(EXTRA_DOWNLOAD_ID);
String title = extras.getString(EXTRA_NOTIFICATION_TITLE);
String text =extras.getString(EXTRA_NOTIFICATION_TEXT);
String ticker =extras.getString(EXTRA_NOTIFICATION_TICKER);
builder.setContentTitle(title)
.setContentText(text)
.setTicker(ticker)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
;
Notification notification = builder.build();
notificationManager.notify(id, notification);
}
}
}

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

@ -3,14 +3,16 @@ package gr.thmmy.mthmmy.services;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.receiver.Receiver;
import mthmmy.utils.Report;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -24,14 +26,38 @@ import okio.Okio;
*/
public class DownloadService extends IntentService {
private static final String TAG = "DownloadService";
private static int sDownloadId =0;
private Receiver receiver;
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_ID = "gr.thmmy.mthmmy.services.extra.DOWNLOAD_ID";
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 DownloadService() {
super("DownloadService");
}
@Override
public void onCreate() {
super.onCreate();
final IntentFilter filter = new IntentFilter(DownloadService.ACTION_DOWNLOAD);
receiver = new Receiver();
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(receiver);
}
/**
* Starts this service to perform action Download with the given parameters. If
* the service is already performing a task this action will be queued.
@ -63,6 +89,10 @@ public class DownloadService extends IntentService {
private void handleActionDownload(String downloadLink) {
OkHttpClient client = BaseApplication.getInstance().getClient();
BufferedSink sink = null;
String trueName = null;
int downloadId = sDownloadId;
sDownloadId++;
try {
Request request = new Request.Builder().url(downloadLink).build();
Response response = client.newCall(request).execute();
@ -103,13 +133,17 @@ public class DownloadService extends IntentService {
file = new File(dirPath, String.format(nameFormat, i));
}
Report.v(TAG, "Start saving file " + file.getName());
trueName = file.getName();
Report.v(TAG, "Started saving file " + trueName);
sendNotification("Started", trueName);
sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.flush();
Report.i(TAG, "Download OK!");
sendNotification("Completed", trueName);
}
else
Report.e(TAG, "Response not a binary!");
@ -117,10 +151,18 @@ public class DownloadService extends IntentService {
catch (FileNotFoundException e){
Report.e(TAG, "FileNotFound", e);
Report.i(TAG, "Download failed...");
if(trueName!=null)
sendNotification("Failed", trueName);
else
sendNotification("Failed", "file");
}
catch (IOException e){
Report.e(TAG, "IOException", e);
Report.i(TAG, "Download failed...");
if(trueName!=null)
sendNotification("Failed", trueName);
else
sendNotification("Failed", "file");
} finally {
if (sink!= null) {
try {
@ -132,4 +174,35 @@ public class DownloadService extends IntentService {
}
}
private void sendNotification(String type, @NonNull String fileName)
{
switch (type) {
case "Started": {
Intent intent = new Intent(ACTION_DOWNLOAD);
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": {
Intent intent = new Intent(ACTION_DOWNLOAD);
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": {
Intent intent = new Intent(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_NOTIFICATION_TITLE, "Download Failed");
intent.putExtra(EXTRA_NOTIFICATION_TEXT, "\"" + fileName + "\" failed.");
intent.putExtra(EXTRA_NOTIFICATION_TICKER, "Download Failed");
sendBroadcast(intent);
break;
}
}
}
}

Loading…
Cancel
Save