Browse Source

Downloads Feature

Files are shown in the Downloads app.
pull/24/head
Unknown 7 years ago
parent
commit
9c11bc89c8
  1. 1
      app/src/main/AndroidManifest.xml
  2. 27
      app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java

1
app/src/main/AndroidManifest.xml

@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<application <application
android:name=".base.BaseApplication" android:name=".base.BaseApplication"

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

@ -1,11 +1,14 @@
package gr.thmmy.mthmmy.services; package gr.thmmy.mthmmy.services;
import android.app.DownloadManager;
import android.app.IntentService; import android.app.IntentService;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log;
import android.webkit.MimeTypeMap;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -107,7 +110,7 @@ public class DownloadService extends IntentService {
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
String contentDisposition = response.headers("Content-Disposition").toString(); //check if link provides an attachment String contentDisposition = response.headers("Content-Disposition").toString(); //check if link provides an attachment
if (contentDisposition.contains("attachment")){ if (contentDisposition.contains("attachment")) {
fileName = contentDisposition.split("\"")[1]; fileName = contentDisposition.split("\"")[1];
File dirPath = new File(SAVE_DIR); File dirPath = new File(SAVE_DIR);
@ -141,7 +144,7 @@ public class DownloadService extends IntentService {
fileName = file.getName(); fileName = file.getName();
Timber.v("Started saving file %s" , fileName); Timber.v("Started saving file %s", fileName);
sendNotification(downloadId, STARTED, fileName); sendNotification(downloadId, STARTED, fileName);
sink = Okio.buffer(Okio.sink(file)); sink = Okio.buffer(Okio.sink(file));
@ -149,6 +152,12 @@ public class DownloadService extends IntentService {
sink.flush(); sink.flush();
Timber.i("Download OK!"); Timber.i("Download OK!");
sendNotification(downloadId, COMPLETED, fileName); sendNotification(downloadId, COMPLETED, fileName);
// Register download
DownloadManager mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long length = file.length();
mManager.addCompletedDownload(fileName, "edo mporei na mpei ena description", false, getMimeType(file), SAVE_DIR +File.separator+ fileName, length, false);
} else } else
Timber.e("No attachment in response!"); Timber.e("No attachment in response!");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -203,4 +212,18 @@ public class DownloadService extends IntentService {
} }
@NonNull
static String getMimeType(@NonNull File file) {
String type = null;
final String url = file.toString();
final String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
}
if (type == null) {
type = ""; // fallback type. You might set it to */*
}
return type;
}
} }

Loading…
Cancel
Save