Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
66544e0
Add TorrentLoader class to load torrents and replace AsyncTask
nikhildahake May 10, 2025
4108eef
Update deprecated method call.
nikhildahake May 10, 2025
85ff097
Remove deprecated method MockitoAnnotations.initMocks()
nikhildahake May 10, 2025
24ac7b7
Add class to manage application preferences.
nikhildahake May 10, 2025
81ad65d
Fix call to a deprecated method.
nikhildahake May 10, 2025
0114480
Use TransmissionRemotePreferenceManager instead of PreferenceManager
nikhildahake May 10, 2025
db0a2d7
Replace getAdapterPosition() with getBindingAdapter() position
nikhildahake May 10, 2025
45c26ff
Fix if statement.
nikhildahake May 10, 2025
03b11e5
Use TransmissionRemotePreferenceManager instead of PreferenceManager
nikhildahake May 10, 2025
a881b76
Update deprecated import
nikhildahake May 10, 2025
733ab9f
Use getSupportFragmentManager() instead of getFragmentManager()
nikhildahake May 10, 2025
c3c2fff
Update method params to not use deprecated data types.
nikhildahake May 10, 2025
f9720c2
Remove the use of deprecated method getUserVisibleHint()
nikhildahake May 10, 2025
02a98fd
Remove the usage of setOptionsMenu()
nikhildahake May 10, 2025
a7a16f3
Remove the usage of setHasOptionsMenu()
nikhildahake May 10, 2025
4065f95
Remove the usage of setHasOptionsMenu()
nikhildahake May 10, 2025
4b4d62a
Remove the usage of setHasOptionsMenu()
nikhildahake May 10, 2025
ca40acb
Remove the usage of setHasOptionsMenu() and use TransmissionRemotePre…
nikhildahake May 10, 2025
a734da6
1. Use TransmissionRemotePreferenceManager instead of PreferenceManager
nikhildahake May 10, 2025
086d4b3
1. Use TransmissionRemotePreferenceManager instead of PreferenceManager
nikhildahake May 10, 2025
e05cb2c
Remove the usage of startActivityForResult
nikhildahake May 10, 2025
11a1af3
Update onAttach() to use Context instead of Activity.
nikhildahake May 10, 2025
5aa3556
Remove the usage of getTargetFragment()
nikhildahake May 10, 2025
a41bf9c
1. Use TransmissionRemotePreferenceManager instead of PreferenceManager
nikhildahake May 10, 2025
95fc72d
Tests were crashing. Turn off: android.experimental.androidTest.useUn…
nikhildahake May 10, 2025
54379c0
1) Update code to use TransmissionRemotePreferenceManager.
nikhildahake May 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.yupol.transmissionremote.app.server.Server;
import net.yupol.transmissionremote.app.transport.request.Request;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -49,10 +50,11 @@ public class RequestExecutorTest {
@Mock private RequestListener<String> mockListener;

@Captor private ArgumentCaptor<SpiceException> spiceExceptionArgumentCaptor;
private AutoCloseable closeable;

@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);

when(mockRequest.getResultType())
.thenReturn(String.class);
Expand Down Expand Up @@ -200,5 +202,9 @@ static <T> RequestListener<T> wrap(@Nonnull RequestListener<T> listener, @Nonnul
return new CountDownRequestListenerWrapper<>(listener, latch);
}
}
//endregion

@After
public void tearDown() throws Exception {
closeable.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.yupol.transmissionremote.app.server.Server;
import net.yupol.transmissionremote.app.transport.request.Request;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -27,15 +28,15 @@ public class SpiceTransportManagerTest {

private static final String SESSION_ID = "fake_session_id";
private static final String REDIRECT_LOCATION = "fake_redirect_location";

private AutoCloseable closeable;
private Server server;

@Mock private Request<String> request;
@Mock private RequestListener<String> requestListener;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);

server = new Server("Test Server", "http://localhost", 9091);
}
Expand Down Expand Up @@ -89,4 +90,9 @@ public <T> void execute(SpiceRequest<T> request, RequestListener<T> requestListe
verifyNoMoreInteractions(requestListener);
assertThat(server.getRedirectLocation()).isEqualTo(REDIRECT_LOCATION);
}

