diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index dee50313..2e27edaa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -203,7 +203,7 @@ android:name=".activities.TransfersActivity" android:excludeFromRecents="true" android:taskAffinity="com.stevenschoen.putio.transfersdialog" - android:theme="@style/Putio.Dialog" /> + android:theme="@style/Putio.Dialog.NoTitle" /> () { + @Override + public void call(PutioTransfer transfer) { + notif.succeeded(); + } + }, new Action1() { + @Override + public void call(Throwable throwable) { + notif.failed(); + throwable.printStackTrace(); + Toast.makeText(TransfersActivity.this, "Error: " + throwable.getMessage(), Toast.LENGTH_LONG).show(); + } + }); + } + + private void addTransferFile(Uri torrentUri, long parentId) { + Log.d("asdf", "add file: " + torrentUri); + final UploadNotif notif = new UploadNotif(); + notif.start(); + + RestAdapter uploadAdapter = utils.makeRestAdapterBuilder(PutioUtils.uploadBaseUrl).build(); + PutioUploadInterface uploadInterface = uploadAdapter.create(PutioUploadInterface.class); + + File file; + if (torrentUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { + file = new File(getCacheDir(), "upload.torrent"); + ContentResolver cr = getContentResolver(); + try { + FileUtils.copyInputStreamToFile(cr.openInputStream(torrentUri), file); + } catch (IOException e) { + e.printStackTrace(); + notif.failed(); + return; + } + } else { + file = new File(torrentUri.getPath()); + } + TypedFile typedFile = new TypedFile("application/x-bittorrent", file); + uploadInterface.uploadFile(typedFile, parentId) + .subscribe(new Action1() { + @Override + public void call(PutioTransferFileUploadResponse putioTransferFileUploadResponse) { + notif.succeeded(); + } + }, new Action1() { + @Override + public void call(Throwable throwable) { + notif.failed(); + throwable.printStackTrace(); + Toast.makeText(TransfersActivity.this, "Error: " + throwable.getMessage(), Toast.LENGTH_LONG).show(); + } + }); + } + + public class UploadNotif { + private NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + + protected void start() { + NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(TransfersActivity.this); + notifBuilder.setOngoing(true) + .setCategory(NotificationCompat.CATEGORY_PROGRESS) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentTitle(getString(R.string.notification_title_uploading_torrent)) + .setSmallIcon(R.drawable.ic_notificon_transfer) + .setTicker(getString(R.string.notification_ticker_uploading_torrent)) + .setProgress(1, 0, true); + Notification notif = notifBuilder.build(); + notif.ledARGB = Color.parseColor("#FFFFFF00"); + try { + notifManager.notify(1, notif); + } catch (IllegalArgumentException e) { + notifBuilder.setContentIntent(PendingIntent.getActivity( + TransfersActivity.this, 0, new Intent(TransfersActivity.this, Putio.class), 0)); + notif = notifBuilder.build(); + notif.ledARGB = Color.parseColor("#FFFFFF00"); + notifManager.notify(1, notif); + } + } + + protected void succeeded() { + NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(TransfersActivity.this); + notifBuilder.setOngoing(false) + .setAutoCancel(true) + .setCategory(NotificationCompat.CATEGORY_PROGRESS) + .setPriority(NotificationCompat.PRIORITY_LOW) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentTitle(getString(R.string.notification_title_uploaded_torrent)) + .setContentText(getString(R.string.notification_body_uploaded_torrent)) + .setSmallIcon(R.drawable.ic_notificon_transfer); + Intent viewTransfersIntent = new Intent(TransfersActivity.this, TransfersActivity.class); + viewTransfersIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + notifBuilder.setContentIntent(PendingIntent.getActivity( + TransfersActivity.this, 0, viewTransfersIntent, PendingIntent.FLAG_CANCEL_CURRENT)); +// notifBuilder.addAction(R.drawable.ic_notif_watch, "Watch", null); TODO + notifBuilder.setTicker(getString(R.string.notification_ticker_uploaded_torrent)) + .setProgress(0, 0, false); + Notification notif = notifBuilder.build(); + notif.ledARGB = Color.parseColor("#FFFFFF00"); + notifManager.notify(1, notif); + } + + protected void failed() { + NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(TransfersActivity.this); + notifBuilder.setOngoing(false) + .setAutoCancel(true) + .setCategory(NotificationCompat.CATEGORY_ERROR) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + .setContentTitle(getString(R.string.notification_title_error)) + .setContentText(getString(R.string.notification_body_error)) + .setSmallIcon(R.drawable.ic_notificon_transfer); + PendingIntent retryNotifIntent = PendingIntent.getActivity( + TransfersActivity.this, 0, getIntent(), PendingIntent.FLAG_ONE_SHOT); + notifBuilder.addAction(R.drawable.ic_notif_retry, getString(R.string.notification_button_retry), retryNotifIntent) + .setContentIntent(retryNotifIntent) + .setTicker(getString(R.string.notification_ticker_error)); + Notification notif = notifBuilder.build(); + notif.ledARGB = Color.parseColor("#FFFFFF00"); + notifManager.notify(1, notif); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/model/PutioRestInterface.java b/app/src/main/java/com/stevenschoen/putionew/model/PutioRestInterface.java index c6dbcf70..82f5894b 100644 --- a/app/src/main/java/com/stevenschoen/putionew/model/PutioRestInterface.java +++ b/app/src/main/java/com/stevenschoen/putionew/model/PutioRestInterface.java @@ -1,20 +1,8 @@ package com.stevenschoen.putionew.model; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.support.v4.app.NotificationCompat; - import com.path.android.jobqueue.Job; import com.path.android.jobqueue.Params; -import com.squareup.okhttp.Response; import com.stevenschoen.putionew.PutioUtils; -import com.stevenschoen.putionew.R; -import com.stevenschoen.putionew.activities.Putio; -import com.stevenschoen.putionew.activities.TransfersActivity; import com.stevenschoen.putionew.model.responses.AccountInfoResponse; import com.stevenschoen.putionew.model.responses.BasePutioResponse; import com.stevenschoen.putionew.model.responses.FileResponse; @@ -22,9 +10,8 @@ import com.stevenschoen.putionew.model.responses.FilesSearchResponse; import com.stevenschoen.putionew.model.responses.Mp4StatusResponse; import com.stevenschoen.putionew.model.responses.TransfersListResponse; +import com.stevenschoen.putionew.model.transfers.PutioTransfer; -import retrofit.Callback; -import retrofit.RetrofitError; import retrofit.http.Body; import retrofit.http.Field; import retrofit.http.FormUrlEncoded; @@ -75,7 +62,7 @@ public interface PutioRestInterface { @FormUrlEncoded @POST("/transfers/add") - void addTransferUrl(@Field("url") String url, @Field("extract") boolean extract, @Field("save_parent_id") long saveParentId, Callback callback); + Observable addTransferUrl(@Field("url") String url, @Field("extract") boolean extract, @Field("save_parent_id") long saveParentId); @FormUrlEncoded @POST("/transfers/retry") @@ -122,99 +109,6 @@ protected boolean shouldReRunOnThrowable(Throwable throwable) { } } - public static abstract class PutioUploadJob extends PutioJob implements Callback { - protected Context context; - private NotificationManager notifManager; - private Intent retryIntent; - - protected PutioUploadJob(PutioUtils utils, Context context, Intent retryIntent) { - super(utils); - - this.context = context; - this.retryIntent = retryIntent; - this.notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - } - - @Override - public void onRun() throws Throwable { - notifStart(); - } - - @Override - public void success(Response response, retrofit.client.Response response2) { - notifSucceeded(); - } - - @Override - public void failure(RetrofitError error) { - notifFailed(); - } - - protected void notifStart() { - NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context); - notifBuilder.setOngoing(true); - notifBuilder.setCategory(NotificationCompat.CATEGORY_PROGRESS); - notifBuilder.setPriority(NotificationCompat.PRIORITY_LOW); - notifBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); - notifBuilder.setContentTitle(context.getString(R.string.notification_title_uploading_torrent)); - notifBuilder.setSmallIcon(R.drawable.ic_notificon_transfer); - notifBuilder.setTicker(context.getString(R.string.notification_ticker_uploading_torrent)); - notifBuilder.setProgress(1, 0, true); - Notification notif = notifBuilder.build(); - notif.ledARGB = Color.parseColor("#FFFFFF00"); - try { - notifManager.notify(1, notif); - } catch (IllegalArgumentException e) { - notifBuilder.setContentIntent(PendingIntent.getActivity( - context, 0, new Intent(context, Putio.class), 0)); - notif = notifBuilder.build(); - notif.ledARGB = Color.parseColor("#FFFFFF00"); - notifManager.notify(1, notif); - } - } - - protected void notifSucceeded() { - NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context); - notifBuilder.setOngoing(false); - notifBuilder.setAutoCancel(true); - notifBuilder.setCategory(NotificationCompat.CATEGORY_PROGRESS); - notifBuilder.setPriority(NotificationCompat.PRIORITY_LOW); - notifBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); - notifBuilder.setContentTitle(context.getString(R.string.notification_title_uploaded_torrent)); - notifBuilder.setContentText(context.getString(R.string.notification_body_uploaded_torrent)); - notifBuilder.setSmallIcon(R.drawable.ic_notificon_transfer); - notifBuilder.setContentIntent(PendingIntent.getActivity( - context, 0, new Intent(context, TransfersActivity.class), - PendingIntent.FLAG_ONE_SHOT)); -// notifBuilder.addAction(R.drawable.ic_notif_watch, "Watch", null); TODO - notifBuilder.setTicker(context.getString(R.string.notification_ticker_uploaded_torrent)); - notifBuilder.setProgress(0, 0, false); - Notification notif = notifBuilder.build(); - notif.ledARGB = Color.parseColor("#FFFFFF00"); - notifManager.notify(1, notif); - } - - protected void notifFailed() { - NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context); - notifBuilder.setOngoing(false); - notifBuilder.setAutoCancel(true); - notifBuilder.setCategory(NotificationCompat.CATEGORY_ERROR); - notifBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT); - notifBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); - notifBuilder.setContentTitle(context.getString(R.string.notification_title_error)); - notifBuilder.setContentText(context.getString(R.string.notification_body_error)); - notifBuilder.setSmallIcon(R.drawable.ic_notificon_transfer); - PendingIntent retryNotifIntent = PendingIntent.getActivity( - context, 0, retryIntent, PendingIntent.FLAG_ONE_SHOT); - notifBuilder.addAction(R.drawable.ic_notif_retry, context.getString(R.string.notification_button_retry), retryNotifIntent); - notifBuilder.setContentIntent(retryNotifIntent); - notifBuilder.setTicker(context.getString(R.string.notification_ticker_error)); - Notification notif = notifBuilder.build(); - notif.ledARGB = Color.parseColor("#FFFFFF00"); - notifManager.notify(1, notif); - } - } - public static class GetMp4StatusJob extends PutioJob { private long id; @@ -243,23 +137,4 @@ public void onRun() throws Throwable { getUtils().getRestInterface().convertToMp4(id); } } - - public static class PostAddTransferJob extends PutioUploadJob { - private String url; - private boolean extract; - private long saveParentId; - - public PostAddTransferJob(PutioUtils utils, String url, boolean extract, long saveParentId, Context context, Intent retryIntent) { - super(utils, context, retryIntent); - this.url = url; - this.extract = extract; - this.saveParentId = saveParentId; - } - - @Override - public void onRun() throws Throwable { - super.onRun(); - getUtils().getRestInterface().addTransferUrl(url, extract, saveParentId, this); - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/model/PutioUploadInterface.java b/app/src/main/java/com/stevenschoen/putionew/model/PutioUploadInterface.java index ce570716..e690cb7f 100644 --- a/app/src/main/java/com/stevenschoen/putionew/model/PutioUploadInterface.java +++ b/app/src/main/java/com/stevenschoen/putionew/model/PutioUploadInterface.java @@ -1,61 +1,15 @@ package com.stevenschoen.putionew.model; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; +import com.stevenschoen.putionew.model.responses.PutioTransferFileUploadResponse; -import com.squareup.okhttp.Response; -import com.stevenschoen.putionew.PutioUtils; - -import org.apache.commons.io.FileUtils; - -import java.io.File; - -import retrofit.Callback; import retrofit.http.Multipart; import retrofit.http.POST; import retrofit.http.Part; import retrofit.mime.TypedFile; +import rx.Observable; public interface PutioUploadInterface { @Multipart @POST("/files/upload") - void uploadFile(@Part("file") TypedFile file, @Part("parent_id") long parentId, Callback callback); - - public static class PostUploadFileJob extends PutioRestInterface.PutioUploadJob { - private PutioUploadInterface uploadInterface; - - private Uri fileUri; - private long parentId; - - public PostUploadFileJob(PutioUtils utils, PutioUploadInterface uploadInterface, Context context, Intent retryIntent, Uri fileUri, long parentId) { - super(utils, context, retryIntent); - this.uploadInterface = uploadInterface; - - this.fileUri = fileUri; - this.parentId = parentId; - } - - @Override - public void onRun() throws Throwable { - super.onRun(); - File file; - if (fileUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) { - file = new File(context.getCacheDir(), "upload.torrent"); - ContentResolver cr = context.getContentResolver(); - FileUtils.copyInputStreamToFile(cr.openInputStream(fileUri), file); - } else { - file = new File(fileUri.getPath()); - } - - try { - uploadInterface.uploadFile( - new TypedFile("application/x-bittorrent", file), parentId, this); - } catch (Exception e) { - file.delete(); - throw e; - } - } - } + Observable uploadFile(@Part("file") TypedFile file, @Part("parent_id") long parentId); } \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/model/responses/PutioTransferFileUploadResponse.java b/app/src/main/java/com/stevenschoen/putionew/model/responses/PutioTransferFileUploadResponse.java new file mode 100644 index 00000000..94f864f3 --- /dev/null +++ b/app/src/main/java/com/stevenschoen/putionew/model/responses/PutioTransferFileUploadResponse.java @@ -0,0 +1,7 @@ +package com.stevenschoen.putionew.model.responses; + +import com.stevenschoen.putionew.model.transfers.PutioTransfer; + +public class PutioTransferFileUploadResponse extends BasePutioResponse { + private PutioTransfer transfer; +}