Skip to content

Commit

Permalink
Convert transfer uploading (by URL and file) to RxJava, fix transfers…
Browse files Browse the repository at this point in the history
… not saving to folders
  • Loading branch information
DSteve595 committed Nov 21, 2015
1 parent 6bf92ee commit 8bcb3e6
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 192 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<!-- TV -->
<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package com.stevenschoen.putionew.activities;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NotificationCompat;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

import com.stevenschoen.putionew.PutioApplication;
import com.stevenschoen.putionew.PutioUtils;
import com.stevenschoen.putionew.R;
import com.stevenschoen.putionew.UIUtils;
import com.stevenschoen.putionew.model.PutioRestInterface;
import com.stevenschoen.putionew.model.PutioUploadInterface;
import com.stevenschoen.putionew.model.responses.PutioTransferFileUploadResponse;
import com.stevenschoen.putionew.model.transfers.PutioTransfer;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

import retrofit.RestAdapter;
import retrofit.mime.TypedFile;
import rx.functions.Action1;

public class TransfersActivity extends FragmentActivity {
public class TransfersActivity extends BottomSheetActivity {

SharedPreferences sharedPrefs;

Expand All @@ -30,6 +46,7 @@ public class TransfersActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
utils = ((PutioApplication) getApplication()).getPutioUtils();
Expand All @@ -38,20 +55,16 @@ protected void onCreate(Bundle savedInstanceState) {
int mode = getIntent().getIntExtra("mode", 0);
switch (mode) {
case AddTransfers.TYPE_URL:
utils.getJobManager().addJobInBackground(new PutioRestInterface.PostAddTransferJob(
utils,
getIntent().getStringExtra("url"),
getIntent().getBooleanExtra("extract", false),
getIntent().getIntExtra("saveParentId", 0),
this, getIntent()));
addTransferUrl(
getIntent().getStringExtra("url"),
getIntent().getBooleanExtra("extract", false),
getIntent().getLongExtra("saveParentId", 0));
break;
case AddTransfers.TYPE_FILE:
RestAdapter uploadAdapter = utils.makeRestAdapterBuilder(PutioUtils.uploadBaseUrl).build();
PutioUploadInterface uploadInterface = uploadAdapter.create(PutioUploadInterface.class);
utils.getJobManager().addJobInBackground(new PutioUploadInterface.PostUploadFileJob(
utils, uploadInterface, this, getIntent(),
(Uri) getIntent().getParcelableExtra("torrenturi"),
getIntent().getIntExtra("parentId", 0)));
addTransferFile(
(Uri) getIntent().getParcelableExtra("torrenturi"),
getIntent().getLongExtra("parentId", 0)
);
break;
}

Expand Down Expand Up @@ -95,4 +108,131 @@ public void onClick(View v) {
});
}
}

private void addTransferUrl(String url, boolean extract, long saveParentId) {
final UploadNotif notif = new UploadNotif();
notif.start();
utils.getRestInterface().addTransferUrl(url, extract, saveParentId)
.subscribe(new Action1<PutioTransfer>() {
@Override
public void call(PutioTransfer transfer) {
notif.succeeded();
}
}, new Action1<Throwable>() {
@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<PutioTransferFileUploadResponse>() {
@Override
public void call(PutioTransferFileUploadResponse putioTransferFileUploadResponse) {
notif.succeeded();
}
}, new Action1<Throwable>() {
@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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
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;
import com.stevenschoen.putionew.model.responses.FilesListResponse;
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;
Expand Down Expand Up @@ -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<Response> callback);
Observable<PutioTransfer> addTransferUrl(@Field("url") String url, @Field("extract") boolean extract, @Field("save_parent_id") long saveParentId);

@FormUrlEncoded
@POST("/transfers/retry")
Expand Down Expand Up @@ -122,99 +109,6 @@ protected boolean shouldReRunOnThrowable(Throwable throwable) {
}
}

public static abstract class PutioUploadJob extends PutioJob implements Callback<Response> {
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;

Expand Down Expand Up @@ -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);
}
}
}
Loading

0 comments on commit 8bcb3e6

Please sign in to comment.