@After
public void tearDown() throws Exception {
closeable.close();
}
}
123 changes: 65 additions & 58 deletions app/src/main/java/net/yupol/transmissionremote/app/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.yupol.transmissionremote.app;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
Expand All @@ -14,10 +13,8 @@
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;
Expand Down Expand Up @@ -95,6 +92,8 @@
import net.yupol.transmissionremote.app.transport.request.StartTorrentRequest;
import net.yupol.transmissionremote.app.transport.request.StopTorrentRequest;
import net.yupol.transmissionremote.app.transport.request.TorrentRemoveRequest;
import net.yupol.transmissionremote.app.utils.TorrentLoader;
import net.yupol.transmissionremote.app.utils.TransmissionRemotePreferenceManager;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
Expand All @@ -106,7 +105,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Timer;
Expand Down Expand Up @@ -214,6 +212,8 @@ public void onRequestSuccess(AddTorrentResult addTorrentResult) {
private boolean showFab;
private FreeSpaceFooterDrawerItem freeSpaceFooterDrawerItem;
private FinishedTorrentsNotificationManager finishedTorrentsNotificationManager;
private ActivityResultLauncher<Intent> chooseTorrentLauncher;
private ActivityResultLauncher<Intent> addServerLauncher;

private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
Expand Down Expand Up @@ -245,6 +245,41 @@ protected void onCreate(Bundle savedInstanceState) {
openTorrentUri = savedInstanceState.getParcelable(KEY_OPEN_TORRENT_URI);
openTorrentScheme = savedInstanceState.getString(KEY_OPEN_TORRENT_SCHEME);
}

chooseTorrentLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if (data != null) {
Uri uri = data.getData();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
openTorrentByLocalFile(inputStream);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
);

addServerLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
if (data != null) {
Server server = data.getParcelableExtra(AddServerActivity.EXTRA_SEVER);
addNewServer(server);
switchServer(server);
if (application.getServers().size() == 1 && application.isNotificationEnabled()) {
requestNotificationsPermissionIfRequired();
}
}
}
}
);
}

