Skip to content

Commit

Permalink
Fix downloading on Marshmallow
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Nov 21, 2015
1 parent 8bcb3e6 commit 6b885ef
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 54 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 93
versionName '3.1.5-beta1'
versionCode 94
versionName '3.1.5-beta2'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
Expand Down Expand Up @@ -42,6 +42,7 @@ dependencies {
compile 'com.trello:rxlifecycle-components:0.3.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.path:android-priority-jobqueue:1.1.2'
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.4.2@aar'
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
Expand Down
122 changes: 71 additions & 51 deletions app/src/main/java/com/stevenschoen/putionew/PutioUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.stevenschoen.putionew;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
Expand Down Expand Up @@ -51,6 +53,7 @@
import com.stevenschoen.putionew.model.PutioRestInterface;
import com.stevenschoen.putionew.model.files.PutioFile;
import com.stevenschoen.putionew.model.responses.BasePutioResponse;
import com.tbruyelle.rxpermissions.RxPermissions;

import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
Expand All @@ -62,6 +65,8 @@
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.net.ssl.HttpsURLConnection;
Expand All @@ -76,6 +81,7 @@
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.FuncN;

public class PutioUtils {
public static final int TYPE_AUDIO = 1;
Expand Down Expand Up @@ -293,72 +299,68 @@ public InputStream getNotificationsJsonData() throws SocketTimeoutException {
return null;
}

public void downloadFiles(final Context context, final int actionWhenDone, final PutioFile... files) {
class DownloadFileTask extends AsyncTask<Void, Integer, long[]> {
private boolean resolveRedirect = false;
private Dialog dialog;
public void downloadFile(Activity activity, int actionWhenDone, Uri downloadUri) {

@Override
protected long[] doInBackground(Void... params) {
long[] downloadIds = new long[files.length];
for (int i = 0; i < files.length; i++) {
PutioFile file = files[i];
if (file.isFolder()) {
long[] folder = new long[]{file.id};
downloadIds[i] = downloadZipWithoutUrl(context, folder, file.name);
} else {
downloadIds[i] = downloadFileWithoutUrl(context, file.id, file.name);
}
}
}

return downloadIds;
public void downloadFiles(final Activity activity, final int actionWhenDone, final PutioFile... files) {
List<Observable<Long>> downloadIds = new ArrayList<>(files.length);
for (PutioFile file : files) {
if (file.isFolder()) {
long[] folder = new long[]{file.id};
downloadIds.add(downloadZipWithoutUrl(activity, folder, file.name));
} else {
downloadIds.add(downloadFileWithoutUrl(activity, file.id, file.name));
}
}

Observable.zip(downloadIds, new FuncN<long[]>() {
@Override
protected void onProgressUpdate(Integer... nothing) {
resolveRedirect = true;

dialog = showPutioDialog(context, context.getString(R.string.downloadpreparing), R.layout.dialog_loading);
dialog.setCanceledOnTouchOutside(false);
public long[] call(Object... args) {
long[] downloadIds = new long[args.length];
for (int i = 0; i < args.length; i++) {
downloadIds[i] = (long) args[i];
}
return downloadIds;
}

}).subscribe(new Action1<long[]>() {
@Override
protected void onPostExecute(long[] downloadIds) {
if (resolveRedirect) {
dialog.dismiss();
}

public void call(long[] downloadIds) {
switch (actionWhenDone) {
case ACTION_OPEN:
Intent serviceOpenIntent = new Intent(context, PutioOpenFileService.class);
Intent serviceOpenIntent = new Intent(activity, PutioOpenFileService.class);
serviceOpenIntent.putExtra("downloadIds", downloadIds);
serviceOpenIntent.putExtra("id", files[0].id);
serviceOpenIntent.putExtra("filename", files[0].name);
serviceOpenIntent.putExtra("mode", actionWhenDone);
context.startService(serviceOpenIntent);
Toast.makeText(context, context.getString(R.string.downloadwillopen),
activity.startService(serviceOpenIntent);
Toast.makeText(activity, activity.getString(R.string.downloadwillopen),
Toast.LENGTH_LONG).show();
break;
case ACTION_NOTHING:
Toast.makeText(context, context.getString(R.string.downloadstarted), Toast.LENGTH_SHORT).show();
Toast.makeText(activity, activity.getString(R.string.downloadstarted), Toast.LENGTH_SHORT).show();
break;
}
}
}
new DownloadFileTask().execute();
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}

private long downloadFileWithoutUrl(final Context context, final long fileId, String filename) {
private Observable<Long> downloadFileWithoutUrl(final Activity activity, final long fileId, String filename) {
Uri uri = Uri.parse(getFileDownloadUrl(fileId));
return download(context, fileId, false, filename, uri);
return download(activity, fileId, false, filename, uri);
}

private long downloadZipWithoutUrl(final Context context, final long[] fileId, String filename) {
private Observable<Long> downloadZipWithoutUrl(final Activity activity, final long[] fileId, String filename) {
Uri uri = Uri.parse(getZipDownloadUrl(fileId));
return download(context, 0, true, filename + ".zip", uri);
return download(activity, 0, true, filename + ".zip", uri);
}

private long download(Context context, long fileId, boolean isZip, String filename, Uri uri) {
private Observable<Long> download(Activity activity, long fileId, boolean isZip, String filename, Uri uri) {
if (idIsDownloaded(fileId) && !isZip) {
deleteId(fileId);
}
Expand All @@ -370,23 +372,41 @@ private long download(Context context, long fileId, boolean isZip, String filena
name = fileId + File.separator + filename;
}

return download(context, uri, name);
return download(activity, uri, name);
}

public static long download(Context context, Uri uri, String path) {
String subPath = "put.io" + File.separator + path;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + subPath);
file.getParentFile().mkdirs();
public static Observable<Long> download(final Activity activity, final Uri uri, final String path) {
return Observable.create(new Observable.OnSubscribe<Long>() {
@Override
public void call(final Subscriber<? super Long> subscriber) {
RxPermissions.getInstance(activity)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean granted) {
String subPath = "put.io" + File.separator + path;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + subPath);
file.getParentFile().mkdirs();

final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);

request.setDescription("put.io");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath);
request.setDescription("put.io");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, subPath);

return manager.enqueue(request);
subscriber.onNext(manager.enqueue(request));
subscriber.onCompleted();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
subscriber.onError(throwable);
}
});
}
});
}

public static void stream(Context context, String url, Uri[] subtitles, int type) {
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/stevenschoen/putionew/fragments/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,19 @@ public void onClick(View v) {
name += (", " + files[i].name);
}
}
PutioUtils.download(getActivity(), Uri.parse(utils.getZipDownloadUrl(fileIds)), name);
name += ".zip";
PutioUtils.download(getActivity(), Uri.parse(utils.getZipDownloadUrl(fileIds)), name)
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
Toast.makeText(getContext(), getString(R.string.downloadstarted), Toast.LENGTH_SHORT).show();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
dialog.dismiss();
}
});
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ buildscript {
allprojects {
repositories {
jcenter()
maven {
url "http://dl.bintray.com/tbruyelle/tbruyelle"
}
}
}

0 comments on commit 6b885ef

Please sign in to comment.