private void logAppStartupTime() {
Expand Down Expand Up @@ -559,7 +594,7 @@ private void openTorrentFileByUriWithScheme(Uri uri, String scheme) {
protected void onResume() {
super.onResume();
isActivityResumed = true;
getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
TransmissionRemotePreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);

List<Server> servers = application.getServers();
if (servers.isEmpty()) {
Expand All @@ -576,7 +611,7 @@ protected void onResume() {

binding.addTorrentButton.collapseImmediately();

showFab = PreferenceManager.getDefaultSharedPreferences(this)
showFab = TransmissionRemotePreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.show_add_torrent_fab_key), true);
boolean isListVisible = getTorrentListFragment() != null;
binding.addTorrentButton.setVisibility(showFab && isListVisible ? View.VISIBLE : View.GONE);
Expand All @@ -602,7 +637,7 @@ protected void onPause() {

stopPreferencesUpdateTimer();

getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
TransmissionRemotePreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
application.persist();
}

Expand Down Expand Up @@ -770,20 +805,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SERVER_PARAMS) {
if (resultCode == RESULT_OK) {
Server server = data.getParcelableExtra(AddServerActivity.EXTRA_SEVER);
addNewServer(server);
switchServer(server);
if (application.getServers().size() == 1 && application.isNotificationEnabled()) {
requestNotificationsPermissionIfRequired();
}
}
} else if (requestCode == REQUEST_CODE_CHOOSE_TORRENT) {
if (resultCode == RESULT_OK) {
openTorrentUri = data.getData();
openTorrentUriOnResume = true;
}
if (requestCode == REQUEST_CODE_CHOOSE_TORRENT && resultCode == RESULT_OK) {
openTorrentUri = data.getData();
openTorrentUriOnResume = true;
}
}

Expand Down Expand Up @@ -942,22 +966,28 @@ public void onDownloadLocationSelected(Bundle args, final String downloadDir, fi
new AddTorrentByFileRequest(args.getByteArray(DownloadLocationDialogFragment.KEY_FILE_BYTES), downloadDir, !startWhenAdded),
addTorrentResultListener);
break;

case DownloadLocationDialogFragment.REQUEST_CODE_BY_REMOTE_FILE:
Uri fileUri = args.getParcelable(DownloadLocationDialogFragment.KEY_FILE_URI);
new RetrieveTorrentContentAsyncTask() {
TorrentLoader.loadTorrentFromUri(fileUri, new TorrentLoader.Callback() {
@Override
protected void onPostExecute(byte[] bytes) {
if (bytes != null) {
TransportManager tm = getTransportManager();
if (tm.isStarted()) {
tm.doRequest(new AddTorrentByFileRequest(bytes, downloadDir, !startWhenAdded), addTorrentResultListener);
}
} else {
Toast.makeText(MainActivity.this, getString(R.string.error_cannot_read_file_msg), Toast.LENGTH_SHORT).show();
public void onSuccess(byte[] data) {
TransportManager tm = getTransportManager();
if (tm.isStarted()) {
tm.doRequest(new AddTorrentByFileRequest(data, downloadDir, !startWhenAdded), addTorrentResultListener);
}
}
}.execute(fileUri);

@Override
public void onError() {
Toast.makeText(MainActivity.this,
getString(R.string.error_cannot_read_file_msg),
Toast.LENGTH_SHORT
).show();
}
});
break;

case DownloadLocationDialogFragment.REQUEST_CODE_BY_MAGNET:
String magnetUri = args.getString(DownloadLocationDialogFragment.KEY_MAGNET_URI);
if (magnetUri != null) {
Expand All @@ -984,7 +1014,7 @@ public void onRemoveTorrentsSelected(int[] torrentsToRemove, boolean removeData)

public void openAddServerActivity(View view) {
Intent intent = new Intent(this, AddServerActivity.class);
startActivityForResult(intent, REQUEST_CODE_SERVER_PARAMS);
addServerLauncher.launch(intent);
}

private void addNewServer(Server server) {
Expand Down Expand Up @@ -1153,9 +1183,8 @@ private void showFileChooser() {
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(
Intent.createChooser(intent, getResources().getString(R.string.select_torrent_file)),
MainActivity.REQUEST_CODE_CHOOSE_TORRENT);
Intent chooser = Intent.createChooser(intent, getResources().getString(R.string.select_torrent_file));
chooseTorrentLauncher.launch(chooser);
} catch (ActivityNotFoundException ex) {
Toast.makeText(this,
getResources().getString(R.string.error_install_file_manager_msg),
Expand Down Expand Up @@ -1211,26 +1240,4 @@ private void pauseAllTorrents() {
getTransportManager().doRequest(new StopTorrentRequest(application.getTorrents()), null);
torrentUpdater.scheduleUpdate(UPDATE_REQUEST_DELAY);
}

private static abstract class RetrieveTorrentContentAsyncTask extends AsyncTask<Uri, Void, byte[]> {

@Override
protected byte[] doInBackground(Uri... torrentFileUris) {
String uri = torrentFileUris[0].toString();
InputStream inputStream = null;
try {
inputStream = new URL(uri).openConnection().getInputStream();
return IOUtils.toByteArray(inputStream);
} catch (IOException e) {
Log.e(TAG, "Failed to retrieve Uri '" + uri + "'", e);
} finally {
if (inputStream != null) try {
inputStream.close();
} catch (IOException e) {
Log.e(TAG, "Failed to close InputStream", e);
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ActionBarNavigationAdapter(Context context) {
textColorPrimary = ColorUtils.resolveColor(context, android.R.attr.textColorPrimary, R.color.text_primary);
textColorPrimaryInverse = ColorUtils.resolveColor(context, android.R.attr.textColorPrimaryInverse, R.color.text_primary_inverse);
accentColor = ColorUtils.resolveColor(context, R.attr.colorAccent, R.color.accent);
alternativeAccentColor = context.getResources().getColor(R.color.alternative_accent);
alternativeAccentColor = context.getResources().getColor(R.color.alternative_accent, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SpeedTextView(Context context, @DrawableRes int iconRes, @ColorRes int ic
setPadding(horPadding, 0, horPadding, 0);

setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.speed_text_size));
setTextColor(context.getResources().getColor(R.color.text_primary_inverse));
setTextColor(context.getResources().getColor(R.color.text_primary_inverse, null));
setTypeface(null, Typeface.BOLD);

setSpeed(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
Expand All @@ -23,6 +22,7 @@
import net.yupol.transmissionremote.app.TransmissionRemote;
import net.yupol.transmissionremote.app.server.Server;
import net.yupol.transmissionremote.app.utils.ColorUtils;
import net.yupol.transmissionremote.app.utils.TransmissionRemotePreferenceManager;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -149,7 +149,7 @@ public void setServers(List<Server> servers, int currentServerPosition) {
}

TransmissionRemote app = TransmissionRemote.getApplication(getContext());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences sp = TransmissionRemotePreferenceManager.getDefaultSharedPreferences(getContext());
List<String> orderedServers;
if (sp.contains(KEY_ORDERED_SERVERS)) {
orderedServers = serversFromJson(sp.getString(KEY_ORDERED_SERVERS, ""));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package net.yupol.transmissionremote.app.notifications;

import android.content.Context;
import android.preference.PreferenceManager;

import net.yupol.transmissionremote.app.R;
import net.yupol.transmissionremote.app.utils.TransmissionRemotePreferenceManager;

public class BackgroundUpdater {

public static void start(Context context) {
boolean onlyUnmeteredNetwork = PreferenceManager.getDefaultSharedPreferences(context)
boolean onlyUnmeteredNetwork = TransmissionRemotePreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(context.getString(R.string.background_update_only_unmetered_wifi_key), true);
BackgroundUpdateJob.schedule(onlyUnmeteredNetwork);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.yupol.transmissionremote.app.preferences;

import android.os.Bundle;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.appcompat.app.ActionBar;
import android.util.Log;
import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

Expand Down Expand Up @@ -96,7 +97,7 @@ public void onBackPressed() {
SessionSetRequest.Builder requestBuilder = fragment.getPreferencesRequestBuilder();
if (requestBuilder.isChanged()) {
saveChangesRequest = requestBuilder.build();
new SaveChangesDialogFragment().show(getFragmentManager(), TAG_SAVE_CHANGES_DIALOG);
new SaveChangesDialogFragment().show(getSupportFragmentManager(), TAG_SAVE_CHANGES_DIALOG);
} else {
super.onBackPressed();
}
Expand Down
Loading